-
Notifications
You must be signed in to change notification settings - Fork 0
/
rastertocbm1k.c
454 lines (387 loc) · 13.2 KB
/
rastertocbm1k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Citizen Systems
*
* CUPS Filter
*
* compile cmd: gcc -Wl,-rpath,/usr/lib -Wall -fPIC -O2 -o rastertocbm1k rastertocbm1k.c -lcupsimage -lcups
* compile requires cups-devel-1.1.19-13.i386.rpm (version not neccessarily important?)
* find cups-devel location here: http://rpmfind.net/linux/rpm2html/search.php?query=cups-devel&submit=Search+...&system=&arch=
*/
/*
* Copyright (C) 2006 Citizen Systems Co. Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Include necessary headers...
*/
#include <cups/cups.h>
#include <cups/ppd.h>
#include <cups/raster.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef RPMBUILD
#include <dlfcn.h>
typedef cups_raster_t* (*cupsRasterOpen_fndef) (int fd,cups_mode_t mode);
typedef unsigned (*cupsRasterReadHeader_fndef) (cups_raster_t *r,cups_page_header_t *h);
typedef unsigned (*cupsRasterReadPixels_fndef) (cups_raster_t *r,unsigned char *p,unsigned len);
typedef void (*cupsRasterClose_fndef) (cups_raster_t *r);
static cupsRasterOpen_fndef cupsRasterOpen_fn;
static cupsRasterReadHeader_fndef cupsRasterReadHeader_fn;
static cupsRasterReadPixels_fndef cupsRasterReadPixels_fn;
static cupsRasterClose_fndef cupsRasterClose_fn;
#define CUPSRASTEROPEN (*cupsRasterOpen_fn)
#define CUPSRASTERREADHEADER (*cupsRasterReadHeader_fn)
#define CUPSRASTERREADPIXELS (*cupsRasterReadPixels_fn)
#define CUPSRASTERCLOSE (*cupsRasterClose_fn)
typedef void(*ppdClose_fndef) (ppd_file_t*ppd);
typedef ppd_choice_t*(*ppdFindChoice_fndef) (ppd_option_t*o,constchar*option);
typedef ppd_choice_t*(*ppdFindMarkedChoice_fndef) (ppd_file_t*ppd,constchar*keyword);
typedef ppd_option_t*(*ppdFindOption_fndef) (ppd_file_t*ppd,constchar*keyword);
typedef void(*ppdMarkDefaults_fndef) (ppd_file_t*ppd);
typedef ppd_file_t*(*ppdOpenFile_fndef) (constchar*filename);
typedef void(*cupsFreeOptions_fndef) (intnum_options,cups_option_t*options);
typedef int(*cupsParseOptions_fndef) (constchar*arg,intnum_options,cups_option_t**options);
typedef int(*cupsMarkOptions_fndef) (ppd_file_t*ppd,intnum_options,cups_option_t*options);
static ppdClose_fndefppdClose_fn;
static ppdFindChoice_fndefppdFindChoice_fn;
static ppdFindMarkedChoice_fndefppdFindMarkedChoice_fn;
static ppdFindOption_fndefppdFindOption_fn;
static ppdMarkDefaults_fndefppdMarkDefaults_fn;
static ppdOpenFile_fndefppdOpenFile_fn;
static cupsFreeOptions_fndefcupsFreeOptions_fn;
static cupsParseOptions_fndefcupsParseOptions_fn;
static cupsMarkOptions_fndefcupsMarkOptions_fn;
#define PPDCLOSE (*ppdClose_fn)
#define PPDFINDCHOICE (*ppdFindChoice_fn)
#define PPDFINDMARKEDCHOICE (*ppdFindMarkedChoice_fn)
#define PPDFINDOPTION (*ppdFindOption_fn)
#define PPDMARKDEFAULTS (*ppdMarkDefaults_fn)
#define PPDOPENFILE (*ppdOpenFile_fn)
#define CUPSFREEOPTIONS (*cupsFreeOptions_fn)
#define CUPSPARSEOPTIONS (*cupsParseOptions_fn)
#define CUPSMARKOPTIONS (*cupsMarkOptions_fn)
#else
#define CUPSRASTEROPEN cupsRasterOpen
#define CUPSRASTERREADHEADER cupsRasterReadHeader
#define CUPSRASTERREADPIXELS cupsRasterReadPixels
#define CUPSRASTERCLOSE cupsRasterClose
#define PPDCLOSE ppdClose
#define PPDFINDCHOICE ppdFindChoice
#define PPDFINDMARKEDCHOICE ppdFindMarkedChoice
#define PPDFINDOPTION ppdFindOption
#define PPDMARKDEFAULTS ppdMarkDefaults
#define PPDOPENFILE ppdOpenFile
#define CUPSFREEOPTIONS cupsFreeOptions
#define CUPSPARSEOPTIONS cupsParseOptions
#define CUPSMARKOPTIONS cupsMarkOptions
#endif
#define FALSE 0
#define TRUE (!FALSE)
// definitions for printable width
#define MAX_WIDTH 80
#define STD_WIDTH 72
struct settings_
{
float pageWidth;
float pageHeight;
int pageCutType;
int docCutType;
int cashDrawer;
int cashDrawerPulseWidth;
int bytesPerScanLine;
int bytesPerScanLineStd;
};
struct command
{
int length;
char* command;
};
// define printer initialize command
static const struct command printerInitializeCommand=
{2,(char[2]){0x1b,'@'}};
// define start page command
const struct command startPageCommand={2,(char[2]){27,'2'}};
// define end page command
const struct command endPageCommand={0,NULL};
// define end job command
const struct command endJobCommand= {0,NULL};
// define page cut command
const struct command PageCutCommand={4,(char[]){/*0x1b,'J',176,*/0x1d,'V',66,0}};
inline void outputCommand(struct command output){
int i;
for(i=0;i<output.length;i++) putchar(output.command[i]);
}
inline int getOptionChoiceIndex(const char * choiceName, ppd_file_t * ppd)
{
ppd_choice_t *choice;
ppd_option_t *option;
choice=PPDFINDMARKEDCHOICE(ppd,choiceName);
if(!choice){
if(!(option=PPDFINDOPTION(ppd,choiceName))) return -1;
if(!(choice=PPDFINDCHOICE(option,option->defchoice))) return -1;
}
return atoi(choice->choice);
}
inline void getPageWidthPageHeight(ppd_file_t * ppd, struct settings_ * settings)
{
ppd_choice_t* choice;
ppd_option_t* option;
char width[20];
char height[20];
int Idx=0;
char* pageSize;
enum{sINIT,sWIDTH,sHEIGHT,sCOMPLETE,sFAIL} state=sINIT;
choice=PPDFINDMARKEDCHOICE(ppd,"PageSize");
if(!choice){
option=PPDFINDOPTION(ppd,"PageSize");
choice=PPDFINDCHOICE(option,option->defchoice);
}
pageSize=choice->choice;
while(*pageSize){
if(state==sINIT){
if(*pageSize=='X'){
state=sWIDTH;
pageSize++;
continue;
}
}else if(state==sWIDTH){
if ((*pageSize>='0')&&(*pageSize<='9')){
width[Idx++]=*pageSize++;
continue;
}else if(*pageSize=='D'){
width[Idx++]='.';
pageSize++;
continue;
}else if(*pageSize=='M'){
pageSize++;
continue;
}else if(*pageSize=='Y'){
state=sHEIGHT;
width[Idx]=0;
Idx=0;
pageSize++;
continue;
}
}else if(state==sHEIGHT){
if((*pageSize>='0')&&(*pageSize<='9')){
height[Idx++]=*pageSize++;
continue;
}else if(*pageSize=='D'){
height[Idx++]='.';
pageSize++;
continue;
}else if(*pageSize=='M'){
height[Idx]=0;
state=sCOMPLETE;
break;
}
}
state=sFAIL;
break;
}
if(state==sCOMPLETE){
settings->pageWidth=atof(width);
settings->pageHeight=atof(height);
}else
settings->pageWidth=
settings->pageHeight=0;
}
void initializeSettings(char * commandLineOptionSettings, struct settings_ * settings){
ppd_file_t* ppd;
cups_option_t* options =NULL;
int numOptions;
ppd=PPDOPENFILE(getenv("PPD"));
PPDMARKDEFAULTS(ppd);
numOptions=CUPSPARSEOPTIONS(commandLineOptionSettings,0,&options);
if((numOptions!=0)&&(options!=NULL)){
CUPSMARKOPTIONS(ppd,numOptions,options);
CUPSFREEOPTIONS(numOptions,options);
}
memset(settings,0,sizeof(struct settings_));
settings->pageCutType =getOptionChoiceIndex("PageCutType" ,ppd);
settings->docCutType =getOptionChoiceIndex("DocCutType" ,ppd);
settings->cashDrawer =getOptionChoiceIndex("CashDrawer" ,ppd);
settings->cashDrawerPulseWidth =getOptionChoiceIndex("CashDrawerPulseWidth",ppd);
settings->bytesPerScanLine =MAX_WIDTH;
settings->bytesPerScanLineStd =STD_WIDTH;
getPageWidthPageHeight(ppd,settings);
PPDCLOSE(ppd);
/*FILE *f;
f=fopen(LogFile,"a+t");
fprintf(f,"pageCutType %d\n" \
"docCutType %d\n" \
"cashDrawer %d\n" \
"cashDrawerPulseWidth %d\n" \
"bytesPerScanLine %d\n" \
"bytesPerScanLineStd %d\n" ,
settings->pageCutType ,
settings->docCutType ,
settings->cashDrawer ,
settings->cashDrawerPulseWidth,
settings->bytesPerScanLine ,
settings->bytesPerScanLineStd );
fclose(f); //*/
}
void jobSetup(struct settings_ settings){
unsigned char pw;
outputCommand(printerInitializeCommand);
if((settings.cashDrawer==1)||(settings.cashDrawer==2)){
pw=(settings.cashDrawerPulseWidth+1)*25;
printf("\x1bp%d%c%c",settings.cashDrawer-1,pw,pw);
}
}
void pageSetup(struct settings_ settings, cups_page_header_t header){
outputCommand(startPageCommand);
}
void endPage(struct settings_ settings)
{
outputCommand(endPageCommand);
if(settings.pageCutType==2)
outputCommand(PageCutCommand);
}
void endJob(struct settings_ settings)
{
if(settings.pageCutType==1)
outputCommand(PageCutCommand);
outputCommand(endJobCommand);
}
#define GET_LIB_FN_OR_EXIT_FAILURE(fn_ptr,lib,fn_name) \
{ \
fn_ptr=dlsym(lib,fn_name); \
if ((dlerror())){ \
fputs("ERROR: required fn not exported from dynamically loaded libary\n",stderr); \
if(libCupsImage) dlclose(libCupsImage); \
if(libCups) dlclose(libCups); \
return EXIT_FAILURE; \
} \
}
#ifdef RPMBUILD
#define CLEANUP \
{ \
if(rasterData) \
free(rasterData); \
CUPSRASTERCLOSE(ras); \
if(fd) \
close(fd); \
dlclose(libCupsImage); \
dlclose(libCups); \
}
#else
#define CLEANUP \
{ \
if(rasterData) \
free(rasterData); \
CUPSRASTERCLOSE(ras); \
if(fd) \
close(fd); \
}
#endif
int main(int argc, char *argv[])
{
int fd; /* File descriptor providing CUPS raster data */
cups_raster_t *ras; /* Raster stream for printing */
cups_page_header_t header; /* CUPS Page header */
int y, /* Vertical position in page 0 <= y <= header.cupsHeight */
i,
page=0; /* Current page */
unsigned char *rasterData=NULL; /* Pointer to raster data buffer */
struct settings_ settings; /* Configuration settings */
#ifdef RPMBUILD
void *libCupsImage; /* Pointer to libCupsImage library */
void *libCups; /* Pointer to libCups library */
FILE *f;
libCups=dlopen("libcups.so",RTLD_NOW|RTLD_GLOBAL);
if(!libCups){
fputs("ERROR: libcups.so load failure\n",stderr);
return EXIT_FAILURE;
}
libCupsImage=dlopen("libcupsimage.so",RTLD_NOW|RTLD_GLOBAL);
if(!libCupsImage){
fputs("ERROR: libcupsimage.so load failure\n",stderr);
dlclose(libCups);
return EXIT_FAILURE;
}
GET_LIB_FN_OR_EXIT_FAILURE(ppdClose_fn, libCups, "ppdClose" );
GET_LIB_FN_OR_EXIT_FAILURE(ppdFindChoice_fn, libCups, "ppdFindChoice" );
GET_LIB_FN_OR_EXIT_FAILURE(ppdFindMarkedChoice_fn, libCups, "ppdFindMarkedChoice" );
GET_LIB_FN_OR_EXIT_FAILURE(ppdFindOption_fn, libCups, "ppdFindOption" );
GET_LIB_FN_OR_EXIT_FAILURE(ppdMarkDefaults_fn, libCups, "ppdMarkDefaults" );
GET_LIB_FN_OR_EXIT_FAILURE(ppdOpenFile_fn, libCups, "ppdOpenFile" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsFreeOptions_fn, libCups, "cupsFreeOptions" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsParseOptions_fn, libCups, "cupsParseOptions" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsMarkOptions_fn, libCups, "cupsMarkOptions" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsRasterOpen_fn, libCupsImage, "cupsRasterOpen" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsRasterReadHeader_fn, libCupsImage, "cupsRasterReadHeader" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsRasterReadPixels_fn, libCupsImage, "cupsRasterReadPixels" );
GET_LIB_FN_OR_EXIT_FAILURE(cupsRasterClose_fn, libCupsImage, "cupsRasterClose" );
#endif
if((argc<6)||(argc>7)){
fputs("ERROR: rastertocbm1k job-id user title copies options [file]\n",stderr);
#ifdef RPMBUILD
dlclose(libCupsImage);
dlclose(libCups);
#endif
return EXIT_FAILURE;
}
if(argc==7){
if((fd=open(argv[6],O_RDONLY))==-1){
perror("ERROR: Unable to open raster file - ");
sleep(1);
#ifdef RPMBUILD
dlclose(libCupsImage);
dlclose(libCups);
#endif
return EXIT_FAILURE;
}
}else
fd=0;
initializeSettings(argv[5],&settings);
jobSetup(settings);
ras=CUPSRASTEROPEN(fd,CUPS_RASTER_READ);
while(CUPSRASTERREADHEADER(ras,&header)){
if((!header.cupsHeight)||(!header.cupsBytesPerLine)) break;
if(!rasterData){
if(!(rasterData=malloc(header.cupsBytesPerLine))){
CLEANUP;
return EXIT_FAILURE;
}
}
pageSetup(settings,header);
page++;
fprintf(stderr,"PAGE: %d %d\n",page,header.NumCopies);
settings.bytesPerScanLine =(header.cupsBytesPerLine<=settings.bytesPerScanLine)
?header.cupsBytesPerLine
:settings.bytesPerScanLineStd;
printf("\x1dv00");
putchar(header.cupsBytesPerLine); putchar(header.cupsBytesPerLine>>8);
putchar(header.cupsHeight); putchar(header.cupsHeight>>8);
for(y=0;y<header.cupsHeight;y++){
if(!(y&127))
fprintf(stderr,"INFO: Printing page %d, %d%% complete...\n",page,(100*y/header.cupsHeight));
if(CUPSRASTERREADPIXELS(ras,rasterData,header.cupsBytesPerLine)<1)
break;
for(i=0;i<header.cupsBytesPerLine;i++){
putchar(rasterData[i]);
}
}
endPage(settings);
}
endJob(settings);
CLEANUP;
fputs(page?"INFO: Ready to print.\n":"ERROR: No pages found!\n",stderr);
return(page)?EXIT_SUCCESS:EXIT_FAILURE;
}
// end of rastertocbm1k.c