home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol126 / ndio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-04-29  |  4.1 KB  |  209 lines

  1. /* June 18, 1983 */
  2. #include "macro.h"    /*June 18, 1983*/
  3.  
  4.  
  5.  
  6. #define CON_INPUT 1            /* BDOS call to read console       */
  7. #define CON_OUTPUT 2            /* BDOS call to write to console   */
  8. #define CON_STATUS 11            /* BDOS call to interrogate status */
  9.  
  10. #define CONTROL_C 3            /* Quit character           */
  11. #define INPIPE 2            /* bit setting to indicate directed
  12.                        input from a temp. pipe fil     */
  13. #define VERBOSE 2            /* bit setting to indicate output is to
  14.                        go to console AND directed output */
  15. #define DIRECTED_OUT 1
  16. #define CONS_TOO 2
  17. #define CPM_LIST_OUT 4
  18. #define PRINTER_OUT 8
  19. #define ROBOTYPE_OUT 16
  20. #define CONSOLE_ONLY 0
  21.  
  22. /* 
  23.     The "dioinit" function must be called at the beginning of the
  24.     "main" function:
  25. */
  26.  
  27. dioflush()
  28. {
  29.     if (_diflag)
  30.     {
  31.         fclose(_dibuf);
  32.         if (_diflag & INPIPE) unlink("tempin.$$$");
  33.     }
  34.  
  35.     if (_doflag)
  36.     {
  37.         putc(CPMEOF,_dobuf);
  38.         fflush(_dobuf);
  39.         fclose(_dobuf);
  40.         if (OK != strcmp ("TEMPOUT.$$$", sav_out_file))
  41.               { unlink (sav_out_file);
  42.             rename ("TEMPOUT.$$$", sav_out_file);
  43.               }
  44.         rename("tempout.$$$","tempin.$$$");
  45.         if (_pipef) 
  46.         {
  47.             *_savei = "<TEMPIN.$$$";
  48.             *_nullpos = NULL;
  49.             execv(_pipedest,_savei);
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. /*
  56.     This version of "getchar" replaces the regular version when using
  57.     directed I/O:
  58. */
  59.  
  60. getchar()
  61. {
  62.     char c;
  63.  
  64.     if (_diflag) {
  65.         if ((c = getc(_dibuf)) == '\r') c = getc(_dibuf);
  66.     } else
  67.         if ((c = bdos(CON_INPUT)) == CONTROL_C) exit();
  68.  
  69.     if (c == CPMEOF) return EOF;         /* Control-Z is EOF key     */
  70.     if (c == '\r') 
  71.     {
  72.         c = '\n';
  73.         if (!_diflag) bdos(2,'\n');  /* echo LF after CR to console */
  74.     }
  75.     return c;
  76. }
  77.  
  78.  
  79. /*
  80.     This version of "putchar" replaces the regular version when using
  81.     directed I/O:
  82. */
  83.  
  84. putchar(c)
  85. char c;
  86. {
  87.     if (_doflag & DIRECTED_OUT)
  88.     {
  89.         if (c == '\n') putc('\r',_dobuf);
  90.         if(putc(c,_dobuf) == ERROR)
  91.         {
  92.             fprintf(STDERR,"File output error; disk full?\n");
  93.             exit();
  94.         }
  95.     }
  96.  
  97.     if (_doflag==0 || _doflag & CONS_TOO)
  98.     {
  99.     if (bdos(CON_STATUS) && bdos(CON_INPUT) == CONTROL_C) exit();
  100.     if (c == '\n') bdos(CON_OUTPUT,'\r');
  101.     bdos(CON_OUTPUT,c);
  102.     }
  103.  
  104.     if (_doflag & CPM_LIST_OUT)
  105.     {
  106.         bdos(5,c);
  107.         if (c=='\n') bdos(5,'\r');
  108.     }
  109.     if (_doflag & PRINTER_OUT)
  110.     {
  111.         bdos(5,c);
  112.     }
  113.     if (_doflag & ROBOTYPE_OUT)
  114.     {
  115.         fprintf(STDERR,"sending ROBO <%c>    ",c);
  116.     }
  117. }
  118. /****************************************/
  119. #define argc *argcp
  120. dioinit(argcp,argv)
  121. int *argcp;
  122. char **argv;
  123. {
  124.     int i,j, argcount;
  125.     int n;    /* this keeps track of location in argument */
  126.  
  127.     _diflag = _doflag = _pipef = FALSE;  /* No directed I/O by default   */
  128.     _nullpos = &argv[argc];
  129.     argcount = 1;
  130.  
  131.     for (i = 1; i < argc; i++)    /* Scan the command line for > and < */
  132.     {
  133.         if (_pipef) break;
  134.         n=0;    /* start with first character */
  135. getmore:    switch(argv[i][n++]) {
  136.  
  137.            case '<':        /* Check for directed input: */
  138.             if (!argv[i][n]) goto barf;
  139.             if (fopen(&argv[i][n], _dibuf) == ERROR)
  140.             {
  141.                 fprintf(STDERR,"Can't open %s\n",&argv[i][n]);
  142.                 exit();
  143.             }
  144.             _diflag = TRUE;
  145.             if (strcmp(argv[i],"<TEMPIN.$$$") == 0)
  146.                  _diflag |= INPIPE;
  147.             goto movargv;
  148.  
  149.            case '|':    /* Check for pipe: */
  150.             _pipef++;
  151.             _pipedest = &argv[i][n]; /* save prog name for execl */
  152.             if (argv[i][n]) 
  153.             {
  154.                 argv[i] = ".TEMPOUT.$$$";  /* temp. output */
  155.                 _savei = &argv[i];
  156.             }
  157.             goto foo;
  158.  
  159.            case '+': 
  160.             _doflag |= VERBOSE;
  161.             goto getmore;
  162.            case ')':
  163.             _doflag |= CPM_LIST_OUT;
  164.             goto getmore;
  165.            case '}':
  166.             _doflag |= PRINTER_OUT;
  167.             goto getmore;
  168.            case ']':
  169.             _doflag |= ROBOTYPE_OUT;
  170.             goto getmore;
  171.             
  172.          foo:   case '>':    /* Check for directed output    */
  173.         
  174.             if (!argv[i][n]) 
  175.             {
  176.             barf:   fprintf(STDERR,"Bad redirection/pipe specifier");
  177.                 exit();
  178.             }
  179.             strcpy (sav_out_file, &argv[i][n] );
  180.             if (fcreat("TEMPOUT.$$$", _dobuf) == ERROR)
  181.             {
  182.                 fprintf(STDERR,"\nCan't create <%s>\n",
  183.                                                      "TEMPOUT.$$$");
  184.                    exit();
  185.             }
  186.             _doflag++;
  187.  
  188.          movargv:    if (!_pipef) {
  189.                 for (j = i; j < argc; j++) argv[j] = argv[j+1];
  190.                 (argc)--;
  191.                 i--;
  192.                 _nullpos--;
  193.              } else {
  194.                 argc = argcount;
  195.                 argv[argc] = 0;
  196.              }
  197.             break;
  198.  
  199.             default:    /* handle normal arguments: */
  200.                 if (n!=1) goto movargv;
  201.             argcount++;
  202.         }
  203.     }
  204. }
  205.  
  206.  
  207. #undef argc
  208.  
  209.