r19755: always make LDAP_SCOPE_ONELEVEL available
[Samba.git] / source / printing / pcap.c
blob9b4b5e4f717a2c0f74be76ef86f16e2f856741fb
1 /*
2 Unix SMB/CIFS implementation.
3 printcap parsing
4 Copyright (C) Karl Auer 1993-1998
6 Re-working by Martin Kiff, 1994
8 Re-written again by Andrew Tridgell
10 Modified for SVID support by Norm Jacobs, 1997
12 Modified for CUPS support by Michael Sweet, 1999
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 * This module contains code to parse and cache printcap data, possibly
31 * in concert with the CUPS/SYSV/AIX-specific code found elsewhere.
33 * The way this module looks at the printcap file is very simplistic.
34 * Only the local printcap file is inspected (no searching of NIS
35 * databases etc).
37 * There are assumed to be one or more printer names per record, held
38 * as a set of sub-fields separated by vertical bar symbols ('|') in the
39 * first field of the record. The field separator is assumed to be a colon
40 * ':' and the record separator a newline.
42 * Lines ending with a backspace '\' are assumed to flag that the following
43 * line is a continuation line so that a set of lines can be read as one
44 * printcap entry.
46 * A line stating with a hash '#' is assumed to be a comment and is ignored
47 * Comments are discarded before the record is strung together from the
48 * set of continuation lines.
50 * Opening a pipe for "lpc status" and reading that would probably
51 * be pretty effective. Code to do this already exists in the freely
52 * distributable PCNFS server code.
54 * Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
55 * in smb.conf under Solaris.
57 * Modified to call CUPS support if printcap name is set to "cups"
58 * in smb.conf.
60 * Modified to call iPrint support if printcap name is set to "iprint"
61 * in smb.conf.
64 #include "includes.h"
67 typedef struct pcap_cache {
68 char *name;
69 char *comment;
70 struct pcap_cache *next;
71 } pcap_cache_t;
73 static pcap_cache_t *pcap_cache = NULL;
75 BOOL pcap_cache_add(const char *name, const char *comment)
77 pcap_cache_t *p;
79 if (name == NULL || ((p = SMB_MALLOC_P(pcap_cache_t)) == NULL))
80 return False;
82 p->name = SMB_STRDUP(name);
83 p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
85 p->next = pcap_cache;
86 pcap_cache = p;
88 return True;
91 static void pcap_cache_destroy(pcap_cache_t *cache)
93 pcap_cache_t *p, *next;
95 for (p = cache; p != NULL; p = next) {
96 next = p->next;
98 SAFE_FREE(p->name);
99 SAFE_FREE(p->comment);
100 SAFE_FREE(p);
104 BOOL pcap_cache_loaded(void)
106 return (pcap_cache != NULL);
109 void pcap_cache_reload(void)
111 const char *pcap_name = lp_printcapname();
112 BOOL pcap_reloaded = False;
113 pcap_cache_t *tmp_cache = NULL;
114 XFILE *pcap_file;
115 char *pcap_line;
117 DEBUG(3, ("reloading printcap cache\n"));
119 /* only go looking if no printcap name supplied */
120 if (pcap_name == NULL || *pcap_name == 0) {
121 DEBUG(0, ("No printcap file name configured!\n"));
122 return;
125 tmp_cache = pcap_cache;
126 pcap_cache = NULL;
128 #ifdef HAVE_CUPS
129 if (strequal(pcap_name, "cups")) {
130 pcap_reloaded = cups_cache_reload();
131 goto done;
133 #endif
135 #ifdef HAVE_IPRINT
136 if (strequal(pcap_name, "iprint")) {
137 pcap_reloaded = iprint_cache_reload();
138 goto done;
140 #endif
142 #if defined(SYSV) || defined(HPUX)
143 if (strequal(pcap_name, "lpstat")) {
144 pcap_reloaded = sysv_cache_reload();
145 goto done;
147 #endif
149 #ifdef AIX
150 if (strstr_m(pcap_name, "/qconfig") != NULL) {
151 pcap_reloaded = aix_cache_reload();
152 goto done;
154 #endif
156 /* handle standard printcap - moved from pcap_printer_fn() */
158 if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) {
159 DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
160 goto done;
163 for (; (pcap_line = fgets_slash(NULL, sizeof(pstring), pcap_file)) != NULL; safe_free(pcap_line)) {
164 pstring name, comment;
165 char *p, *q;
167 if (*pcap_line == '#' || *pcap_line == 0)
168 continue;
170 /* now we have a real printer line - cut at the first : */
171 if ((p = strchr_m(pcap_line, ':')) != NULL)
172 *p = 0;
175 * now find the most likely printer name and comment
176 * this is pure guesswork, but it's better than nothing
178 for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) {
179 BOOL has_punctuation;
181 if ((q = strchr_m(p, '|')) != NULL)
182 *q++ = 0;
184 has_punctuation = (strchr_m(p, ' ') ||
185 strchr_m(p, '\t') ||
186 strchr_m(p, '(') ||
187 strchr_m(p, ')'));
189 if (strlen(p) > strlen(comment) && has_punctuation) {
190 pstrcpy(comment, p);
191 continue;
194 if (strlen(p) <= MAXPRINTERLEN &&
195 strlen(p) > strlen(name) && !has_punctuation) {
196 if (!*comment)
197 pstrcpy(comment, name);
199 pstrcpy(name, p);
200 continue;
203 if (!strchr_m(comment, ' ') &&
204 strlen(p) > strlen(comment)) {
205 pstrcpy(comment, p);
206 continue;
210 comment[60] = 0;
211 name[MAXPRINTERLEN] = 0;
213 if (*name && !pcap_cache_add(name, comment)) {
214 x_fclose(pcap_file);
215 goto done;
219 x_fclose(pcap_file);
220 pcap_reloaded = True;
222 done:
223 DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
225 if (pcap_reloaded)
226 pcap_cache_destroy(tmp_cache);
227 else {
228 pcap_cache_destroy(pcap_cache);
229 pcap_cache = tmp_cache;
232 return;
236 BOOL pcap_printername_ok(const char *printername)
238 pcap_cache_t *p;
240 for (p = pcap_cache; p != NULL; p = p->next)
241 if (strequal(p->name, printername))
242 return True;
244 return False;
247 /***************************************************************************
248 run a function on each printer name in the printcap file. The function is
249 passed the primary name and the comment (if possible). Note the fn() takes
250 strings in DOS codepage. This means the xxx_printer_fn() calls must be fixed
251 to return DOS codepage. FIXME !! JRA.
253 XXX: I'm not sure if this comment still applies.. Anyone? -Rob
254 ***************************************************************************/
255 void pcap_printer_fn(void (*fn)(char *, char *))
257 pcap_cache_t *p;
259 for (p = pcap_cache; p != NULL; p = p->next)
260 fn(p->name, p->comment);
262 return;