home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / assembler / as / src / c / input < prev    next >
Encoding:
Text File  |  1993-02-08  |  2.3 KB  |  157 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.  
  15. #define MAX_LINE 1024
  16.  
  17. FILE *asmfile;
  18. static char buff[MAX_LINE];
  19. static char *pos,*mark;
  20.  
  21. int inputLineNo;
  22.  
  23. char inputLook(void)
  24. {
  25.   return *pos;
  26. }
  27.  
  28. char inputLookN(int n)  /* Unsafe */
  29. {
  30.   return pos[n];
  31. }
  32.  
  33. char inputGet(void)
  34. {
  35.   if(*pos) return *pos++;
  36.   else     return *pos;
  37. }
  38. void inputUnGet(char c)
  39. {
  40.   if(pos>buff && pos[-1] == c)
  41.     pos--;
  42.   else
  43.     if(*pos || c) error(ErrorSerious,FALSE,"Internal inputUnGet: Illegal character.");
  44. }
  45.  
  46. void inputSkip(void)
  47. {
  48.   if(*pos) pos++;
  49. }
  50.  
  51. void inputSkipN(int n)
  52. {
  53.   while(*pos && n--) pos++;
  54.   if(n>-1)
  55.     error(ErrorSerious,FALSE,"Internal inputSkipN: Trying to skip more characters than are availible.");
  56. }
  57.  
  58. char *inputRest(void)
  59. {
  60.   char *t = pos;
  61.   while(*pos) pos++;
  62.   return t;
  63. }
  64. char *inputLine(void)
  65. {
  66.   return buff;
  67. }
  68.  
  69. char inputSkipLook(void)
  70. {
  71.   if(*pos) return *++pos;
  72.   else     return *pos;
  73. }
  74.  
  75. void skipblanks(void)
  76. {
  77.   char c;
  78.   while((c=*pos)!=0 && isspace(c))
  79.     pos++;
  80. }
  81.  
  82. void skiprest(void)
  83. {
  84.   buff[0] = 0;
  85.   pos = mark = &buff[0];
  86. }
  87.  
  88. void inputMark(void)
  89. {
  90.   mark = pos;
  91. }
  92.  
  93. void inputRollback(void)
  94. {
  95.   pos = mark;
  96. }
  97.  
  98. void inputInit(char *infile)
  99. {
  100.   if(infile && strcmp(infile,"-")) {
  101.     if((asmfile = fopen(toriscos(infile,"s",'s'),"r")) == NULL) {
  102.       errorLine(0,ErrorAbort,FALSE,"As can't read %s.",infile);
  103.     }
  104.   } else
  105.     asmfile = stdin;
  106.  
  107.   inputLineNo = 0;
  108.   skiprest();
  109. }
  110.  
  111. void inputFinish(void)
  112. {
  113.   if(asmfile != stdin)
  114.     fclose(asmfile);
  115. }
  116.  
  117. BOOL inputNextLine(void)
  118. {
  119.   int l;
  120.   inputLineNo++;
  121.   if(fgets(buff,MAX_LINE,asmfile)==NULL) {
  122.     inputLineNo = -1;
  123.     return FALSE;
  124.   }
  125.   l = strlen(buff);
  126.   if(l && buff[l-1] != '\n') {
  127.     if((l=getc(asmfile))!= EOF && l!='\n') {
  128.       error(ErrorSerious,TRUE,"Line truncated");
  129.       while((l=getc(asmfile))!= EOF && l!='\n')
  130.          ;
  131.     }
  132.   }
  133.   pos = &buff[0];
  134.   return TRUE;
  135. }
  136.  
  137. char *inputSymbol(int *ilen, char del)
  138. {
  139.   char *res = pos;
  140.   char c;
  141.   if(del) {
  142.     while((c=*pos)!=0 && c != del) {
  143.       pos++;
  144.       if(c == '\\' && *pos)
  145.         pos++;
  146.     }
  147.   } else {
  148.     while((c=*pos)!=0 && (isalnum(c) || c == '_')) {
  149.       pos++;
  150.       if(c == '\\' && *pos)
  151.         pos++;
  152.     }
  153.   }
  154.   *ilen = pos-res;
  155.   return res;
  156. }
  157.