home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / language / bcpl4ami.lzh / bcpl / src / sub.c < prev   
Encoding:
C/C++ Source or Header  |  1991-02-03  |  3.2 KB  |  257 lines

  1. #include    "icint.h"
  2.  
  3. #ifdef    unix
  4. #define    MAXFILES    20
  5. static    FILE    *filetable[MAXFILES]    = { 0, stdin, stdout, stderr };
  6. /* slot 0 is wasted so we can use 0 as an invalid value */
  7. static    int    curinfile    = S_IN;
  8. static    int    curoutfile    = S_OUT;
  9. #endif
  10.  
  11. static FP    theinfile;
  12. static FP    theoutfile;
  13. static FP    theerrfile;
  14.  
  15. initio()
  16. {
  17. #ifdef    unix
  18.     theinfile = stdin;
  19.     theoutfile = stdout;
  20.     theerrfile = stderr;
  21. #endif
  22. #ifdef    cware
  23.     theinfile = stdin;
  24.     theoutfile = stdout;
  25.     theerrfile = stderr;
  26. #endif
  27. #ifdef    c80
  28.     theinfile = S_IN = fopen("con:", "r");
  29.     theoutfile = S_OUT = theerrfile = S_ERR = fopen("con:", "w");
  30. #endif
  31. }
  32.  
  33. finio()
  34. {
  35. #ifdef    cware
  36.     int    i;
  37.  
  38.     for (i = 3; i < 20; ++i)
  39.         fclose(i);
  40. #endif
  41. #ifdef    c80
  42.     int    i;
  43.  
  44.     for (i = 1; i < 7; ++i)
  45.         fclose(i);
  46. #endif
  47. }
  48.  
  49. #ifdef    unix
  50. int getfileindex(f)
  51.     FILE    *f;
  52. {
  53.     register int    i;
  54.  
  55.     for (i = 4; i < MAXFILES; ++i)
  56.         if (filetable[i] == NULL)
  57.             break;
  58.     if (i < MAXFILES)
  59.     {
  60.         filetable[i] = f;
  61.         return (i);
  62.     }
  63.     return (0);
  64. }
  65.  
  66. clearfileindex(i)
  67.     int    i;
  68. {
  69.     if (4 <= i && i < MAXFILES)
  70.         filetable[i] = NULL;
  71. }
  72.  
  73. int setsysin(name)
  74.     char    *name;
  75. {
  76.     if (freopen(name, "r", stdin) == NULL)
  77.         return (0);
  78.     return (1);
  79. }
  80. #endif
  81.  
  82. #ifdef    cware
  83. int setsysin(name)
  84.     char    *name;
  85. {
  86.     if ((S_IN = fopen(name, "r")) == NULL)
  87.         return (0);
  88.     return (1);
  89. }
  90. #endif
  91.  
  92. #ifdef    c80
  93. int fread(buf, sz, n, f)
  94.     char    *buf;
  95.     int    sz, n;
  96.     int    f;
  97. {
  98.     int    nb;
  99.  
  100.     nb = sz * n;
  101.     while (nb-- > 0)
  102.         *buf++ = getc(f);
  103.     return (n);
  104. }
  105.  
  106. int fwrite(buf, sz, n, f)
  107.     char    *buf;
  108.     int    sz, n;
  109.     int    f;
  110. {
  111.     int    nb;
  112.  
  113.     nb = sz * n;
  114.     while (nb-- > 0)
  115.         putc(*buf++, f);    /* putc function, not macro, in c80 */
  116.     return (n);
  117. }
  118.  
  119. int setsysin(name)
  120.     char    *name;
  121. {
  122.     if ((S_IN = fopen(name, "r")) == NULL)
  123.         return (0);
  124.     return (1);
  125. }
  126. #endif
  127.  
  128. int findinput(i)
  129.     int    i;
  130. {
  131.     register FP    f;
  132.     char        fn[MAXFN];
  133.  
  134.     strbc(i, fn);
  135.     if (strcmp(fn, "SYSIN") == 0)
  136.         return (S_IN);
  137.     if ((f = fopen(fn, "r")) == NULL)
  138.         return (0);
  139. #ifdef    unix
  140.     return (getfileindex(f));
  141. #endif
  142. #ifdef    cware
  143.     return (f);
  144. #endif
  145. #ifdef    c80
  146.     return (f);
  147. #endif
  148. }
  149.  
  150. int findoutput(i)
  151.     int    i;
  152. {
  153.     register FP    f;
  154.     char        fn[MAXFN];
  155.  
  156.     strbc(i, fn);
  157.     if (strcmp(fn, "SYSPRINT") == 0)
  158.         return (S_OUT);
  159.     if (strcmp(fn, "SYSERROR") == 0)
  160.         return (S_ERR);
  161.     if ((f = fopen(fn, "w")) == NULL)
  162.         return (0);
  163. #ifdef    unix
  164.     return (getfileindex(f));
  165. #endif
  166. #ifdef    cware
  167.     return (f);
  168. #endif
  169. #ifdef    c80
  170.     return (f);
  171. #endif
  172. }
  173.  
  174. slctinput(f)
  175.     int        f;
  176. {
  177. #ifdef    unix
  178.     if (1 <= f && f < MAXFILES)
  179.     {
  180.         theinfile = filetable[f];
  181.         curinfile = f;
  182.     }
  183. #endif
  184. #ifdef    cware
  185.     theinfile = f;
  186. #endif
  187. #ifdef    c80
  188.     theinfile = f;
  189. #endif
  190. }
  191.  
  192. slctoutput(f)
  193.     int        f;
  194. {
  195. #ifdef    unix
  196.     if (1 <= f && f < MAXFILES)
  197.     {
  198.         theoutfile = filetable[f];
  199.         curinfile = f;
  200.     }
  201. #endif
  202. #ifdef    cware
  203.     theoutfile = f;
  204. #endif
  205. #ifdef    c80
  206.     theoutfile = f;
  207. #endif
  208. }
  209.  
  210. int rdch()
  211. {
  212. #ifdef    cware
  213.     int    c;
  214.  
  215.     do
  216.         c = getc(theinfile);
  217.     while (c == '\r');
  218.     return (c);
  219. #else
  220.     return (getc(theinfile));
  221. #endif
  222. }
  223.  
  224. wrch(c)
  225.     int        c;
  226. {
  227. #ifdef    cware
  228.     if (c == '\n')
  229.         putc('\r', theoutfile);
  230. #endif
  231.     putc(c, theoutfile);
  232. }
  233.  
  234. endread()
  235. {
  236.     if (theinfile != NULL)
  237.         fclose(theinfile);
  238.     theinfile = NULL;
  239. #ifdef    unix
  240.     clearfileindex(curinfile);
  241. #endif
  242. }
  243.  
  244. endwrite()
  245. {
  246.     if (theoutfile != NULL)
  247.         fclose(theoutfile);
  248.     theoutfile = NULL;
  249. #ifdef    unix
  250.     clearfileindex(curoutfile);
  251. #endif
  252. }
  253.  
  254. mapstore()
  255. {
  256. }
  257.