138e23ab5d3908538afeccfaea03d2018d13211b
[gapil.git] / sources / BarCode.c
1 /* BarCode.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 echod 
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: BarCode.c,v 1.1 2002/06/18 22:11:06 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 /* */
42 #include <stdio.h>       /* include standard I/O library */
43 #include <stdlib.h>      /* include standard library */
44 #include <string.h>      /* include string library */
45 #include <wait.h>        /* include wait call */
46 #include <fcntl.h>
47 #include <assert.h>
48 #include <time.h>
49
50 #include"macros.h"
51 void WriteMess(char *mess);
52
53
54 /* Program begin */
55 int main(int argc, char *argv[], char *envp[])
56 {
57 /*
58  * Variables definition  
59  */
60     char buffer[8192];
61     pid_t pid;
62     size_t n;
63     int retval;
64     int pipein[2];
65     int pipeout[2];
66     char size[]="-pA9";
67     char psize[]="-sPAPERSIZE=a9";
68     char content[]="Content-type: image/jpeg\n\n";
69     /* 
70      * Begin
71      */
72     /* create two pipes to handle process communication */
73     if ( (retval = pipe(pipein)) ) {
74         WriteMess("input pipe creation error");
75         exit(0);        
76     }
77     if ( (retval = pipe(pipeout)) ) {
78         WriteMess("output pipe creation error");
79         exit(0);        
80     }    
81     /* fork child to run barcode program */
82     pid = fork();
83     if (pid == -1) {
84         WriteMess("child creation error");
85         exit(0);        
86     }
87     /* if child */
88     if (pid == 0) {
89         /*
90          * Child exec barcode program, that take input (string to encode)
91          * from pipein, remapped to stdin, and write the output (a ppm 
92          * image) to stdout, remapped to pipeout 
93          */
94         close(pipein[1]);                /* close output side of input pipe */
95         dup2(pipein[0], STDIN_FILENO);   /* remap stdin in pipe input */
96         close(pipeout[0]);
97         dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
98         execlp("barcode", "barcode", size, NULL); //"-o", "-",  NULL);
99     } 
100     /*
101      * Parent write string to pipe input and close it, 
102      * then wait child execution and results form pipeout, 
103      * then fork to convert ppm to gif using ppmtogif
104      */
105     close(pipein[0]);        /* close input side of input pipe */
106     n=write(pipein[1], argv[1], strlen(argv[1]));
107     close(pipein[1]);
108     waitpid(pid, NULL, 0);
109     /* 
110      * refork to use ppmtogif
111      */
112     pid = fork();
113     if (pid == -1) {
114         WriteMess("child creation error");
115         exit(0);
116     }
117     /*
118      * second child, convert ppm to gif
119      */
120     if (pid == 0) {
121         /* send mime type */
122         close(pipeout[1]);
123         dup2(pipeout[0], STDIN_FILENO);
124         write(STDOUT_FILENO, content, strlen(content));
125         n=read(pipeout[0], buffer, sizeof(buffer));
126         printf("Letti %n di %n, %s\n", n, sizeof(buffer), buffer);
127         exit(0);
128         execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
129     }
130     /*
131      * still parent
132      */
133     close(pipeout[1]);
134     waitpid(pid, NULL, 0);
135     exit(0);
136 }
137 /*
138  * Routine to produce an HTML error message on output 
139  */
140 void WriteMess(char *mess)
141 {
142     printf("Content-type: text/html\n\n");
143     perror(mess);
144     printf("<br>\n");
145 }