printable.c 620 B

12345678910111213141516171819202122232425262728293031323334
  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. }