1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate *
35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate * contributors.
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <string.h>
43*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
44*7c478bd9Sstevel@tonic-gate #include "print.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #define BACKSLASH '\\'
47*7c478bd9Sstevel@tonic-gate #define BACKBACK "\\\\"
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate extern char *strcat();
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate static char retbuffer[1024];
52*7c478bd9Sstevel@tonic-gate static char ret2buffer[1024];
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate * Remove the padding sequences from the input string.
56*7c478bd9Sstevel@tonic-gate * Return the new string without the padding sequences
57*7c478bd9Sstevel@tonic-gate * and the padding itself in padbuffer.
58*7c478bd9Sstevel@tonic-gate */
59*7c478bd9Sstevel@tonic-gate char
rmpadding(char * str,char * padbuffer,int * padding)60*7c478bd9Sstevel@tonic-gate *rmpadding(char *str, char *padbuffer, int *padding)
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate static char rmbuffer[1024];
63*7c478bd9Sstevel@tonic-gate register char ch;
64*7c478bd9Sstevel@tonic-gate register char *pbufptr;
65*7c478bd9Sstevel@tonic-gate register char *retptr = rmbuffer;
66*7c478bd9Sstevel@tonic-gate char *svbufptr;
67*7c478bd9Sstevel@tonic-gate int padbylines = 0;
68*7c478bd9Sstevel@tonic-gate int paddigits = 0;
69*7c478bd9Sstevel@tonic-gate int paddecimal = 0;
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate padbuffer[0] = rmbuffer[0] = '\0';
72*7c478bd9Sstevel@tonic-gate if (padding)
73*7c478bd9Sstevel@tonic-gate *padding = 0;
74*7c478bd9Sstevel@tonic-gate if (str == NULL)
75*7c478bd9Sstevel@tonic-gate return (rmbuffer);
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate while (ch = (*str++ & 0377))
78*7c478bd9Sstevel@tonic-gate switch (ch) {
79*7c478bd9Sstevel@tonic-gate case '$':
80*7c478bd9Sstevel@tonic-gate if (*str == '<') {
81*7c478bd9Sstevel@tonic-gate svbufptr = ++str; /* skip '<' */
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate /* copy number */
84*7c478bd9Sstevel@tonic-gate pbufptr = padbuffer;
85*7c478bd9Sstevel@tonic-gate for (; *str && isdigit(*str); str++) {
86*7c478bd9Sstevel@tonic-gate *svbufptr++ = *str;
87*7c478bd9Sstevel@tonic-gate *pbufptr++ = *str;
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate *pbufptr = '\0';
90*7c478bd9Sstevel@tonic-gate paddigits += atoi(padbuffer);
91*7c478bd9Sstevel@tonic-gate /* check for decimal */
92*7c478bd9Sstevel@tonic-gate if (*str == '.') {
93*7c478bd9Sstevel@tonic-gate str++;
94*7c478bd9Sstevel@tonic-gate pbufptr = padbuffer;
95*7c478bd9Sstevel@tonic-gate for (; *str && isdigit(ch);
96*7c478bd9Sstevel@tonic-gate str++) {
97*7c478bd9Sstevel@tonic-gate *svbufptr++ = *str;
98*7c478bd9Sstevel@tonic-gate *pbufptr++ = *str;
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate *pbufptr = '\0';
101*7c478bd9Sstevel@tonic-gate paddecimal += atoi(padbuffer);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate for (; (*str == '*') || (*str == '/');
104*7c478bd9Sstevel@tonic-gate str++) {
105*7c478bd9Sstevel@tonic-gate if (*str == '*')
106*7c478bd9Sstevel@tonic-gate padbylines = 1;
107*7c478bd9Sstevel@tonic-gate /* Termcap does not support mandatory padding */
108*7c478bd9Sstevel@tonic-gate /* marked with /. Just remove it. */
109*7c478bd9Sstevel@tonic-gate else {
110*7c478bd9Sstevel@tonic-gate extern char *progname;
111*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
112*7c478bd9Sstevel@tonic-gate "%s: mandatory "
113*7c478bd9Sstevel@tonic-gate "padding removed\n",
114*7c478bd9Sstevel@tonic-gate progname);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate /* oops, not a padding spec after all */
118*7c478bd9Sstevel@tonic-gate /* put us back after the '$<' */
119*7c478bd9Sstevel@tonic-gate if (*str != '>') {
120*7c478bd9Sstevel@tonic-gate str = svbufptr;
121*7c478bd9Sstevel@tonic-gate *retptr++ = '$';
122*7c478bd9Sstevel@tonic-gate *retptr++ = '<';
123*7c478bd9Sstevel@tonic-gate } else
124*7c478bd9Sstevel@tonic-gate str++; /* skip the '>' */
125*7c478bd9Sstevel@tonic-gate /* Flag padding info that is not at the end */
126*7c478bd9Sstevel@tonic-gate /* of the string. */
127*7c478bd9Sstevel@tonic-gate if (*str != '\0') {
128*7c478bd9Sstevel@tonic-gate extern char *progname;
129*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
130*7c478bd9Sstevel@tonic-gate "%s: padding information "
131*7c478bd9Sstevel@tonic-gate "moved to end\n", progname);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate } else
134*7c478bd9Sstevel@tonic-gate *retptr++ = ch;
135*7c478bd9Sstevel@tonic-gate break;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate default:
138*7c478bd9Sstevel@tonic-gate *retptr++ = ch;
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate *retptr = '\0';
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate if (paddecimal > 10) {
143*7c478bd9Sstevel@tonic-gate paddigits += paddecimal / 10;
144*7c478bd9Sstevel@tonic-gate paddecimal %= 10;
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate if (paddigits > 0 && paddecimal > 0)
148*7c478bd9Sstevel@tonic-gate (void) sprintf(padbuffer, "%d.%d", paddigits, paddecimal);
149*7c478bd9Sstevel@tonic-gate else if (paddigits > 0)
150*7c478bd9Sstevel@tonic-gate (void) sprintf(padbuffer, "%d", paddigits);
151*7c478bd9Sstevel@tonic-gate else if (paddecimal > 0)
152*7c478bd9Sstevel@tonic-gate (void) sprintf(padbuffer, ".%d", paddecimal);
153*7c478bd9Sstevel@tonic-gate if (padbylines)
154*7c478bd9Sstevel@tonic-gate (void) strcat(padbuffer, "*");
155*7c478bd9Sstevel@tonic-gate if (padding)
156*7c478bd9Sstevel@tonic-gate *padding = paddecimal;
157*7c478bd9Sstevel@tonic-gate return (rmbuffer);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate /*
161*7c478bd9Sstevel@tonic-gate * Convert a character, making appropriate changes to make it printable
162*7c478bd9Sstevel@tonic-gate * for a termcap source entry. Change escape, tab, etc., into their
163*7c478bd9Sstevel@tonic-gate * appropriate equivalents. Return the number of characters printed.
164*7c478bd9Sstevel@tonic-gate */
165*7c478bd9Sstevel@tonic-gate char
cconvert(char * string)166*7c478bd9Sstevel@tonic-gate *cconvert(char *string)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate register int c;
169*7c478bd9Sstevel@tonic-gate register char *retptr = retbuffer;
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate retbuffer[0] = '\0';
172*7c478bd9Sstevel@tonic-gate if (string == NULL)
173*7c478bd9Sstevel@tonic-gate return (retbuffer);
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate while (c = *string++) {
176*7c478bd9Sstevel@tonic-gate /* should check here to make sure that there is enough room */
177*7c478bd9Sstevel@tonic-gate /* in retbuffer and realloc it if necessary. */
178*7c478bd9Sstevel@tonic-gate c &= 0377;
179*7c478bd9Sstevel@tonic-gate /* we ignore the return value from sprintf because BSD/V7 */
180*7c478bd9Sstevel@tonic-gate /* systems return a (char *) rather than an int. */
181*7c478bd9Sstevel@tonic-gate if (c >= 0200) {
182*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\%.3o", c); retptr += 4; }
183*7c478bd9Sstevel@tonic-gate else if (c == '\033') {
184*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\E"); retptr += 2; }
185*7c478bd9Sstevel@tonic-gate else if (c == '\t') {
186*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\t"); retptr += 2; }
187*7c478bd9Sstevel@tonic-gate else if (c == '\b') {
188*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\b"); retptr += 2; }
189*7c478bd9Sstevel@tonic-gate else if (c == '\f') {
190*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\f"); retptr += 2; }
191*7c478bd9Sstevel@tonic-gate else if (c == '\n') {
192*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\n"); retptr += 2; }
193*7c478bd9Sstevel@tonic-gate else if (c == '\r') {
194*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\r"); retptr += 2; }
195*7c478bd9Sstevel@tonic-gate
196*7c478bd9Sstevel@tonic-gate /* unfortunately \: did not work */
197*7c478bd9Sstevel@tonic-gate else if (c == ':') {
198*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\072"); retptr += 4; }
199*7c478bd9Sstevel@tonic-gate else if (c == '^') {
200*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "\\^"); retptr += 2; }
201*7c478bd9Sstevel@tonic-gate else if (c == BACKSLASH) {
202*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, BACKBACK); retptr += 2; }
203*7c478bd9Sstevel@tonic-gate else if (c < ' ' || c == 0177) {
204*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "^%c", c ^ 0100); retptr += 2; }
205*7c478bd9Sstevel@tonic-gate else {
206*7c478bd9Sstevel@tonic-gate (void) sprintf(retptr, "%c", c); retptr++; }
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate *retptr = '\0';
209*7c478bd9Sstevel@tonic-gate return (retbuffer);
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate * Convert the terminfo string into a termcap string.
214*7c478bd9Sstevel@tonic-gate * Most of the work is done by rmpadding() above and cconvert(); this
215*7c478bd9Sstevel@tonic-gate * function mainly just pieces things back together. A pointer to the
216*7c478bd9Sstevel@tonic-gate * return buffer is returned.
217*7c478bd9Sstevel@tonic-gate *
218*7c478bd9Sstevel@tonic-gate * NOTE: Some things can not be done at all: converting the terminfo
219*7c478bd9Sstevel@tonic-gate * parameterized strings into termcap parameterized strings.
220*7c478bd9Sstevel@tonic-gate */
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate char
cexpand(char * str)223*7c478bd9Sstevel@tonic-gate *cexpand(char *str)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate char padbuffer[512];
226*7c478bd9Sstevel@tonic-gate char *retptr;
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate retptr = rmpadding(str, padbuffer, (int *)0);
229*7c478bd9Sstevel@tonic-gate (void) sprintf(ret2buffer, "%s%s", padbuffer, cconvert(retptr));
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate return (ret2buffer);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate * Print out a string onto a stream, changing unprintables into
236*7c478bd9Sstevel@tonic-gate * termcap printables.
237*7c478bd9Sstevel@tonic-gate */
238*7c478bd9Sstevel@tonic-gate int
cpr(FILE * stream,char * string)239*7c478bd9Sstevel@tonic-gate cpr(FILE *stream, char *string)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate register char *ret;
242*7c478bd9Sstevel@tonic-gate if (string != NULL) {
243*7c478bd9Sstevel@tonic-gate ret = cexpand(string);
244*7c478bd9Sstevel@tonic-gate (void) fprintf(stream, "%s", ret);
245*7c478bd9Sstevel@tonic-gate return (strlen(ret));
246*7c478bd9Sstevel@tonic-gate } else
247*7c478bd9Sstevel@tonic-gate return (0);
248*7c478bd9Sstevel@tonic-gate }
249