xref: /original-bsd/usr.bin/tset/wrterm.c (revision 3705696b)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)wrterm.c	8.1 (Berkeley) 06/09/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include "extern.h"
17 
18 /*
19  * Output termcap entry to stdout, quoting characters that would give the
20  * shell problems and omitting empty fields.
21  */
22 void
23 wrtermcap(bp)
24 	char *bp;
25 {
26 	register int ch;
27 	register char *p;
28 	char *t, *sep;
29 
30 	/* Find the end of the terminal names. */
31 	if ((t = index(bp, ':')) == NULL)
32 		err("termcap names not colon terminated");
33 	*t++ = '\0';
34 
35 	/* Output terminal names that don't have whitespace. */
36 	sep = "";
37 	while ((p = strsep(&bp, "|")) != NULL)
38 		if (*p != '\0' && strpbrk(p, " \t") == NULL) {
39 			(void)printf("%s%s", sep, p);
40 			sep = "|";
41 		}
42 	(void)putchar(':');
43 
44 	/*
45 	 * Output fields, transforming any dangerous characters.  Skip
46 	 * empty fields or fields containing only whitespace.
47 	 */
48 	while ((p = strsep(&t, ":")) != NULL) {
49 		while ((ch = *p) != '\0' && isspace(ch))
50 			++p;
51 		if (ch == '\0')
52 			continue;
53 		while ((ch = *p++) != '\0')
54 			switch(ch) {
55 			case '\033':
56 				(void)printf("\\E");
57 			case  ' ':		/* No spaces. */
58 				(void)printf("\\040");
59 				break;
60 			case '!':		/* No csh history chars. */
61 				(void)printf("\\041");
62 				break;
63 			case ',':		/* No csh history chars. */
64 				(void)printf("\\054");
65 				break;
66 			case '"':		/* No quotes. */
67 				(void)printf("\\042");
68 				break;
69 			case '\'':		/* No quotes. */
70 				(void)printf("\\047");
71 				break;
72 			case '`':		/* No quotes. */
73 				(void)printf("\\140");
74 				break;
75 			case '\\':		/* Anything following is OK. */
76 			case '^':
77 				(void)putchar(ch);
78 				if ((ch = *p++) == '\0')
79 					break;
80 				/* FALLTHROUGH */
81 			default:
82 				(void)putchar(ch);
83 		}
84 		(void)putchar(':');
85 	}
86 }
87