The repository formerly known as dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttpre.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <wchar.h>
  21. static void put(const char *tag, wchar_t ch) {
  22. if (tag) printf("<%s>", tag);
  23. switch (ch) {
  24. break; case L'&': printf("&amp;");
  25. break; case L'<': printf("&lt;");
  26. break; case L'>': printf("&gt;");
  27. break; default: printf("%lc", ch);
  28. }
  29. if (tag) printf("</%s>", tag);
  30. }
  31. static void push(wchar_t ch) {
  32. static wchar_t q[3];
  33. if (q[1] == L'\b' && q[0] == L'_') {
  34. put("i", q[2]);
  35. memset(q, 0, sizeof(q));
  36. } else if (q[1] == L'\b' && q[0] == q[2]) {
  37. put("b", q[2]);
  38. memset(q, 0, sizeof(q));
  39. } else if (q[0]) {
  40. put(NULL, q[0]);
  41. }
  42. memmove(q, &q[1], sizeof(q) - sizeof(wchar_t));
  43. q[2] = ch;
  44. }
  45. int main() {
  46. setlocale(LC_CTYPE, "");
  47. printf("<pre>");
  48. wchar_t ch;
  49. while (WEOF != (ch = getwchar())) push(ch);
  50. push(0); push(0); push(0);
  51. printf("</pre>\n");
  52. return EXIT_SUCCESS;
  53. }