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