]> arthur.barton.de Git - netatalk.git/blob - etc/papd/printcap.c
Removed unneed sccsid string. (-Wall warning)
[netatalk.git] / etc / papd / printcap.c
1 /*
2  * $Id: printcap.c,v 1.9 2003-02-17 01:33:01 srittau Exp $
3  *
4  * Copyright (c) 1990,1994 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  *
7  * Copyright (c) 1983 Regents of the University of California.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif /* HAVE_CONFIG_H */
42
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif /* HAVE_UNISTD_H */
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #ifdef HAVE_FCNTL_H
52 #include <fcntl.h>
53 #endif /* HAVE_FCNTL_H */
54 #include <atalk/paths.h>
55
56 #include "printcap.h"
57
58 #ifndef BUFSIZ
59 #define BUFSIZ  1024
60 #endif /* ! BUFSIZ */
61 #define MAXHOP  32      /* max number of tc= indirections */
62
63 /*
64  * termcap - routines for dealing with the terminal capability data base
65  *
66  * BUG:         Should use a "last" pointer in tbuf, so that searching
67  *              for capabilities alphabetically would not be a n**2/2
68  *              process when large numbers of capabilities are given.
69  * Note:        If we add a last pointer now we will screw up the
70  *              tc capability. We really should compile termcap.
71  *
72  * Essentially all the work here is scanning and decoding escapes
73  * in string capabilities.  We don't use stdio because the editor
74  * doesn't, and because living w/o it is not hard.
75  */
76
77 #define PRINTCAP
78
79 #ifdef PRINTCAP
80 #define tgetent pgetent
81 #define tskip   pskip
82 #define tgetstr pgetstr
83 #define tdecode pdecode
84 #define tgetnum pgetnum
85 #define tgetflag pgetflag
86 #define tdecode pdecode
87 #define tnchktc pnchktc
88 #define tnamatch pnamatch
89 #define V6
90 #endif /* PRINTCAP */
91
92 static  FILE *pfp = NULL;       /* printcap data base file pointer */
93 static  char *tbuf;
94 static  int hopcount;           /* detect infinite loops in termcap, init 0 */
95 static char     *tskip();
96 char    *tgetstr();
97 static char     *tdecode();
98 char    *getenv();
99
100 /*
101  * Similar to tgetent except it returns the next entry instead of
102  * doing a lookup.
103  *
104  * Added a "cap" parameter, so we can use these calls for printcap
105  * and papd.conf.
106  */
107 int getprent( cap, bp, bufsize )
108         register char *cap;
109         register char *bp;
110         register int bufsize;
111 {
112         register int c, skip = 0, i;
113
114         if (pfp == NULL && (pfp = fopen( cap, "r")) == NULL)
115                 return(-1);
116         tbuf = bp;
117         i = 0;
118         for (;;) {
119                 switch (c = getc(pfp)) {
120                 case EOF:
121                         fclose(pfp);
122                         pfp = NULL;
123                         return(0);
124                 case '\n':
125                         if (bp == tbuf) {
126                                 skip = 0;
127                                 continue;
128                         }
129                         if (bp[-1] == '\\') {
130                                 bp--;
131                                 continue;
132                         }
133                         *bp = '\0';
134                         return(1);
135                 case '#':
136                         if (bp == tbuf)
137                                 skip++;
138                 default:
139                         if (skip)
140                                 continue;
141                         if (bp >= tbuf+BUFSIZ) {
142                                 write(2, "Termcap entry too long\n", 23);
143                                 *bp = '\0';
144                                 return(1);
145                         }
146                         *bp++ = c;
147                         if (++i >= bufsize) {
148                                 write(2, "config file too large\n", 22);
149                                 fclose(pfp);
150                                 pfp = NULL;
151                                 *bp = '\0';
152                                 return(1);
153                         }
154                 }
155         }
156 }
157
158 void endprent()
159 {
160         if (pfp != NULL)
161                 fclose(pfp);
162 }
163
164 /*
165  * Get an entry for terminal name in buffer bp,
166  * from the termcap file.  Parse is very rudimentary;
167  * we just notice escaped newlines.
168  *
169  * Added a "cap" parameter, so we can use these calls for printcap
170  * and papd.conf.
171  */
172 int tgetent( cap, bp, name)
173         char *cap, *bp, *name;
174 {
175         register char *cp;
176         register int c;
177         register int i = 0, cnt = 0;
178         char ibuf[BUFSIZ];
179         int tf;
180         int skip;
181
182         hopcount = 0;
183         tbuf = bp;
184         tf = 0;
185 #ifndef V6
186         cp = getenv("TERMCAP");
187         /*
188          * TERMCAP can have one of two things in it. It can be the
189          * name of a file to use instead of /etc/termcap. In this
190          * case it better start with a "/". Or it can be an entry to
191          * use so we don't have to read the file. In this case it
192          * has to already have the newlines crunched out.
193          */
194         if (cp && *cp) {
195                 if (*cp!='/') {
196                         cp2 = getenv("TERM");
197                         if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
198                                 strcpy(bp,cp);
199                                 return(tnchktc(cap));
200                         } else {
201                                 tf = open(cap, 0);
202                         }
203                 } else
204                         tf = open(cp, 0);
205         }
206         if (tf==0)
207                 tf = open(cap, 0);
208 #else /* V6 */
209         tf = open(cap, 0);
210 #endif /* V6 */
211         if (tf < 0)
212                 return (-1);
213         for (;;) {
214                 cp = bp;
215                 skip = 0;
216                 for (;;) {
217                         if (i == cnt) {
218                                 cnt = read(tf, ibuf, BUFSIZ);
219                                 if (cnt <= 0) {
220                                         close(tf);
221                                         return (0);
222                                 }
223                                 i = 0;
224                         }
225                         c = ibuf[i++];
226                         if (c == '\n') {
227                                 if (!skip && cp > bp && cp[-1] == '\\') {
228                                         cp--;
229                                         continue;
230                                 }
231                                 skip = 0;
232                                 if (cp == bp)
233                                         continue;
234                                 else
235                                         break;
236                         }
237                         if (c == '#' && cp == bp)
238                                 skip++;
239                         if (skip)
240                                 continue;
241                         if (cp >= bp+BUFSIZ) {
242                                 write(2,"Termcap entry too long\n", 23);
243                                 break;
244                         } else
245                                 *cp++ = c;
246                 }
247                 *cp = 0;
248
249                 /*
250                  * The real work for the match.
251                  */
252                 if (tnamatch(name)) {
253                         close(tf);
254                         return(tnchktc(cap));
255                 }
256         }
257 }
258
259 /*
260  * tnchktc: check the last entry, see if it's tc=xxx. If so,
261  * recursively find xxx and append that entry (minus the names)
262  * to take the place of the tc=xxx entry. This allows termcap
263  * entries to say "like an HP2621 but doesn't turn on the labels".
264  * Note that this works because of the left to right scan.
265  *
266  * Added a "cap" parameter, so we can use these calls for printcap
267  * and papd.conf.
268  */
269 int tnchktc( cap )
270     char *cap;
271 {
272         register char *p, *q;
273         char tcname[16];        /* name of similar terminal */
274         char tcbuf[BUFSIZ];
275         char *holdtbuf = tbuf;
276         int l;
277
278         p = tbuf + strlen(tbuf) - 2;    /* before the last colon */
279         while (*--p != ':')
280                 if (p<tbuf) {
281                         write(2, "Bad termcap entry\n", 18);
282                         return (0);
283                 }
284         p++;
285         /* p now points to beginning of last field */
286         if (p[0] != 't' || p[1] != 'c')
287                 return(1);
288         strcpy(tcname,p+3);
289         q = tcname;
290         while (q && *q != ':')
291                 q++;
292         *q = 0;
293         if (++hopcount > MAXHOP) {
294                 write(2, "Infinite tc= loop\n", 18);
295                 return (0);
296         }
297         if (tgetent( cap, tcbuf, tcname) != 1)
298                 return(0);
299         for (q=tcbuf; *q != ':'; q++)
300                 ;
301         l = p - holdtbuf + strlen(q);
302         if (l > BUFSIZ) {
303                 write(2, "Termcap entry too long\n", 23);
304                 q[BUFSIZ - (p-tbuf)] = 0;
305         }
306         strcpy(p, q+1);
307         tbuf = holdtbuf;
308         return(1);
309 }
310
311 /*
312  * Tnamatch deals with name matching.  The first field of the termcap
313  * entry is a sequence of names separated by |'s, so we compare
314  * against each such name.  The normal : terminator after the last
315  * name (before the first field) stops us.
316  */
317 int tnamatch(np)
318         char *np;
319 {
320         register char *Np, *Bp;
321
322         Bp = tbuf;
323         if (*Bp == '#')
324                 return(0);
325         for (;;) {
326                 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
327                         continue;
328                 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
329                         return (1);
330                 while (*Bp && *Bp != ':' && *Bp != '|')
331                         Bp++;
332                 if (*Bp == 0 || *Bp == ':')
333                         return (0);
334                 Bp++;
335         }
336 }
337
338 /*
339  * Skip to the next field.  Notice that this is very dumb, not
340  * knowing about \: escapes or any such.  If necessary, :'s can be put
341  * into the termcap file in octal.
342  */
343 static char *
344 tskip(bp)
345         register char *bp;
346 {
347
348         while (*bp && *bp != ':')
349                 bp++;
350         if (*bp == ':')
351                 bp++;
352         return (bp);
353 }
354
355 /*
356  * Return the (numeric) option id.
357  * Numeric options look like
358  *      li#80
359  * i.e. the option string is separated from the numeric value by
360  * a # character.  If the option is not found we return -1.
361  * Note that we handle octal numbers beginning with 0.
362  */
363 int tgetnum(id)
364         char *id;
365 {
366         register int i, base;
367         register char *bp = tbuf;
368
369         for (;;) {
370                 bp = tskip(bp);
371                 if (*bp == 0)
372                         return (-1);
373                 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
374                         continue;
375                 if (*bp == '@')
376                         return(-1);
377                 if (*bp != '#')
378                         continue;
379                 bp++;
380                 base = 10;
381                 if (*bp == '0')
382                         base = 8;
383                 i = 0;
384                 while (isdigit(*bp))
385                         i *= base, i += *bp++ - '0';
386                 return (i);
387         }
388 }
389
390 /*
391  * Handle a flag option.
392  * Flag options are given "naked", i.e. followed by a : or the end
393  * of the buffer.  Return 1 if we find the option, or 0 if it is
394  * not given.
395  */
396 int tgetflag(id)
397         char *id;
398 {
399         register char *bp = tbuf;
400
401         for (;;) {
402                 bp = tskip(bp);
403                 if (!*bp)
404                         return (0);
405                 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
406                         if (!*bp || *bp == ':')
407                                 return (1);
408                         else if (*bp == '@')
409                                 return(0);
410                 }
411         }
412 }
413
414 /*
415  * Get a string valued option.
416  * These are given as
417  *      cl=^Z
418  * Much decoding is done on the strings, and the strings are
419  * placed in area, which is a ref parameter which is updated.
420  * No checking on area overflow.
421  */
422 char *
423 tgetstr(id, area)
424         char *id, **area;
425 {
426         register char *bp = tbuf;
427
428         for (;;) {
429                 bp = tskip(bp);
430                 if (!*bp)
431                         return (0);
432                 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
433                         continue;
434                 if (*bp == '@')
435                         return(0);
436                 if (*bp != '=')
437                         continue;
438                 bp++;
439                 return (tdecode(bp, area));
440         }
441 }
442
443 /*
444  * Tdecode does the grung work to decode the
445  * string capability escapes.
446  */
447 static char *
448 tdecode(str, area)
449         register char *str;
450         char **area;
451 {
452         register char *cp;
453         register int c;
454         register char *dp;
455         int i;
456
457         cp = *area;
458         while ((c = *str++) && c != ':') {
459                 switch (c) {
460
461                 case '^':
462                         c = *str++ & 037;
463                         break;
464
465                 case '\\':
466                         dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
467                         c = *str++;
468 nextc:
469                         if (*dp++ == c) {
470                                 c = *dp++;
471                                 break;
472                         }
473                         dp++;
474                         if (*dp)
475                                 goto nextc;
476                         if (isdigit(c)) {
477                                 c -= '0', i = 2;
478                                 do
479                                         c <<= 3, c |= *str++ - '0';
480                                 while (--i && isdigit(*str));
481                         }
482                         break;
483                 }
484                 *cp++ = c;
485         }
486         *cp++ = 0;
487         str = *area;
488         *area = cp;
489         return (str);
490 }
491
492 static char *
493 decodename(str, area, bufsize)
494         register char *str;
495         char **area;
496         int bufsize;
497 {
498         register char *cp;
499         register int c;
500         register char *dp;
501         int i;
502
503         cp = *area;
504         while ((c = *str++) && --bufsize && c != ':' && c != '|' ) {
505                 switch (c) {
506
507                 case '^':
508                         c = *str++ & 037;
509                         break;
510
511                 case '\\':
512                         dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
513                         c = *str++;
514 nextc:
515                         if (*dp++ == c) {
516                                 c = *dp++;
517                                 break;
518                         }
519                         dp++;
520                         if (*dp)
521                                 goto nextc;
522                         if (isdigit(c)) {
523                                 c -= '0', i = 2;
524                                 do
525                                         c <<= 3, c |= *str++ - '0';
526                                 while (--i && isdigit(*str));
527                         }
528                         break;
529                 }
530                 *cp++ = c;
531         }
532         *cp++ = 0;
533         str = *area;
534         *area = cp;
535         return (str);
536 }
537
538 char *
539 getpname( area, bufsize )
540         char    **area;
541         int     bufsize;
542 {
543         return( decodename( tbuf, area, bufsize));
544 }