419e9c0e1898ae9dacd460716dba303d9f70ce4d
[gapil.git] / sources / BarCodePage.c
1 /* BarCodePage.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program barcode 
22  * CGI for barcode generation
23  *
24  * Author: Simone Piccardi
25  * Jun. 2002
26  *
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
31  *
32  * $Id: BarCodePage.c,v 1.2 2002/08/10 14:22:53 piccardi Exp $ 
33  *
34  ****************************************************************/
35 /* 
36  * Include needed headers
37  */
38 #include <sys/types.h>   /* predefined types */
39 #include <sys/stat.h>    /* stat deinitiions */
40 #include <unistd.h>      /* include unix standard library */
41 #include <stdio.h>       /* include standard I/O library */
42 #include <stdlib.h>      /* include standard library */
43 #include <string.h>      /* include string library */
44 #include <wait.h>        /* include wait call */
45 #include <fcntl.h>
46 #include <assert.h>
47 #include <time.h>
48
49 #include"macros.h"
50 void WriteMess(char *mess);
51
52 /* Program begin */
53 int main(int argc, char *argv[], char *envp[])
54 {
55 /*
56  * Variables definition  
57  */
58     pid_t pid;
59     int retval;
60     int pipein[2];
61     int pipeout[2];
62     char content[]="Content-type: image/jpeg\n\n";
63     char size[]="-pA9";
64     /* 
65      * Begin
66      */
67     /* create two pipes, pipein and pipeout, to handle communication */
68     if ( (retval = pipe(pipein)) ) {
69         WriteMess("input pipe creation error");
70         exit(0);        
71     }
72     if ( (retval = pipe(pipeout)) ) {
73         WriteMess("output pipe creation error");
74         exit(0);        
75     }    
76     /* First fork: use child to run barcode program */
77     if ( (pid = fork()) == -1 ) {
78         WriteMess("child creation error");
79         exit(0);
80     }
81     /* if child */
82     if (pid == 0) {
83         /*
84          * Child exec barcode program, that take input (string to encode)
85          * from pipein, remapped to stdin, and write the output (a PS
86          * image) to stdout, remapped to pipeout 
87          */
88         close(pipein[1]);                /* close output side of input pipe */
89         dup2(pipein[0], STDIN_FILENO);   /* remap stdin in pipe input */
90         close(pipeout[0]);
91         dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
92         execlp("barcode", "barcode", size, NULL); 
93     } 
94     /*
95      * Parent write string to pipe input and close it, 
96      * then wait child execution and results form pipeout, 
97      * then fork to convert PS to JPEG using gs
98      */
99     close(pipein[0]);        /* close input side of input pipe */
100     write(pipein[1], argv[1], strlen(argv[1]));
101     close(pipein[1]);
102     waitpid(pid, NULL, 0);
103     /* Second fork: use child to run ghostscript*/
104     if ( (pid = fork()) == -1) {
105         WriteMess("child creation error");
106         exit(0);
107     }
108     /* second child, convert PS to JPEG */
109     if (pid == 0) {
110         close(pipeout[1]);              /* close write end */
111         dup2(pipeout[0], STDIN_FILENO); /* remap read end to stdin */
112         /* send mime type */
113         write(STDOUT_FILENO, content, strlen(content));
114         execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
115     }
116     /* still parent */
117     close(pipeout[1]); 
118     waitpid(pid, NULL, 0);
119     exit(0);
120 }
121 /*
122  * Routine to produce an HTML error message on output 
123  */
124 void WriteMess(char *mess)
125 {
126     printf("Content-type: text/html\n\n");
127     perror(mess);
128     printf("<br>\n");
129 }