]> git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/printerdb.c
26bde6218c3306f20b3ef89c46e7a4305e822f9a
[apple/libinfo.git] / gen.subproj / printerdb.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * /etc/printcap reader (in case NetInfo is not running)
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <printerdb.h>
32
33 static FILE *pf;
34 static char *getline(FILE *);
35 static int emptyfield(char *);
36
37 static void
38 _prdb_free_ent(
39 prdb_ent *ent
40 )
41 {
42 int i;
43
44 for (i = 0; ent->pe_name[i]; i++) {
45 free(ent->pe_name[i]);
46 }
47 free(ent->pe_name);
48 ent->pe_name = NULL;
49 for (i = 0; i < ent->pe_nprops; i++) {
50 free(ent->pe_prop[i].pp_key);
51 free(ent->pe_prop[i].pp_value);
52 }
53 free(ent->pe_prop);
54 ent->pe_prop = NULL;
55 }
56
57 void
58 _old_prdb_end(
59 void
60 )
61 {
62 if (pf != NULL) {
63 fclose(pf);
64 pf = NULL;
65 }
66 }
67
68 void
69 _old_prdb_set(
70 void
71 )
72 {
73 if (pf == NULL) {
74 pf = fopen("/etc/printcap", "r");
75 }
76 }
77
78 static void
79 pename_insert(
80 char ***target,
81 char *name,
82 int which
83 )
84 {
85 if (which == 0) {
86 *target = malloc(sizeof(char *) * 2);
87 } else {
88 *target = realloc(*target, sizeof(char *) * (which + 2));
89 }
90 (*target)[which] = strdup(name);
91 (*target)[which + 1] = NULL;
92 }
93
94 static void
95 peprop_insert(
96 prdb_property **target,
97 prdb_property prop,
98 int which
99 )
100 {
101 if (which == 0) {
102 *target = malloc(sizeof(prop));
103 } else {
104 *target = realloc(*target, (which + 1) * sizeof(prop));
105 }
106 (*target)[which] = prop;
107 }
108
109 prdb_ent *
110 _old_prdb_get(
111 void
112 )
113 {
114 char *line;
115 char *p;
116 char *end;
117 char *hash;
118 char *equal;
119 char *where;
120 static prdb_ent ent;
121 prdb_property prop;
122 int which;
123
124 _old_prdb_set();
125 if (pf == NULL) {
126 return (NULL);
127 }
128 do {
129 line = getline(pf);
130 if (line == NULL) {
131 return (NULL);
132 }
133 } while (*line == 0);
134 where = line;
135 end = index(where, ':');
136 if (end != NULL) {
137 *end++ = 0;
138 }
139 which = 0;
140 if (ent.pe_name != NULL) {
141 _prdb_free_ent(&ent);
142 }
143 for (;;) {
144 p = index(where, '|');
145 if (p != NULL && (end == NULL || p < end)) {
146 *p++ = 0;
147 pename_insert(&ent.pe_name, where, which++);
148 where = p;
149 } else {
150 pename_insert(&ent.pe_name, where, which);
151 break;
152 }
153 }
154 where = end;
155 which = 0;
156 for (;;) {
157 end = index(where, ':');
158 if (end != NULL) {
159 *end++ = 0;
160 }
161 hash = index(where, '#');
162 equal = index(where, '=');
163 if (hash != NULL && (end == NULL || hash < end)) {
164 *hash = 0;
165 prop.pp_key = strdup(where);
166 *hash = '#';
167 prop.pp_value = strdup(hash);
168 peprop_insert(&ent.pe_prop, prop, which++);
169 } else if (equal != NULL && (end == NULL ||
170 equal < end)) {
171 *equal++ = 0;
172 prop.pp_key = strdup(where);
173 prop.pp_value = strdup(equal);
174 peprop_insert(&ent.pe_prop, prop, which++);
175 } else if (!emptyfield(where)) {
176 prop.pp_key = strdup(where);
177 prop.pp_value = strdup("");
178 peprop_insert(&ent.pe_prop, prop, which++);
179 }
180 where = end;
181 if (end == NULL) {
182 break;
183 }
184 }
185 free(line);
186 ent.pe_nprops = which;
187 return (&ent);
188 }
189
190 static int
191 prmatch(
192 prdb_ent *ent,
193 char *name
194 )
195 {
196 int i;
197
198 for (i = 0; ent->pe_name[i] != NULL; i++) {
199 if (strcmp(ent->pe_name[i], name) == 0) {
200 return (1);
201 }
202 }
203 return (0);
204 }
205
206 prdb_ent *
207 _old_prdb_getbyname(
208 char *prname
209 )
210 {
211 prdb_ent *ent;
212
213 _old_prdb_set();
214 if (pf == NULL) {
215 return (NULL);
216 }
217 while ((ent = _old_prdb_get())) {
218 if (prmatch(ent, prname)) {
219 break;
220 }
221 }
222 _old_prdb_end();
223 return (ent);
224 }
225
226
227
228 static char *
229 getline(
230 FILE *f
231 )
232 {
233 char line[BUFSIZ];
234 char *res = NULL;
235 int more = 1;
236 int len;
237 int inclen;
238
239 len = 0;
240 while (more && fgets(line, sizeof(line), f)) {
241 inclen = strlen(line);
242 if (line[inclen - 1] == '\n') {
243 line[inclen - 1] = 0;
244 inclen--;
245 }
246 if (*line == '#') {
247 continue;
248 }
249 if (res == NULL) {
250 res = malloc(inclen + 1);
251 } else {
252 res = realloc(res, len + inclen + 1);
253 }
254 if (line[inclen - 1] == '\\') {
255 line[inclen - 1] = 0;
256 inclen--;
257 } else {
258 more = 0;
259 }
260 bcopy(line, res + len, inclen);
261 len += inclen;
262 res[len] = 0;
263 }
264 return (res);
265 }
266
267 static int
268 emptyfield(
269 char *line
270 )
271 {
272 while (*line) {
273 if (*line != ' ' && *line != '\t') {
274 return (0);
275 }
276 line++;
277 }
278 return (1);
279 }