Fix decoding of line numbers.
[bascat] / bascat.c
index 139d58d..48e032e 100644 (file)
--- a/bascat.c
+++ b/bascat.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: bascat.c,v 1.3 1999/10/28 10:18:17 mdw Exp $
+ * $Id: bascat.c,v 1.5 1999/11/25 01:08:57 mdw Exp $
  *
  * Display BBC BASIC programs more or less anywhere
  *
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: bascat.c,v $
+ * Revision 1.5  1999/11/25 01:08:57  mdw
+ * Fix decoding of line numbers.
+ *
+ * Revision 1.4  1999/10/28 10:42:23  mdw
+ * More minor twiddling.
+ *
  * Revision 1.3  1999/10/28 10:18:17  mdw
  * Minor name changes for new coding standards.
  *
@@ -79,7 +85,7 @@
  * carried out on an A440 with RISC OS 3.1
  */
 
-static const char *bcTok__base[] = {
+static const char *tok_base[] = {
   "OTHERWISE",
   "AND", "DIV", "EOR", "MOD", "OR", "ERROR", "LINE", "OFF",
   "STEP", "SPC", "TAB(", "ELSE", "THEN", "*", "OPENIN", "PTR",
@@ -99,18 +105,18 @@ static const char *bcTok__base[] = {
   "RETURN", "RUN", "STOP", "COLOUR", "TRACE", "UNTIL", "WIDTH", "OSCLI"
 };
 
-static const char *bcTok__c6[] = {
+static const char *tok_c6[] = {
   "SUM", "BEAT"
 };
 
-static const char *bcTok__c7[] = {
+static const char *tok_c7[] = {
   "APPEND", "AUTO",
   "CRUNCH", "DELETE", "EDIT", "HELP", "LIST", "LOAD", "LVAR", "NEW",
   "OLD", "RENUMBER", "SAVE", "TEXTLOAD", "TEXTSAVE", "TWIN", "TWINO",
     "INSTALL"
 };
 
-static const char *bcTok__c8[] = {
+static const char *tok_c8[] = {
   "CASE", "CIRCLE",
   "FILL", "ORIGIN", "POINT", "RECTANGLE", "SWAP", "WHILE", "WAIT", "MOUSE",
   "QUIT", "SYS", "INSTALL", "LIBRARY", "TINT", "ELLIPSE", "BEATS", "TEMPO",
@@ -223,7 +229,7 @@ static void keyword(const char *s, FILE *fp)
  * Use:         Decodes multibyte tokens.
  */
 
-static int mbtok(int byte, const char *t[], int n, FILE * fp)
+static int mbtok(int byte, const char *t[], int n, FILE *fp)
 {
   byte -= 0x8E;
   if (byte >= n)
@@ -243,7 +249,7 @@ static int mbtok(int byte, const char *t[], int n, FILE * fp)
  * Use:         Decodes a byte, changing states as necessary.
  */
 
-static int decode(int byte, FILE * fp)
+static int decode(int byte, FILE *fp)
 {
   switch (state) {
 
@@ -283,7 +289,7 @@ static int decode(int byte, FILE * fp)
            /* Fall through here */
          default:
          keyword:
-           keyword(bcTok__base[byte - 0x7F], fp);
+           keyword(tok_base[byte - 0x7F], fp);
            break;
        }
       } else {
@@ -307,31 +313,34 @@ static int decode(int byte, FILE * fp)
     /* --- Double-byte token states --- */
 
     case s_c6:
-      return (mbtok(byte, bcTok__c6, ITEMS(bcTok__c6), fp));
+      return (mbtok(byte, tok_c6, ITEMS(tok_c6), fp));
       break;
 
     case s_c7:
-      return (mbtok(byte, bcTok__c7, ITEMS(bcTok__c7), fp));
+      return (mbtok(byte, tok_c7, ITEMS(tok_c7), fp));
       break;
 
     case s_c8:
-      return (mbtok(byte, bcTok__c8, ITEMS(bcTok__c8), fp));
+      return (mbtok(byte, tok_c8, ITEMS(tok_c8), fp));
       break;
 
     /* --- Encoded line number states --- */
 
     case s_linex:
-      lineno = ((((byte << 2) | (byte << 12)) & 0xc0c0u) ^ 0x4040u);
+      byte ^= 0x54;
+      lineno = (((byte & 0x30) << 2) |
+               ((byte & 0x0c) << 12) |
+               ((byte & 0x03)  << 16));
       state++;
       break;
 
     case s_liney:
-      lineno |= byte & 0x3fu;
+      lineno |= byte & 0x3f;
       state++;
       break;
 
     case s_linez:
-      lineno |= (byte << 8) & 0x3fu;
+      lineno |= (byte & 0x3f) << 8;
       fprintf(fp, "%u", lineno);
       state = s_normal;
       break;
@@ -383,7 +392,7 @@ static int line(FILE *in, FILE *out)
       goto eof;
     len -= 4;
 
-    state = s_normal;
+    state = s_keyword;
     while (len) {
       byte = getc(in);
       D( fprintf(stderr, "state == %i, byte == %i\n",
@@ -440,7 +449,7 @@ static void file(FILE *in, FILE *out)
     die("Found data after end of program");
 }
 
-/* --- @sigPipe@ --- *
+/* --- @sig_pipe@ --- *
  *
  * Arguments:   @int s@ = signal number
  *
@@ -449,7 +458,7 @@ static void file(FILE *in, FILE *out)
  * Use:         Handles SIGPIPE signals, and gracefully kills the program.
  */
 
-static void sigPipe(int s)
+static void sig_pipe(int s)
 {
   (void) s;
   exit(0);                             /* Gracefully, oh yes */
@@ -586,7 +595,7 @@ int main(int argc, char *argv[])
        out = stdout;
       else {
        flags |= f_tty;
-       signal(SIGPIPE, sigPipe);
+       signal(SIGPIPE, sig_pipe);
       }
     } else
       out = stdout;