1 /*****************************************************************************\
2 hplipjs.c : HP CUPS filter for PostScript printers
4 Copyright (c) 2008, Hewlett-Packard Co.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name of the Hewlett-Packard nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 \*****************************************************************************/
38 #include <cups/cups.h>
39 #include <sys/types.h>
45 int HPWrite (int fd, void *pBuffer, int len)
47 fwrite (pBuffer, 1, len, HPFp);
54 static char *szJSStrings[] = {"HOLDKEY", "USERNAME", "JOBNAME", "HOLDTYPE", "DUPLICATEJOB"};
56 void SendJobHoldCommands (char *szJSOptions, int fd)
62 p = strstr (szJSOptions, "HOLD");
63 if (!(strncmp (p+5, "OFF", 3)))
65 while (*p && *p != '=')
69 memset (szStr, 0, sizeof (szStr));
70 strcpy (szStr, "@PJL SET HOLD=");
71 while (*p && *p > ' ' && k < 254)
76 HPWrite (fd, szStr, strlen (szStr));
79 iJS = sizeof (szJSStrings) / sizeof (szJSStrings[0]);
80 for (i = 0; i < iJS; i++)
82 if ((p = strstr (szJSOptions, szJSStrings[i])))
84 memset (szStr, 0, sizeof (szStr));
85 sprintf (szStr, "@PJL SET %s=", szJSStrings[i]);
86 while (*p && *p != '=')
92 while (*p && *p > ' ' && k < 254)
102 HPWrite (fd, szStr, strlen (szStr));
108 void GetOptionStringFromCups (char *pPrinter, int fd, char *user)
111 char szlpOptionsFile[1024];
114 pwd = getpwnam (user);
117 fprintf (stderr, "DEBUG: getpwnam failed for user %s\n", user);
120 sprintf (szlpOptionsFile, "%s/.cups/lpoptions", pwd->pw_dir);
121 fp = fopen (szlpOptionsFile, "r");
124 fprintf (stderr, "DEBUG: Unable to open lpoptions file %s\n", szlpOptionsFile);
129 fgets (szLine, 1020, fp);
130 if (strstr (szLine, pPrinter))
132 fprintf (stderr, "DEBUG: Got %s from lpoptions file\n", szLine);
133 SendJobHoldCommands (szLine + strlen (pPrinter) + 5, fd);
141 void GetOptionStringFromCups (char *pPrinter, int fd)
151 char szJSOptionString[1024];
153 fprintf (stderr, "DEBUG: In GetOption.... printer = %s, fd = %d\n", pPrinter, fd);
154 ppdFileName = (char *) cupsGetPPD (pPrinter);
157 fprintf (stderr, "DEBUG: did not get ppdfilename\n");
160 fprintf (stderr, "DEBUG: ppdFileName = %s\n", ppdFileName);
161 ppdFile = ppdOpenFile (ppdFileName);
162 fprintf (stderr, "DEBUG: ppdFileName = %s\n", ppdFileName);
165 fprintf (stderr, "DEBUG: unable to open ppdfile, %s\n", ppdFileName);
168 num_dests = cupsGetDests (&dests);
171 fprintf (stderr, "DEBUG: num_dests is zero\n");
175 dest = cupsGetDest (pPrinter, NULL, num_dests, dests);
178 fprintf (stderr, "DEBUG: did not get dest for printer %s\n", pPrinter);
182 ppdMarkDefaults (ppdFile);
183 cupsMarkOptions (ppdFile, dest->num_options, dest->options);
184 iJS = sizeof (szJSStrings) / sizeof (szJSStrings[0]);
185 if ((opt = (char *) cupsGetOption ("HOLD", dest->num_options, dest->options)) == NULL)
187 fprintf (stderr, "DEBUG: did not see HOLD option\nOptions in the list are:\n");
188 for (i = 0; i < dest->num_options; i++)
190 fprintf (stderr, "DEBUG: Option = %s : value = %s\n", dest->options[i].name, dest->options[i].value);
196 memset (szJSOptionString, 0, sizeof (szJSOptionString));
197 j = sprintf (szJSOptionString, "HOLD=%s", opt);
198 for (i = 0; i < iJS; i++)
200 if ((opt = (char *) cupsGetOption (szJSStrings[i], dest->num_options, dest->options)))
202 fprintf (stderr, "DEBUG: cupsGetOption returned %s = %s\n", szJSStrings[i], opt);
203 j += sprintf (szJSOptionString + j, " %s=%s", szJSStrings[i], opt);
207 cupsFreeDests (num_dests, dests);
208 fprintf (stderr, "DEBUG: Calling SendJobHoldCommands with %s\n", szJSOptionString);
209 SendJobHoldCommands (szJSOptionString, fd);
213 int main (int argc, char **argv)
222 char *szStartJob = "\x1B%-12345X@PJL JOBNAME=";
223 char *szUEL = "@PJL ENTER LANGUAGE=POSTSCRIPT\x0A";
224 char *szEndJob = "\x1B%-12345X@PJL EOJ\x0A\x1B%-12345X";
227 * Job storage command strings
229 * @PJL SET HOLD=ON | OFF | STORE | REPRINT
230 * hold in memory, regular job, store to hdd, reprint from memory
231 * @PJL SET HOLDTYPE=PRIVATE | PUBLIC
232 * @PJL SET HOLDKEY=1234
233 * @PJL SET USERNAME="user_name"
234 * @PJL SET JOBNAME="job_name"
235 * @PJL SET DUPLICATEJOB=APPEND | REPLACE
238 setbuf (stderr, NULL);
239 if (argc < 6 || argc > 7)
241 fprintf (stderr, "ERROR: Uage - %s JobId UserName Title NCopies OptionList [InputFile]\n", *argv);
245 // Doesn't really work
\r
246 // cupsSetUser (argv[2]);
249 for (i = 0; i < argc; i++)
251 fprintf (stderr, "DEBUG: HPLIPJS: argv[%d] = %s\n", i, argv[i]);
254 HPFp = fopen ("/tmp/output.ps", "w");
256 fd = 0; // read from stdin
257 Outfd = 1; // HPWrite to stdout
260 if ((fd = open (argv[6], O_RDONLY)) == -1)
262 fprintf (stderr, "ERROR: Unable to open input file %s for reading\n", argv[6]);
267 HPWrite (Outfd, szStartJob, strlen (szStartJob));
268 sprintf (pBuffer, "hplip-%s\x0A", argv[1]);
269 HPWrite (Outfd, pBuffer, strlen (pBuffer));
272 * Check if job storage option is selected. If so, send the PJL header and
273 * job storage commands.
276 if ((strstr (argv[5], "HOLD")))
278 fprintf (stderr, "DEBUG: found HOLD option\n");
279 SendJobHoldCommands (argv[5], Outfd);
283 fprintf (stderr, "DEBUG: did not find HOLD option, calling GetOption....\n");
284 GetOptionStringFromCups (argv[0], Outfd, argv[2]);
287 HPWrite (Outfd, szUEL, strlen (szUEL));
289 while ((n = read (fd, pBuffer, 256)) > 0)
291 HPWrite (Outfd, pBuffer, n);
294 HPWrite (Outfd, szEndJob, strlen (szEndJob));