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