3 * $Id: bascat.c,v 1.3 1999/10/28 10:18:17 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.3 1999/10/28 10:18:17 mdw
33 * Minor name changes for new coding standards.
35 * Revision 1.2 1998/06/26 11:29:28 mdw
36 * Added support for line-numbers.
38 * Revision 1.1 1998/03/16 15:21:37 mdw
39 * Files placed under CVS control.
41 * Revision 1.1 1997/07/23 01:19:33 mdw
46 /*----- Header files ------------------------------------------------------*/
48 /* --- ANSI library headers --- */
57 /* --- Operating system specific headers --- */
59 #ifdef HAVE_LIBTERMCAP
64 /* --- Private headers --- */
68 /*----- Version information -----------------------------------------------*/
76 /*----- Tokenisation tables -----------------------------------------------*
78 * These tables are from the BBC BASIC guide. Additional verification
79 * carried out on an A440 with RISC OS 3.1
82 static const char *bcTok__base
[] = {
84 "AND", "DIV", "EOR", "MOD", "OR", "ERROR", "LINE", "OFF",
85 "STEP", "SPC", "TAB(", "ELSE", "THEN", "*", "OPENIN", "PTR",
86 "PAGE", "TIME", "LOMEM", "HIMEM", "ABS", "ACS", "ADVAL", "ASC",
87 "ASN", "ATN", "BGET", "COS", "COUNT", "DEG", "ERL", "ERR",
88 "EVAL", "EXP", "EXT", "FALSE", "FN", "GET", "INKEY", "INSTR(",
89 "INT", "LEN", "LN", "LOG", "NOT", "OPENUP", "OPENOUT", "PI",
90 "POINT(", "POS", "RAD", "RND", "SGN", "SIN", "SQR", "TAN",
91 "TO", "TRUE", "USR", "VAL", "VPOS", "CHR$", "GET$", "INKEY$",
92 "LEFT$(", "MID$(", "RIGHT$(", "STR$", "STRING$(", "EOF", "*", "*",
93 "*", "WHEN", "OF", "ENDCASE", "ELSE", "ENDIF", "ENDWHILE", "PTR",
94 "PAGE", "TIME", "LOMEM", "HIMEM", "SOUND", "BPUT", "CALL", "CHAIN",
95 "CLEAR", "CLOSE", "CLG", "CLS", "DATA", "DEF", "DIM", "DRAW",
96 "END", "ENDPROC", "ENVELOPE", "FOR", "GOSUB", "GOTO", "GCOL", "IF",
97 "INPUT", "LET", "LOCAL", "MODE", "MOVE", "NEXT", "ON", "VDU",
98 "PLOT", "PRINT", "PROC", "READ", "REM", "REPEAT", "REPORT", "RESTORE",
99 "RETURN", "RUN", "STOP", "COLOUR", "TRACE", "UNTIL", "WIDTH", "OSCLI"
102 static const char *bcTok__c6
[] = {
106 static const char *bcTok__c7
[] = {
108 "CRUNCH", "DELETE", "EDIT", "HELP", "LIST", "LOAD", "LVAR", "NEW",
109 "OLD", "RENUMBER", "SAVE", "TEXTLOAD", "TEXTSAVE", "TWIN", "TWINO",
113 static const char *bcTok__c8
[] = {
115 "FILL", "ORIGIN", "POINT", "RECTANGLE", "SWAP", "WHILE", "WAIT", "MOUSE",
116 "QUIT", "SYS", "INSTALL", "LIBRARY", "TINT", "ELLIPSE", "BEATS", "TEMPO",
117 "VOICES", "VOICE", "STEREO", "OVERLAY"
120 #define ITEMS(array) (sizeof(array) / sizeof((array)[0]))
122 /*----- Static variables --------------------------------------------------*/
125 s_keyword
, /* Expecting a keyword next */
126 s_normal
, /* Normal state, reading input */
127 s_comment
, /* In a command (or literal *cmd) */
128 s_quote
, /* Inside a quoted string */
129 s_c6
, s_c7
, s_c8
, /* Various shift states */
130 s_linex
, s_liney
, s_linez
, /* Line number states */
134 static int state
= s_normal
; /* Current detokenisation state */
135 static unsigned int lineno
; /* Line number */
138 f_highlight
= 1, /* Highlight keywords and things */
139 f_linenumbers
= 2, /* Display linenumbers on left */
140 f_tty
= 4, /* We're writing to TTY (or pager) */
141 f_less
= 8, /* We're piping through `less' */
145 static int flags
; /* Various options flags */
147 #ifdef HAVE_LIBTERMCAP
148 static char termcap
[2048]; /* Terminal capabilities buffer */
151 static char *pager
= 0; /* Pointer to pager to use */
153 /*----- Main code ---------------------------------------------------------*/
157 * Arguments: @char *p@ = pointer to a string
161 * Use: Reports an error to the user and falls over flat on its face.
164 static int die(const char *p
)
166 fprintf(stderr
, "%s: %s\n", optprog
, p
);
170 /* --- @keyword@ --- *
172 * Arguments: @char *s@ = pointer to keyword string
173 * @FILE *fp@ = stream to write onto
177 * Use: Displays a keyword in a nice way. There's some nasty hacking
178 * here to make GNU's `less' work properly. `more' appears to
179 * cope with highlighting codes OK, so that's fine. `less'
180 * prefers it if we attempt to `overstrike' the bolded
181 * characters. What fun...
184 static void keyword(const char *s
, FILE *fp
)
186 #ifdef HAVE_LIBTERMCAP
187 if ((~flags
& (f_less
| f_highlight
)) == 0) {
195 static char buff
[24];
196 static char *hs
, *he
, *p
= buff
;
199 if (flags
& f_highlight
) {
200 hs
= tgetstr("md", &p
);
201 he
= tgetstr("me", &p
);
216 * Arguments: @int byte@ = the current byte
217 * @const char *t[]@ = pointer to token table
218 * @int n@ = number of items in token table
219 * @FILE *fp@ = stream to write onto
221 * Returns: 0 if everything's OK
223 * Use: Decodes multibyte tokens.
226 static int mbtok(int byte
, const char *t
[], int n
, FILE * fp
)
230 return (die("Bad program: invalid multibyte token"));
231 keyword(t
[byte
], fp
);
236 /* --- @decode@ --- *
238 * Arguments: @int byte@ = byte to decode
239 * @FILE *fp@ = stream to write onto
241 * Returns: 0 if everything's going OK
243 * Use: Decodes a byte, changing states as necessary.
246 static int decode(int byte
, FILE * fp
)
250 /* --- Tokenised states --- */
257 /* Fall through here */
274 case 0x8B: /* ELSE */
275 case 0x8C: /* THEN */
276 case 0xF5: /* REPEAT (a funny one) */
280 case 0xDC: /* DATA */
283 /* Fall through here */
286 keyword(bcTok__base
[byte
- 0x7F], fp
);
296 /* --- Non-tokenised states --- */
301 /* Fall through here */
307 /* --- Double-byte token states --- */
310 return (mbtok(byte
, bcTok__c6
, ITEMS(bcTok__c6
), fp
));
314 return (mbtok(byte
, bcTok__c7
, ITEMS(bcTok__c7
), fp
));
318 return (mbtok(byte
, bcTok__c8
, ITEMS(bcTok__c8
), fp
));
321 /* --- Encoded line number states --- */
324 lineno
= ((((byte
<< 2) | (byte
<< 12)) & 0xc0c0u
) ^ 0x4040u
);
329 lineno
|= byte
& 0x3fu
;
334 lineno
|= (byte
<< 8) & 0x3fu
;
335 fprintf(fp
, "%u", lineno
);
345 * Arguments: @FILE *in@ = input stream to read
346 * @FILE *out@ = output stream to write
348 * Returns: Zero if there's another line after this one.
350 * Use: Decodes a BASIC line into stuff to be written.
353 static int line(FILE *in
, FILE *out
)
355 /* --- Read the line number --- */
361 D( fprintf(stderr
, "ln_0 == %i\n", a
); )
368 D( fprintf(stderr
, "ln_1 == %i\n", b
); )
372 if (flags
& f_linenumbers
)
373 fprintf(out
, "%5i", (a
<< 8) + b
);
381 D( fprintf(stderr
, "linelen == %i\n", len
); )
389 D( fprintf(stderr
, "state == %i, byte == %i\n",
399 D( fprintf(stderr
, "eol == %i\n", byte
); )
402 else if (byte
!= 0x0D)
403 return (die("Bad program: expected end-of-line delimiter"));
409 return (die("Bad program: unexpected end-of-file"));
414 * Arguments: @FILE *in@ = the input stream
415 * @FILE *out@ = the output stream
419 * Use: Decodes an entire file.
422 static void file(FILE *in
, FILE *out
)
426 /* --- Check for the inital newline char --- */
430 die("Bad program: doesn't start with a newline");
432 /* --- Now read the lines in one by one --- */
434 while (!line(in
, out
)) ;
436 /* --- Check that we're really at end-of-file --- */
440 die("Found data after end of program");
443 /* --- @sigPipe@ --- *
445 * Arguments: @int s@ = signal number
449 * Use: Handles SIGPIPE signals, and gracefully kills the program.
452 static void sigPipe(int s
)
455 exit(0); /* Gracefully, oh yes */
458 /* --- @options@ --- *
460 * Arguments: @int c@ = number of arguments
461 * @char *v[]@ = pointer to arguments
462 * @char *s@ = pointer to short options
463 * @struct option *o@ = pointer to long options
467 * Use: Parses lots of arguments.
470 static void options(int c
, char *v
[], const char *s
,
471 const struct option
*o
)
476 i
= mdwopt(c
, v
, s
, o
, 0, 0, OPTF_NEGATION
| OPTF_ENVVAR
);
483 printf("%s version " VERSION
"\n", optprog
);
487 "%s [-hv] [-n|+n] [-l|+l] [-p PAGER] [file...]\n"
489 "Types BBC BASIC programs in a readable way. Options "
490 "currently supported are as\n"
493 "-h, --help: Displays this evil help message\n"
494 "-v, --version: Displays the current version number\n"
495 "-n, --line-numbers: Displays line numbers for each line\n"
496 "-l, --highlight: Attempts to highlight keywords\n"
497 "-p, --pager=PAGER: Sets pager to use (default $PAGER)\n"
499 "Prefix long options with `no-' to cancel them. Use `+' to "
500 "cancel short options.\n"
501 "Options can also be placed in the `BASCAT' environment "
502 "variable, if you don't\n"
503 "like the standard settings.\n",
508 flags
|= f_linenumbers
;
510 case 'n' | OPTF_NEGATED
:
511 flags
&= ~f_linenumbers
;
514 flags
|= f_highlight
;
516 case 'l' | OPTF_NEGATED
:
517 flags
&= ~f_highlight
;
528 * Arguments: @int argc@ = number of arguments
529 * @char *argc[]@ = pointer to what the arguments are
531 * Returns: 0 if it all worked
533 * Use: Displays BASIC programs.
536 int main(int argc
, char *argv
[])
538 static struct option opts
[] = {
539 { "help", 0, 0, 'h' },
540 { "version", 0, 0, 'v' },
541 { "line-numbers", OPTF_NEGATE
, 0, 'n' },
542 { "highlight", OPTF_NEGATE
, 0, 'l' },
543 { "pager", OPTF_ARGREQ
, 0, 'p' },
546 static char *shortopts
= "hvn+l+p:";
548 /* --- Parse the command line options --- */
550 options(argc
, argv
, shortopts
, opts
);
552 /* --- Now do the job --- */
554 if (optind
== argc
&& isatty(0)) {
556 "%s: No filenames given, and standard input is a tty\n"
557 "To force reading from stdin, use `%s -'. For help, type "
559 optprog
, optprog
, optprog
);
563 #ifdef HAVE_LIBTERMCAP
564 if (flags
& f_highlight
)
565 tgetent(termcap
, getenv("TERM"));
572 /* --- If output is to a terminal, try paging --- *
574 * All programs which spew text should do this ;-)
579 pager
= getenv("PAGER");
581 pager
= PAGER
; /* Worth a try */
582 if (strstr(pager
, "less"))
583 flags
|= f_less
; /* HACK!!! */
584 out
= popen(pager
, "w");
589 signal(SIGPIPE
, sigPipe
);
594 /* --- Now go through all the files --- */
599 while (optind
< argc
) {
600 if (strcmp(argv
[optind
], "-") == 0)
603 in
= fopen(argv
[optind
], "rb");
606 "%s: Couldn't open input file: %s\n",
607 optprog
, strerror(errno
));
622 /*----- That's all, folks -------------------------------------------------*/