5 * Display BBC BASIC programs more or less anywhere
7 * (c) 1996, 1997 Matthew Wilcox and Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Bascat.
14 * Bascat is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Bascat is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Bascat; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 /* --- ANSI library headers --- */
40 /* --- Operating system specific headers --- */
42 #ifdef HAVE_LIBTERMCAP
47 /* --- Private headers --- */
51 /*----- Version information -----------------------------------------------*/
59 /*----- Tokenisation tables -----------------------------------------------*
61 * These tables are from the BBC BASIC guide. Additional verification
62 * carried out on an A440 with RISC OS 3.1
65 static const char *tok_base[] = {
67 "AND", "DIV", "EOR", "MOD", "OR", "ERROR", "LINE", "OFF",
68 "STEP", "SPC", "TAB(", "ELSE", "THEN", "*", "OPENIN", "PTR",
69 "PAGE", "TIME", "LOMEM", "HIMEM", "ABS", "ACS", "ADVAL", "ASC",
70 "ASN", "ATN", "BGET", "COS", "COUNT", "DEG", "ERL", "ERR",
71 "EVAL", "EXP", "EXT", "FALSE", "FN", "GET", "INKEY", "INSTR(",
72 "INT", "LEN", "LN", "LOG", "NOT", "OPENUP", "OPENOUT", "PI",
73 "POINT(", "POS", "RAD", "RND", "SGN", "SIN", "SQR", "TAN",
74 "TO", "TRUE", "USR", "VAL", "VPOS", "CHR$", "GET$", "INKEY$",
75 "LEFT$(", "MID$(", "RIGHT$(", "STR$", "STRING$(", "EOF", "*", "*",
76 "*", "WHEN", "OF", "ENDCASE", "ELSE", "ENDIF", "ENDWHILE", "PTR",
77 "PAGE", "TIME", "LOMEM", "HIMEM", "SOUND", "BPUT", "CALL", "CHAIN",
78 "CLEAR", "CLOSE", "CLG", "CLS", "DATA", "DEF", "DIM", "DRAW",
79 "END", "ENDPROC", "ENVELOPE", "FOR", "GOSUB", "GOTO", "GCOL", "IF",
80 "INPUT", "LET", "LOCAL", "MODE", "MOVE", "NEXT", "ON", "VDU",
81 "PLOT", "PRINT", "PROC", "READ", "REM", "REPEAT", "REPORT", "RESTORE",
82 "RETURN", "RUN", "STOP", "COLOUR", "TRACE", "UNTIL", "WIDTH", "OSCLI"
85 static const char *tok_c6[] = {
89 static const char *tok_c7[] = {
91 "CRUNCH", "DELETE", "EDIT", "HELP", "LIST", "LOAD", "LVAR", "NEW",
92 "OLD", "RENUMBER", "SAVE", "TEXTLOAD", "TEXTSAVE", "TWIN", "TWINO",
96 static const char *tok_c8[] = {
98 "FILL", "ORIGIN", "POINT", "RECTANGLE", "SWAP", "WHILE", "WAIT", "MOUSE",
99 "QUIT", "SYS", "INSTALL", "LIBRARY", "TINT", "ELLIPSE", "BEATS", "TEMPO",
100 "VOICES", "VOICE", "STEREO", "OVERLAY"
103 #define ITEMS(array) (sizeof(array) / sizeof((array)[0]))
105 /*----- Static variables --------------------------------------------------*/
108 s_keyword, /* Expecting a keyword next */
109 s_normal, /* Normal state, reading input */
110 s_comment, /* In a command (or literal *cmd) */
111 s_quote, /* Inside a quoted string */
112 s_c6, s_c7, s_c8, /* Various shift states */
113 s_linex, s_liney, s_linez, /* Line number states */
117 static int state = s_normal; /* Current detokenisation state */
118 static unsigned int lineno; /* Line number */
121 f_highlight = 1, /* Highlight keywords and things */
122 f_linenumbers = 2, /* Display linenumbers on left */
123 f_tty = 4, /* We're writing to TTY (or pager) */
124 f_less = 8, /* We're piping through `less' */
128 static int flags; /* Various options flags */
130 #ifdef HAVE_LIBTERMCAP
131 static char termcap[2048]; /* Terminal capabilities buffer */
134 static char *pager = 0; /* Pointer to pager to use */
136 /*----- Main code ---------------------------------------------------------*/
140 * Arguments: @char *p@ = pointer to a string
144 * Use: Reports an error to the user and falls over flat on its face.
147 static int die(const char *p)
149 fprintf(stderr, "%s: %s\n", optprog, p);
153 /* --- @keyword@ --- *
155 * Arguments: @char *s@ = pointer to keyword string
156 * @FILE *fp@ = stream to write onto
160 * Use: Displays a keyword in a nice way. There's some nasty hacking
161 * here to make GNU's `less' work properly. `more' appears to
162 * cope with highlighting codes OK, so that's fine. `less'
163 * prefers it if we attempt to `overstrike' the bolded
164 * characters. What fun...
167 static void keyword(const char *s, FILE *fp)
169 #ifdef HAVE_LIBTERMCAP
170 if ((~flags & (f_less | f_highlight)) == 0) {
178 static char buff[24];
179 static char *hs, *he, *p = buff;
182 if (flags & f_highlight) {
183 hs = tgetstr("md", &p);
184 he = tgetstr("me", &p);
199 * Arguments: @int byte@ = the current byte
200 * @const char *t[]@ = pointer to token table
201 * @int n@ = number of items in token table
202 * @FILE *fp@ = stream to write onto
204 * Returns: 0 if everything's OK
206 * Use: Decodes multibyte tokens.
209 static int mbtok(int byte, const char *t[], int n, FILE *fp)
213 return (die("Bad program: invalid multibyte token"));
214 keyword(t[byte], fp);
219 /* --- @decode@ --- *
221 * Arguments: @int byte@ = byte to decode
222 * @FILE *fp@ = stream to write onto
224 * Returns: 0 if everything's going OK
226 * Use: Decodes a byte, changing states as necessary.
229 static int decode(int byte, FILE *fp)
233 /* --- Tokenised states --- */
240 /* Fall through here */
257 case 0x8B: /* ELSE */
258 case 0x8C: /* THEN */
259 case 0xF5: /* REPEAT (a funny one) */
263 case 0xDC: /* DATA */
266 /* Fall through here */
269 keyword(tok_base[byte - 0x7F], fp);
279 /* --- Non-tokenised states --- */
284 /* Fall through here */
290 /* --- Double-byte token states --- */
293 return (mbtok(byte, tok_c6, ITEMS(tok_c6), fp));
297 return (mbtok(byte, tok_c7, ITEMS(tok_c7), fp));
301 return (mbtok(byte, tok_c8, ITEMS(tok_c8), fp));
304 /* --- Encoded line number states --- */
308 lineno = (((byte & 0x30) << 2) |
309 ((byte & 0x0c) << 12) |
310 ((byte & 0x03) << 16));
315 lineno |= byte & 0x3f;
320 lineno |= (byte & 0x3f) << 8;
321 fprintf(fp, "%u", lineno);
331 * Arguments: @FILE *in@ = input stream to read
332 * @FILE *out@ = output stream to write
334 * Returns: Zero if there's another line after this one.
336 * Use: Decodes a BASIC line into stuff to be written.
339 static int line(FILE *in, FILE *out)
341 /* --- Read the line number --- */
347 D( fprintf(stderr, "ln_0 == %i\n", a); )
354 D( fprintf(stderr, "ln_1 == %i\n", b); )
358 if (flags & f_linenumbers)
359 fprintf(out, "%5i", (a << 8) + b);
367 D( fprintf(stderr, "linelen == %i\n", len); )
375 D( fprintf(stderr, "state == %i, byte == %i\n",
385 D( fprintf(stderr, "eol == %i\n", byte); )
388 else if (byte != 0x0D)
389 return (die("Bad program: expected end-of-line delimiter"));
395 return (die("Bad program: unexpected end-of-file"));
400 * Arguments: @FILE *in@ = the input stream
401 * @FILE *out@ = the output stream
405 * Use: Decodes an entire file.
408 static void file(FILE *in, FILE *out)
412 /* --- Check for the inital newline char --- */
416 die("Bad program: doesn't start with a newline");
418 /* --- Now read the lines in one by one --- */
420 while (!line(in, out)) ;
422 /* --- Check that we're really at end-of-file --- */
426 die("Found data after end of program");
429 /* --- @sig_pipe@ --- *
431 * Arguments: @int s@ = signal number
435 * Use: Handles SIGPIPE signals, and gracefully kills the program.
438 static void sig_pipe(int s)
441 exit(0); /* Gracefully, oh yes */
444 /* --- @options@ --- *
446 * Arguments: @int c@ = number of arguments
447 * @char *v[]@ = pointer to arguments
448 * @char *s@ = pointer to short options
449 * @struct option *o@ = pointer to long options
453 * Use: Parses lots of arguments.
456 static void options(int c, char *v[], const char *s,
457 const struct option *o)
462 i = mdwopt(c, v, s, o, 0, 0, OPTF_NEGATION | OPTF_ENVVAR);
469 printf("%s version " VERSION "\n", optprog);
473 "%s [-hv] [-n|+n] [-l|+l] [-p PAGER] [file...]\n"
475 "Types BBC BASIC programs in a readable way. Options "
476 "currently supported are as\n"
479 "-h, --help: Displays this evil help message\n"
480 "-v, --version: Displays the current version number\n"
481 "-n, --line-numbers: Displays line numbers for each line\n"
482 "-l, --highlight: Attempts to highlight keywords\n"
483 "-p, --pager=PAGER: Sets pager to use (default $PAGER)\n"
485 "Prefix long options with `no-' to cancel them. Use `+' to "
486 "cancel short options.\n"
487 "Options can also be placed in the `BASCAT' environment "
488 "variable, if you don't\n"
489 "like the standard settings.\n",
494 flags |= f_linenumbers;
496 case 'n' | OPTF_NEGATED:
497 flags &= ~f_linenumbers;
500 flags |= f_highlight;
502 case 'l' | OPTF_NEGATED:
503 flags &= ~f_highlight;
514 * Arguments: @int argc@ = number of arguments
515 * @char *argc[]@ = pointer to what the arguments are
517 * Returns: 0 if it all worked
519 * Use: Displays BASIC programs.
522 int main(int argc, char *argv[])
524 static struct option opts[] = {
525 { "help", 0, 0, 'h' },
526 { "version", 0, 0, 'v' },
527 { "line-numbers", OPTF_NEGATE, 0, 'n' },
528 { "highlight", OPTF_NEGATE, 0, 'l' },
529 { "pager", OPTF_ARGREQ, 0, 'p' },
532 static char *shortopts = "hvn+l+p:";
534 /* --- Parse the command line options --- */
536 options(argc, argv, shortopts, opts);
538 /* --- Now do the job --- */
540 if (optind == argc && isatty(0)) {
542 "%s: No filenames given, and standard input is a tty\n"
543 "To force reading from stdin, use `%s -'. For help, type "
545 optprog, optprog, optprog);
549 #ifdef HAVE_LIBTERMCAP
550 if (flags & f_highlight)
551 tgetent(termcap, getenv("TERM"));
558 /* --- If output is to a terminal, try paging --- *
560 * All programs which spew text should do this ;-)
565 pager = getenv("PAGER");
567 pager = PAGER; /* Worth a try */
568 if (strstr(pager, "less"))
569 flags |= f_less; /* HACK!!! */
570 out = popen(pager, "w");
575 signal(SIGPIPE, sig_pipe);
580 /* --- Now go through all the files --- */
585 while (optind < argc) {
586 if (strcmp(argv[optind], "-") == 0)
589 in = fopen(argv[optind], "rb");
592 "%s: Couldn't open input file: %s\n",
593 optprog, strerror(errno));
608 /*----- That's all, folks -------------------------------------------------*/