1/*	$NetBSD: wrterm.c,v 1.8 2009/04/14 05:45:23 lukem Exp $	*/
2
3/*-
4 * Copyright (c) 1991, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)wrterm.c	8.1 (Berkeley) 6/9/93";
36#endif
37__RCSID("$NetBSD: wrterm.c,v 1.8 2009/04/14 05:45:23 lukem Exp $");
38#endif /* not lint */
39
40#include <sys/types.h>
41#include <ctype.h>
42#include <err.h>
43#include <stdio.h>
44#include <string.h>
45#include "extern.h"
46
47/*
48 * Output termcap entry to stdout, quoting characters that would give the
49 * shell problems and omitting empty fields.
50 */
51void
52wrtermcap(char *bp)
53{
54	int ch;
55	char *p;
56	char *t;
57	const char *sep;
58
59	/* Find the end of the terminal names. */
60	if ((t = strchr(bp, ':')) == NULL)
61		errx(1, "termcap names not colon terminated");
62	*t++ = '\0';
63
64	/* Output terminal names that don't have whitespace or quotes. */
65	sep = "";
66	while ((p = strsep(&bp, "|")) != NULL)
67		if (*p != '\0' && strpbrk(p, " \t'\"") == NULL) {
68			(void)printf("%s%s", sep, p);
69			sep = "|";
70		}
71	(void)putchar(':');
72
73	/*
74	 * Output fields, transforming any dangerous characters.  Skip
75	 * empty fields or fields containing only whitespace.
76	 */
77	while ((p = strsep(&t, ":")) != NULL) {
78		while ((ch = *p) != '\0' && isspace(ch))
79			++p;
80		if (ch == '\0')
81			continue;
82		while ((ch = *p++) != '\0')
83			switch(ch) {
84			case '\033':
85				(void)printf("\\E");
86			case  ' ':		/* No spaces. */
87				(void)printf("\\040");
88				break;
89			case '!':		/* No csh history chars. */
90				(void)printf("\\041");
91				break;
92			case ',':		/* No csh history chars. */
93				(void)printf("\\054");
94				break;
95			case '"':		/* No quotes. */
96				(void)printf("\\042");
97				break;
98			case '\'':		/* No quotes. */
99				(void)printf("\\047");
100				break;
101			case '`':		/* No quotes. */
102				(void)printf("\\140");
103				break;
104			case '\\':		/* Anything following is OK. */
105			case '^':
106				(void)putchar(ch);
107				if ((ch = *p++) == '\0')
108					break;
109				/* FALLTHROUGH */
110			default:
111				(void)putchar(ch);
112		}
113		(void)putchar(':');
114	}
115}
116