nsswitch: Fix bogus #include line.
[Samba.git] / source3 / printing / print_standard.c
blobb5f1056b2e6f1a6e8bbe55d43676e01b1d0811ea
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 3 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, see <http://www.gnu.org/licenses/>.
29 * This module contains code to parse and cache printcap data, possibly
30 * in concert with the CUPS/SYSV/AIX-specific code found elsewhere.
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 /* printcap parsing specific code moved here from printing/pcap.c */
57 #include "includes.h"
58 #include "system/filesys.h"
59 #include "printing/pcap.h"
61 /* handle standard printcap - moved from pcap_printer_fn() */
62 bool std_pcap_cache_reload(const char *pcap_name, struct pcap_cache **_pcache)
64 XFILE *pcap_file;
65 char *pcap_line;
66 struct pcap_cache *pcache = NULL;
68 if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) {
69 DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
70 return false;
73 for (; (pcap_line = fgets_slash(NULL, 1024, pcap_file)) != NULL; free(pcap_line)) {
74 char name[MAXPRINTERLEN+1];
75 char comment[62];
76 char *p, *q;
78 if (*pcap_line == '#' || *pcap_line == 0)
79 continue;
81 /* now we have a real printer line - cut at the first : */
82 if ((p = strchr_m(pcap_line, ':')) != NULL)
83 *p = 0;
86 * now find the most likely printer name and comment
87 * this is pure guesswork, but it's better than nothing
89 for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) {
90 bool has_punctuation;
92 if ((q = strchr_m(p, '|')) != NULL)
93 *q++ = 0;
95 has_punctuation = (strchr_m(p, ' ') ||
96 strchr_m(p, '\t') ||
97 strchr_m(p, '"') ||
98 strchr_m(p, '\'') ||
99 strchr_m(p, ';') ||
100 strchr_m(p, ',') ||
101 strchr_m(p, '(') ||
102 strchr_m(p, ')'));
104 if (strlen(p) > strlen(comment) && has_punctuation) {
105 strlcpy(comment, p, sizeof(comment));
106 continue;
109 if (strlen(p) <= MAXPRINTERLEN && *name == '\0' && !has_punctuation) {
110 strlcpy(name, p, sizeof(name));
111 continue;
114 if (!strchr_m(comment, ' ') &&
115 strlen(p) > strlen(comment)) {
116 strlcpy(comment, p, sizeof(comment));
117 continue;
121 if ((*name != '\0')
122 && !pcap_cache_add_specific(&pcache, name, comment, NULL)) {
123 x_fclose(pcap_file);
124 pcap_cache_destroy_specific(&pcache);
125 return false;
129 x_fclose(pcap_file);
130 *_pcache = pcache;
131 return true;