File: utilPrinter.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (341 lines) | stat: -rw-r--r-- 7,999 bytes parent folder | download
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
/************************************************************************/
/*									*/
/*  Utility functions for printing.					*/
/*									*/
/************************************************************************/

#   include	"appUtilConfig.h"

#   include	<stdio.h>
#   include	<stdlib.h>
#   include	<string.h>

#   include	"utilPrinter.h"

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Find out what printers are available on the system.			*/
/*									*/
/*  1)  BSD/Linux style.						*/
/*  2)  AIX style. ( Thanks to Marc Selis from axi.nl )			*/
/*  3)  SysV/LPRng style.						*/
/*									*/
/*  NOTE: Before the CUPS, all commands were tried. Now that CUPS	*/
/*	implements them both on the same machine, the first one that	*/
/*	returns any printers is assumed to return them all.		*/
/*									*/
/************************************************************************/

/*  2,3 */
static int utilPrinterSetLpCommand(	PrintDestination *	pd,
					const char *		name,
					int			l )
    {
    pd->pdCommand= (char *)malloc( l+ 12 );
    if  ( ! pd->pdCommand )
	{ LXDEB(l,pd->pdCommand); return -1;	}
    sprintf( pd->pdCommand, "lp -s -d '%s'", name );
    pd->pdPrintKind= APPprinterPIPE;
    pd->pdPrinterName= (char *)malloc( l+ 1 );
    if  ( ! pd->pdPrinterName )
	{ LXDEB(l,pd->pdPrinterName); return -1;	}

    pd->pdCommandLength= strlen( pd->pdCommand );
    pd->pdPercentCount= 0;

    strcpy( pd->pdPrinterName, name );

    return 0;
    }

/*  1  */
static int utilPrinterGetLprPrinters(	const char *		command,
					int *			pDefaultPrinter,
					int *			pPrinterCount,
					PrintDestination **	pDestinations )
    {
    char			scratch[250+1];

    int				l;
    FILE *			f;
    int				found= 0;

    PrintDestination *		fresh;
    int				count= *pPrinterCount;
    const char *		defaultPrinterName;

    f= popen( command, "r" );
    if  ( ! f )
	{ return 0;	}

    while( fgets( scratch, 250, f ) )
	{
	l= strlen( scratch );
	if  ( l > 0 && scratch[l-1] == '\n' )
	    { scratch[l-1]= '\0';	}

	if  ( scratch[0] == '\t' || scratch[l-2] != ':' )
	    { continue;	}

	scratch[l-2]= '\0';

	fresh= (PrintDestination *)realloc( *pDestinations,
				    (count+ 1)*sizeof( PrintDestination ) );
	if  ( ! fresh )
	    { XDEB(fresh); return -1;	}
	*pDestinations= fresh;

	fresh += count;

	fresh->pdCommand= (char *)malloc( l+ 23 );
	if  ( ! fresh->pdCommand )
	    { LXDEB(l,fresh->pdCommand); return -1;	}
#	if 0
	Has the advantage that there is no maximum on the file size.
	Has the disadvantage that it does not work on SuSE Linux 6.0
	sprintf( fresh->pdCommand, "lpr -r -s -P '%s' '%s'",
							scratch, "%f" );
#	else
	sprintf( fresh->pdCommand, "lpr -r -P '%s' '%s'",
							scratch, "%f" );
#	endif
	fresh->pdPrintKind= APPprinterTMPFILE;
	fresh->pdPrinterName= strdup( scratch );
	if  ( ! fresh->pdPrinterName )
	    { LXDEB(l,fresh->pdPrinterName); return -1;	}

	fresh->pdCommandLength= strlen( fresh->pdCommand );
	fresh->pdPercentCount= 1;

	found++; count++;
	}

    pclose( f );

    defaultPrinterName= getenv( "PRINTER" );
    if  ( defaultPrinterName )
	{
	PrintDestination *	pd= *pDestinations;

	for ( l= count- found; l < count; pd++, l++ )
	    {
	    if  ( ! strcmp( defaultPrinterName, pd->pdPrinterName ) )
		{ *pDefaultPrinter= l; break;	}
	    }
	}

    *pPrinterCount= count;
    return found;
    }

/*  2  */
static int utilPrinterGetAixPrinters(	const char *		command,
					int *			pDefaultPrinter,
					int *			pPrinterCount,
					PrintDestination **	pDestinations )
    {
    char			scratch[250+1];

    int				l;
    FILE *			f;
    int				found= 0;

    PrintDestination *		fresh;
    int				count= *pPrinterCount;
    const char *		defaultPrinterName;

    f= popen( command, "r" );
    if  ( ! f )
	{ return 0;	}

    if  ( fgets( scratch, 250, f ) && fgets( scratch, 250, f ) )
	{
	while( fgets( scratch, 250, f ) )
	    {
	    char *	s= strchr( scratch, ' ' );
	    if  ( ! s )
		{ continue;	}
	    *s= '\0'; l= s- scratch;
    
	    fresh= (PrintDestination *)realloc( *pDestinations,
				    (count+ 1)*sizeof( PrintDestination ) );
	    if  ( ! fresh )
		{ XDEB(fresh); return -1;	}
	    *pDestinations= fresh;
    
	    if  ( utilPrinterSetLpCommand( fresh+ count, scratch, l ) )
		{ LDEB(l); return -1;	}
    
	    found++; count++;
	    }
	}

    pclose( f );

    defaultPrinterName= getenv( "LPDEST" );
    if  ( defaultPrinterName )
	{
	PrintDestination *	pd= *pDestinations;

	for ( l= count- found; l < count; pd++, l++ )
	    {
	    if  ( ! strcmp( defaultPrinterName, pd->pdPrinterName ) )
		{ *pDefaultPrinter= l; break;	}
	    }
	}

    *pPrinterCount= count;
    return found;
    }

/*  3  */
static int utilPrinterGetLpPrinters(	const char *		command,
					int *			pDefaultPrinter,
					int *			pPrinterCount,
					PrintDestination **	pDestinations )
    {
    char			scratch[250+1];

    int				l;
    FILE *			f;
    int				found= 0;

    PrintDestination *		fresh;
    int				count= *pPrinterCount;
    const char *		defaultPrinterName;

    /*  2  */
    f= popen( command, "r" );
    if  ( ! f )
	{ return 0;	}

    while( fgets( scratch, 250, f ) )
	{
	char *	s= strchr( scratch, ' ' );
	if  ( ! s )
	    { continue;	}
	*s= '\0'; l= s- scratch;

	fresh= (PrintDestination *)realloc( *pDestinations,
				(count+ 1)*sizeof( PrintDestination ) );
	if  ( ! fresh )
	    { XDEB(fresh); return -1;	}
	*pDestinations= fresh;

	if  ( utilPrinterSetLpCommand( fresh+ count, scratch, l ) )
	    { LDEB(l); return -1;	}

	found++; count++;
	}

    pclose( f );

    defaultPrinterName= getenv( "LPDEST" );
    if  ( defaultPrinterName )
	{
	PrintDestination *	pd= *pDestinations;

	for ( l= count- found; l < count; pd++, l++ )
	    {
	    if  ( ! strcmp( defaultPrinterName, pd->pdPrinterName ) )
		{ *pDefaultPrinter= l; break;	}
	    }
	}

    *pPrinterCount= count;
    return found;
    }

int utilPrinterGetPrinters(	int *			pPrinterCount,
				int *			pDefaultPrinter,
				PrintDestination **	pDestinations,
				const char *		customCommand,
				const char *		customName )
    {
    PrintDestination *		pd= (PrintDestination *)0;
    int				count= 0;
    int				defaultPrinter= -1;

    /*  1  */
    if  ( count == 0 )
	{
	utilPrinterGetLprPrinters(  "( lpc status ) 2>/dev/null",
						&defaultPrinter, &count, &pd );
	}

    /*  1b  */
    if  ( count == 0 )
	{
	utilPrinterGetLprPrinters(  "( /usr/sbin/lpc status ) 2>/dev/null",
						&defaultPrinter, &count, &pd );
	}

    /*  2  */
    if  ( count == 0 )
	{
	utilPrinterGetAixPrinters(  "( enq -As ) 2>/dev/null",
						&defaultPrinter, &count, &pd );
	}

    /*  3  */
    if  ( count == 0 )
	{
	utilPrinterGetLpPrinters(  "( lpstat -a ) 2>/dev/null",
						&defaultPrinter, &count, &pd );
	}

    if  ( defaultPrinter < 0 && count > 0 )
	{ defaultPrinter= 0;	}

    if  ( customName || customCommand )
	{
	if  ( ! customName )
	    { XXDEB(customName,customCommand);	}
	if  ( ! customCommand )
	    { XXDEB(customName,customCommand);	}
	}

    if  ( customName && customCommand )
	{
	const char *		p;
	PrintDestination *	fresh;

	fresh= (PrintDestination *)realloc( pd,
				(count+ 1)*sizeof( PrintDestination ) );
	if  ( ! fresh )
	    { XDEB(fresh); return -1;	}
	pd= fresh;

	fresh += count;

	fresh->pdCommand= strdup( customCommand );
	fresh->pdPrinterName= strdup( customName );

	fresh->pdCommandLength= strlen( customCommand );
	fresh->pdPercentCount= 0;

	p= customCommand;
	while( *p )
	    {
	    if  ( p[0] == '%' && p[1] == 'f' )
		{ fresh->pdPercentCount++;	}
	    p++;
	    }

	if  ( fresh->pdPercentCount > 0 )
	    { fresh->pdPrintKind= APPprinterTMPFILE;	}
	else{ fresh->pdPrintKind= APPprinterPIPE;	}

	if  ( fresh->pdCommand && fresh->pdPrinterName )
	    { defaultPrinter= count++;				}
	else{ XXDEB(fresh->pdCommand,fresh->pdPrinterName);	}
	}

    *pPrinterCount= count;
    *pDefaultPrinter= defaultPrinter;
    *pDestinations= pd;

    return 0;
    }