home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / as / source / c / input < prev    next >
Encoding:
Text File  |  1994-09-27  |  2.6 KB  |  173 lines

  1.  
  2. /*
  3.  *  input.c
  4.  * Copyright © 1992 Niklas Röjemo
  5.  */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include "error.h"
  11. #include "global.h"
  12. #include "input.h"
  13. #include "riscos.h"
  14. #include "filestack.h"
  15. #include "uname.h"
  16.  
  17. #define MAX_LINE 4096
  18.  
  19. extern int gcc_backend;
  20. extern int dde;
  21.  
  22. FILE *asmfile;
  23. static char buff[MAX_LINE];
  24. static char *pos,*mark;
  25.  
  26. int inputLineNo;
  27.  
  28. char inputLook(void)
  29. {
  30.   return *pos;
  31. }
  32.  
  33. int inputComment(void)
  34. {
  35.   int c;
  36.   return 0==(c=*pos) || c == ';' || (gcc_backend && c == '@');
  37. }
  38.  
  39. char inputLookN(int n)  /* Unsafe */
  40. {
  41.   return pos[n];
  42. }
  43.  
  44. char inputGet(void)
  45. {
  46.   if(*pos) return *pos++;
  47.   else     return *pos;
  48. }
  49. void inputUnGet(char c)
  50. {
  51.   if(pos>buff && pos[-1] == c)
  52.     pos--;
  53.   else
  54.     if(*pos || c) error(ErrorSerious,FALSE,"Internal inputUnGet: Illegal character.");
  55. }
  56.  
  57. void inputSkip(void)
  58. {
  59.   if(*pos) pos++;
  60. }
  61.  
  62. void inputSkipN(int n)
  63. {
  64.   while(*pos && n--) pos++;
  65.   if(n>-1)
  66.     error(ErrorSerious,FALSE,"Internal inputSkipN: Trying to skip more characters than are availible.");
  67. }
  68.  
  69. char *inputRest(void)
  70. {
  71.   char *t = pos;
  72.   while(*pos) pos++;
  73.   return t;
  74. }
  75. char *inputLine(void)
  76. {
  77.   return buff;
  78. }
  79.  
  80. char inputSkipLook(void)
  81. {
  82.   if(*pos) return *++pos;
  83.   else     return *pos;
  84. }
  85.  
  86. void skipblanks(void)
  87. {
  88.   char c;
  89.   while((c=*pos)!=0 && isspace(c))
  90.     pos++;
  91. }
  92.  
  93. void skiprest(void)
  94. {
  95.   buff[0] = 0;
  96.   pos = mark = &buff[0];
  97. }
  98.  
  99. void inputMark(void)
  100. {
  101.   mark = pos;
  102. }
  103.  
  104. void inputRollback(void)
  105. {
  106.   pos = mark;
  107. }
  108.  
  109. void inputInit(char *infile)
  110. {
  111.   if(infile && strcmp(infile,"-")) {
  112.     if ((asmfile = fopen (uname (infile, dde), "r")) == NULL) {
  113.       errorLine(0,ErrorAbort,FALSE,"As can't read %s.",infile);
  114.     }
  115.   } else
  116.     asmfile = stdin;
  117.  
  118.   inputLineNo = 0;
  119.   skiprest();
  120. }
  121.  
  122. void inputFinish(void)
  123. {
  124.   if(asmfile != stdin)
  125.     fclose(asmfile);
  126. }
  127.  
  128. BOOL inputNextLine(void)
  129. {
  130.   int l;
  131.   inputLineNo++;
  132.   while (fgets(buff,MAX_LINE,asmfile)==NULL) {
  133.     if ((asmfile=pop_file())==NULL) {
  134.       inputLineNo=-1;
  135.       return NULL;
  136.     }
  137.   }
  138.   l = strlen(buff);
  139.   if(l) {
  140.     if(buff[l-1] != '\n') {
  141.       if((l=getc(asmfile))!= EOF && l!='\n') {
  142.         error(ErrorSerious,TRUE,"Line truncated");
  143.         while((l=getc(asmfile))!= EOF && l!='\n')
  144.            ;
  145.       }
  146.     } else
  147.       buff[l-1] = 0;
  148.   }
  149.   pos = &buff[0];
  150.   return TRUE;
  151. }
  152.  
  153. char *inputSymbol(int *ilen, char del)
  154. {
  155.   char *res = pos;
  156.   char c;
  157.   if(del) {
  158.     while((c=*pos)!=0 && c != del) {
  159.       pos++;
  160.       if(c == '\\' && *pos)
  161.         pos++;
  162.     }
  163.   } else {
  164.     while((c=*pos)!=0 && (isalnum(c) || c == '_')) {
  165.       pos++;
  166.       if(c == '\\' && *pos)
  167.         pos++;
  168.     }
  169.   }
  170.   *ilen = pos-res;
  171.   return res;
  172. }
  173.