Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / printable.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 2007 Denys Vlasenko
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 #include "libbb.h"
10
11 void FAST_FUNC fputc_printable(int ch, FILE *file)
12 {
13         if ((ch & (0x80 + PRINTABLE_META)) == (0x80 + PRINTABLE_META)) {
14                 fputs("M-", file);
15                 ch &= 0x7f;
16         }
17         ch = (unsigned char) ch;
18         if (ch == 0x9b) {
19                 /* VT100's CSI, aka Meta-ESC, is not printable on vt-100 */
20                 ch = '{';
21                 goto print_caret;
22         }
23         if (ch < ' ') {
24                 ch += '@';
25                 goto print_caret;
26         }
27         if (ch == 0x7f) {
28                 ch = '?';
29  print_caret:
30                 fputc('^', file);
31         }
32         fputc(ch, file);
33 }
34
35 void FAST_FUNC visible(unsigned ch, char *buf, int flags)
36 {
37         if (ch == '\t' && !(flags & VISIBLE_SHOW_TABS)) {
38                 goto raw;
39         }
40         if (ch == '\n') {
41                 if (flags & VISIBLE_ENDLINE)
42                         *buf++ = '$';
43         } else {
44                 if (ch >= 128) {
45                         ch -= 128;
46                         *buf++ = 'M';
47                         *buf++ = '-';
48                 }
49                 if (ch < 32 || ch == 127) {
50                         *buf++ = '^';
51                         ch ^= 0x40;
52                 }
53         }
54  raw:
55         *buf++ = ch;
56         *buf = '\0';
57 }