[sane-devel] scanadf: waiting for child processes to finish

Tristan Hill stan@saticed.me.uk
Wed, 13 Oct 2004 16:23:29 +0100


--VS++wcV0S1rZb1Fb
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline


I use the --scan-script option with scanadf, however, if I want to do
some work on all the files processed by the script is it difficult
because scanadf may finish before one or more of the script processes
do.  For example, I want to use it to combine the processed images into
a single file.  As scanadf knows what processes it has forked I thought
it easiest to use wait(), implemented as per attached patch.  However, I
am open to other suggestions.

Thanks

--VS++wcV0S1rZb1Fb
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="scanadf-script-wait.patch"

--- scanadf.c.orig	2004-10-11 08:52:34.000000000 +0100
+++ scanadf.c	2004-10-13 15:47:42.000000000 +0100
@@ -39,6 +39,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 
 #include "sane/sane.h"
 #include "sane/sanei.h"
@@ -102,6 +103,7 @@
   { "start-count", required_argument, 0, 's' },
   { "end-count", required_argument, 0, 'e' },
   { "scan-script", required_argument, 0, 'S' },
+  { "script-wait", no_argument, 0, 128 },
   { "raw", no_argument, 0, 'r' },
   {0, }
 };
@@ -137,6 +139,7 @@
    [ -o | --output-file <name> ]     name of file to write image data\n\
                                      (%%d replacement in output file name).\n\
    [ -S | --scan-script <name> ]     name of script to run after every scan.\n\
+   [ --script-wait ]                 wait for scripts to finish before exit\n\
    [ -s | --start-count <num> ]      page count of first scanned image.\n\
    [ -e | --end-count <num> ]        last page number to scan.\n\
    [ -r | --raw ]                    write raw image data to file.\n";
@@ -1281,6 +1284,7 @@
   char *full_optstring;
   SANE_Bool raw = SANE_FALSE;
   const char *scanScript = NULL;		/* script to run at end of scan */
+  int scriptWait = 0;
   const char *outputFile = "image-%04d";	/* file name(format) to write output to */
   int startNum = 1, endNum = -1;		/* start/end numbers of pages to scan */
   int no_overwrite = 0;
@@ -1335,6 +1339,7 @@
 
 	case 'o': outputFile = optarg; break;
 	case 'S': scanScript = optarg; break;
+	case 128: scriptWait = 1; break;
 	case 's': startNum = atoi(optarg); break;
 	case 'e': endNum = atoi(optarg); break;
 	case 'r': raw = SANE_TRUE; break;
@@ -1348,6 +1353,9 @@
 	}
     }
 
+  if (scriptWait && !scanScript)
+    scriptWait = 0;
+
   if (help)
     printf (usage, prog_name);
 
@@ -1574,6 +1582,9 @@
   sane_cancel (device);
   sane_close (device);
 
+  if (scriptWait)
+    while (wait(NULL) != -1);
+
   return (status == SANE_STATUS_GOOD) ? 0 : 1;
 }
 

--VS++wcV0S1rZb1Fb--