*/
/****************************************************************
*
- * Program echod
+ * Program barcode
* CGI for barcode generation
*
* Author: Simone Piccardi
* http://localhost/cgi-bin/barcode?string
* where string is the code to be converted
*
- * $Id: BarCode.c,v 1.1 2002/06/18 22:11:06 piccardi Exp $
+ * $Id: BarCode.c,v 1.7 2003/01/06 16:44:20 piccardi Exp $
*
****************************************************************/
/*
#include <time.h>
#include"macros.h"
-void WriteMess(char *mess);
-
/* Program begin */
int main(int argc, char *argv[], char *envp[])
/*
* Variables definition
*/
- char buffer[8192];
- pid_t pid;
- size_t n;
- int retval;
- int pipein[2];
- int pipeout[2];
- char size[]="-pA9";
- char psize[]="-sPAPERSIZE=a9";
- char content[]="Content-type: image/jpeg\n\n";
- /*
- * 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);
+ 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);
}
- /* 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 ppm
- * 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 ppm to gif using ppmtogif
- */
- close(pipein[0]); /* close input side of input pipe */
- n=write(pipein[1], argv[1], strlen(argv[1]));
- close(pipein[1]);
- waitpid(pid, NULL, 0);
- /*
- * refork to use ppmtogif
- */
- pid = fork();
- if (pid == -1) {
- WriteMess("child creation error");
- exit(0);
+ /* 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]));
}
- /*
- * second child, convert ppm to gif
- */
- if (pid == 0) {
- /* send mime type */
- close(pipeout[1]);
- dup2(pipeout[0], STDIN_FILENO);
- write(STDOUT_FILENO, content, strlen(content));
- n=read(pipeout[0], buffer, sizeof(buffer));
- printf("Letti %n di %n, %s\n", n, sizeof(buffer), buffer);
- exit(0);
- execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
- }
- /*
- * 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("<br>\n");
-}