home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 127_01 / rapio.c < prev    next >
Text File  |  1985-03-10  |  9KB  |  359 lines

  1. /*********************************************************************\
  2. ** .---------------------------------------------------------------. **
  3. ** |                                                               | **
  4. ** |                                                               | **
  5. ** |         Copyright (c) 1981, 1982, 1983 by Eric Martz.         | **
  6. ** |                                                               | **
  7. ** |                                                               | **
  8. ** |       Permission is hereby granted to use this source         | **
  9. ** |       code only for non-profit purposes. Publication of       | **
  10. ** |       all or any part of this source code, as well as         | **
  11. ** |       use for business purposes is forbidden without          | **
  12. ** |       written permission of the author and copyright          | **
  13. ** |       holder:                                                 | **
  14. ** |                                                               | **
  15. ** |                          Eric Martz                           | **
  16. ** |                         POWER  TOOLS                          | **
  17. ** |                    48 Hunter's Hill Circle                    | **
  18. ** |                      Amherst MA 01002 USA                     | **
  19. ** |                                                               | **
  20. ** |                                                               | **
  21. ** `---------------------------------------------------------------' **
  22. \*********************************************************************/
  23.  
  24. /*
  25. REDIRECTIONS SUPPORTED:
  26.  
  27.     )filename
  28.     
  29.     )/x...
  30.     
  31.         where x is:
  32.         
  33.             l or L for LIST
  34.             pu or PU for PUNCH
  35.             po or PO for user port
  36.             s or S for SCREEN (CONSOLE)
  37. */
  38.  
  39. #include "rap.h"
  40.  
  41.  
  42. #define SCREEN 1
  43. #define LIST_OUT 2
  44. #define PUNCH_OUT 4
  45. #define PORT_OUT 8
  46. #define FILE_OUT 16
  47.  
  48. #define CTRL_C '\3'
  49. #define CTRL_W '\27'
  50. #define CTRL_E '\5'
  51.  
  52. #define INTOREV "\33[7m"
  53. #define OUTAREV "\33[0m"
  54.  
  55. /* 
  56.     The "dioinit" function must be called at the beginning of the
  57.     "main" function:
  58. */
  59. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  60. #define argc *p_argc
  61.  
  62. dioinit(p_argc,argv)
  63. int *p_argc;
  64. char **argv;
  65. {
  66.     int i,j;
  67.     int n;    /* this keeps track of location in argument */
  68.     char *p;
  69.  
  70. #ifdef DEBUG
  71.     if (Debug) fprintf(STDERR, "DIOINIT: (argc = %d)\n", argc);
  72. #endif
  73.  
  74.     Xflag = YES;
  75.  
  76.     for (i = 1; i < argc; i++) { /* Scan the command line for / */
  77.  
  78. #ifdef DEBUG
  79.         if (Debug) fprintf(STDERR, "\tArg %d:%s\n", i, argv[i]);
  80. #endif
  81.  
  82.         if (*argv[i] EQ ')') {
  83.             if (!argv[i][1]) {
  84.                 _Outflag = 0;
  85.                 goto delarg;
  86.             }
  87.             /* DEVICE? */
  88.             if (argv[i][1] EQ '/') {
  89.                 p = argv[i]+2;
  90.                 switch (*p) {
  91.                     case 'L':
  92.                         _Outflag |= LIST_OUT;
  93.                         break;
  94.                     case 'P':
  95.                         switch (*(p+1)) {
  96.                             case 'U':
  97.                                 _Outflag |= PUNCH_OUT;
  98.                                 break;
  99.                             case 'O':
  100.                                 _Outflag |= PORT_OUT;
  101.                                 break;
  102.                             default:
  103.                                 goto badout;
  104.                         }
  105.                         break;
  106.                     case 'S':
  107.                         _Outflag |= SCREEN;
  108.                         Screen_size = SCREEN_SIZE;
  109.                         break;
  110.                     default:
  111.                         goto badout;
  112.                 }
  113.             }
  114.             
  115.             /* FILE OUT */
  116.             else {
  117.                 strcpy (Out_file, argv[i]+1 );
  118.                 if (badname(Out_file)) exit(0);
  119.                 if (fcreat("TEMPOUT.$$$", _Dobuf) == ERROR) {
  120.                     fprintf(STDERR,"\nCan't create <%s>; disk full?\n",
  121.                         "TEMPOUT.$$$");
  122.                     exit();
  123.                 }
  124.                 _Outflag |= FILE_OUT;
  125.             }
  126.  
  127.             /* DELETE ARGUMENT FOR PASSING TO MAIN */
  128. delarg:
  129.             for (j = i; j < argc; j++) argv[j] = argv[j+1];
  130.             (argc)--;
  131.             i--;
  132.         }
  133.     }
  134. #ifdef DEBUG
  135.     if (Debug) fprintf(STDERR, "Returning from DIOINIT\n");
  136. #endif
  137.     return(0);
  138. badout:
  139.     fprintf(STDERR,
  140.         "Bad output specifier:%s",
  141.         argv[i]);
  142.     exit();
  143. }
  144. #undef argc
  145. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  146. /*
  147.     The "dioflush" function must be called before exiting the program:
  148. */
  149.  
  150. dioflush()
  151.     {
  152.     if (_Outflag & FILE_OUT) {
  153. #ifdef DEBUG
  154.         if (Debug) fprintf(STDERR,"FLUSHING FILE_OUT\n");
  155. #endif
  156.         putc(CPMEOF,_Dobuf);
  157.         fflush(_Dobuf);
  158.         bf_close(_Dobuf);
  159.         if (strcmp ("TEMPOUT.$$$", Out_file)) {
  160.             unlink (Out_file);
  161.             rename ("TEMPOUT.$$$", Out_file);
  162.         }
  163.     }
  164. }
  165. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  166. /*
  167.     This version of "putchar" replaces the regular version when using
  168.     directed I/O:
  169. */
  170.  
  171. putchar(c)
  172.     char c;
  173.     {
  174.     char *pc;
  175.     int control;
  176.     int suppress_cr, compress;
  177.  
  178.     if (First_text) {
  179.         First_text = NO;
  180.         if (Screen_size) fprintf(STDERR,
  181.             "%s<Top of first page>%s\n", Bar, Bar);
  182.         if (Init) puts(Init); /* RECURSIVE CALL OF PUTCHAR! */
  183.         if (Font NE 0 AND Font_o) printf("%s%d%s", Font_o, Font, Font_c);
  184.         if (Justify NE 0) restore_mode();
  185.     }
  186.  
  187.     if (bdos(CON_STATUS)) {
  188.         control = bdos(CONIN);
  189.         if (!Interrupted AND control EQ CTRL_C) abort();
  190.         if (control EQ CTRL_W) wait();
  191.         if (control EQ CTRL_E) End_wait = YES;
  192.     }
  193.         
  194.     if (c > 127) {    /* META CHARACTER */
  195.         if (c EQ c1(WAIT)) wait();
  196.  
  197.         /* TOGGLE PROMPT FLAG */
  198.         else if (c EQ c1(PROMPT)) {
  199.             if(!Prompt) Prompt = YES;
  200.             else {
  201.                 Prompt = NO;
  202.                 if (!Screen_size) {
  203.                     bdos(CONOUT, '\r');
  204.                     bdos(CONOUT, '\n');
  205.                 }
  206.             }
  207.         }
  208.         else {
  209.             fprintf(STDERR,
  210. "Warning: illegal meta character (decimal %d) in output: page %d line %d\n",
  211.                 c, Curpag, Lineno-1);
  212.         }
  213.     }
  214.     else if (Prompt) {
  215.         if (!Screen_size) bdos(CONOUT, c);
  216.     }
  217.     else {
  218.         suppress_cr = NO;
  219.  
  220.         if (Comp_carat AND !Carat AND c EQ '^') {
  221.             Carat = YES;
  222.             return(0);
  223.         }
  224.  
  225.         if (Carat) {
  226.             Carat = NO;
  227.             if (c >= 64 AND c <=95) {
  228.                 if (c EQ 'J') suppress_cr = YES;
  229.                 c -= 64;
  230.             }
  231.             else {
  232.                 compress = Comp_carat;
  233.                 Comp_carat = NO;
  234.                 putchar('^');
  235.                 Comp_carat = compress;
  236.             }
  237.         }
  238.  
  239.         if (c EQ Blank_c) c = ' '; /* BLANK OUT SPECIAL CHARACTER */
  240.  
  241.         /* PRECEDE NL WITH CR UNLESS NL WAS COMPRESSED ^J */
  242.         if (c EQ NEWLINE AND !suppress_cr) put_char(CR);
  243.  
  244.         /* PRECEDE LITERAL CR WITH REVERSE LINE FEED IF PRINTER GENERATES
  245.         AUTO LF ON CR */
  246.         if (c EQ CR AND Suppress_lf)
  247.             for(pc=Rev_lf; *pc; pc++) put_char(*pc);
  248.  
  249.         /* AT LONG LAST, PUT OUT THE SILLY CHARACTER UNLESS IT IS
  250.         AN ORDINARY NEWLINE, IN WHICH CASE, PUT IT OUT ONLY IF
  251.         EITHER
  252.             WE ARE NOT SUPPRESSING LINE FEEDS
  253.         OR
  254.             WE ARE SUPPRESSING LINE FEEDS BUT IT IS A COMPRESSED ^J */
  255.         if (c NE NEWLINE OR !Suppress_lf OR suppress_cr)
  256.             put_char(c);
  257.     }
  258. }
  259. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  260. put_char(c)
  261.     int c;
  262.     {
  263.  
  264.     if (_Outflag & FILE_OUT) {
  265.         if(putc(c,_Dobuf) == ERROR) {
  266.             fprintf(STDERR,"File output error; disk full?\n");
  267.             exit();
  268.         }
  269.     }
  270.  
  271.     if (_Outflag & SCREEN /* OR Debug */) {
  272. #ifdef DEBUG
  273.         if (Debug) fprintf(STDERR,"%s",INTOREV);
  274. #endif
  275.         bdos(CONOUT,c);
  276. #ifdef DEBUG
  277.         if (Debug) fprintf(STDERR,"%s",OUTAREV);
  278. #endif
  279.     }
  280.  
  281.     if (_Outflag & LIST_OUT)    bdos(5,c);
  282.  
  283.     if (_Outflag & PUNCH_OUT) bdos(4,c);
  284.  
  285.     if (_Outflag & PORT_OUT) {
  286.         if (Pdata EQ -1) {
  287.             fprintf(STDERR,
  288.                 "Error: SS file did not define i/o port.\n");
  289.             exit(0);
  290.         }
  291.         xxoutc(c);
  292.     }
  293. }
  294. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  295. xxoutc(c)
  296.     char c;
  297.     {
  298.     int x;
  299.  
  300.     do {
  301.         while (miready()) {
  302.             if ((x = getmod()) == XOFF) Xflag = NO;
  303.             if (x EQ NULL) Xflag = NO;
  304.             if (x == XON) Xflag = YES;
  305.         }
  306.     } while (Xflag == NO);
  307.  
  308.     /* LOOP UNTIL READY */
  309.     while ((inp(Pstatus) & Pomask) != (Pready ? Pomask : 0));
  310.  
  311.     outp(Pdata,c);
  312. }    
  313. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  314. miready()
  315.     {
  316.     return (inp(Pstatus) & Pimask) == (Pready ? Pimask : 0);
  317. }
  318. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  319. getmod()
  320.     {
  321.     char c;
  322.     c = inp(Pdata);
  323.     c &= '\177';
  324.     if (Preset) outp(Pstatus,Presetval);
  325.     return c;
  326. }
  327. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  328. wait(){
  329.     int c;
  330.  
  331.     if (Screen_size) return(0);
  332.  
  333.     while (!bdos(CON_STATUS));
  334.     c = bdos(CONIN);
  335.     puts("\b \b");
  336.     if (c EQ CTRL_C) abort();
  337. }
  338. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  339. abort(){
  340.     Interrupted = YES;
  341.     fprintf(STDERR,"\n--- RAP ABORTED WITH ^C ---\n");
  342.     if (!Screen_size) {
  343.         printf("\n--- RAP ABORTED WITH ^C ---\n%c", FORMFEED);
  344.         if (Forceprint) puts(Forceprint);
  345.         if (Deinit) puts (Deinit);
  346.     }
  347.     dioflush();
  348.     exit(1);
  349. }
  350. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  351. c1(s)
  352.     char *s;
  353.     {
  354.     return(*s);
  355. }
  356. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  357. /*    END OF RAPIO.C    */
  358. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  359.  - - - - - -