Makefile: Added AIX targets from Ole Holm Nielsen <Ole.H.Nielsen@uni-c.dk>
[Samba.git] / source / printing / pcap.c
blob549ebcd70df6b63d63efe426232325f4f82357af
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 printcap parsing
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
34 * databases etc).
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
43 * printcap entry.
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.
54 #include "includes.h"
56 #include "smb.h"
58 extern int DEBUGLEVEL;
60 #ifdef AIX
61 /* ******************************************
62 Extend for AIX system and qconfig file
63 from 'boulard@univ-rennes1.fr
64 ****************************************** */
65 static int strlocate(char *xpLine,char *xpS)
67 int iS,iL,i,iRet;
68 char *p;
69 iS = strlen(xpS);
70 iL = strlen(xpLine);
72 iRet = 0;
73 p = xpLine;
74 while (iL >= iS)
76 if (strncmp(p,xpS,iS) == 0) {iRet =1;break;};
77 p++;
78 iL--;
80 /*DEBUG(3,(" strlocate %s in line '%s',ret=%d\n",xpS,xpLine,iRet));*/
82 return(iRet);
86 /* ******************************************************************* */
87 /* * Scan qconfig and search all virtual printer (device printer) * */
88 /* ******************************************************************* */
89 static void ScanQconfig_fn(char *psz,void (*fn)())
91 int iLg,iEtat;
92 FILE *pfile;
93 char *line,*p;
94 pstring name,comment;
95 line = NULL;
96 *name = 0;
97 *comment = 0;
99 if ((pfile = fopen(psz, "r")) == NULL)
101 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
102 return;
105 iEtat = 0;
106 /* scan qconfig file for searching <printername>: */
107 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
109 if (*line == '*' || *line == 0)
110 continue;
111 switch (iEtat)
113 case 0: /* locate an entry */
114 if (*line == '\t' || *line == ' ') continue;
115 if ((p=strchr(line,':')))
117 *p = '\0';
118 p = strtok(line,":");
119 if (strcmp(p,"bsh")!=0)
121 strcpy(name,p);
122 iEtat = 1;
123 continue;
126 break;
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 ??? */
133 fn(name,comment);
134 iEtat = 0;
135 continue;
138 if (strlocate(line,"backend"))
140 /* it's a device, not a virtual printer*/
141 iEtat = 0;
143 else if (strlocate(line,"device"))
145 /* it's a good virtual printer */
146 fn(name,comment);
147 iEtat = 0;
148 continue;
150 break;
153 fclose(pfile);
156 /* Scan qconfig file and locate de printername */
158 static BOOL ScanQconfig(char *psz,char *pszPrintername)
160 int iLg,iEtat;
161 FILE *pfile;
162 char *pName;
163 char *line;
165 pName = NULL;
166 line = NULL;
167 if ((pszPrintername!= NULL) && ((iLg = strlen(pszPrintername)) > 0))
168 pName = malloc(iLg+10);
169 if (pName == NULL)
171 DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
172 return(False);
174 if ((pfile = fopen(psz, "r")) == NULL)
176 DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
177 free(pName);
178 return(False);
180 sprintf(pName,"%s:",pszPrintername);
181 iLg = strlen(pName);
182 /*DEBUG(3,( " Looking for entry %s\n",pName));*/
183 iEtat = 0;
184 /* scan qconfig file for searching <printername>: */
185 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
187 if (*line == '*' || *line == 0)
188 continue;
189 switch (iEtat)
191 case 0: /* scanning entry */
192 if (strncmp(line,pName,iLg) == 0)
194 iEtat = 1;
195 continue;
197 break;
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 ??? */
204 free (line);
205 free(pName);
206 fclose(pfile);
207 return(True);
210 if (strlocate(line,"backend"))
212 /* it's a device, not a virtual printer*/
213 iEtat = 0;
215 else if (strlocate(line,"device"))
217 /* it's a good virtual printer */
218 free (line);
219 free(pName);
220 fclose(pfile);
221 return(True);
223 break;
226 free (pName);
227 fclose(pfile);
228 return(False);
231 #endif
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)
240 char *line=NULL;
241 char *psz;
242 char *p,*q;
243 FILE *pfile;
245 if (pszPrintername == NULL || pszPrintername[0] == '\0')
247 DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
248 return(False);
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"));
256 return(False);
258 #ifdef AIX
259 if (strlocate(psz,"/qconfig") != NULL)
260 return(ScanQconfig(psz,pszPrintername));
261 #endif
262 if ((pfile = fopen(psz, "r")) == NULL)
264 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
265 return(False);
268 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
270 if (*line == '#' || *line == 0)
271 continue;
273 /* now we have a real printer line - cut it off at the first : */
274 p = strchr(line,':');
275 if (p) *p = 0;
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 */
279 for (p=line; p; p=q)
281 if ((q = strchr(p,'|'))) *q++ = 0;
283 if (strequal(p,pszPrintername))
285 /* normalise the case */
286 strcpy(pszPrintername,p);
287 free(line);
288 fclose(pfile);
289 return(True);
291 p = q;
296 fclose(pfile);
297 return(False);
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;
308 char *line;
309 char *psz;
310 char *p,*q;
311 FILE *pfile;
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"));
317 return;
320 #ifdef AIX
321 if (strlocate(psz,"/qconfig") != NULL)
323 ScanQconfig_fn(psz,fn);
324 return;
326 #endif
327 if ((pfile = fopen(psz, "r")) == NULL)
329 DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
330 return;
333 for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
335 if (*line == '#' || *line == 0)
336 continue;
338 /* now we have a real printer line - cut it off at the first : */
339 p = strchr(line,':');
340 if (p) *p = 0;
342 /* now find the most likely printer name and comment
343 this is pure guesswork, but it's better than nothing */
344 *name = 0;
345 *comment = 0;
346 for (p=line; p; p=q)
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);
356 continue;
359 if (strlen(p) <= 8 && strlen(p)>strlen(name) && !has_punctuation)
361 if (!*comment) strcpy(comment,name);
362 strcpy(name,p);
363 continue;
366 if (!strchr(comment,' ') &&
367 strlen(p) > strlen(comment))
369 StrnCpy(comment,p,sizeof(comment)-1);
370 continue;
374 comment[60] = 0;
375 name[8] = 0;
377 if (*name)
378 fn(name,comment);
380 fclose(pfile);