Post History
How about BSD vis? It's basically perfect for this. It translates all unprintable characters into escape sequences like \^C or (for characters above 128) \M-A. A corresponding unvis program can tr...
#2: Post edited
- How about BSD `vis`? It's basically perfect for this.
It translates all unprintable characters into escape sequences like `\^C` or (for characters above 128) `\M-A`.- Unfortunately we don't know of any good way to install it on our Debian box, there doesn't seem to be a package for it. So we wrote our own at some point. It's pretty simple.
- ```
- // vis.c
- // written by Topaz <[email protected]>
- #include <stdio.h>
- int main (int argc, char **argv) {
- int c;
- while ((c = getchar()) != EOF) {
- int printable = 0;
- if (c >= 32 && c < 127) {
- printable = 1;
- }
- if (c == ' ' || c == '\t' || c == '\n') {
- printable = 1;
- }
- if (c == '\\') {
- printf("\\\\");
- } else if (printable) {
- putchar(c);
- } else {
- if (c & 0b10000000) {
- // high bit set, M-x
- c -= 128;
- if (c < 32 || c == 127) {
- printf("\\M^%c", (c + 64) % 128);
- } else {
- printf("\\M-%c", c);
- }
- } else {
- // 0x7F should be \^?,
- // a control character but +64 would be >128;
- // so wrap it around
- printf("\\^%c", (c + 64) % 128);
- }
- }
- }
- }
- ```
- How about BSD `vis`? It's basically perfect for this.
- It translates all unprintable characters into escape sequences like `\^C` or (for characters above 128) `\M-A`. A corresponding `unvis` program can translate it back (it's unambiguous because `vis` also doubles backslashes in the input).
- Unfortunately we don't know of any good way to install it on our Debian box, there doesn't seem to be a package for it. So we wrote our own at some point. It's pretty simple.
- ```
- // vis.c
- // written by Topaz <[email protected]>
- #include <stdio.h>
- int main (int argc, char **argv) {
- int c;
- while ((c = getchar()) != EOF) {
- int printable = 0;
- if (c >= 32 && c < 127) {
- printable = 1;
- }
- if (c == ' ' || c == '\t' || c == '\n') {
- printable = 1;
- }
- if (c == '\\') {
- printf("\\\\");
- } else if (printable) {
- putchar(c);
- } else {
- if (c & 0b10000000) {
- // high bit set, M-x
- c -= 128;
- if (c < 32 || c == 127) {
- printf("\\M^%c", (c + 64) % 128);
- } else {
- printf("\\M-%c", c);
- }
- } else {
- // 0x7F should be \^?,
- // a control character but +64 would be >128;
- // so wrap it around
- printf("\\^%c", (c + 64) % 128);
- }
- }
- }
- }
- ```
#1: Initial revision
How about BSD `vis`? It's basically perfect for this. It translates all unprintable characters into escape sequences like `\^C` or (for characters above 128) `\M-A`. Unfortunately we don't know of any good way to install it on our Debian box, there doesn't seem to be a package for it. So we wrote our own at some point. It's pretty simple. ``` // vis.c // written by Topaz <[email protected]> #include <stdio.h> int main (int argc, char **argv) { int c; while ((c = getchar()) != EOF) { int printable = 0; if (c >= 32 && c < 127) { printable = 1; } if (c == ' ' || c == '\t' || c == '\n') { printable = 1; } if (c == '\\') { printf("\\\\"); } else if (printable) { putchar(c); } else { if (c & 0b10000000) { // high bit set, M-x c -= 128; if (c < 32 || c == 127) { printf("\\M^%c", (c + 64) % 128); } else { printf("\\M-%c", c); } } else { // 0x7F should be \^?, // a control character but +64 would be >128; // so wrap it around printf("\\^%c", (c + 64) % 128); } } } } ```
