r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[kai/samba.git] / source / printing / pcap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    printcap parsing
4    Copyright (C) Karl Auer 1993-1998
5
6    Re-working by Martin Kiff, 1994
7    
8    Re-written again by Andrew Tridgell
9
10    Modified for SVID support by Norm Jacobs, 1997
11
12    Modified for CUPS support by Michael Sweet, 1999
13    
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 3 of the License, or
17    (at your option) any later version.
18    
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.
23    
24    You should have received a copy of the GNU General Public License
25    along with this program.  If not, see <http://www.gnu.org/licenses/>.
26 */
27
28 /*
29  *  This module contains code to parse and cache printcap data, possibly
30  *  in concert with the CUPS/SYSV/AIX-specific code found elsewhere.
31  *
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).
35  *
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.
40  * 
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.
44  *
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.
48  *
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.
52  *
53  *  Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
54  *  in smb.conf under Solaris.
55  *
56  *  Modified to call CUPS support if printcap name is set to "cups"
57  *  in smb.conf.
58  *
59  *  Modified to call iPrint support if printcap name is set to "iprint"
60  *  in smb.conf.
61  */
62
63 #include "includes.h"
64
65
66 typedef struct pcap_cache {
67         char *name;
68         char *comment;
69         struct pcap_cache *next;
70 } pcap_cache_t;
71
72 static pcap_cache_t *pcap_cache = NULL;
73
74 BOOL pcap_cache_add(const char *name, const char *comment)
75 {
76         pcap_cache_t *p;
77
78         if (name == NULL || ((p = SMB_MALLOC_P(pcap_cache_t)) == NULL))
79                 return False;
80
81         p->name = SMB_STRDUP(name);
82         p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
83
84         p->next = pcap_cache;
85         pcap_cache = p;
86
87         return True;
88 }
89
90 static void pcap_cache_destroy(pcap_cache_t *cache)
91 {
92         pcap_cache_t *p, *next;
93
94         for (p = cache; p != NULL; p = next) {
95                 next = p->next;
96
97                 SAFE_FREE(p->name);
98                 SAFE_FREE(p->comment);
99                 SAFE_FREE(p);
100         }
101 }
102
103 BOOL pcap_cache_loaded(void)
104 {
105         return (pcap_cache != NULL);
106 }
107
108 void pcap_cache_reload(void)
109 {
110         const char *pcap_name = lp_printcapname();
111         BOOL pcap_reloaded = False;
112         pcap_cache_t *tmp_cache = NULL;
113         XFILE *pcap_file;
114         char *pcap_line;
115
116         DEBUG(3, ("reloading printcap cache\n"));
117
118         /* only go looking if no printcap name supplied */
119         if (pcap_name == NULL || *pcap_name == 0) {
120                 DEBUG(0, ("No printcap file name configured!\n"));
121                 return;
122         }
123
124         tmp_cache = pcap_cache;
125         pcap_cache = NULL;
126
127 #ifdef HAVE_CUPS
128         if (strequal(pcap_name, "cups")) {
129                 pcap_reloaded = cups_cache_reload();
130                 goto done;
131         }
132 #endif
133
134 #ifdef HAVE_IPRINT
135         if (strequal(pcap_name, "iprint")) {
136                 pcap_reloaded = iprint_cache_reload();
137                 goto done;
138         }
139 #endif
140
141 #if defined(SYSV) || defined(HPUX)
142         if (strequal(pcap_name, "lpstat")) {
143                 pcap_reloaded = sysv_cache_reload();
144                 goto done;
145         }
146 #endif
147
148 #ifdef AIX
149         if (strstr_m(pcap_name, "/qconfig") != NULL) {
150                 pcap_reloaded = aix_cache_reload();
151                 goto done;
152         }
153 #endif
154
155         /* handle standard printcap - moved from pcap_printer_fn() */
156
157         if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) {
158                 DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
159                 goto done;
160         }
161
162         for (; (pcap_line = fgets_slash(NULL, sizeof(pstring), pcap_file)) != NULL; safe_free(pcap_line)) {
163                 pstring name, comment;
164                 char *p, *q;
165
166                 if (*pcap_line == '#' || *pcap_line == 0)
167                         continue;
168
169                 /* now we have a real printer line - cut at the first : */      
170                 if ((p = strchr_m(pcap_line, ':')) != NULL)
171                         *p = 0;
172       
173                 /*
174                  * now find the most likely printer name and comment 
175                  * this is pure guesswork, but it's better than nothing
176                  */
177                 for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) {
178                         BOOL has_punctuation;
179
180                         if ((q = strchr_m(p, '|')) != NULL)
181                                 *q++ = 0;
182
183                         has_punctuation = (strchr_m(p, ' ') ||
184                                            strchr_m(p, '\t') ||
185                                            strchr_m(p, '(') ||
186                                            strchr_m(p, ')'));
187
188                         if (strlen(p) > strlen(comment) && has_punctuation) {
189                                 pstrcpy(comment, p);
190                                 continue;
191                         }
192
193                         if (strlen(p) <= MAXPRINTERLEN &&
194                             strlen(p) > strlen(name) && !has_punctuation) {
195                                 if (!*comment)
196                                         pstrcpy(comment, name);
197
198                                 pstrcpy(name, p);
199                                 continue;
200                         }
201
202                         if (!strchr_m(comment, ' ') &&
203                             strlen(p) > strlen(comment)) {
204                                 pstrcpy(comment, p);
205                                 continue;
206                         }
207                 }
208
209                 comment[60] = 0;
210                 name[MAXPRINTERLEN] = 0;
211
212                 if (*name && !pcap_cache_add(name, comment)) {
213                         x_fclose(pcap_file);
214                         goto done;
215                 }
216         }
217
218         x_fclose(pcap_file);
219         pcap_reloaded = True;
220
221 done:
222         DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
223
224         if (pcap_reloaded)
225                 pcap_cache_destroy(tmp_cache);
226         else {
227                 pcap_cache_destroy(pcap_cache);
228                 pcap_cache = tmp_cache;
229         }
230
231         return;
232 }
233
234
235 BOOL pcap_printername_ok(const char *printername)
236 {
237         pcap_cache_t *p;
238
239         for (p = pcap_cache; p != NULL; p = p->next)
240                 if (strequal(p->name, printername))
241                         return True;
242
243         return False;
244 }
245
246 /***************************************************************************
247 run a function on each printer name in the printcap file. The function is 
248 passed the primary name and the comment (if possible). Note the fn() takes
249 strings in DOS codepage. This means the xxx_printer_fn() calls must be fixed
250 to return DOS codepage. FIXME !! JRA.
251
252 XXX: I'm not sure if this comment still applies.. Anyone?  -Rob
253 ***************************************************************************/
254 void pcap_printer_fn(void (*fn)(char *, char *))
255 {
256         pcap_cache_t *p;
257
258         for (p = pcap_cache; p != NULL; p = p->next)
259                 fn(p->name, p->comment);
260
261         return;
262 }