aboutsummaryrefslogtreecommitdiff
path: root/a8cat.c
diff options
context:
space:
mode:
Diffstat (limited to 'a8cat.c')
-rw-r--r--a8cat.c130
1 files changed, 23 insertions, 107 deletions
diff --git a/a8cat.c b/a8cat.c
index 3c58510..92eae9f 100644
--- a/a8cat.c
+++ b/a8cat.c
@@ -6,24 +6,12 @@
#include <wchar.h>
#include <errno.h>
-#include "atables.h"
+#include "atascii.h"
#include "wtable.h"
-const char **table = ata2utf;
+int reverse = 0, verbose = 0;
-/* XXX: hard-coded ANSI/vt100 escape sequences. would be
- better but more complex to use terminfo to support any ol'
- terminal... */
-/* 20250316 bkw: used to use this:
-const char *inverse_off = "\x1b[0m";
- ...but 27m is widely supported, and makes it easier for
- colorize-amsb to postprocess a8cat's output. */
-const char *inverse_on = "\x1b[7m";
-const char *inverse_off = "\x1b[27m";
-
-int verbose = 0;
-int underline = 0, reverse = 0, textmode = 0, ics = 0;
-int magazine = 0, stripinv = 0;
+atascii_ctx actx;
const char *self;
@@ -77,9 +65,9 @@ int handle_escape_seq(int inv, FILE *input, const char *file, int line) {
buf[count] = c; /* FIXME: might be a wide char! */
}
- if(strcmp(inverse_on, buf) == 0) {
+ if(strcmp(atascii_inverse_on, buf) == 0) {
return 0x80;
- } else if(strcmp(inverse_off, buf) == 0) {
+ } else if(strcmp(atascii_inverse_off, buf) == 0) {
return 0;
} else {
fprintf(stderr, "%s: warning: %s:%d: unrecognized ANSI escape sequence.\n", self, file, line);
@@ -112,7 +100,7 @@ int a8revcat(const char *file) {
} else if(c < 0x80) {
putchar(c | inv);
} else {
- d = wchar2atascii(c, ics);
+ d = wchar2atascii(c, actx.flags & ATA_FLAG_ICS);
if(d == -1) {
fprintf(stderr, "%s: warning: %s:%d: unrecognized Unicode character %04x.\n", self, file, line, c);
} else {
@@ -128,103 +116,24 @@ int a8revcat(const char *file) {
return 0;
}
-void inverse(int onoff) {
- fputs((onoff ? inverse_on : inverse_off ), stdout);
-}
-
int a8cat(const char *file) {
FILE *input;
- int c, inv = 0, line = 1;
+ int c;
+ char converted[20];
if( !(input = open_input(file)) )
return 1;
while( (c = fgetc(input)) != EOF ) {
- if(c == 0x9b) {
- putchar('\n');
- line++;
- continue;
- }
-
- if(stripinv) c &= 0x7f;
-
- if(textmode) {
- switch(c) {
- case 0x7f: /* tab */
- putchar('\t');
- continue;
- case 0xfd: /* bell */
- putchar('\a');
- continue;
- case 0x7e: /* backspace */
- putchar('\b');
- continue;
- default: break;
- }
- }
-
- if(!underline) {
- /* strings of inverse chars only get one "inverse on" ANSI
- sequence, and one "inverse off" afterwards. */
- if(c & 0x80) {
- if(!inv) {
- inv = 1;
- if(magazine)
- fputs("{inv}", stdout);
- else
- inverse(1);
- }
- } else {
- if(inv) {
- inv = 0;
- if(magazine)
- fputs("{norm}", stdout);
- else
- inverse(0);
- }
- }
- }
-
- if(magazine) {
- /* special cases: control codes with bit 7 set can't go
- in the table since it's only got 128 entries. */
- switch(c) {
- case 0x9c:
- fputs("{del-line}", stdout); continue;
- case 0x9d:
- fputs("{ins-line}", stdout); continue;
- case 0x9e:
- fputs("{clr-tab}", stdout); continue;
- case 0x9f:
- fputs("{set-tab}", stdout); continue;
- case 0xfd:
- fputs("{bell}", stdout); continue;
- case 0xfe:
- fputs("{del-char}", stdout); continue;
- case 0xff:
- fputs("{ins-char}", stdout); continue;
- }
- }
-
- fputs(table[c & 0x7f], stdout);
-
- if(underline && (c & 0x80)) {
- putchar('\b');
- putchar('_');
- }
+ fputs(atascii_a2utf(&actx, c, converted), stdout);
}
/* gotta turn off inverse, so if there's another file after this one,
it doesn't start out being printed in inverse. */
- if(inv && !underline) {
- if(magazine)
- fputs("{norm}", stdout);
- else
- inverse(0);
- }
+ fputs(atascii_a2utf(&actx, ATA_CHR_FINISH, converted), stdout);
if(verbose)
- fprintf(stderr, "%s: %s: converted %d lines, closing file.\n", self, file, line - 1);
+ fprintf(stderr, "%s: %s: converted %d lines, closing file.\n", self, file, actx.lines - 1);
fclose(input);
return 0;
@@ -232,6 +141,7 @@ int a8cat(const char *file) {
int main(int argc, char **argv) {
int opt, result = 0;
+ int mode = ATA_MODE_UTF8, flags = 0;
set_self(argv[0]);
@@ -247,36 +157,42 @@ int main(int argc, char **argv) {
while( (opt = getopt(argc, argv, "ihurtmsv")) != -1) {
switch(opt) {
- case 'i': table = ics2utf; ics = 1; break;
+ case 'i': flags |= ATA_FLAG_ICS; break;
case 'h': print_help(); exit(0); break;
- case 'u': underline = 1; break;
+ case 'u': flags |= ATA_FLAG_UNDERLINE; break;
case 'r': reverse = 1; break;
- case 't': textmode = 1; break;
- case 'm': table = ata2mag; magazine = 1; break;
- case 's': stripinv = 1; break;
+ case 't': flags |= ATA_FLAG_TEXTMODE; break;
+ case 'm': mode = ATA_MODE_MAGAZINE; break;
+ case 's': flags |= ATA_FLAG_STRIP_INVERSE; break;
case 'v': verbose = 1; break;
default: print_help(); exit(1); break;
}
}
+ atascii_context_init(&actx, mode, flags);
+
if(reverse) {
if(isatty(fileno(stdout)))
die("Refusing to write ATASCII to a terminal.");
+ /*
if(underline || textmode || stripinv || magazine) {
die("-t, -u, -m, -s options don't make sense with -r.\n");
}
+ */
/* the language_country part of the locale doesn't matter,
since we aren't doing localization. the encoding *has*
to be UTF-8. */
setlocale(LC_CTYPE, "en_US.UTF-8");
}
+ /*
if(magazine) {
if(ics || stripinv || underline) {
die("-i, -s, -u options don't make sense with -m.\n");
}
}
+ */
if(optind >= argc) {
result = (reverse ? a8revcat("-") : a8cat("-"));