wrterm.c revision 229403
112151Sphk/*-
212151Sphk * Copyright (c) 1991, 1993
312151Sphk *	The Regents of the University of California.  All rights reserved.
412151Sphk *
512151Sphk * Redistribution and use in source and binary forms, with or without
612151Sphk * modification, are permitted provided that the following conditions
712151Sphk * are met:
812151Sphk * 1. Redistributions of source code must retain the above copyright
912151Sphk *    notice, this list of conditions and the following disclaimer.
1012151Sphk * 2. Redistributions in binary form must reproduce the above copyright
1112151Sphk *    notice, this list of conditions and the following disclaimer in the
1212151Sphk *    documentation and/or other materials provided with the distribution.
1312151Sphk * 4. Neither the name of the University nor the names of its contributors
1412151Sphk *    may be used to endorse or promote products derived from this software
1512151Sphk *    without specific prior written permission.
1612151Sphk *
1712151Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1812151Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1912151Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2012151Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2112151Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2212151Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2312151Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2412151Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2512151Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2612151Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2712151Sphk * SUCH DAMAGE.
2812151Sphk */
2912151Sphk
30116189Sobrien#include <sys/cdefs.h>
31116189Sobrien
32116189Sobrien__FBSDID("$FreeBSD: head/usr.bin/tset/wrterm.c 229403 2012-01-03 18:51:58Z ed $");
33132228Sglebius
3437289Sphk#ifndef lint
3512151Sphkstatic const char sccsid[] = "@(#)wrterm.c	8.1 (Berkeley) 6/9/93";
36132228Sglebius#endif
37132228Sglebius
38132228Sglebius#include <sys/types.h>
39132228Sglebius
40132228Sglebius#include <ctype.h>
41132228Sglebius#include <err.h>
4292741Salfred#include <stdio.h>
4312151Sphk#include <string.h>
44106696Salfred
4512151Sphk#include "extern.h"
4612151Sphk
4712151Sphk/*
4812151Sphk * Output termcap entry to stdout, quoting characters that would give the
4912151Sphk * shell problems and omitting empty fields.
5012151Sphk */
5112151Sphkvoid
5212151Sphkwrtermcap(char *bp)
5312151Sphk{
5412151Sphk	register int ch;
5512151Sphk	register char *p;
5612151Sphk	char *t, *sep;
5712151Sphk
5812151Sphk	/* Find the end of the terminal names. */
5912151Sphk	if ((t = strchr(bp, ':')) == NULL)
6012151Sphk		errx(1, "termcap names not colon terminated");
6112151Sphk	*t++ = '\0';
6212151Sphk
6335210Sbde	/* Output terminal names that don't have whitespace. */
64132228Sglebius	sep = strdup("");
6512151Sphk	while ((p = strsep(&bp, "|")) != NULL)
6612151Sphk		if (*p != '\0' && strpbrk(p, " \t") == NULL) {
6712151Sphk			(void)printf("%s%s", sep, p);
6812151Sphk			sep = strdup("|");
6912151Sphk		}
7012151Sphk	(void)putchar(':');
7112151Sphk
7212151Sphk	/*
7312151Sphk	 * Output fields, transforming any dangerous characters.  Skip
7412151Sphk	 * empty fields or fields containing only whitespace.
7512151Sphk	 */
7612151Sphk	while ((p = strsep(&t, ":")) != NULL) {
7712151Sphk		while ((ch = *p) != '\0' && isspace(ch))
7812151Sphk			++p;
7912151Sphk		if (ch == '\0')
8012151Sphk			continue;
8112151Sphk		while ((ch = *p++) != '\0')
82132228Sglebius			switch(ch) {
83132228Sglebius			case '\033':
84132228Sglebius				(void)printf("\\E");
85132228Sglebius			case  ' ':		/* No spaces. */
86132228Sglebius				(void)printf("\\040");
87132228Sglebius				break;
8835210Sbde			case '!':		/* No csh history chars. */
89132228Sglebius				(void)printf("\\041");
90132228Sglebius				break;
91132228Sglebius			case ',':		/* No csh history chars. */
92132228Sglebius				(void)printf("\\054");
93132228Sglebius				break;
9412151Sphk			case '"':		/* No quotes. */
95132228Sglebius				(void)printf("\\042");
96132228Sglebius				break;
97132228Sglebius			case '\'':		/* No quotes. */
9812151Sphk				(void)printf("\\047");
9912151Sphk				break;
100132228Sglebius			case '`':		/* No quotes. */
10112151Sphk				(void)printf("\\140");
102132228Sglebius				break;
103132228Sglebius			case '\\':		/* Anything following is OK. */
104132228Sglebius			case '^':
105132228Sglebius				(void)putchar(ch);
106132228Sglebius				if ((ch = *p++) == '\0')
107132228Sglebius					break;
10812151Sphk				/* FALLTHROUGH */
10912151Sphk			default:
11012151Sphk				(void)putchar(ch);
11112151Sphk		}
11212151Sphk		(void)putchar(':');
11312151Sphk	}
11412151Sphk}
11517971Sbde