home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / northc_384.lzh / NorthC / Example2.LZH / top / io.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  4KB  |  199 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. #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 = -1;
  21.  
  22. #define    BSS    0
  23. #define    DATA    1
  24. #define    TEXT    2
  25.  
  26. static    char    *mnames[] = {
  27. #ifdef NORTHC
  28.     "bss",
  29.     "data",
  30.     "code"
  31. #else
  32.     ".bss",
  33.     ".data",
  34.     ".text"
  35. #endif
  36. };
  37.  
  38. /*
  39.  * Tokens from the current line...
  40.  */
  41. char    *t_line;        /* the entire line */
  42. char    *t_lab;            /* label, if any */
  43. char    *t_op;            /* opcode */
  44. char    *t_arg;            /* arguments */
  45.  
  46. #define    ISWHITE(c)    ((c) == '\t' || (c) == ' ' || (c) == '\n')
  47.  
  48. #define    LSIZE    2048    /* max. size of an input line */
  49.  
  50. static    void    tokenize();
  51.  
  52. /*
  53.  * readline() - read the next line from the file
  54.  *
  55.  * readline passes data and bss through to the output, only returning
  56.  * when a line of text has been read. Returns FALSE on end of file.
  57.  */
  58.  
  59. #ifdef NORTHC
  60. static    char    buf[LSIZE];
  61. extern  int out_code;
  62. #endif
  63.  
  64. bool
  65. readline()
  66. {
  67.     char    *fgets();
  68. #ifndef NORTHC
  69.     static    char    buf[LSIZE];
  70. #endif
  71.  
  72.     /*
  73.      * Keep looping until we get a line of text
  74.      */
  75.     for (;;) {
  76.         if (fgets(buf, LSIZE, ifp) == NULL)
  77.             return FALSE;
  78.     
  79.         t_line = buf;
  80.     
  81.         /*
  82.          * Find out if the mode is changing.
  83.          */
  84.         tokenize(buf);
  85.  
  86. #ifdef NORTHC
  87.         /* is it a pseudo-op? */
  88.                 if(stricmp(t_op,"SECTION")==0)
  89.                    {/* New section, find out what type */
  90.                     char typeStr[5];
  91.                     char *parser = t_arg;
  92.                     int  i;
  93.  
  94.                     /* The section type is the second argument */
  95.                     while(*parser!=',' && *parser!='\0')
  96.                         parser++;
  97.                     if(parser!='\0')
  98.                        {parser++;
  99.                         i = 0;
  100.                         while(isalpha(*parser) && i<4)
  101.                            {typeStr[i] = *parser;
  102.                             parser++;
  103.                             i++;
  104.                             }
  105.                         typeStr[i] = '\0';
  106.                      if(stricmp(typeStr,mnames[BSS])==0)
  107.                            {mode = BSS;
  108. /*                            out_code = 0;
  109.                 newBSS(t_arg); */
  110.                 continue;
  111.                             }
  112.                   else if(stricmp(typeStr,mnames[DATA])==0)
  113.                            {mode = DATA;
  114. /*                            out_code = 0;
  115.                 newDATA(t_arg); */
  116.                 continue;
  117.                             }
  118.                   else if(stricmp(typeStr,mnames[TEXT])==0)
  119.                            {mode = TEXT;
  120.                             continue;
  121.                             }
  122.                         }
  123.                     }
  124.           else if(stricmp(t_op, "END" ) == 0)
  125.             continue;
  126.           else if(stricmp(t_op, "XDEF" ) == 0)
  127.            {fputs(buf, ofp);
  128.                     continue;
  129.                     }
  130. #else
  131.         if (t_op[0] == '.') {        /* is it a pseudo-op? */
  132.             if (strcmp(t_op, mnames[BSS]) == 0)
  133.                 mode = BSS;
  134.             else if (strcmp(t_op, mnames[DATA]) == 0)
  135.                 mode = DATA;
  136.             else if (strcmp(t_op, mnames[TEXT]) == 0) {
  137.                 mode = TEXT;
  138.                 continue;
  139.             }
  140.         }
  141. #endif
  142.         if (mode == TEXT)
  143.             return TRUE;
  144. /*        fputs(buf, ofp); */
  145.         store(mode);
  146.     }
  147. }
  148.  
  149. #ifdef NORTHC
  150. static    char    label[LSIZE], opcode[LSIZE], args[LSIZE];
  151. #endif
  152.  
  153. static void
  154. tokenize(s)
  155. register char    *s;
  156. {
  157. #ifndef NORTHC
  158.     static    char    label[LSIZE], opcode[LSIZE], args[LSIZE];
  159. #endif
  160.     register int    i;
  161.  
  162.     /*
  163.      * Grab the label, if any
  164.      */
  165.     i = 0;
  166.     while (*s && !ISWHITE(*s) && *s != ':')
  167.         label[i++] = *s++;
  168.     label[i] = '\0';
  169.  
  170.     if (*s == ':')
  171.         s++;
  172.  
  173.     while (ISWHITE(*s))
  174.         s++;
  175.  
  176.     /*
  177.      * Grab the opcode
  178.      */
  179.     i = 0;
  180.     while (*s && !ISWHITE(*s))
  181.         opcode[i++] = *s++;
  182.     opcode[i] = '\0';
  183.  
  184.     while (ISWHITE(*s))
  185.         s++;
  186.  
  187.     /*
  188.      * Grab the arguments
  189.      */
  190.     i = 0;
  191.     while (*s && !ISWHITE(*s))
  192.         args[i++] = *s++;
  193.     args[i] = '\0';
  194.  
  195.     t_lab = label;
  196.     t_op = opcode;
  197.     t_arg = args;
  198. }
  199.