1
/*****************************************************************************\
2
hppsfilter.c : HP PS filter for PostScript printers
4
Copyright (c) 2011, 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.
31
Author: Yashwant Kumar Sahu
32
\*****************************************************************************/
40
#include <cups/cups.h>
42
#include <sys/types.h>
45
#define PJL_HEADER "\x1B%-12345X@PJL\x0A"
46
#define BOD_PJL_FIXED "0400040101020D10100115"
47
#define BOD_PPD_ATR "HPBOD"
48
#define BOD_PJL "@PJL DMINFO ASCIIHEX=\"%s%s\"\012"
49
#define BOD_DATETIME_FORMAT "%04d%02d%02d%02d%02d%02d"
50
#define DBG_PSFILE "hpps_job"
51
#define DBG_TMP_FOLDER "/var/log/hp/tmp/"
54
#define FILE_NAME_SIZE 128
56
#define SAVE_PS_FILE 2
58
/* save final output ps file: in cupsd.conf file value #hpLogLevel 15 */
59
static int g_savepsfile = 0;
61
/* final ps outfile file handle */
62
static FILE *g_fp_outdbgps = NULL;
64
/* get log level from the cups config file */
70
fp = fopen ("/etc/cups/cupsd.conf", "r");
75
if (!fgets (str, 256, fp))
79
if ((p = strstr (str, "hpLogLevel")))
81
p += strlen ("hpLogLevel") + 1;
82
g_savepsfile = atoi (p);
90
/* create ps file for debugging purpose using job id */
91
void open_dbg_outfile(char* szjob_id)
94
if (g_savepsfile & SAVE_PS_FILE)
96
char sfile_name[FILE_NAME_SIZE] = {0};
97
sprintf(sfile_name, "%s/%s_%d.out",DBG_TMP_FOLDER,DBG_PSFILE, szjob_id);
98
g_fp_outdbgps= fopen(sfile_name, "w");
99
chmod(sfile_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
103
/* Writting into out file and debug file if debug level is set to 15 */
104
int hpwrite (void *pBuffer, size_t size)
106
int ndata_written = 0;
109
ndata_written = fwrite (pBuffer, 1, size, g_fp_outdbgps);
112
ndata_written = write (STDOUT_FILENO, pBuffer, size);
113
return ndata_written;
116
/* Read HPBOD attribute from PPD
117
return 1 if set as 1 */
121
ppd_attr_t *pattr = NULL;
122
ppd_file_t *pppd = NULL;
123
pppd = ppdOpenFile(getenv("PPD"));
125
fprintf (stderr, "HP PS : ppdOpenFile failed for %s\n", getenv("PPD"));
129
if (((pattr = ppdFindAttr(pppd, BOD_PPD_ATR, NULL)) != NULL) &&
130
(pattr && pattr->value != NULL)) {
131
bodRequire = atoi(pattr->value);
136
/* Write BOD PJL command DMINFO */
137
void emmit_bod_command()
139
struct tm *tgmt = NULL;
141
char sBOD[100] = {0};
142
char sBOD_Var[29] = {0};
143
char sDateTime[15] = {0};
147
tgmt = gmtime(&long_time);
149
sprintf(sDateTime, BOD_DATETIME_FORMAT, (tgmt->tm_year + 1900),
150
(tgmt->tm_mon + 1), tgmt->tm_mday, tgmt->tm_hour, tgmt->tm_min, tgmt->tm_sec);
152
for (index = 0; index < (sizeof(sDateTime) -1); index++)
154
sprintf( sBOD_Var + (index*2), "%2X", sDateTime[index ]);
157
sprintf(sBOD, BOD_PJL, BOD_PJL_FIXED, sBOD_Var);
158
fprintf (stderr, "HP PS filter sending BOD PJL: - %s\n", sBOD);
159
hpwrite (sBOD, strlen (sBOD));
162
int main (int argc, char **argv)
164
cups_file_t *fp_input = NULL; /* input file: stdin or physical file */
165
char line[LINE_SIZE] = {0};
168
setbuf (stderr, NULL);
169
fprintf (stderr, "HP PS filter starting : %s \n", *argv);
172
/* Logging cups filter arguments */
173
for ( i = 0; i < argc; i++)
175
fprintf (stderr, "DEBUG: hppsfilter: argv[%d] = %s\n", i, argv[i]);
178
/* Logging debug information and creating outfile for debug */
179
if (g_savepsfile & SAVE_PS_FILE)
181
/* opening ps debug file */
182
open_dbg_outfile(argv[1]);
185
/* Check command-line... */
186
if (argc < 6 || argc > 7)
188
fputs("ERROR: hppsfilter job-id user title copies options [file]\n", stderr);
192
/* If we have 7 arguments, print the file named on the command-line.
193
* Otherwise, send stdin instead... */
195
fp_input = cupsFileStdin();
198
/* Try to open the print file... */
199
if ((fp_input = cupsFileOpen(argv[6], "r")) == NULL)
201
fprintf(stderr, "ERROR: Unable to open print file \"%s\" - %s\n",
209
int ireqire_bod = require_bod();
210
while ( ( len = cupsFileGetLine(fp_input, line, sizeof(line) )) > 0)
213
/* if BOD is reqire, writting BOD command */
214
if ( !ibodwritten && (!strncmp(line, "@PJL JOB NAME", 13)) && ireqire_bod)
216
fprintf (stderr, "HP PS filter: Matched start PJL\n");
222
if ( (argc == 7) && (fp_input != NULL) )
223
cupsFileClose (fp_input);
225
if(g_fp_outdbgps != NULL)
227
fclose (g_fp_outdbgps);
230
fprintf (stderr, "HP PS filter Ends\n");