3
0

printable.c 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. void FAST_FUNC fputc_printable(int ch, FILE *file)
  11. {
  12. if ((ch & (0x80 + PRINTABLE_META)) == (0x80 + PRINTABLE_META)) {
  13. fputs("M-", file);
  14. ch &= 0x7f;
  15. }
  16. ch = (unsigned char) ch;
  17. if (ch == 0x9b) {
  18. /* VT100's CSI, aka Meta-ESC, is not printable on vt-100 */
  19. ch = '{';
  20. goto print_caret;
  21. }
  22. if (ch < ' ') {
  23. ch += '@';
  24. goto print_caret;
  25. }
  26. if (ch == 0x7f) {
  27. ch = '?';
  28. print_caret:
  29. fputc('^', file);
  30. }
  31. fputc(ch, file);
  32. }
  33. void FAST_FUNC visible(unsigned ch, char *buf, int flags)
  34. {
  35. if (ch == '\t' && !(flags & VISIBLE_SHOW_TABS)) {
  36. goto raw;
  37. }
  38. if (ch == '\n') {
  39. if (flags & VISIBLE_ENDLINE)
  40. *buf++ = '$';
  41. } else {
  42. if (ch >= 128) {
  43. ch -= 128;
  44. *buf++ = 'M';
  45. *buf++ = '-';
  46. }
  47. if (ch < 32 || ch == 127) {
  48. *buf++ = '^';
  49. ch ^= 0x40;
  50. }
  51. }
  52. raw:
  53. *buf++ = ch;
  54. *buf = '\0';
  55. }