2 Unix SMB/Netbios implementation.
5 Copyright (C) Karl Auer 1993-1997
7 Re-working by Martin Kiff, 1994
9 Re-written again by Andrew Tridgell
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Parse printcap file.
29 * This module does exactly one thing - it looks into the printcap file
30 * and tells callers if a specified string appears as a printer name.
32 * The way this module looks at the printcap file is very simplistic.
33 * Only the local printcap file is inspected (no searching of NIS
36 * There are assumed to be one or more printer names per record, held
37 * as a set of sub-fields separated by vertical bar symbols ('|') in the
38 * first field of the record. The field separator is assumed to be a colon
39 * ':' and the record separator a newline.
41 * Lines ending with a backspace '\' are assumed to flag that the following
42 * line is a continuation line so that a set of lines can be read as one
45 * A line stating with a hash '#' is assumed to be a comment and is ignored
46 * Comments are discarded before the record is strung together from the
47 * set of continuation lines.
49 * Opening a pipe for "lpc status" and reading that would probably
50 * be pretty effective. Code to do this already exists in the freely
51 * distributable PCNFS server code.
58 extern int DEBUGLEVEL;
61 /* ******************************************
62 Extend for AIX system and qconfig file
63 from 'boulard@univ-rennes1.fr
64 ****************************************** */
65 static int strlocate(char *xpLine,char *xpS)
76 if (strncmp(p,xpS,iS) == 0) {iRet =1;break;};
80 /*DEBUG(3,(" strlocate %s in line '%s',ret=%d\n",xpS,xpLine,iRet));*/
86 /* ******************************************************************* */
87 /* * Scan qconfig and search all virtual printer (device printer) * */
88 /* ******************************************************************* */
89 static void ScanQconfig_fn(char *psz,void (*fn)())
99 if ((pfile = fopen(psz, "r")) == NULL)
101 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
106 /* scan qconfig file for searching <printername>: */
107 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
109 if (*line == '*' || *line == 0)
113 case 0: /* locate an entry */
114 if (*line == '\t' || *line == ' ') continue;
115 if ((p=strchr(line,':')))
118 p = strtok(line,":");
119 if (strcmp(p,"bsh")!=0)
127 case 1: /* scanning device stanza */
128 if (*line == '*' || *line == 0) continue;
129 if (*line != '\t' && *line != ' ')
131 /* name is found without stanza device */
132 /* probably a good printer ??? */
138 if (strlocate(line,"backend"))
140 /* it's a device, not a virtual printer*/
143 else if (strlocate(line,"device"))
145 /* it's a good virtual printer */
156 /* Scan qconfig file and locate de printername */
158 static BOOL ScanQconfig(char *psz,char *pszPrintername)
167 if ((pszPrintername!= NULL) && ((iLg = strlen(pszPrintername)) > 0))
168 pName = malloc(iLg+10);
171 DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
174 if ((pfile = fopen(psz, "r")) == NULL)
176 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
180 sprintf(pName,"%s:",pszPrintername);
182 /*DEBUG(3,( " Looking for entry %s\n",pName));*/
184 /* scan qconfig file for searching <printername>: */
185 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
187 if (*line == '*' || *line == 0)
191 case 0: /* scanning entry */
192 if (strncmp(line,pName,iLg) == 0)
198 case 1: /* scanning device stanza */
199 if (*line == '*' || *line == 0) continue;
200 if (*line != '\t' && *line != ' ')
202 /* name is found without stanza device */
203 /* probably a good printer ??? */
210 if (strlocate(line,"backend"))
212 /* it's a device, not a virtual printer*/
215 else if (strlocate(line,"device"))
217 /* it's a good virtual printer */
232 /***************************************************************************
233 Scan printcap file pszPrintcapname for a printer called pszPrintername.
234 Return True if found, else False. Returns False on error, too, after logging
235 the error at level 0. For generality, the printcap name may be passed - if
236 passed as NULL, the configuration will be queried for the name.
237 ***************************************************************************/
238 BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
245 if (pszPrintername == NULL || pszPrintername[0] == '\0')
247 DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
251 /* only go looking if no printcap name supplied */
252 if ((psz = pszPrintcapname) == NULL || psz[0] == '\0')
253 if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
255 DEBUG(0,( "No printcap file name configured!\n"));
259 if (strlocate(psz,"/qconfig") != NULL)
260 return(ScanQconfig(psz,pszPrintername));
262 if ((pfile = fopen(psz, "r")) == NULL)
264 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
268 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
270 if (*line == '#' || *line == 0)
273 /* now we have a real printer line - cut it off at the first : */
274 p = strchr(line,':');
277 /* now just check if the name is in the list */
278 /* NOTE: I avoid strtok as the fn calling this one may be using it */
281 if ((q = strchr(p,'|'))) *q++ = 0;
283 if (strequal(p,pszPrintername))
285 /* normalise the case */
286 strcpy(pszPrintername,p);
301 /***************************************************************************
302 run a function on each printer name in the printcap file. The function is
303 passed the primary name and the comment (if possible)
304 ***************************************************************************/
305 void pcap_printer_fn(void (*fn)())
307 pstring name,comment;
313 /* only go looking if no printcap name supplied */
314 if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
316 DEBUG(0,( "No printcap file name configured!\n"));
321 if (strlocate(psz,"/qconfig") != NULL)
323 ScanQconfig_fn(psz,fn);
327 if ((pfile = fopen(psz, "r")) == NULL)
329 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
333 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
335 if (*line == '#' || *line == 0)
338 /* now we have a real printer line - cut it off at the first : */
339 p = strchr(line,':');
342 /* now find the most likely printer name and comment
343 this is pure guesswork, but it's better than nothing */
348 BOOL has_punctuation;
349 if ((q = strchr(p,'|'))) *q++ = 0;
351 has_punctuation = (strchr(p,' ') || strchr(p,'(') || strchr(p,')'));
353 if (strlen(p)>strlen(comment) && has_punctuation)
355 StrnCpy(comment,p,sizeof(comment)-1);
359 if (strlen(p) <= 8 && strlen(p)>strlen(name) && !has_punctuation)
361 if (!*comment) pstrcpy(comment,name);
366 if (!strchr(comment,' ') &&
367 strlen(p) > strlen(comment))
369 StrnCpy(comment,p,sizeof(comment)-1);