Nuovo esempio
authorSimone Piccardi <piccardi@gnulinux.it>
Tue, 18 Jun 2002 22:11:06 +0000 (22:11 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Tue, 18 Jun 2002 22:11:06 +0000 (22:11 +0000)
ipc.tex
sources/BarCode.c [new file with mode: 0644]
sources/Makefile

diff --git a/ipc.tex b/ipc.tex
index 7dd8c67907ad02b670c4e76a5a8482cee72705f8..21fae712f9ee9894e9c5061670b5b847cd8c4cbc 100644 (file)
--- a/ipc.tex
+++ b/ipc.tex
@@ -1,4 +1,4 @@
-\chapter{La comunicazione fra processi}
+f\chapter{La comunicazione fra processi}
 \label{cha:IPC}
 
 
 \label{cha:IPC}
 
 
@@ -240,12 +240,12 @@ capi inutilizzati delle pipe (input della prima ed output della seconda), poi
 scrive (\texttt{\small 30}) la stringa da convertire sull'output della prima
 pipe così che \cmd{barcode} possa riceverla dallo standard input; a questo
 punto l'uso della prima pipe è finito ed essa può essere definitivamente
 scrive (\texttt{\small 30}) la stringa da convertire sull'output della prima
 pipe così che \cmd{barcode} possa riceverla dallo standard input; a questo
 punto l'uso della prima pipe è finito ed essa può essere definitivamente
-chiusa (\texttt{\small 31}), si attenderà poi (\texttt{\small 32}) che pure
+chiusa (\texttt{\small 31}), si attenderà poi (\texttt{\small 32}) che
 l'esecuzione di \cmd{barcode} venga completata.
 
 Alla conclusione della sua esecuzione \cmd{barcode} avrà effettuato inviato
 l'immagine postscript del codice a barre sul capo in scrittura della seconda
 l'esecuzione di \cmd{barcode} venga completata.
 
 Alla conclusione della sua esecuzione \cmd{barcode} avrà effettuato inviato
 l'immagine postscript del codice a barre sul capo in scrittura della seconda
-pipe; 
+pipe; a questo punto 
 
 
 
 
 
 
diff --git a/sources/BarCode.c b/sources/BarCode.c
new file mode 100644 (file)
index 0000000..138e23a
--- /dev/null
@@ -0,0 +1,145 @@
+/* BarCode.c
+ * 
+ * Copyright (C) 2002 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program echod 
+ * CGI for barcode generation
+ *
+ * Author: Simone Piccardi
+ * Jun. 2002
+ *
+ * Usage: cgi-bin for apache.
+ * Called by downloading something like:
+ * 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 $ 
+ *
+ ****************************************************************/
+/* 
+ * Include needed headers
+ */
+#include <sys/types.h>   /* predefined types */
+#include <sys/stat.h>    /* stat deinitiions */
+#include <unistd.h>      /* include unix standard library */
+/* */
+#include <stdio.h>      /* include standard I/O library */
+#include <stdlib.h>     /* include standard library */
+#include <string.h>     /* include string library */
+#include <wait.h>       /* include wait call */
+#include <fcntl.h>
+#include <assert.h>
+#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);        
+    }
+    /* 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);
+    }
+    /*
+     * 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");
+}
index 7ce1d44acdcdb9081d4c9d5a472aa7f9e0aaf0bf..5579f28c2066861df1190a79a3043fcce525e6df 100644 (file)
@@ -11,6 +11,8 @@ OBJ = SockRead.o SockWrite.o
 FINAL = forktest errcode echo echod daytimed iterdaytimed daytime testfopen \
        testren
 
 FINAL = forktest errcode echo echod daytimed iterdaytimed daytime testfopen \
        testren
 
+barcode: BarCode.c
+       $(CC) $(CFLAGS) $^ -o $@
 
 getparam: getparam.c
        $(CC) $(CFLAGS) $^ -o $@
 
 getparam: getparam.c
        $(CC) $(CFLAGS) $^ -o $@