]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/cupsfilter.c
Merge changes from CUPS 1.5svn-r9022.
[thirdparty/cups.git] / scheduler / cupsfilter.c
index 17f6f89ea8eb924b3df9cd33fbe9292e7a948e1a..5470bf549d6a93f86b0e611d0a7f0717154579d9 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   CUPS filtering program for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007-2009 by Apple Inc.
+ *   Copyright 2007-2010 by Apple Inc.
  *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -347,7 +347,7 @@ main(int  argc,                             /* I - Number of command-line args */
     else
     {
       _cupsLangPuts(stderr,
-                    _("cupsfilter: Only one filename can be specified!\n"));
+                    _("cupsfilter: Only one filename can be specified\n"));
       usage(command, NULL);
     }
 
@@ -382,7 +382,7 @@ main(int  argc,                             /* I - Number of command-line args */
   {
     _cupsLangPrintf(stderr,
                     _("%s: Unable to read MIME database from \"%s\" or "
-                     "\"%s\"!\n"),
+                     "\"%s\"\n"),
                    command, mimedir, ServerRoot);
     return (1);
   }
@@ -408,7 +408,7 @@ main(int  argc,                             /* I - Number of command-line args */
     if ((src = mimeType(mime, super, type)) == NULL)
     {
       _cupsLangPrintf(stderr,
-                     _("%s: Unknown source MIME type %s/%s!\n"),
+                     _("%s: Unknown source MIME type %s/%s\n"),
                      command, super, type);
       return (1);
     }
@@ -416,7 +416,7 @@ main(int  argc,                             /* I - Number of command-line args */
   else if ((src = mimeFileType(mime, infile, infile, &compression)) == NULL)
   {
     _cupsLangPrintf(stderr,
-                    _("%s: Unable to determine MIME type of \"%s\"!\n"),
+                    _("%s: Unable to determine MIME type of \"%s\"\n"),
                    command, infile);
     return (1);
   }
@@ -427,7 +427,7 @@ main(int  argc,                             /* I - Number of command-line args */
   else if ((dst = mimeType(mime, super, type)) == NULL)
   {
     _cupsLangPrintf(stderr,
-                    _("%s: Unknown destination MIME type %s/%s!\n"),
+                    _("%s: Unknown destination MIME type %s/%s\n"),
                    command, super, type);
     return (1);
   }
@@ -450,7 +450,7 @@ main(int  argc,                             /* I - Number of command-line args */
   else if ((filters = mimeFilter(mime, src, dst, &cost)) == NULL)
   {
     _cupsLangPrintf(stderr,
-                    _("%s: No filter to convert from %s/%s to %s/%s!\n"),
+                    _("%s: No filter to convert from %s/%s to %s/%s\n"),
                    command, src->super, src->type, dst->super, dst->type);
     return (1);
   }
@@ -721,7 +721,8 @@ exec_filter(const char *filter,             /* I - Filter to execute */
            int        infd,            /* I - Stdin file descriptor */
            int        outfd)           /* I - Stdout file descriptor */
 {
-  int          pid;                    /* Process ID */
+  int          pid,                    /* Process ID */
+               fd;                     /* Temporary file descriptor */
 #if defined(__APPLE__)
   char         processPath[1024],      /* CFProcessPath environment variable */
                linkpath[1024];         /* Link path for symlinks... */
@@ -765,28 +766,40 @@ exec_filter(const char *filter,           /* I - Filter to execute */
 
     if (infd != 0)
     {
-      close(0);
+      if (infd < 0)
+        infd = open("/dev/null", O_RDONLY);
+
       if (infd > 0)
-        dup(infd);
-      else
-        open("/dev/null", O_RDONLY);
+      {
+        dup2(infd, 0);
+       close(infd);
+      }
     }
 
     if (outfd != 1)
     {
-      close(1);
-      if (outfd > 0)
-       dup(outfd);
-      else
-        open("/dev/null", O_WRONLY);
+      if (outfd < 0)
+        outfd = open("/dev/null", O_WRONLY);
+
+      if (outfd > 1)
+      {
+       dup2(outfd, 1);
+       close(outfd);
+      }
     }
 
-    close(3);
-    open("/dev/null", O_RDWR);
+    if ((fd = open("/dev/null", O_RDWR)) > 3)
+    {
+      dup2(fd, 3);
+      close(fd);
+    }
     fcntl(3, F_SETFL, O_NDELAY);
 
-    close(4);
-    open("/dev/null", O_RDWR);
+    if ((fd = open("/dev/null", O_RDWR)) > 4)
+    {
+      dup2(fd, 4);
+      close(fd);
+    }
     fcntl(4, F_SETFL, O_NDELAY);
 
    /*
@@ -822,7 +835,7 @@ exec_filters(mime_type_t   *srctype,        /* I - Source type */
 {
   int          i;                      /* Looping var */
   const char   *argv[8],               /* Command-line arguments */
-               *envp[12],              /* Environment variables */
+               *envp[15],              /* Environment variables */
                *temp;                  /* Temporary string */
   char         *optstr,                /* Filter options */
                content_type[1024],     /* CONTENT_TYPE */
@@ -833,6 +846,9 @@ exec_filters(mime_type_t   *srctype,        /* I - Source type */
                lang[1024],             /* LANG */
                path[1024],             /* PATH */
                ppd[1024],              /* PPD */
+               printer_info[255],      /* PRINTER_INFO env variable */
+               printer_location[255],  /* PRINTER_LOCATION env variable */
+               printer_name[255],      /* PRINTER env variable */
                rip_cache[1024],        /* RIP_CACHE */
                userenv[1024],          /* USER */
                program[1024];          /* Program to run */
@@ -846,6 +862,7 @@ exec_filters(mime_type_t   *srctype,        /* I - Source type */
   cups_array_t *pids;                  /* Executed filters array */
   mime_filter_t        key;                    /* Search key for filters */
   cups_lang_t  *language;              /* Current language */
+  cups_dest_t  *dest;                  /* Destination information */
 
 
  /*
@@ -887,6 +904,34 @@ exec_filters(mime_type_t   *srctype,       /* I - Source type */
   snprintf(rip_cache, sizeof(rip_cache), "RIP_CACHE=%s", RIPCache);
   snprintf(userenv, sizeof(userenv), "USER=%s", user);
 
+  if (printer &&
+      (dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer, NULL)) != NULL)
+  {
+    if ((temp = cupsGetOption("printer-info", dest->num_options,
+                              dest->options)) != NULL)
+      snprintf(printer_info, sizeof(printer_info), "PRINTER_INFO=%s", temp);
+    else
+      snprintf(printer_info, sizeof(printer_info), "PRINTER_INFO=%s", printer);
+
+    if ((temp = cupsGetOption("printer-location", dest->num_options,
+                              dest->options)) != NULL)
+      snprintf(printer_location, sizeof(printer_location),
+               "PRINTER_LOCATION=%s", temp);
+    else
+      strlcpy(printer_location, "PRINTER_LOCATION=Unknown",
+              sizeof(printer_location));
+  }
+  else
+  {
+    snprintf(printer_info, sizeof(printer_info), "PRINTER_INFO=%s",
+             printer ? printer : "Unknown");
+    strlcpy(printer_location, "PRINTER_LOCATION=Unknown",
+            sizeof(printer_location));
+  }
+
+  snprintf(printer_name, sizeof(printer_name), "PRINTER=%s",
+          printer ? printer : "Unknown");
+
   argv[0] = (char *)printer;
   argv[1] = "1";
   argv[2] = user;
@@ -908,9 +953,12 @@ exec_filters(mime_type_t   *srctype,       /* I - Source type */
   envp[6]  = lang;
   envp[7]  = path;
   envp[8]  = ppd;
-  envp[9]  = rip_cache;
-  envp[10] = userenv;
-  envp[11] = NULL;
+  envp[9]  = printer_info;
+  envp[10] = printer_location;
+  envp[11] = printer_name;
+  envp[12] = rip_cache;
+  envp[13] = userenv;
+  envp[14] = NULL;
 
   for (i = 0; argv[i]; i ++)
     fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
@@ -1018,10 +1066,10 @@ exec_filters(mime_type_t   *srctype,    /* I - Source type */
       if (status)
       {
        if (WIFEXITED(status))
-         fprintf(stderr, "ERROR: %s (PID %d) stopped with status %d!\n",
+         fprintf(stderr, "ERROR: %s (PID %d) stopped with status %d\n",
                  filter->filter, pid, WEXITSTATUS(status));
        else
-         fprintf(stderr, "ERROR: %s (PID %d) crashed on signal %d!\n",
+         fprintf(stderr, "ERROR: %s (PID %d) crashed on signal %d\n",
                  filter->filter, pid, WTERMSIG(status));
 
         retval = 1;
@@ -1072,13 +1120,13 @@ get_job_file(const char *job)           /* I - Job ID */
 
   if (jobid < 1 || jobid > INT_MAX)
   {
-    _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid);
+    _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d\n"), (int)jobid);
     exit(1);
   }
 
   if (docnum < 1 || docnum > INT_MAX)
   {
-    _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d!\n"),
+    _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d\n"),
                     (int)docnum);
     exit(1);
   }
@@ -1296,7 +1344,7 @@ usage(const char *command,                /* I - Command name */
       const char *opt)                 /* I - Incorrect option, if any */
 {
   if (opt)
-    _cupsLangPrintf(stderr, _("%s: Unknown option '%c'!\n"), command, *opt);
+    _cupsLangPrintf(stderr, _("%s: Unknown option '%c'\n"), command, *opt);
 
   if (!strcmp(command, "cupsfilter"))
     _cupsLangPuts(stdout,