X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FBarCode.c;h=872eacedf6fcc246126b9288029b25dd38dc59af;hp=7852dae397a3bd165cab915e290b7743516f121e;hb=fa15a3f1ecd64efd8440e46d398fd9976abc3d25;hpb=a29d9eebcd859ca662351848cc856e69f50c3ece diff --git a/sources/BarCode.c b/sources/BarCode.c index 7852dae..872eace 100644 --- a/sources/BarCode.c +++ b/sources/BarCode.c @@ -18,7 +18,7 @@ */ /**************************************************************** * - * Program echod + * Program barcode * CGI for barcode generation * * Author: Simone Piccardi @@ -29,27 +29,22 @@ * http://localhost/cgi-bin/barcode?string * where string is the code to be converted * - * $Id: BarCode.c,v 1.2 2002/06/21 22:24:10 piccardi Exp $ - * ****************************************************************/ /* * Include needed headers */ -#include /* predefined types */ -#include /* stat deinitiions */ -#include /* include unix standard library */ -/* */ -#include /* include standard I/O library */ -#include /* include standard library */ -#include /* include string library */ -#include /* include wait call */ -#include -#include -#include +#include /* primitive system data types */ +#include /* file characteristics constants and functions */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* C standard library */ +#include /* C strings library */ +#include /* process termination constants and functions */ +#include /* file control functions */ +#include /* C assertion functions */ +#include /* date and time constants, types and functions */ #include"macros.h" -void WriteMess(char *mess); - /* Program begin */ int main(int argc, char *argv[], char *envp[]) @@ -57,83 +52,31 @@ int main(int argc, char *argv[], char *envp[]) /* * Variables definition */ - pid_t pid; - int retval; - int pipein[2]; - int pipeout[2]; - char content[]="Content-type: image/jpeg\n\n"; - char size[]="-pA9"; - /* - * Begin - */ - /* create two pipes to handle process communication */ - if ( (retval = pipe(pipein)) ) { - WriteMess("input pipe creation error"); - exit(0); - } - if ( (retval = pipe(pipeout)) ) { - WriteMess("output pipe creation error"); - exit(0); - } - /* fork child to run barcode program */ - pid = fork(); - if (pid == -1) { - WriteMess("child creation error"); - exit(0); - } - /* if child */ - if (pid == 0) { - /* - * Child exec barcode program, that take input (string to encode) - * from pipein, remapped to stdin, and write the output (a PS - * image) to stdout, remapped to pipeout - */ - close(pipein[1]); /* close output side of input pipe */ - dup2(pipein[0], STDIN_FILENO); /* remap stdin in pipe input */ - close(pipeout[0]); - dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */ - execlp("barcode", "barcode", size, NULL); //"-o", "-", NULL); - } - /* - * Parent write string to pipe input and close it, - * then wait child execution and results form pipeout, - * then fork to convert PS to JPEG using gs - */ - close(pipein[0]); /* close input side of input pipe */ - write(pipein[1], argv[1], strlen(argv[1])); - close(pipein[1]); - waitpid(pid, NULL, 0); - /* - * refork to use gs - */ - pid = fork(); - if (pid == -1) { - WriteMess("child creation error"); - exit(0); + FILE *pipe[4]; + FILE *pipein; + char *cmd_string[4]={ + "pnmtopng", + "pnmmargin -white 10", + "pnmcrop", + "gs -sDEVICE=ppmraw -sOutputFile=- -sNOPAUSE -q - -c showpage -c quit" + }; + char content[]="Content-type: image/png\n\n"; + int i; + /* write mime-type to stdout */ + write(STDOUT_FILENO, content, strlen(content)); + /* execute chain of command */ + for (i=0; i<4; i++) { + pipe[i] = popen(cmd_string[i], "w"); + dup2(fileno(pipe[i]), STDOUT_FILENO); } - /* - * second child, convert PS to JPEG - */ - if (pid == 0) { - /* send mime type */ - close(pipeout[1]); - dup2(pipeout[0], STDIN_FILENO); - write(STDOUT_FILENO, content, strlen(content)); - execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL); + /* create barcode (in PS) */ + pipein = popen("barcode", "w"); + /* send barcode string to barcode program */ + write(fileno(pipein), argv[1], strlen(argv[1])); + pclose(pipein); + /* close all pipes (in reverse order) */ + for (i=4; i==0; i--) { + pclose((pipe[i])); } - /* - * still parent - */ - close(pipeout[1]); - waitpid(pid, NULL, 0); exit(0); } -/* - * Routine to produce an HTML error message on output - */ -void WriteMess(char *mess) -{ - printf("Content-type: text/html\n\n"); - perror(mess); - printf("
\n"); -}