aboutsummaryrefslogtreecommitdiff
path: root/listbas.c
diff options
context:
space:
mode:
Diffstat (limited to 'listbas.c')
-rw-r--r--listbas.c173
1 files changed, 121 insertions, 52 deletions
diff --git a/listbas.c b/listbas.c
index d085460..e3b4225 100644
--- a/listbas.c
+++ b/listbas.c
@@ -13,43 +13,76 @@
#include "tokens.h"
#include "atables.h"
+#define COLOR_FMT "\x1b[%d;3%dm" /* 1st %d is 1 for bold, 2nd is color */
+
+#define COLOR_OFF "\x1b[0m" /* clears inverse, too! */
+#define INV_OFF "\x1b[0m" /* clears color, too! */
+
+/* These would be better to use, allow turning off color and inverse
+ independently, but are not as widely supported by various terminals. */
+/* #define COLOR_OFF "\x1b[39;49m" */
+/* #define INV_OFF "\x1b[27m"" */
+
+#define INV_ON "\x1b[7m" /* goes all the way back to the original VT100 */
+#define ULINE_ON "\x1b[4m"
+
+#define MAG_INV "{inv}"
+#define MAG_NORM "{norm}"
+
+/* colors. don't use blue, it's fugly. */
#define C_RED 1
#define C_GREEN 2
#define C_YELLOW 3
#define C_PURPLE 5
#define C_CYAN 6
-int immediate = 0, utf8 = 1, magazine = 0, inv = 0,
- color = 1, bold = 0, badtok = 0, dots = 0;
+/* output modes */
+#define M_UTF8 0 /* default */
+#define M_UTF8_I 1 /* -x */
+#define M_ATASCII 2 /* -a */
+#define M_MAG 3 /* -m */
+#define M_DOTS 4 /* -d */
+
+int output_mode = M_UTF8;
-const char **table = ata2utf;
+int bold = 0; /* 1 with -b */
+int color = 1; /* 0 with -n */
+int immediate = 0; /* 1 with -i */
+int underline = 0; /* 1 with -u */
+int badtok = 0; /* set to 1 if we find a bad token */
+int inv = 0; /* set to 1 when we're printing inverse */
+int cur_color = -1; /* -1 = no color */
FILE *outfh;
void print_help(void) {
- printf("Usage: %s [-a | -d | -m] [-b] [-i] [-n] [-v] <inputfile>\n", self);
+ printf("Usage: %s [-a | -d | -m | -x] [-b] [-i] [-n] [-v] <inputfile>\n", self);
printf(" -a: output raw ATASCII.\n");
printf(" -d: use dots instead of Unicode/UTF-8.\n");
printf(" -m: magazine style listing (see a8cat(1)).\n");
+ printf(" -x: XL international character set (UTF-8).\n");
printf(" -b: use bold for color output.\n");
printf(" -i: show immediate mode command (line 32768).\n");
printf(" -n: disable color syntax highlighting.\n");
+ printf(" -n: use underline for inverse video.\n");
printf(" -v: verbose.\n");
}
void parse_args(int argc, char **argv) {
int opt;
- int opt_a = 0, opt_m = 0, opt_d = 0;
+ int opt_a = 0, opt_m = 0, opt_d = 0, opt_x = 0;
- while( (opt = getopt(argc, argv, "viamnbdh")) != -1) {
+ while( (opt = getopt(argc, argv, "viamnbdhxu")) != -1) {
switch(opt) {
- case 'v': verbose = 1; break;
+ case 'v': verbose = 1; break;
case 'i': immediate = 1; break;
- case 'a': opt_a = 1; break;
- case 'm': opt_m = 1; break;
- case 'd': opt_d = 1; break;
- case 'n': color = 0; break;
- case 'b': bold = 1; break;
+ case 'a': opt_a = 1; break;
+ case 'm': opt_m = 1; break;
+ case 'd': opt_d = 1; break;
+ case 'x': opt_x = 1; break;
+ case 'b': bold = 1; break;
+ case 'u': underline = 1; break;
+ case 'n': color = 0; break;
case 'h': print_help(); exit(0);
default:
print_help();
@@ -57,19 +90,17 @@ void parse_args(int argc, char **argv) {
}
}
- if(opt_a + opt_d + opt_m > 1)
- die("Only use one of the -a, -d, -m options.");
+ if(opt_a + opt_d + opt_m + opt_x > 1)
+ die("Only use one of the -a, -d, -m, -x options.");
if(opt_a) {
- utf8 = magazine = color = dots = 0;
- table = ata2utf;
+ output_mode = M_ATASCII;
} else if(opt_d) {
- magazine = 0;
- utf8 = dots = 1;
+ output_mode = M_DOTS;
} else if(opt_m) {
- magazine = 1;
- utf8 = color = dots = 0;
- table = ata2mag;
+ output_mode = M_MAG;
+ } else if(opt_x) {
+ output_mode = M_UTF8_I;
}
if(optind >= argc)
@@ -79,11 +110,12 @@ void parse_args(int argc, char **argv) {
}
void setup_outfh(void) {
- if(! (utf8 || magazine) ) {
+ if(output_mode == M_ATASCII) {
if(isatty(fileno(stdout))) {
die("Refusing to write ATASCII data to the terminal.");
}
}
+ /* TODO: reopen in binary mode! */
outfh = stdout;
return;
}
@@ -118,12 +150,13 @@ double bcd2double(const unsigned char *num) {
}
void color_on(unsigned char c) {
- printf("\x1b[%d;3%dm", bold, c);
+ cur_color = c;
+ printf(COLOR_FMT, bold, c);
}
void color_off(void) {
- /* fputs("\x1b[0m", outfh); */ /* NO: clears inverse, too! */
- fputs("\x1b[39;49m", outfh);
+ cur_color = -1;
+ fputs(COLOR_OFF, outfh);
}
void print_number(unsigned int pos) {
@@ -162,27 +195,48 @@ int affects_inv(unsigned char c) {
}
void start_inv(unsigned char c) {
- if(utf8) {
- fputs("\x1b[7m", outfh);
- inv = 1;
- } else if(magazine) {
- if(affects_inv(c)) {
- fputs("{inv}", outfh);
+ switch(output_mode) {
+ case M_DOTS:
+ case M_UTF8:
+ case M_UTF8_I:
+ if(underline)
+ fputs(ULINE_ON, outfh);
+ else
+ fputs(INV_ON, outfh);
inv = 1;
- }
+ break;
+ case M_MAG:
+ if(affects_inv(c)) {
+ fputs(MAG_INV, outfh);
+ inv = 1;
+ }
+ break;
+ case M_ATASCII:
+ default:
+ break;
}
}
void end_inv(unsigned char c) {
- if(utf8) {
- /* fputs("\x1b[0m", outfh); */ /* NO: turns off color, too. */
- fputs("\x1b[27m", outfh);
- inv = 0;
- } else if(magazine) {
- if(affects_inv(c)) {
- fputs("{norm}", outfh);
+ switch(output_mode) {
+ case M_DOTS:
+ case M_UTF8:
+ case M_UTF8_I:
+ fputs(INV_OFF, outfh);
inv = 0;
- }
+ /* work around INV_OFF also turning off color. */
+ if(cur_color > -1)
+ color_on(cur_color);
+ break;
+ case M_MAG:
+ if(affects_inv(c)) {
+ fputs(MAG_NORM, outfh);
+ inv = 0;
+ }
+ break;
+ case M_ATASCII:
+ default:
+ break;
}
}
@@ -196,22 +250,37 @@ void print_ata_chr(unsigned char c) {
end_inv(c);
}
}
- if(dots)
- fputc(isprint(c & 0x7f) ? c & 0x7f : '.', outfh);
- else if(utf8 || magazine)
- fputs(table[c & 0x7f], outfh);
- else
- putchar(c);
+
+ switch(output_mode) {
+ case M_DOTS:
+ fputc(isprint(c & 0x7f) ? c & 0x7f : '.', outfh);
+ break;
+ case M_UTF8:
+ fputs(ata2utf[c & 0x7f], outfh);
+ break;
+ case M_UTF8_I:
+ fputs(ics2utf[c & 0x7f], outfh);
+ break;
+ case M_MAG:
+ fputs(ata2mag[c & 0x7f], outfh);
+ break;
+ case M_ATASCII:
+ default:
+ outchr(c);
+ break;
+ }
}
void print_string(unsigned int pos, unsigned int len) {
inv = 0;
- if(color) color_on(C_RED);
outchr('"');
+ if(color) color_on(C_RED);
while(len--) print_ata_chr(program[pos++]);
- if(inv) end_inv(0);
- outchr('"');
+ if(inv) {
+ end_inv(0);
+ }
if(color) color_off();
+ outchr('"');
}
CALLBACK(print_lineno) {
@@ -310,10 +379,10 @@ CALLBACK(print_text) {
}
CALLBACK(print_newline) {
- if(utf8 || magazine)
- outchr('\n');
- else
+ if(output_mode == M_ATASCII)
outchr(0x9b);
+ else
+ outchr('\n');
}
CALLBACK(code_prot) {