2 Unix SMB/Netbios implementation.
5 Copyright (C) Karl Auer 1993-1998
7 Re-working by Martin Kiff, 1994
9 Re-written again by Andrew Tridgell
11 Modified for SVID support by Norm Jacobs, 1997
13 Modified for CUPS support by Michael Sweet, 1999
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * Parse printcap file.
33 * This module does exactly one thing - it looks into the printcap file
34 * and tells callers if a specified string appears as a printer name.
36 * The way this module looks at the printcap file is very simplistic.
37 * Only the local printcap file is inspected (no searching of NIS
40 * There are assumed to be one or more printer names per record, held
41 * as a set of sub-fields separated by vertical bar symbols ('|') in the
42 * first field of the record. The field separator is assumed to be a colon
43 * ':' and the record separator a newline.
45 * Lines ending with a backspace '\' are assumed to flag that the following
46 * line is a continuation line so that a set of lines can be read as one
49 * A line stating with a hash '#' is assumed to be a comment and is ignored
50 * Comments are discarded before the record is strung together from the
51 * set of continuation lines.
53 * Opening a pipe for "lpc status" and reading that would probably
54 * be pretty effective. Code to do this already exists in the freely
55 * distributable PCNFS server code.
57 * Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
58 * in smb.conf under Solaris.
60 * Modified to call CUPS support if printcap name is set to "cups"
69 /* ******************************************
70 Extend for AIX system and qconfig file
71 from 'boulard@univ-rennes1.fr
72 ****************************************** */
73 static int strlocate(char *xpLine
,char *xpS
)
84 if (strncmp(p
,xpS
,iS
) == 0) {iRet
=1;break;};
88 /*DEBUG(3,(" strlocate %s in line '%s',ret=%d\n",xpS,xpLine,iRet));*/
94 /* ******************************************************************* */
95 /* * Scan qconfig and search all virtual printer (device printer) * */
96 /* ******************************************************************* */
97 static void ScanQconfig_fn(char *psz
,void (*fn
)(char *, char *))
102 pstring name
,comment
;
107 if ((pfile
= sys_fopen(psz
, "r")) == NULL
)
109 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz
));
114 /* scan qconfig file for searching <printername>: */
115 for (;(line
= fgets_slash(NULL
,sizeof(pstring
),pfile
)); safe_free(line
))
117 if (*line
== '*' || *line
== 0)
121 case 0: /* locate an entry */
122 if (*line
== '\t' || *line
== ' ') continue;
123 if ((p
=strchr(line
,':')))
126 p
= strtok(line
,":");
127 if (strcmp(p
,"bsh")!=0)
135 case 1: /* scanning device stanza */
136 if (*line
== '*' || *line
== 0) continue;
137 if (*line
!= '\t' && *line
!= ' ')
139 /* name is found without stanza device */
140 /* probably a good printer ??? */
146 if (strlocate(line
,"backend"))
148 /* it's a device, not a virtual printer*/
151 else if (strlocate(line
,"device"))
153 /* it's a good virtual printer */
164 /* Scan qconfig file and locate de printername */
166 static BOOL
ScanQconfig(char *psz
,char *pszPrintername
)
175 if ((pszPrintername
!= NULL
) && ((iLg
= strlen(pszPrintername
)) > 0))
176 pName
= malloc(iLg
+10);
179 DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername
));
182 if ((pfile
= sys_fopen(psz
, "r")) == NULL
)
184 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz
));
188 slprintf(pName
, iLg
+ 9, "%s:",pszPrintername
);
190 /*DEBUG(3,( " Looking for entry %s\n",pName));*/
192 /* scan qconfig file for searching <printername>: */
193 for (;(line
= fgets_slash(NULL
,sizeof(pstring
),pfile
)); safe_free(line
))
195 if (*line
== '*' || *line
== 0)
199 case 0: /* scanning entry */
200 if (strncmp(line
,pName
,iLg
) == 0)
206 case 1: /* scanning device stanza */
207 if (*line
== '*' || *line
== 0) continue;
208 if (*line
!= '\t' && *line
!= ' ')
210 /* name is found without stanza device */
211 /* probably a good printer ??? */
218 if (strlocate(line
,"backend"))
220 /* it's a device, not a virtual printer*/
223 else if (strlocate(line
,"device"))
225 /* it's a good virtual printer */
241 /***************************************************************************
242 Scan printcap file pszPrintcapname for a printer called pszPrintername.
243 Return True if found, else False. Returns False on error, too, after logging
244 the error at level 0. For generality, the printcap name may be passed - if
245 passed as NULL, the configuration will be queried for the name. pszPrintername
246 must be in DOS codepage.
247 The xxx_printername_ok functions need fixing to understand they are being
248 given a DOS codepage. FIXME !! JRA.
249 ***************************************************************************/
251 BOOL
pcap_printername_ok(char *pszPrintername
, const char *pszPrintcapname
)
258 if (pszPrintername
== NULL
|| pszPrintername
[0] == '\0')
260 DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
264 /* only go looking if no printcap name supplied */
265 if ((psz
= pszPrintcapname
) == NULL
|| psz
[0] == '\0')
266 if (((psz
= lp_printcapname()) == NULL
) || (psz
[0] == '\0'))
268 DEBUG(0,( "No printcap file name configured!\n"));
273 if (strequal(psz
, "cups"))
274 return (cups_printername_ok(pszPrintername
));
275 #endif /* HAVE_CUPS */
278 if (strequal(psz
, "lpstat"))
279 return (sysv_printername_ok(pszPrintername
));
283 if (strlocate(psz
,"/qconfig"))
284 return(ScanQconfig(psz
,pszPrintername
));
287 if ((pfile
= sys_fopen(psz
, "r")) == NULL
)
289 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz
));
293 for (;(line
= fgets_slash(NULL
,sizeof(pstring
),pfile
)); safe_free(line
))
295 if (*line
== '#' || *line
== 0)
300 /* now we have a real printer line - cut it off at the first : */
301 p
= strchr(line
,':');
304 /* now just check if the name is in the list */
305 /* NOTE: I avoid strtok as the fn calling this one may be using it */
308 if ((q
= strchr(p
,'|'))) *q
++ = 0;
310 if (strequal(p
,pszPrintername
))
312 /* normalise the case */
313 pstrcpy(pszPrintername
,p
);
327 /***************************************************************************
328 run a function on each printer name in the printcap file. The function is
329 passed the primary name and the comment (if possible). Note the fn() takes
330 strings in DOS codepage. This means the xxx_printer_fn() calls must be fixed
331 to return DOS codepage. FIXME !! JRA.
332 ***************************************************************************/
333 void pcap_printer_fn(void (*fn
)(char *, char *))
335 pstring name
,comment
;
341 /* only go looking if no printcap name supplied */
342 if (((psz
= lp_printcapname()) == NULL
) || (psz
[0] == '\0'))
344 DEBUG(0,( "No printcap file name configured!\n"));
349 if (strequal(psz
, "cups")) {
353 #endif /* HAVE_CUPS */
356 if (strequal(psz
, "lpstat")) {
363 if (strlocate(psz
,"/qconfig"))
365 ScanQconfig_fn(psz
,fn
);
370 if ((pfile
= sys_fopen(psz
, "r")) == NULL
)
372 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz
));
376 for (;(line
= fgets_slash(NULL
,sizeof(pstring
),pfile
)); safe_free(line
))
378 if (*line
== '#' || *line
== 0)
381 /* now we have a real printer line - cut it off at the first : */
382 p
= strchr(line
,':');
387 /* now find the most likely printer name and comment
388 this is pure guesswork, but it's better than nothing */
393 BOOL has_punctuation
;
394 if ((q
= strchr(p
,'|'))) *q
++ = 0;
396 has_punctuation
= (strchr(p
,' ') || strchr(p
,'\t') || strchr(p
,'(') || strchr(p
,')'));
398 if (strlen(p
)>strlen(comment
) && has_punctuation
)
400 StrnCpy(comment
,p
,sizeof(comment
)-1);
404 if (strlen(p
) <= MAXPRINTERLEN
&& strlen(p
)>strlen(name
) && !has_punctuation
)
406 if (!*comment
) pstrcpy(comment
,name
);
411 if (!strchr(comment
,' ') &&
412 strlen(p
) > strlen(comment
))
414 StrnCpy(comment
,p
,sizeof(comment
)-1);
420 name
[MAXPRINTERLEN
] = 0;