]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/ul.c
Imported from util-linux-2.10f tarball.
[thirdparty/util-linux.git] / text-utils / ul.c
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * modified by Kars de Jong <jongk@cs.utwente.nl>
36 * to use terminfo instead of termcap.
37 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
38 * added Native Language Support
39 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
40 * modified to work correctly in multi-byte locales
41 */
42
43 #include <stdio.h>
44 #include <unistd.h> /* for getopt(), isatty() */
45 #include <string.h> /* for bzero(), strcpy() */
46 #include <term.h> /* for setupterm() */
47 #include <stdlib.h> /* for getenv() */
48 #include <limits.h> /* for INT_MAX */
49 #include "nls.h"
50
51 #include "widechar.h"
52
53 #ifdef ENABLE_WIDECHAR
54 static int put1wc(int c) /* Output an ASCII character as a wide character */
55 {
56 if (putwchar(c) == WEOF)
57 return EOF;
58 else
59 return c;
60 }
61 #define putwp(s) tputs(s,1,put1wc)
62 #else
63 #define putwp(s) putp(s)
64 #endif
65
66 void filter(FILE *f);
67 void flushln(void);
68 void overstrike(void);
69 void iattr(void);
70 void initbuf(void);
71 void fwd(void);
72 void reverse(void);
73 void initinfo(void);
74 void outc(wint_t c);
75 void setmode(int newmode);
76 void setcol(int newcol);
77
78 #define IESC '\033'
79 #define SO '\016'
80 #define SI '\017'
81 #define HFWD '9'
82 #define HREV '8'
83 #define FREV '7'
84
85 #define NORMAL 000
86 #define ALTSET 001 /* Reverse */
87 #define SUPERSC 002 /* Dim */
88 #define SUBSC 004 /* Dim | Ul */
89 #define UNDERL 010 /* Ul */
90 #define BOLD 020 /* Bold */
91 #define INITBUF 512
92
93 int must_use_uc, must_overstrike;
94 char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
95 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
96 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
97
98 struct CHAR {
99 char c_mode;
100 wchar_t c_char;
101 } ;
102
103 struct CHAR *obuf;
104 int obuflen; /* Tracks number of elements in obuf. */
105 int col, maxcol;
106 int mode;
107 int halfpos;
108 int upln;
109 int iflag;
110
111 #define PRINT(s) if (s == NULL) /* void */; else putwp(s)
112
113 int main(int argc, char **argv)
114 {
115 extern int optind;
116 extern char *optarg;
117 int c, ret;
118 char *termtype;
119 FILE *f;
120
121 setlocale(LC_ALL, "");
122 bindtextdomain(PACKAGE, LOCALEDIR);
123 textdomain(PACKAGE);
124
125 termtype = getenv("TERM");
126 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
127 termtype = "lpr";
128 while ((c=getopt(argc, argv, "it:T:")) != EOF)
129 switch(c) {
130
131 case 't':
132 case 'T': /* for nroff compatibility */
133 termtype = optarg;
134 break;
135 case 'i':
136 iflag = 1;
137 break;
138
139 default:
140 fprintf(stderr,
141 _("usage: %s [ -i ] [ -tTerm ] file...\n"),
142 argv[0]);
143 exit(1);
144 }
145 setupterm(termtype, 1, &ret);
146 switch(ret) {
147
148 case 1:
149 break;
150
151 default:
152 fprintf(stderr,_("trouble reading terminfo"));
153 /* fall through to ... */
154
155 case 0:
156 /* No such terminal type - assume dumb */
157 setupterm("dumb", 1, (int *)0);
158 break;
159 }
160 initinfo();
161 if ( (tigetflag("os") && ENTER_BOLD==NULL ) ||
162 (tigetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
163 must_overstrike = 1;
164 initbuf();
165 if (optind == argc)
166 filter(stdin);
167 else for (; optind<argc; optind++) {
168 f = fopen(argv[optind],"r");
169 if (f == NULL) {
170 perror(argv[optind]);
171 exit(1);
172 } else
173 filter(f);
174 }
175 if (ferror(stdout) || fclose(stdout))
176 return 1;
177 return 0;
178 }
179
180 void filter(FILE *f)
181 {
182 wint_t c;
183
184 while ((c = getwc(f)) != WEOF) switch(c) {
185
186 case '\b':
187 setcol(col - 1);
188 continue;
189
190 case '\t':
191 setcol((col+8) & ~07);
192 continue;
193
194 case '\r':
195 setcol(0);
196 continue;
197
198 case SO:
199 mode |= ALTSET;
200 continue;
201
202 case SI:
203 mode &= ~ALTSET;
204 continue;
205
206 case IESC:
207 switch (c = getwc(f)) {
208
209 case HREV:
210 if (halfpos == 0) {
211 mode |= SUPERSC;
212 halfpos--;
213 } else if (halfpos > 0) {
214 mode &= ~SUBSC;
215 halfpos--;
216 } else {
217 halfpos = 0;
218 reverse();
219 }
220 continue;
221
222 case HFWD:
223 if (halfpos == 0) {
224 mode |= SUBSC;
225 halfpos++;
226 } else if (halfpos < 0) {
227 mode &= ~SUPERSC;
228 halfpos++;
229 } else {
230 halfpos = 0;
231 fwd();
232 }
233 continue;
234
235 case FREV:
236 reverse();
237 continue;
238
239 default:
240 fprintf(stderr,
241 _("Unknown escape sequence in input: %o, %o\n"),
242 IESC, c);
243 exit(1);
244 }
245 continue;
246
247 case '_':
248 if (obuf[col].c_char)
249 obuf[col].c_mode |= UNDERL | mode;
250 else
251 obuf[col].c_char = '_';
252 case ' ':
253 setcol(col + 1);
254 continue;
255
256 case '\n':
257 flushln();
258 continue;
259
260 case '\f':
261 flushln();
262 putwchar('\f');
263 continue;
264
265 default:
266 if (!iswprint(c)) /* non printing */
267 continue;
268 if (obuf[col].c_char == '\0') {
269 obuf[col].c_char = c;
270 obuf[col].c_mode = mode;
271 } else if (obuf[col].c_char == '_') {
272 obuf[col].c_char = c;
273 obuf[col].c_mode |= UNDERL|mode;
274 } else if (obuf[col].c_char == c)
275 obuf[col].c_mode |= BOLD|mode;
276 else
277 obuf[col].c_mode = mode;
278 setcol(col + 1);
279 continue;
280 }
281 if (maxcol)
282 flushln();
283 }
284
285 void flushln()
286 {
287 int lastmode;
288 int i;
289 int hadmodes = 0;
290
291 lastmode = NORMAL;
292 for (i=0; i<maxcol; i++) {
293 if (obuf[i].c_mode != lastmode) {
294 hadmodes++;
295 setmode(obuf[i].c_mode);
296 lastmode = obuf[i].c_mode;
297 }
298 if (obuf[i].c_char == '\0') {
299 if (upln) {
300 PRINT(CURS_RIGHT);
301 } else
302 outc(' ');
303 } else
304 outc(obuf[i].c_char);
305 }
306 if (lastmode != NORMAL) {
307 setmode(0);
308 }
309 if (must_overstrike && hadmodes)
310 overstrike();
311 putwchar('\n');
312 if (iflag && hadmodes)
313 iattr();
314 (void)fflush(stdout);
315 if (upln)
316 upln--;
317 initbuf();
318 }
319
320 /*
321 * For terminals that can overstrike, overstrike underlines and bolds.
322 * We don't do anything with halfline ups and downs, or Greek.
323 */
324 void overstrike()
325 {
326 register int i;
327 #ifdef __GNUC__
328 register wchar_t *lbuf = __builtin_alloca((maxcol+1)*sizeof(wchar_t));
329 #else
330 wchar_t lbuf[256];
331 #endif
332 register wchar_t *cp = lbuf;
333 int hadbold=0;
334
335 /* Set up overstrike buffer */
336 for (i=0; i<maxcol; i++)
337 switch (obuf[i].c_mode) {
338 case NORMAL:
339 default:
340 *cp++ = ' ';
341 break;
342 case UNDERL:
343 *cp++ = '_';
344 break;
345 case BOLD:
346 *cp++ = obuf[i].c_char;
347 hadbold=1;
348 break;
349 }
350 putwchar('\r');
351 for (*cp=' '; *cp==' '; cp--)
352 *cp = 0;
353 for (cp=lbuf; *cp; cp++)
354 putwchar(*cp);
355 if (hadbold) {
356 putwchar('\r');
357 for (cp=lbuf; *cp; cp++)
358 putwchar(*cp=='_' ? ' ' : *cp);
359 putwchar('\r');
360 for (cp=lbuf; *cp; cp++)
361 putwchar(*cp=='_' ? ' ' : *cp);
362 }
363 }
364
365 void iattr()
366 {
367 register int i;
368 #ifdef __GNUC__
369 register char *lbuf = __builtin_alloca((maxcol+1)*sizeof(char));
370 #else
371 char lbuf[256];
372 #endif
373 register char *cp = lbuf;
374
375 for (i=0; i<maxcol; i++)
376 switch (obuf[i].c_mode) {
377 case NORMAL: *cp++ = ' '; break;
378 case ALTSET: *cp++ = 'g'; break;
379 case SUPERSC: *cp++ = '^'; break;
380 case SUBSC: *cp++ = 'v'; break;
381 case UNDERL: *cp++ = '_'; break;
382 case BOLD: *cp++ = '!'; break;
383 default: *cp++ = 'X'; break;
384 }
385 for (*cp=' '; *cp==' '; cp--)
386 *cp = 0;
387 for (cp=lbuf; *cp; cp++)
388 putwchar(*cp);
389 putwchar('\n');
390 }
391
392 void initbuf()
393 {
394 if (obuf == NULL) { /* First time. */
395 obuflen = INITBUF;
396 obuf = malloc(sizeof(struct CHAR) * obuflen);
397 if (obuf == NULL) {
398 fprintf(stderr, _("Unable to allocate buffer.\n"));
399 exit(1);
400 }
401 }
402
403 /* assumes NORMAL == 0 */
404 bzero((char *)obuf, sizeof(struct CHAR) * obuflen);
405 setcol(0);
406 maxcol = 0;
407 mode &= ALTSET;
408 }
409
410 void fwd()
411 {
412 int oldcol, oldmax;
413
414 oldcol = col;
415 oldmax = maxcol;
416 flushln();
417 setcol(oldcol);
418 maxcol = oldmax;
419 }
420
421 void reverse()
422 {
423 upln++;
424 fwd();
425 PRINT(CURS_UP);
426 PRINT(CURS_UP);
427 upln++;
428 }
429
430 void initinfo()
431 {
432 char *tigetstr();
433
434 CURS_UP = tigetstr("cuu1");
435 CURS_RIGHT = tigetstr("cuf1");
436 CURS_LEFT = tigetstr("cub1");
437 if (CURS_LEFT == NULL)
438 CURS_LEFT = "\b";
439
440 ENTER_STANDOUT = tigetstr("smso");
441 EXIT_STANDOUT = tigetstr("rmso");
442 ENTER_UNDERLINE = tigetstr("smul");
443 EXIT_UNDERLINE = tigetstr("rmul");
444 ENTER_DIM = tigetstr("dim");
445 ENTER_BOLD = tigetstr("bold");
446 ENTER_REVERSE = tigetstr("rev");
447 EXIT_ATTRIBUTES = tigetstr("sgr0");
448
449 if (!ENTER_BOLD && ENTER_REVERSE)
450 ENTER_BOLD = ENTER_REVERSE;
451 if (!ENTER_BOLD && ENTER_STANDOUT)
452 ENTER_BOLD = ENTER_STANDOUT;
453 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
454 ENTER_UNDERLINE = ENTER_STANDOUT;
455 EXIT_UNDERLINE = EXIT_STANDOUT;
456 }
457 if (!ENTER_DIM && ENTER_STANDOUT)
458 ENTER_DIM = ENTER_STANDOUT;
459 if (!ENTER_REVERSE && ENTER_STANDOUT)
460 ENTER_REVERSE = ENTER_STANDOUT;
461 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
462 EXIT_ATTRIBUTES = EXIT_STANDOUT;
463
464 /*
465 * Note that we use REVERSE for the alternate character set,
466 * not the as/ae capabilities. This is because we are modelling
467 * the model 37 teletype (since that's what nroff outputs) and
468 * the typical as/ae is more of a graphics set, not the greek
469 * letters the 37 has.
470 */
471
472 UNDER_CHAR = tigetstr("uc");
473 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
474 }
475
476 static int curmode = 0;
477
478 void outc(wint_t c)
479 {
480 putwchar(c);
481 if (must_use_uc && (curmode&UNDERL)) {
482 PRINT(CURS_LEFT);
483 PRINT(UNDER_CHAR);
484 }
485 }
486
487 void setmode(int newmode)
488 {
489 if (!iflag) {
490 if (curmode != NORMAL && newmode != NORMAL)
491 setmode(NORMAL);
492 switch (newmode) {
493 case NORMAL:
494 switch(curmode) {
495 case NORMAL:
496 break;
497 case UNDERL:
498 PRINT(EXIT_UNDERLINE);
499 break;
500 default:
501 /* This includes standout */
502 PRINT(EXIT_ATTRIBUTES);
503 break;
504 }
505 break;
506 case ALTSET:
507 PRINT(ENTER_REVERSE);
508 break;
509 case SUPERSC:
510 /*
511 * This only works on a few terminals.
512 * It should be fixed.
513 */
514 PRINT(ENTER_UNDERLINE);
515 PRINT(ENTER_DIM);
516 break;
517 case SUBSC:
518 PRINT(ENTER_DIM);
519 break;
520 case UNDERL:
521 PRINT(ENTER_UNDERLINE);
522 break;
523 case BOLD:
524 PRINT(ENTER_BOLD);
525 break;
526 default:
527 /*
528 * We should have some provision here for multiple modes
529 * on at once. This will have to come later.
530 */
531 PRINT(ENTER_STANDOUT);
532 break;
533 }
534 }
535 curmode = newmode;
536 }
537
538
539
540
541 void setcol(int newcol)
542 {
543 col = newcol;
544
545 if (col < 0)
546 col = 0;
547 else if (col > maxcol) {
548 maxcol = col;
549
550 /* If col >= obuflen, expand obuf until obuflen > col. */
551 while (col >= obuflen) {
552 /* Paranoid check for obuflen == INT_MAX. */
553 if (obuflen == INT_MAX) {
554 fprintf(stderr,
555 _("Input line too long.\n"));
556 exit(1);
557 }
558
559 /* Similar paranoia: double only up to INT_MAX. */
560 obuflen = ((INT_MAX / 2) < obuflen)
561 ? INT_MAX
562 : obuflen * 2;
563
564 /* Now we can try to expand obuf. */
565 obuf = realloc(obuf, sizeof(struct CHAR) * obuflen);
566 if (obuf == NULL) {
567 fprintf(stderr,
568 _("Out of memory when growing buffer.\n"));
569 exit(1);
570 }
571 }
572 }
573 }