]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
foomatic-rip: correct handling of jobs 209/head
authorJaiLuthra1 <luthrajaiji@gmail.com>
Mon, 24 Feb 2020 12:07:40 +0000 (17:37 +0530)
committerJaiLuthra1 <luthrajaiji@gmail.com>
Mon, 24 Feb 2020 12:08:34 +0000 (17:38 +0530)
filter/foomatic-rip/foomaticrip.c
filter/foomatic-rip/pdf.c
filter/foomatic-rip/pdf.h
filter/foomatic-rip/postscript.c

index 73ef28cde1806daa4d3540b12847ed04884082da..ac248c02628f5129175aafcc2b77e953e1bc3a42 100644 (file)
@@ -668,6 +668,16 @@ int print_file(const char *filename, int convert)
                             "Couldn't dup stdout of pdf-to-ps\n");
 
                 clearerr(stdin);
+                int pagecount = pdf_count_pages(filename);
+                _log("File contains %d pages.\n", pagecount);
+                if (pagecount < 0) {
+                    _log("Unexpected page_count\n");
+                    return 0;
+                }
+                if (pagecount == 0) {
+                  _log("No pages left, outputting empty file.\n");
+                  return 1;
+                }
                 ret = print_file("<STDIN>", 0);
 
                 wait_for_process(renderer_pid);
@@ -687,7 +697,68 @@ int print_file(const char *filename, int convert)
         case PS_FILE:
             _log("Filetype: PostScript\n");
             if (file == stdin)
-                return print_ps(stdin, buf, n, filename);
+            {
+                if (convert)
+                {
+                    int fd;
+                    FILE *tmpfile;
+                    char tmpfilename[4096];
+
+                    snprintf(tmpfilename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir());
+                    fd = mkstemp(tmpfilename);
+                    if (fd < 0) {
+                        _log("Could not create temporary file: %s\n", strerror(errno));
+                        return EXIT_PRNERR_NORETRY_BAD_SETTINGS;
+                    }
+
+                    if (write(fd,buf,n) != n) {
+                        _log("ERROR: Can't copy stdin to temporary file\n");
+                        close(fd);
+                    }
+                    /* copy stdin to the tmp file */
+                    while ((n = read(0,buf,BUFSIZ)) > 0) {
+                        if (write(fd,buf,n) != n) {
+                            _log("ERROR: Can't copy stdin to temporary file\n");
+                            close(fd);
+                        }
+                    }
+                    if (lseek(fd,0,SEEK_SET) < 0) {
+                        _log("ERROR: Can't rewind temporary file\n");
+                        close(fd);
+                    }
+                    FILE *fp;
+
+                    if ((fp = fdopen(fd,"rb")) == 0) {
+                        _log("ERROR: Can't fdopen temporary file\n");
+                        close(fd);
+                    }
+                    char gscommand[65536];
+                    char output[31] = "";
+                    int pagecount;
+                    size_t bytes;
+                    filename = strdup(tmpfilename);
+                    snprintf(gscommand, 65536, "%s -q -dNOPAUSE -dBATCH -sDEVICE=bbox %s 2>&1 | grep -c HiResBoundingBox",
+                            CUPS_GHOSTSCRIPT, filename);
+                    FILE *pd = popen(gscommand, "r");
+                    bytes = fread(output, 1, 31, pd);
+                    pclose(pd);
+
+                    if (bytes <= 0 || sscanf(output, "%d", &pagecount) < 1)
+                    pagecount = -1;
+                    _log("File contains %d pages.\n", pagecount);
+                    if (pagecount < 0) {
+                        _log("Unexpected page_count\n");
+                        return 0;
+                    }
+                    if (pagecount == 0) {
+                        _log("No pages left, outputting empty file.\n");
+                        return 1;
+                    }
+                    return print_ps(fp, NULL, 0, filename);
+                }
+                else
+                    return print_ps(stdin, buf, n, filename);
+            }
             else
                 return print_ps(file, NULL, 0, filename);
 
index 1631f96f5b1e44ccc6ef33ce4cd68aa9023bbb71..7364a73d41d0166a8a7bd8a0860944d5ffc7c6a5 100644 (file)
@@ -39,7 +39,7 @@
 static int wait_for_renderer();
 
 
-static int pdf_count_pages(const char *filename)
+int pdf_count_pages(const char *filename)
 {
     char gscommand[CMDLINE_MAX];
     char output[63] = "";
index c9472a01a826a18531e19a1208b64c851f916e77..07e2f32a627e8546c541d29f659a3b6db926ad74 100644 (file)
@@ -25,6 +25,7 @@
 #define pdf_h
 
 int print_pdf(FILE *s, const char *alreadyread, size_t len, const char *filename, size_t startpos);
+int pdf_count_pages(const char *filemame);
 
 #endif
 
index 8b6f0ad59f089f9f50647b56c199a2490032f244..f0ddf0180b4ca6e7aa0200eb44e63bb2380acd07 100644 (file)
@@ -322,7 +322,6 @@ void _print_ps(stream_t *stream)
     pid_t rendererpid = 0;
     FILE *rendererhandle = NULL;
 
-    int empty = 1;
     int retval;
 
     dstr_t *tmp = create_dstr();
@@ -1010,7 +1009,6 @@ void _print_ps(stream_t *stream)
                         /* No renderer running, start it */
                         dstrcpy(tmp, psheader->data);
                         dstrcat(tmp, psfifo->data);
-                        empty = 0;
                         get_renderer_handle(tmp, &rendererhandle, &rendererpid);
                         /* psfifo is sent out, flush it */
                         dstrclear(psfifo);
@@ -1075,12 +1073,6 @@ void _print_ps(stream_t *stream)
 
     } while ((maxlines == 0 || linect < maxlines) && more_stuff != 0);
 
-    if (empty)
-    {
-        _log("No pages left, outputting empty file.\n");
-        return;
-    }
-
     /* Some buffer still containing data? Send it out to the renderer */
     if (more_stuff || inheader || !isempty(psfifo->data)) {
         /* Flush psfifo and send the remaining data to the renderer, this