3 * $Id: bascat.c,v 1.1 1998/03/16 15:21:37 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/03/16 15:21:37 mdw
33 * Files placed under CVS control.
35 * Revision 1.1 1997/07/23 01:19:33 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI library headers --- */
51 /* --- Operating system specific headers --- */
53 #ifdef HAVE_LIBTERMCAP
58 /* --- Private headers --- */
62 /*----- Version information -----------------------------------------------*/
70 /*----- Tokenisation tables -----------------------------------------------*
72 * These tables are from the BBC BASIC guide. Additional verification
73 * carried out on an A440 with RISC OS 3.1
76 static const char *bcTok__base[] = {
78 "AND", "DIV", "EOR", "MOD", "OR", "ERROR", "LINE", "OFF",
79 "STEP", "SPC", "TAB(", "ELSE", "THEN", "*", "OPENIN", "PTR",
80 "PAGE", "TIME", "LOMEM", "HIMEM", "ABS", "ACS", "ADVAL", "ASC",
81 "ASN", "ATN", "BGET", "COS", "COUNT", "DEG", "ERL", "ERR",
82 "EVAL", "EXP", "EXT", "FALSE", "FN", "GET", "INKEY", "INSTR(",
83 "INT", "LEN", "LN", "LOG", "NOT", "OPENUP", "OPENOUT", "PI",
84 "POINT(", "POS", "RAD", "RND", "SGN", "SIN", "SQR", "TAN",
85 "TO", "TRUE", "USR", "VAL", "VPOS", "CHR$", "GET$", "INKEY$",
86 "LEFT$(", "MID$(", "RIGHT$(", "STR$", "STRING$(", "EOF", "*", "*",
87 "*", "WHEN", "OF", "ENDCASE", "ELSE", "ENDIF", "ENDWHILE", "PTR",
88 "PAGE", "TIME", "LOMEM", "HIMEM", "SOUND", "BPUT", "CALL", "CHAIN",
89 "CLEAR", "CLOSE", "CLG", "CLS", "DATA", "DEF", "DIM", "DRAW",
90 "END", "ENDPROC", "ENVELOPE", "FOR", "GOSUB", "GOTO", "GCOL", "IF",
91 "INPUT", "LET", "LOCAL", "MODE", "MOVE", "NEXT", "ON", "VDU",
92 "PLOT", "PRINT", "PROC", "READ", "REM", "REPEAT", "REPORT", "RESTORE",
93 "RETURN", "RUN", "STOP", "COLOUR", "TRACE", "UNTIL", "WIDTH", "OSCLI"
96 static const char *bcTok__c6[] = {
100 static const char *bcTok__c7[] = {
102 "CRUNCH", "DELETE", "EDIT", "HELP", "LIST", "LOAD", "LVAR", "NEW",
103 "OLD", "RENUMBER", "SAVE", "TEXTLOAD", "TEXTSAVE", "TWIN", "TWINO",
107 static const char *bcTok__c8[] = {
109 "FILL", "ORIGIN", "POINT", "RECTANGLE", "SWAP", "WHILE", "WAIT", "MOUSE",
110 "QUIT", "SYS", "INSTALL", "LIBRARY", "TINT", "ELLIPSE", "BEATS", "TEMPO",
111 "VOICES", "VOICE", "STEREO", "OVERLAY"
114 #define ITEMS(array) (sizeof(array) / sizeof((array)[0]))
116 /*----- Static variables --------------------------------------------------*/
119 s_keyword, /* Expecting a keyword next */
120 s_normal, /* Normal state, reading input */
121 s_comment, /* In a command (or literal *cmd) */
122 s_quote, /* Inside a quoted string */
123 s_c6, s_c7, s_c8, /* Various shift states */
127 static bc__state = s_normal; /* Current detokenisation state */
130 f_highlight = 1, /* Highlight keywords and things */
131 f_linenumbers = 2, /* Display linenumbers on left */
132 f_tty = 4, /* We're writing to TTY (or pager) */
133 f_less = 8, /* We're piping through `less' */
137 static int bc__flags; /* Various options flags */
139 #ifdef HAVE_LIBTERMCAP
140 static char bc__termcap[2048]; /* Terminal capabilities buffer */
143 static char *bc__pager = 0; /* Pointer to pager to use */
145 /*----- Main code ---------------------------------------------------------*/
149 * Arguments: @char *p@ = pointer to a string
153 * Use: Reports an error to the user and falls over flat on its face.
156 static int die(const char *p)
158 fprintf(stderr, "%s: %s\n", optprog, p);
162 /* --- @bc__keyword@ --- *
164 * Arguments: @char *s@ = pointer to keyword string
165 * @FILE *fp@ = stream to write onto
169 * Use: Displays a keyword in a nice way. There's some nasty hacking
170 * here to make GNU's `less' work properly. `more' appears to
171 * cope with highlighting codes OK, so that's fine. `less'
172 * prefers it if we attempt to `overstrike' the bolded
173 * characters. What fun...
176 static void bc__keyword(const char *s, FILE *fp)
178 #ifdef HAVE_LIBTERMCAP
179 if ((~bc__flags & (f_less | f_highlight)) == 0) {
182 putc(8, fp); /* evil... */
187 static char buff[24];
188 static char *hs, *he, *p = buff;
191 if (bc__flags & f_highlight) {
192 hs = tgetstr("md", &p);
193 he = tgetstr("me", &p);
206 /* --- @bc__mbtok@ --- *
208 * Arguments: @int byte@ = the current byte
209 * @const char *t[]@ = pointer to token table
210 * @int n@ = number of items in token table
211 * @FILE *fp@ = stream to write onto
213 * Returns: 0 if everything's OK
215 * Use: Decodes multibyte tokens.
218 static int bc__mbtok(int byte, const char *t[], int n, FILE * fp)
222 return (die("Bad program: invalid multibyte token"));
223 bc__keyword(t[byte], fp);
224 bc__state = s_normal;
228 /* --- @bc__decode@ --- *
230 * Arguments: @int byte@ = byte to decode
231 * @FILE *fp@ = stream to write onto
233 * Returns: 0 if everything's going OK
235 * Use: Decodes a byte, changing states as necessary.
238 static int bc__decode(int byte, FILE * fp)
243 bc__state = s_comment;
245 bc__state = s_normal;
246 /* Fall through here */
259 case 0x8B: /* ELSE */
260 case 0x8C: /* THEN */
261 case 0xF5: /* REPEAT (a funny one) */
262 bc__state = s_keyword;
263 bc__keyword(bcTok__base[byte - 0x7F], fp);
265 case 0xDC: /* DATA */
267 bc__state = s_comment;
268 /* Fall through here */
270 bc__keyword(bcTok__base[byte - 0x7F], fp);
281 bc__state = s_normal;
282 /* Fall through here */
287 return (bc__mbtok(byte, bcTok__c6, ITEMS(bcTok__c6), fp));
290 return (bc__mbtok(byte, bcTok__c7, ITEMS(bcTok__c7), fp));
293 return (bc__mbtok(byte, bcTok__c8, ITEMS(bcTok__c8), fp));
299 /* --- @bc__line@ --- *
301 * Arguments: @FILE *in@ = input stream to read
302 * @FILE *out@ = output stream to write
304 * Returns: Zero if there's another line after this one.
306 * Use: Decodes a BASIC line into stuff to be written.
309 static int bc__line(FILE *in, FILE *out)
311 /* --- Read the line number --- */
317 D( fprintf(stderr, "ln_0 == %i\n", a); )
324 D( fprintf(stderr, "ln_1 == %i\n", b); )
328 if (bc__flags & f_linenumbers)
329 fprintf(out, "%5i", (a << 8) + b);
337 D( fprintf(stderr, "linelen == %i\n", len); )
342 bc__state = s_normal;
345 D( fprintf(stderr, "state == %i, byte == %i\n", \
349 bc__decode(byte, out);
355 D( fprintf(stderr, "eol == %i\n", byte); )
358 else if (byte != 0x0D)
359 return (die("Bad program: expected end-of-line delimiter"));
365 return (die("Bad program: unexpected end-of-file"));
368 /* --- @bc__file@ --- *
370 * Arguments: @FILE *in@ = the input stream
371 * @FILE *out@ = the output stream
375 * Use: Decodes an entire file.
378 static void bc__file(FILE *in, FILE *out)
382 /* --- Check for the inital newline char --- */
386 die("Bad program: doesn't start with a newline");
388 /* --- Now read the lines in one by one --- */
390 while (!bc__line(in, out)) ;
392 /* --- Check that we're really at end-of-file --- */
396 die("Found data after end of program");
399 /* --- @bc__sigPipe@ --- *
401 * Arguments: @int s@ = signal number
405 * Use: Handles SIGPIPE signals, and gracefully kills the program.
408 static void bc__sigPipe(int s)
411 exit(0); /* Gracefully, oh yes */
414 /* --- @bc__options@ --- *
416 * Arguments: @int c@ = number of arguments
417 * @char *v[]@ = pointer to arguments
418 * @char *s@ = pointer to short options
419 * @struct option *o@ = pointer to long options
423 * Use: Parses lots of arguments.
426 static void bc__options(int c, char *v[], const char *s,
427 const struct option *o)
432 i = mdwopt(c, v, s, o, 0, 0, gFlag_negation | gFlag_envVar);
439 printf("%s v. " VERSION " (" __DATE__ ")\n", optprog);
443 "%s [-hv] [-n|+n] [-l|+l] [-p PAGER] [file...]\n"
445 "Types BBC BASIC programs in a readable way. Options "
446 "currently supported are as\n"
449 "-h, --help: Displays this evil help message\n"
450 "-v, --version: Displays the current version number\n"
451 "-n, --line-numbers: Displays line numbers for each line\n"
452 "-l, --highlight: Attempts to highlight keywords\n"
453 "-p, --pager=PAGER: Sets pager to use (default $PAGER)\n"
455 "Prefix long options with `no-' to cancel them. Use `+' to "
456 "cancel short options.\n"
457 "Options can also be placed in the `BASCAT' environment "
458 "variable, if you don't\n"
459 "like the standard settings.\n",
464 bc__flags |= f_linenumbers;
466 case 'n' | gFlag_negated:
467 bc__flags &= ~f_linenumbers;
470 bc__flags |= f_highlight;
472 case 'l' | gFlag_negated:
473 bc__flags &= ~f_highlight;
484 * Arguments: @int argc@ = number of arguments
485 * @char *argc[]@ = pointer to what the arguments are
487 * Returns: 0 if it all worked
489 * Use: Displays BASIC programs.
492 int main(int argc, char *argv[])
494 static struct option opts[] = {
495 { "help", 0, 0, 'h' },
496 { "version", 0, 0, 'v' },
497 { "line-numbers", gFlag_negate, 0, 'n' },
498 { "highlight", gFlag_negate, 0, 'l' },
499 { "pager", gFlag_argReq, 0, 'p' },
502 static char *shortopts = "hvn+l+p:";
504 /* --- Parse the command line options --- */
506 bc__options(argc, argv, shortopts, opts);
508 /* --- Now do the job --- */
510 if (optind == argc && isatty(0)) {
512 "%s: No filenames given, and standard input is a tty\n"
513 "To force reading from stdin, use `%s -'. For help, type "
515 optprog, optprog, optprog);
519 #ifdef HAVE_LIBTERMCAP
520 if (bc__flags & f_highlight)
521 tgetent(bc__termcap, getenv("TERM"));
528 /* --- If output is to a terminal, try paging --- *
530 * All programs which spew text should do this ;-)
535 bc__pager = getenv("PAGER");
537 bc__pager = PAGER; /* Worth a try */
538 if (strstr(bc__pager, "less"))
539 bc__flags |= f_less; /* HACK!!! */
540 out = popen(bc__pager, "w");
545 signal(SIGPIPE, bc__sigPipe);
550 /* --- Now go through all the files --- */
553 bc__file(stdin, out);
555 while (optind < argc) {
556 if (strcmp(argv[optind], "-") == 0)
557 bc__file(stdin, out);
559 in = fopen(argv[optind], "rb");
562 "%s: Couldn't open input file: %s\n",
563 optprog, strerror(errno));
571 if (bc__flags & f_tty)
578 /*----- That's all, folks -------------------------------------------------*/