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