home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-1 / DIO.C < prev    next >
Text File  |  2000-06-30  |  8KB  |  303 lines

  1. /*
  2.     Directed I/O package for BDS C v1.45   LZ -- 12/81
  3.  
  4.     The following functions make up the directed I/O library:
  5.  
  6.     1. dioinit(&argc,argv)        Make this the first thing you do in
  7.                     your "main" function, to process
  8.                     redirection commands on the CP/M
  9.                     command line.
  10.  
  11.     2. getchar()            Gets a character from the keyboard,
  12.                     or from a directed input file if one
  13.                     was specified on the command line.
  14.  
  15.     3. putchar(c)            Puts a character out to the console,
  16.                     or to a directed output file if one
  17.                     was specified on the command line.
  18.  
  19.     4. dioflush()            Flushes directed output file, if open,
  20.                     and closes all directed I/O files (if
  21.                     any.) This must be called before your
  22.                     program exits or returns to CP/M.
  23.  
  24.     To activate redirection: Four special arguments may be given
  25.     on the command line to the generated COM file...
  26.  
  27.         >foo    causes "putchar" to place characters into the file
  28.             named "foo" instead of to the console.
  29.  
  30.         +foo    like >foo except that the characters are ALSO sent
  31.             to the console.
  32.  
  33.         <foo    causes "getchar" to return characters from the file
  34.             named "foo" instead of from the keyboard.
  35.  
  36.      command |prog    causes the standard output of the command specified in
  37.             "command" to be fed into the standard input of another
  38.             program, "prog". (BOTH "command" and "prog" must be
  39.             compiled with DIO)
  40.  
  41.     (Note that there must never be any spaces between >,+,< or | and the
  42.      corresponding filename.)
  43.  
  44.     When no "<" or "|" operator is used, standard input comes from the
  45.     console and all standard line editing characters are recognized (a 
  46.     new feature of v1.45). To indicate end-of-file, you must type
  47.         ^Z <CR>
  48.     (control-Z followed by    a carriage-return.)
  49.  
  50.     When no ">" or "|" operator is used, standard output goes to the
  51.     console.
  52.      
  53.     A program allowing redirection must have the following form:
  54.  
  55.         #include "bdscio.h"        /* standard header file    */
  56.         #include "dio.h"        /* directed I/O header    */
  57.  
  58.         ...                /* other externals, if any */
  59.  
  60.         main(argc,argv)
  61.         char **argv;
  62.         {
  63.             ...            /* declarations        */
  64.             dioinit(&argc,argv)    /* initialize redirection */
  65.             ...            /* body of program    */
  66.             dioflush();
  67.         }
  68.             
  69.     NOTES:
  70.  
  71.     0. The console input may be raw (unbuffered, one char. at a time) or
  72.        buffered (entire line must be typed before chars are returned,
  73.        allowing standard editing features, and characters come back one
  74.        at a time AFTER the entire line is typed). The default is raw; to
  75.        have buffered console input, uncomment the "#define BUF_CONS" line
  76.        in DIO.H and recompile this file and all files in your program.
  77.  
  78.     1. Redirection and pipes work only for TEXT. This mechanism should
  79.        not be used for binary data.
  80.  
  81.     2. Use "-f sio" to link the program; this ensures that the proper
  82.        versions of "getchar" and "putchar" are used. Do not define
  83.        your own "getchar" or "putchar", or things will get confused.
  84.  
  85.     3. Multiple pipes may be chained on one command line. For example,
  86.        the following command feeds the output of program "foo" into the
  87.        input of program "bar", the output of "bar" into the input of
  88.        program "zot", and the output of "zot" into a file called "output":
  89.  
  90.         A>foo arg1 |bar |zot arg2 arg3 >output <cr>
  91.  
  92.        "arg1" is an actual argument to "foo", and "arg2" and "arg3" are
  93.        actual arguments to "zot". This illustrates how actual arguments
  94.        may be interspersed with redirection commands. The programs see
  95.        the actual arguments, but command line preprocessing handled by the
  96.        "dioinit" function cause the programs to never need to know about
  97.        the redirection commands. Note that all three programs ("foo", "bar"
  98.        and "zot") must have been compiled and linked to use the "DIO"
  99.        package.
  100. */
  101.  
  102. #include "bdscio.h"
  103. #include "dio.h"
  104.  
  105. #define CON_INPUT    1        /* BDOS call to read console       */
  106. #define CON_OUTPUT    2        /* BDOS call to write to console   */
  107. #define CON_STATUS    11        /* BDOS call to interrogate status */
  108.  
  109. #define CONTROL_C    3        /* Quit character           */
  110. #define STDERR        4        /* Standard Error descriptor (sorry,
  111.                        Unix fans, 2 was already used.) */
  112. #define INPIPE        2        /* bit setting to indicate directed
  113.                        input from a temp. pipe file    */
  114. #define VERBOSE        2        /* bit setting to indicate output is to
  115.                        go to console AND directed output */
  116.  
  117. /* 
  118.     The "dioinit" function must be called at the beginning of the
  119.     "main" function:
  120. */
  121.  
  122. #define argc *argcp
  123.  
  124. dioinit(argcp,argv)
  125. int *argcp;
  126. char **argv;
  127. {
  128.     int i,j, argcount;
  129.  
  130.     _diflag = _doflag = _pipef = FALSE;  /* No directed I/O by default   */
  131.     _nullpos = &argv[argc];
  132.  
  133. #ifdef BUF_CONS
  134.     _conbuf[0] = 0;            /* no characters in buffer yet    */
  135.     _conbufp = _conbuf;        /* point to null buffer     */
  136. #endif
  137.  
  138.     argcount = 1;
  139.  
  140.     for (i = 1; i < argc; i++)    /* Scan the command line for > and < */
  141.     {
  142.         if (_pipef) break;
  143.         switch(*argv[i]) {
  144.  
  145.            case '<':        /* Check for directed input:    */
  146.             if (!argv[i][1]) goto barf;
  147.             if (fopen(&argv[i][1], _dibuf) == ERROR)
  148.             {
  149.                 fprintf(STDERR,"Can't open %s\n",&argv[i][1]);
  150.                 exit();
  151.             }
  152.             _diflag = TRUE;
  153.             if (strcmp(argv[i],"<TEMPIN.$$$") == 0)
  154.                  _diflag |= INPIPE;
  155.             goto movargv;
  156.  
  157.            case '|':    /* Check for pipe: */
  158.             _pipef++;
  159.             _pipedest = &argv[i][1]; /* save prog name for execl */
  160.             if (argv[i][1]) 
  161.             {
  162.                 argv[i] = ".TEMPOUT.$$$";  /* temp. output */
  163.                 _savei = &argv[i];
  164.             }
  165.             goto foo;
  166.  
  167.            case '+': 
  168.             _doflag |= VERBOSE;
  169.             
  170.          foo:   case '>':    /* Check for directed output    */
  171.         
  172.             if (!argv[i][1]) 
  173.             {
  174.             barf:   fprintf(STDERR,"Bad redirection/pipe specifier");
  175.                 exit();
  176.             }
  177.             unlink(&argv[i][1]);
  178.             if (fcreat(&argv[i][1], _dobuf) == ERROR)
  179.             {
  180.                    fprintf(STDERR,"Can't create %s\n",&argv[i][1]);
  181.                    exit();
  182.             }
  183.             _doflag++;
  184.  
  185.          movargv:    if (!_pipef) {
  186.                 for (j = i; j < argc; j++) argv[j] = argv[j+1];
  187.                 (argc)--;
  188.                 i--;
  189.                 _nullpos--;
  190.              } else {
  191.                 argc = argcount;
  192.                 argv[argc] = 0;
  193.              }
  194.             break;
  195.  
  196.             default:    /* handle normal arguments: */
  197.             argcount++;
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203. #undef argc
  204.  
  205. /*
  206.     The "dioflush" function must be called before exiting the program:
  207. */
  208.  
  209. dioflush()
  210. {
  211.     if (_diflag)
  212.     {
  213.         fclose(_dibuf);
  214.         if (_diflag & INPIPE) unlink("tempin.$$$");
  215.     }
  216.  
  217.     if (_doflag)
  218.     {
  219.         putc(CPMEOF,_dobuf);
  220.         fflush(_dobuf);
  221.         fclose(_dobuf);
  222.         unlink("tempin.$$$");    /* in case previous pipe was aborted */
  223.         rename("tempout.$$$","tempin.$$$");
  224.         if (_pipef) 
  225.         {
  226.             *_savei = "<TEMPIN.$$$";
  227.             *_nullpos = NULL;
  228.             if (execv(_pipedest,_savei) == ERROR)
  229.             {
  230.                 fprintf(STDERR,"Broken pipe\n");
  231.                 exit();
  232.             }
  233.         }
  234.     }
  235. }
  236.  
  237.  
  238. /*
  239.     This version of "getchar" replaces the regular version when using
  240.     directed I/O. Note that the "BUF_CONS" defined symbol (in DIO.H)
  241.     controls whether the console input is to be raw or buffered (see
  242.     item 0. in NOTES above)
  243. */
  244.  
  245. getchar()
  246. {
  247.     char c;
  248.  
  249.     if (_diflag) {
  250.         if ((c = getc(_dibuf)) == '\r') c = getc(_dibuf);
  251.     }
  252.     else
  253.  
  254. #ifdef BUF_CONS        /* For buffered console input, get a line of text   */
  255.     {        /* from the BDOS (using "gets"), & insert newline:  */
  256.         if (!*_conbufp) {   
  257.             gets(_conbufp = _conbuf);
  258.             _conbuf[strlen(_conbuf) + 1] = '\0';
  259.             _conbuf[strlen(_conbuf)] = '\n';
  260.         }
  261.         c = *_conbufp++;
  262.     }
  263. #else            /* for raw console input, simulate normal "getchar": */
  264.         if ((c = bdos(CON_INPUT)) == CONTROL_C) exit();
  265. #endif
  266.  
  267.     if (c == CPMEOF) return EOF;         /* Control-Z is EOF key     */
  268.  
  269.     if (c == '\r') 
  270.     {
  271.         c = '\n';
  272. #ifndef BUF_CONS
  273.         if (!_diflag) bdos(2,'\n');  /* echo LF after CR to console */
  274. #endif
  275.     }
  276.     return c;
  277. }
  278.  
  279.  
  280. /*
  281.     This version of "putchar" replaces the regular version when using
  282.     directed I/O:
  283. */
  284.  
  285. putchar(c)
  286. char c;
  287. {
  288.     if (_doflag)
  289.     {
  290.         if (c == '\n') putc('\r',_dobuf);
  291.         if(putc(c,_dobuf) == ERROR)
  292.         {
  293.             fprintf(STDERR,"File output error; disk full?\n");
  294.             exit();
  295.         }
  296.         if (!(_doflag & VERBOSE)) return;
  297.     }
  298.  
  299.     if (bdos(CON_STATUS) && bdos(CON_INPUT) == CONTROL_C) exit();
  300.     if (c == '\n') bdos(CON_OUTPUT,'\r');
  301.     bdos(CON_OUTPUT,c);
  302. }
  303.