3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * CGI for barcode generation
24 * Author: Simone Piccardi
27 * Usage: cgi-bin for apache.
28 * Called by downloading something like:
29 * http://localhost/cgi-bin/barcode?string
30 * where string is the code to be converted
32 ****************************************************************/
34 * Include needed headers
36 #include <sys/types.h> /* primitive system data types */
37 #include <sys/stat.h> /* file characteristics constants and functions */
38 #include <unistd.h> /* unix standard library */
39 #include <stdio.h> /* standard I/O library */
40 #include <stdlib.h> /* C standard library */
41 #include <string.h> /* C strings library */
42 #include <wait.h> /* process termination constants and functions */
43 #include <fcntl.h> /* file control functions */
44 #include <assert.h> /* C assertion functions */
45 #include <time.h> /* date and time constants, types and functions */
49 void WriteMess(char *mess);
52 int main(int argc, char *argv[], char *envp[])
55 * Variables definition
61 char content[]="Content-type: image/jpeg\n\n";
66 /* create two pipes, pipein and pipeout, to handle communication */
67 if ( (retval = pipe(pipein)) ) {
68 WriteMess("input pipe creation error");
71 if ( (retval = pipe(pipeout)) ) {
72 WriteMess("output pipe creation error");
75 /* First fork: use child to run barcode program */
76 if ( (pid = fork()) == -1 ) {
77 WriteMess("child creation error");
83 * Child exec barcode program, that take input (string to encode)
84 * from pipein, remapped to stdin, and write the output (a PS
85 * image) to stdout, remapped to pipeout
87 close(pipein[1]); /* close output side of input pipe */
88 dup2(pipein[0], STDIN_FILENO); /* remap stdin in pipe input */
90 dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
91 execlp("barcode", "barcode", size, NULL);
94 * Parent write string to pipe input and close it,
95 * then wait child execution and results form pipeout,
96 * then fork to convert PS to JPEG using gs
98 close(pipein[0]); /* close input side of input pipe */
99 write(pipein[1], argv[1], strlen(argv[1]));
101 waitpid(pid, NULL, 0);
102 /* Second fork: use child to run ghostscript*/
103 if ( (pid = fork()) == -1) {
104 WriteMess("child creation error");
107 /* second child, convert PS to JPEG */
109 close(pipeout[1]); /* close write end */
110 dup2(pipeout[0], STDIN_FILENO); /* remap read end to stdin */
112 write(STDOUT_FILENO, content, strlen(content));
113 execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
117 waitpid(pid, NULL, 0);
121 * Routine to produce an HTML error message on output
123 void WriteMess(char *mess)
125 printf("Content-type: text/html\n\n");