blob: 224dde406b7df697218207de2e42efcbdcd03c0c [file] [log] [blame]
Denis Vlasenko27963982007-12-30 20:13:39 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko
Denis Vlasenko27963982007-12-30 20:13:39 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko27963982007-12-30 20:13:39 +00008 */
Denis Vlasenko27963982007-12-30 20:13:39 +00009#include "libbb.h"
10
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000011void FAST_FUNC fputc_printable(int ch, FILE *file)
Denis Vlasenko27963982007-12-30 20:13:39 +000012{
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}
Bartosz Golaszewski79c618c2013-07-30 06:29:42 +020034
35void 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}