home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / top / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-17  |  2.8 KB  |  154 lines

  1. /* Copyright (c) 1988 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.  
  12. /*
  13.  * Changes by Jeff Lydiatt to handle Amiga-specific stuff marked Jal.
  14.  */
  15.  
  16. #include "top.h"
  17. /*
  18.  * Low-level i/o routines.
  19.  */
  20.  
  21. /*
  22.  * mode tells what kind of stuff we're reading at the moment.
  23.  */
  24. static    int    mode;
  25.  
  26. #define    BSS    0
  27. #define    DATA    1
  28. #define    TEXT    2
  29.  
  30. static    char    *mnames[] = { /*Jal*/
  31.     "BSS",
  32.     "DATA",
  33.     "CODE"
  34. };
  35.  
  36. /*
  37.  * Tokens from the current line...
  38.  */
  39. char    *t_line;        /* the entire line */
  40. char    *t_lab;            /* label, if any */
  41. char    *t_op;            /* opcode */
  42. char    *t_arg;            /* arguments */
  43.  
  44. #define    ISWHITE(c)    ((c) == '\t' || (c) == ' ' || (c) == '\n')
  45.  
  46. #define    LSIZE    2048    /* max. size of an input line */
  47.  
  48. /*
  49.  * readline() - read the next line from the file
  50.  *
  51.  * readline passes data and bss through to the output, only returning
  52.  * when a line of text has been read. Returns FALSE on end of file.
  53.  */
  54. bool
  55. readline()
  56. {
  57.     char    *fgets();
  58.     static    void    tokenize();
  59.     static    char    buf[LSIZE];
  60.  
  61.     /*
  62.      * Keep looping until we get a line of text
  63.      */
  64.     for (;;) {
  65.         if (fgets(buf, LSIZE, ifp) == NULL)
  66.             return FALSE;
  67.     
  68.         t_line = buf;
  69.     
  70.         /*
  71.          * Find out if the mode is changing.
  72.          */
  73.         tokenize(buf);
  74.     
  75.         /* is it a pseudo-op? */
  76.         if (stricmp(t_op, mnames[BSS]) == 0){
  77.             mode = BSS;
  78.             newBSS(t_arg);
  79.             continue;
  80.         }
  81.         else if (stricmp(t_op, mnames[DATA]) == 0){
  82.             mode = DATA;
  83.             newDATA(t_arg);
  84.             continue;
  85.         }
  86.         else if (stricmp(t_op, mnames[TEXT]) == 0) {
  87.             mode = TEXT;
  88.             continue;
  89.         }
  90.         else if (stricmp(t_op, "END") == 0) /*Jal*/
  91.             continue; /* eat an end. */
  92.  
  93.         else if ( stricmp(t_op, "XDEF") == 0
  94.             ||stricmp(t_op, "XREF") == 0 ){
  95.             store(-1);
  96.             continue;
  97.         }
  98.  
  99.         if (mode == TEXT){ /*Jal - implement noopt option */
  100.             if ( noopt )
  101.                 fputs( buf, ofp );
  102.             else
  103.                 return TRUE;
  104.         }
  105.         else
  106.             store(mode);
  107.     }
  108. }
  109.  
  110. static void
  111. tokenize(s)
  112. register char    *s;
  113. {
  114.     static    char    label[LSIZE], opcode[LSIZE], args[LSIZE];
  115.     register int    i;
  116.  
  117.     /*
  118.      * Grab the label, if any
  119.      */
  120.     i = 0;
  121.     while (*s && !ISWHITE(*s) && *s != ':')
  122.         label[i++] = *s++;
  123.     label[i] = '\0';
  124.  
  125.     if (*s == ':')
  126.         s++;
  127.  
  128.     while (ISWHITE(*s))
  129.         s++;
  130.  
  131.     /*
  132.      * Grab the opcode
  133.      */
  134.     i = 0;
  135.     while (*s && !ISWHITE(*s))
  136.         opcode[i++] = *s++;
  137.     opcode[i] = '\0';
  138.  
  139.     while (ISWHITE(*s))
  140.         s++;
  141.  
  142.     /*
  143.      * Grab the arguments
  144.      */
  145.     i = 0;
  146.     while (*s && !ISWHITE(*s))
  147.         args[i++] = *s++;
  148.     args[i] = '\0';
  149.  
  150.     t_lab = label;
  151.     t_op = opcode;
  152.     t_arg = args;
  153. }
  154.