/* * Copyright (c) 2023 DTB * Copyright (c) 2024 Emma Tebibyte * SPDX-License-Identifier: AGPL-3.0-or-later * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) any * later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ #include /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin, * stdout, EOF */ #include /* EX_IOERR, EX_OK, EX_USAGE */ #include /* getopt(3) */ char *program_name = "npc"; static int ioerr(char *argv0, int err) { perror(argv0); return EX_IOERR; } static int usage(char *argv0) { (void)fprintf(stderr, "Usage: %s [-et]\n", argv0); return EX_USAGE; } int main(int argc, char *argv[]) { int c; char showend = 0; /* print a dollar sign before each newline */ char showtab = 0; /* prints tab characters in caret notation */ if (argc > 0) { program_name = argv[0]; while ((c = getopt(argc, argv, "et")) != -1) { switch (c){ case 'e': showend = 1; break; case 't': showtab = 1; break; default: return usage(program_name); } } } if (argc > optind) { return usage(program_name); } while ((c = getc(stdin)) != EOF) { if ((c & 0x80) != 0 && fputs("M-", stdout) == EOF) { return ioerr(argv[0]); } switch (c ^ 0x80 /* 0b 1000 0000 */) { case 0x7f: /* ASCII DEL (127d) */ if(fputs("^?", stdout) == EOF) { return ioerr(argv[0]); } break; case '\n': if (showend && fputc('$', stdout) == EOF) { return ioerr(argv[0]); } } default: if (c >= ' ' || c == '\n' || (!showtab && c == '\t')) { if (fputc(c, stdout) == EOF) { return ioerr(argv[0]); } } else if (fprintf(stdout, "^%c", c + '@') < 0) { return ioerr(argv[0]); } } } return EX_OK; }