]> arthur.barton.de Git - netatalk.git/blob - etc/papd/printcap.c
termcap patches from NetBSD package by Christos Zoulas
[netatalk.git] / etc / papd / printcap.c
1 /*
2  * $Id: printcap.c,v 1.6 2002-01-17 06:10:43 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 #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 #ifdef 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         int skip;
176
177         hopcount = 0;
178         tbuf = bp;
179         tf = 0;
180 #ifndef V6
181         cp = getenv("TERMCAP");
182         /*
183          * TERMCAP can have one of two things in it. It can be the
184          * name of a file to use instead of /etc/termcap. In this
185          * case it better start with a "/". Or it can be an entry to
186          * use so we don't have to read the file. In this case it
187          * has to already have the newlines crunched out.
188          */
189         if (cp && *cp) {
190                 if (*cp!='/') {
191                         cp2 = getenv("TERM");
192                         if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
193                                 strcpy(bp,cp);
194                                 return(tnchktc(cap));
195                         } else {
196                                 tf = open(cap, 0);
197                         }
198                 } else
199                         tf = open(cp, 0);
200         }
201         if (tf==0)
202                 tf = open(cap, 0);
203 #else /* V6 */
204         tf = open(cap, 0);
205 #endif /* V6 */
206         if (tf < 0)
207                 return (-1);
208         for (;;) {
209                 cp = bp;
210                 skip = 0;
211                 for (;;) {
212                         if (i == cnt) {
213                                 cnt = read(tf, ibuf, BUFSIZ);
214                                 if (cnt <= 0) {
215                                         close(tf);
216                                         return (0);
217                                 }
218                                 i = 0;
219                         }
220                         c = ibuf[i++];
221                         if (c == '\n') {
222                                 if (!skip && cp > bp && cp[-1] == '\\') {
223                                         cp--;
224                                         continue;
225                                 }
226                                 skip = 0;
227                                 if (cp == bp)
228                                         continue;
229                                 else
230                                         break;
231                                 break;
232                         }
233                         if (c == '#' && cp == bp)
234                                 skip++;
235                         if (skip)
236                                 continue;
237                         if (cp >= bp+BUFSIZ) {
238                                 write(2,"Termcap entry too long\n", 23);
239                                 break;
240                         } else
241                                 *cp++ = c;
242                 }
243                 *cp = 0;
244
245                 /*
246                  * The real work for the match.
247                  */
248                 if (tnamatch(name)) {
249                         close(tf);
250                         return(tnchktc(cap));
251                 }
252         }
253 }
254
255 /*
256  * tnchktc: check the last entry, see if it's tc=xxx. If so,
257  * recursively find xxx and append that entry (minus the names)
258  * to take the place of the tc=xxx entry. This allows termcap
259  * entries to say "like an HP2621 but doesn't turn on the labels".
260  * Note that this works because of the left to right scan.
261  *
262  * Added a "cap" parameter, so we can use these calls for printcap
263  * and papd.conf.
264  */
265 int tnchktc( cap )
266     char *cap;
267 {
268         register char *p, *q;
269         char tcname[16];        /* name of similar terminal */
270         char tcbuf[BUFSIZ];
271         char *holdtbuf = tbuf;
272         int l;
273
274         p = tbuf + strlen(tbuf) - 2;    /* before the last colon */
275         while (*--p != ':')
276                 if (p<tbuf) {
277                         write(2, "Bad termcap entry\n", 18);
278                         return (0);
279                 }
280         p++;
281         /* p now points to beginning of last field */
282         if (p[0] != 't' || p[1] != 'c')
283                 return(1);
284         strcpy(tcname,p+3);
285         q = tcname;
286         while (q && *q != ':')
287                 q++;
288         *q = 0;
289         if (++hopcount > MAXHOP) {
290                 write(2, "Infinite tc= loop\n", 18);
291                 return (0);
292         }
293         if (tgetent( cap, tcbuf, tcname) != 1)
294                 return(0);
295         for (q=tcbuf; *q != ':'; q++)
296                 ;
297         l = p - holdtbuf + strlen(q);
298         if (l > BUFSIZ) {
299                 write(2, "Termcap entry too long\n", 23);
300                 q[BUFSIZ - (p-tbuf)] = 0;
301         }
302         strcpy(p, q+1);
303         tbuf = holdtbuf;
304         return(1);
305 }
306
307 /*
308  * Tnamatch deals with name matching.  The first field of the termcap
309  * entry is a sequence of names separated by |'s, so we compare
310  * against each such name.  The normal : terminator after the last
311  * name (before the first field) stops us.
312  */
313 int tnamatch(np)
314         char *np;
315 {
316         register char *Np, *Bp;
317
318         Bp = tbuf;
319         if (*Bp == '#')
320                 return(0);
321         for (;;) {
322                 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
323                         continue;
324                 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
325                         return (1);
326                 while (*Bp && *Bp != ':' && *Bp != '|')
327                         Bp++;
328                 if (*Bp == 0 || *Bp == ':')
329                         return (0);
330                 Bp++;
331         }
332 }
333
334 /*
335  * Skip to the next field.  Notice that this is very dumb, not
336  * knowing about \: escapes or any such.  If necessary, :'s can be put
337  * into the termcap file in octal.
338  */
339 static char *
340 tskip(bp)
341         register char *bp;
342 {
343
344         while (*bp && *bp != ':')
345                 bp++;
346         if (*bp == ':')
347                 bp++;
348         return (bp);
349 }
350
351 /*
352  * Return the (numeric) option id.
353  * Numeric options look like
354  *      li#80
355  * i.e. the option string is separated from the numeric value by
356  * a # character.  If the option is not found we return -1.
357  * Note that we handle octal numbers beginning with 0.
358  */
359 int tgetnum(id)
360         char *id;
361 {
362         register int i, base;
363         register char *bp = tbuf;
364
365         for (;;) {
366                 bp = tskip(bp);
367                 if (*bp == 0)
368                         return (-1);
369                 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
370                         continue;
371                 if (*bp == '@')
372                         return(-1);
373                 if (*bp != '#')
374                         continue;
375                 bp++;
376                 base = 10;
377                 if (*bp == '0')
378                         base = 8;
379                 i = 0;
380                 while (isdigit(*bp))
381                         i *= base, i += *bp++ - '0';
382                 return (i);
383         }
384 }
385
386 /*
387  * Handle a flag option.
388  * Flag options are given "naked", i.e. followed by a : or the end
389  * of the buffer.  Return 1 if we find the option, or 0 if it is
390  * not given.
391  */
392 int tgetflag(id)
393         char *id;
394 {
395         register char *bp = tbuf;
396
397         for (;;) {
398                 bp = tskip(bp);
399                 if (!*bp)
400                         return (0);
401                 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
402                         if (!*bp || *bp == ':')
403                                 return (1);
404                         else if (*bp == '@')
405                                 return(0);
406                 }
407         }
408 }
409
410 /*
411  * Get a string valued option.
412  * These are given as
413  *      cl=^Z
414  * Much decoding is done on the strings, and the strings are
415  * placed in area, which is a ref parameter which is updated.
416  * No checking on area overflow.
417  */
418 char *
419 tgetstr(id, area)
420         char *id, **area;
421 {
422         register char *bp = tbuf;
423
424         for (;;) {
425                 bp = tskip(bp);
426                 if (!*bp)
427                         return (0);
428                 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
429                         continue;
430                 if (*bp == '@')
431                         return(0);
432                 if (*bp != '=')
433                         continue;
434                 bp++;
435                 return (tdecode(bp, area));
436         }
437 }
438
439 /*
440  * Tdecode does the grung work to decode the
441  * string capability escapes.
442  */
443 static char *
444 tdecode(str, area)
445         register char *str;
446         char **area;
447 {
448         register char *cp;
449         register int c;
450         register char *dp;
451         int i;
452
453         cp = *area;
454         while ((c = *str++) && c != ':') {
455                 switch (c) {
456
457                 case '^':
458                         c = *str++ & 037;
459                         break;
460
461                 case '\\':
462                         dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
463                         c = *str++;
464 nextc:
465                         if (*dp++ == c) {
466                                 c = *dp++;
467                                 break;
468                         }
469                         dp++;
470                         if (*dp)
471                                 goto nextc;
472                         if (isdigit(c)) {
473                                 c -= '0', i = 2;
474                                 do
475                                         c <<= 3, c |= *str++ - '0';
476                                 while (--i && isdigit(*str));
477                         }
478                         break;
479                 }
480                 *cp++ = c;
481         }
482         *cp++ = 0;
483         str = *area;
484         *area = cp;
485         return (str);
486 }
487
488 static char *
489 decodename(str, area)
490         register char *str;
491         char **area;
492 {
493         register char *cp;
494         register int c;
495         register char *dp;
496         int i;
497
498         cp = *area;
499         while ((c = *str++) && c != ':' && c != '|' ) {
500                 switch (c) {
501
502                 case '^':
503                         c = *str++ & 037;
504                         break;
505
506                 case '\\':
507                         dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
508                         c = *str++;
509 nextc:
510                         if (*dp++ == c) {
511                                 c = *dp++;
512                                 break;
513                         }
514                         dp++;
515                         if (*dp)
516                                 goto nextc;
517                         if (isdigit(c)) {
518                                 c -= '0', i = 2;
519                                 do
520                                         c <<= 3, c |= *str++ - '0';
521                                 while (--i && isdigit(*str));
522                         }
523                         break;
524                 }
525                 *cp++ = c;
526         }
527         *cp++ = 0;
528         str = *area;
529         *area = cp;
530         return (str);
531 }
532
533 char *
534 getpname( area )
535     char        **area;
536 {
537     return( decodename( tbuf, area ));
538 }