home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / top / io.c < prev    next >
C/C++ Source or Header  |  1991-02-22  |  3KB  |  142 lines

  1. /* Copyright (c) 1988,1991 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11. #include "top.h"
  12.  
  13. /*
  14.  * Low-level i/o routines.
  15.  */
  16.  
  17. /*
  18.  * mode tells what kind of stuff we're reading at the moment.
  19.  */
  20. static    int    mode;
  21.  
  22. #define    BSS    0
  23. #define    DATA    1
  24. #define    TEXT    2
  25.  
  26. static    char    *mnames[] = {
  27.     ".bss",
  28.     ".data",
  29.     ".text"
  30. };
  31.  
  32. /*
  33.  * Tokens from the current line...
  34.  */
  35. char    *t_line;        /* the entire line */
  36. char    *t_lab;            /* label, if any */
  37. char    *t_op;            /* opcode */
  38. char    *t_arg;            /* arguments */
  39.  
  40. #define    ISWHITE(c)    ((c) == '\t' || (c) == ' ' || (c) == '\n')
  41.  
  42. #define    LSIZE    2048    /* max. size of an input line */
  43.  
  44. /*
  45.  * readline() - read the next line from the file
  46.  *
  47.  * readline passes data and bss through to the output, only returning
  48.  * when a line of text has been read. Returns FALSE on end of file.
  49.  */
  50. bool
  51. readline()
  52. {
  53.     char    *fgets();
  54.     static    void    tokenize();
  55.     static    char    buf[LSIZE];
  56.  
  57.     /*
  58.      * Keep looping until we get a line of text
  59.      */
  60.     for (;;) {
  61.         if (fgets(buf, LSIZE, ifp) == NULL)
  62.             return FALSE;
  63.     
  64.         t_line = buf;
  65.     
  66.         /*
  67.          * Find out if the mode is changing.
  68.          */
  69.         tokenize(buf);
  70.     
  71.         /*
  72.          * If we see a "var" hint from the compiler, call addvar()
  73.          * to remember it for later use.
  74.          */
  75.         if (t_lab[0] == ';') {
  76.             if (strcmp(t_lab, ";var") == 0)
  77.                 addvar(atoi(t_op), atoi(t_arg));
  78.             continue;
  79.         }
  80.  
  81.         if (t_op[0] == '.') {    /* is it a pseudo-op? */
  82.             if (strcmp(t_op, mnames[BSS]) == 0)
  83.                 mode = BSS;
  84.             else if (strcmp(t_op, mnames[DATA]) == 0)
  85.                 mode = DATA;
  86.             else if (strcmp(t_op, mnames[TEXT]) == 0) {
  87.                 mode = TEXT;
  88.                 continue;
  89.             }
  90.         }
  91.         if (mode == TEXT)
  92.             return TRUE;
  93.         else
  94.             fputs(buf, ofp);
  95.     }
  96. }
  97.  
  98. static void
  99. tokenize(s)
  100. register char    *s;
  101. {
  102.     static    char    label[LSIZE], opcode[LSIZE], args[LSIZE];
  103.     register int    i;
  104.  
  105.     /*
  106.      * Grab the label, if any
  107.      */
  108.     i = 0;
  109.     while (*s && !ISWHITE(*s) && *s != ':')
  110.         label[i++] = *s++;
  111.     label[i] = '\0';
  112.  
  113.     if (*s == ':')
  114.         s++;
  115.  
  116.     while (ISWHITE(*s))
  117.         s++;
  118.  
  119.     /*
  120.      * Grab the opcode
  121.      */
  122.     i = 0;
  123.     while (*s && !ISWHITE(*s))
  124.         opcode[i++] = *s++;
  125.     opcode[i] = '\0';
  126.  
  127.     while (ISWHITE(*s))
  128.         s++;
  129.  
  130.     /*
  131.      * Grab the arguments
  132.      */
  133.     i = 0;
  134.     while (*s && !ISWHITE(*s))
  135.         args[i++] = *s++;
  136.     args[i] = '\0';
  137.  
  138.     t_lab = label;
  139.     t_op = opcode;
  140.     t_arg = args;
  141. }
  142.