Nuovo esempio (funzionante) e primo abbozzo delle spiegazioni delle fifo.
[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 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: BarCode.c,v 1.5 2002/06/28 21:07:21 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     FILE *pipe[4];
61     FILE *pipein;
62     char *cmd_string[4]={
63         "pnmtopng",
64         "pnmmargin -white 10",
65         "pnmcrop",
66         "gs -sDEVICE=ppmraw -sOutputFile=- -sNOPAUSE -q - -c showpage -c quit"
67     };  
68     char content[]="Content-type: image/png\n\n";
69     int i;
70     /* write mime-type to stout */ 
71     write(STDOUT_FILENO, content, strlen(content));
72     /* execute chain of command */
73     for (i=0; i<4; i++) {
74         pipe[i] = popen(cmd_string[i], "w");
75         dup2(fileno(pipe[i]), STDOUT_FILENO); 
76     }
77     /* create barcode (in PS) */
78     pipein = popen("barcode", "w");
79     /* send barcode string to barcode program */
80     write(fileno(pipein), argv[1], strlen(argv[1]));
81     /* close all pipes (in reverse order) */
82     for (i=4; i==0; i--) {
83         pclose((pipe[i]));
84     }
85     exit(0);
86 }
87 /*
88  * Routine to produce an HTML error message on output 
89  */
90 void WriteMess(char *mess)
91 {
92     printf("Content-type: text/html\n\n");
93     perror(mess);
94     printf("<br>\n");
95 }