~ubuntu-branches/ubuntu/raring/hplip/raring

« back to all changes in this revision

Viewing changes to prnt/hpps/hppsfilter.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2012-10-06 15:03:44 UTC
  • mfrom: (1.6.1) (20.1.16 quantal)
  • Revision ID: package-import@ubuntu.com-20121006150344-2p3xz26br0t3hu2q
Tags: 3.12.10-1
* New upstream release
  - Fixes "Network scanning fails (Closes: #683033)
* quilt refresh hplip-syslog-fix-debug-messages-to-error.dpatch
* Fix "error in clean build env" updated debian/rules (Closes: #687129)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************\
 
2
    hppsfilter.c : HP PS filter for PostScript printers
 
3
 
 
4
    Copyright (c) 2011, Hewlett-Packard Co.
 
5
    All rights reserved.
 
6
 
 
7
    Redistribution and use in source and binary forms, with or without
 
8
    modification, are permitted provided that the following conditions
 
9
    are met:
 
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.
 
18
 
 
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
 
 
31
   Author: Yashwant Kumar Sahu
 
32
\*****************************************************************************/
 
33
 
 
34
#include <stdio.h>
 
35
#include <string.h>
 
36
#include <memory.h>
 
37
#include <stdlib.h>
 
38
#include <unistd.h>
 
39
#include <fcntl.h>
 
40
#include <cups/cups.h>
 
41
#include <cups/ppd.h>
 
42
#include <sys/types.h>
 
43
#include <sys/stat.h>
 
44
 
 
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/"
 
52
 
 
53
#define LINE_SIZE  258
 
54
#define FILE_NAME_SIZE 128
 
55
 
 
56
#define SAVE_PS_FILE      2
 
57
 
 
58
/*  save final output ps file: in cupsd.conf file  value #hpLogLevel 15  */
 
59
static int g_savepsfile = 0;
 
60
 
 
61
/*  final ps outfile file handle */
 
62
static FILE *g_fp_outdbgps = NULL;
 
63
 
 
64
/* get log level from the cups config file */
 
65
void get_LogLevel ()
 
66
{
 
67
    FILE    *fp;
 
68
    char    str[258];
 
69
    char    *p;
 
70
    fp = fopen ("/etc/cups/cupsd.conf", "r");
 
71
    if (fp == NULL)
 
72
        return;
 
73
    while (!feof (fp))
 
74
    {
 
75
        if (!fgets (str, 256, fp))
 
76
        {
 
77
            break;
 
78
        }
 
79
        if ((p = strstr (str, "hpLogLevel")))
 
80
        {
 
81
            p += strlen ("hpLogLevel") + 1;
 
82
            g_savepsfile = atoi (p);
 
83
            break;
 
84
        }
 
85
    }
 
86
    fclose (fp);
 
87
}
 
88
 
 
89
 
 
90
/* create ps file for debugging purpose using job id */
 
91
void open_dbg_outfile(char* szjob_id)
 
92
{
 
93
    g_fp_outdbgps = NULL;
 
94
    if (g_savepsfile & SAVE_PS_FILE)
 
95
    {
 
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);
 
100
    }
 
101
}
 
102
 
 
103
/* Writting into out file and debug file if debug level is set to 15 */
 
104
int hpwrite (void *pBuffer, size_t size)
 
105
{
 
106
        int ndata_written = 0;
 
107
        if(g_fp_outdbgps)
 
108
        {
 
109
                ndata_written = fwrite (pBuffer, 1, size, g_fp_outdbgps);
 
110
        }
 
111
        
 
112
        ndata_written = write (STDOUT_FILENO, pBuffer, size);
 
113
        return ndata_written;
 
114
}
 
115
 
 
116
/* Read HPBOD attribute from PPD 
 
117
 return 1 if set as 1 */
 
118
int require_bod()
 
119
{
 
120
    int bodRequire = 0;
 
121
    ppd_attr_t  *pattr = NULL;
 
122
    ppd_file_t  *pppd = NULL;   
 
123
    pppd = ppdOpenFile(getenv("PPD"));
 
124
    if (pppd == NULL) {
 
125
        fprintf (stderr, "HP PS : ppdOpenFile failed for %s\n", getenv("PPD"));         
 
126
        return 0;
 
127
    }
 
128
        
 
129
    if (((pattr = ppdFindAttr(pppd, BOD_PPD_ATR, NULL)) != NULL) &&
 
130
        (pattr && pattr->value != NULL)) {
 
131
        bodRequire = atoi(pattr->value);
 
132
    }
 
133
    return bodRequire;
 
134
}
 
135
 
 
136
/* Write BOD PJL command DMINFO */
 
137
void emmit_bod_command()
 
138
{
 
139
        struct    tm   *tgmt = NULL;
 
140
        time_t    long_time;
 
141
        char sBOD[100] = {0};
 
142
    char sBOD_Var[29] = {0};
 
143
    char sDateTime[15] = {0};
 
144
        int index = 0;
 
145
        
 
146
        time(&long_time);
 
147
        tgmt = gmtime(&long_time);
 
148
                        
 
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);
 
151
        
 
152
    for (index = 0; index < (sizeof(sDateTime) -1); index++)
 
153
    {
 
154
        sprintf( sBOD_Var + (index*2), "%2X", sDateTime[index ]);
 
155
    }
 
156
                
 
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));
 
160
}
 
161
 
 
162
int main (int argc, char **argv)
 
163
{
 
164
        cups_file_t     *fp_input =  NULL;                      /* input file: stdin or physical file */        
 
165
    char   line[LINE_SIZE] = {0};
 
166
        
 
167
        get_LogLevel();
 
168
        setbuf (stderr, NULL);
 
169
        fprintf (stderr, "HP PS filter starting : %s \n", *argv);
 
170
        
 
171
        int i =  0;
 
172
        /* Logging cups filter arguments */
 
173
        for (   i = 0; i < argc; i++)
 
174
        {
 
175
                fprintf (stderr, "DEBUG: hppsfilter: argv[%d] = %s\n", i, argv[i]);
 
176
        }
 
177
        
 
178
        /* Logging debug information and creating outfile for debug */
 
179
        if (g_savepsfile & SAVE_PS_FILE)
 
180
        {
 
181
                /* opening ps debug file */
 
182
                open_dbg_outfile(argv[1]);
 
183
        }
 
184
        
 
185
        /* Check command-line...  */
 
186
        if (argc < 6 || argc > 7)
 
187
        {
 
188
                fputs("ERROR: hppsfilter job-id user title copies options [file]\n", stderr);
 
189
                return (1);
 
190
        }
 
191
 
 
192
        /* If we have 7 arguments, print the file named on the command-line.
 
193
        * Otherwise, send stdin instead...  */
 
194
        if (argc == 6)
 
195
                fp_input = cupsFileStdin();
 
196
        else
 
197
        {
 
198
                /* Try to open the print file...    */
 
199
                if ((fp_input = cupsFileOpen(argv[6], "r")) == NULL)
 
200
                {
 
201
                        fprintf(stderr, "ERROR: Unable to open print file \"%s\" - %s\n",
 
202
              argv[6], strerror);
 
203
                        return (1);
 
204
                }
 
205
        }
 
206
 
 
207
        int len  =  0;
 
208
        int ibodwritten = 0;
 
209
        int ireqire_bod = require_bod();
 
210
    while ( ( len = cupsFileGetLine(fp_input, line, sizeof(line) )) > 0)
 
211
    {
 
212
                hpwrite (line, len);
 
213
                /* if BOD is reqire, writting BOD command */
 
214
                if ( !ibodwritten && (!strncmp(line, "@PJL JOB NAME", 13)) && ireqire_bod)
 
215
                {
 
216
                        fprintf (stderr, "HP PS filter: Matched start PJL\n");                          
 
217
                        ibodwritten = 1;
 
218
                        emmit_bod_command();
 
219
                }
 
220
    }
 
221
 
 
222
    if ( (argc == 7) && (fp_input != NULL) )
 
223
        cupsFileClose (fp_input);
 
224
        
 
225
        if(g_fp_outdbgps != NULL)
 
226
        {
 
227
            fclose (g_fp_outdbgps);             
 
228
        }
 
229
                           
 
230
        fprintf (stderr, "HP PS filter Ends\n");        
 
231
    return 0;
 
232
}