2 Unix SMB/Netbios implementation.
5 Copyright (C) Karl Auer 1993,1994
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.
60 extern int DEBUGLEVEL;
63 /* ******************************************
64 Extend for AIX system and qconfig file
65 from 'boulard@univ-rennes1.fr
66 ****************************************** */
67 static int strlocate(char *xpLine,char *xpS)
78 if (strncmp(p,xpS,iS) == 0) {iRet =1;break;};
82 /*DEBUG(3,(" strlocate %s in line '%s',ret=%d\n",xpS,xpLine,iRet));*/
88 /* ******************************************************************* */
89 /* * Scan qconfig and search all virtual printer (device printer) * */
90 /* ******************************************************************* */
91 static void ScanQconfig_fn(char *psz,void (*fn)())
101 if ((pfile = fopen(psz, "r")) == NULL)
103 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
108 /* scan qconfig file for searching <printername>: */
109 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
111 if (*line == '*' || *line == 0)
115 case 0: /* locate an entry */
116 if (*line == '\t' || *line == ' ') continue;
117 if ((p=strchr(line,':')))
120 p = strtok(line,":");
121 if (strcmp(p,"bsh")!=0)
129 case 1: /* scanning device stanza */
130 if (*line == '*' || *line == 0) continue;
131 if (*line != '\t' && *line != ' ')
133 /* name is found without stanza device */
134 /* probably a good printer ??? */
140 if (strlocate(line,"backend"))
142 /* it's a device, not a virtual printer*/
145 else if (strlocate(line,"device"))
147 /* it's a good virtual printer */
158 /* Scan qconfig file and locate de printername */
160 static BOOL ScanQconfig(char *psz,char *pszPrintername)
169 if ((pszPrintername!= NULL) && ((iLg = strlen(pszPrintername)) > 0))
170 pName = malloc(iLg+10);
173 DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
176 if ((pfile = fopen(psz, "r")) == NULL)
178 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
182 sprintf(pName,"%s:",pszPrintername);
184 /*DEBUG(3,( " Looking for entry %s\n",pName));*/
186 /* scan qconfig file for searching <printername>: */
187 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
189 if (*line == '*' || *line == 0)
193 case 0: /* scanning entry */
194 if (strncmp(line,pName,iLg) == 0)
200 case 1: /* scanning device stanza */
201 if (*line == '*' || *line == 0) continue;
202 if (*line != '\t' && *line != ' ')
204 /* name is found without stanza device */
205 /* probably a good printer ??? */
212 if (strlocate(line,"backend"))
214 /* it's a device, not a virtual printer*/
217 else if (strlocate(line,"device"))
219 /* it's a good virtual printer */
234 /***************************************************************************
235 Scan printcap file pszPrintcapname for a printer called pszPrintername.
236 Return True if found, else False. Returns False on error, too, after logging
237 the error at level 0. For generality, the printcap name may be passed - if
238 passed as NULL, the configuration will be queried for the name.
239 ***************************************************************************/
240 BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
247 if (pszPrintername == NULL || pszPrintername[0] == '\0')
249 DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
253 /* only go looking if no printcap name supplied */
254 if ((psz = pszPrintcapname) == NULL || psz[0] == '\0')
255 if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
257 DEBUG(0,( "No printcap file name configured!\n"));
261 if (strlocate(psz,"/qconfig") != NULL)
262 return(ScanQconfig(psz,pszPrintername));
264 if ((pfile = fopen(psz, "r")) == NULL)
266 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
270 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
272 if (*line == '#' || *line == 0)
275 /* now we have a real printer line - cut it off at the first : */
276 p = strchr(line,':');
279 /* now just check if the name is in the list */
280 /* NOTE: I avoid strtok as the fn calling this one may be using it */
283 if ((q = strchr(p,'|'))) *q++ = 0;
285 if (strequal(p,pszPrintername))
287 /* normalise the case */
288 strcpy(pszPrintername,p);
303 /***************************************************************************
304 run a function on each printer name in the printcap file. The function is
305 passed the primary name and the comment (if possible)
306 ***************************************************************************/
307 void pcap_printer_fn(void (*fn)())
309 pstring name,comment;
315 /* only go looking if no printcap name supplied */
316 if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
318 DEBUG(0,( "No printcap file name configured!\n"));
323 if (strlocate(psz,"/qconfig") != NULL)
325 ScanQconfig_fn(psz,fn);
329 if ((pfile = fopen(psz, "r")) == NULL)
331 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
335 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
337 if (*line == '#' || *line == 0)
340 /* now we have a real printer line - cut it off at the first : */
341 p = strchr(line,':');
344 /* now find the most likely printer name and comment
345 this is pure guesswork, but it's better than nothing */
350 BOOL has_punctuation;
351 if ((q = strchr(p,'|'))) *q++ = 0;
353 has_punctuation = (strchr(p,' ') || strchr(p,'(') || strchr(p,')'));
355 if (strlen(p)>strlen(comment) && has_punctuation)
357 StrnCpy(comment,p,sizeof(comment)-1);
361 if (strlen(p) <= 8 && strlen(p)>strlen(name) && !has_punctuation)
363 if (!*comment) strcpy(comment,name);
368 if (!strchr(comment,' ') &&
369 strlen(p) > strlen(comment))
371 StrnCpy(comment,p,sizeof(comment)-1);