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