home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msppcp.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  5KB  |  133 lines

  1. /* pcprint.c -- Print files on a printer which is locally attached to a DEC 
  2.  * VT102, VT200, VT300, or compatible, or to a PC that emulates them (e.g. IBM 
  3.  * PC with Kermit 2.31 or later).
  4.  *
  5.  * Usage:
  6.  *   command | pcprint
  7.  *   pcprint < file
  8.  *   pcprint file(s)
  9.  * and in MM, "set print-filter pcprint", then use MM's "print" command.
  10.  *
  11.  * "pcprint" allows printing of text files and binary files with no parity.
  12.  * To do this, the terminal has to be put in "raw mode", in which none of its
  13.  * other functions work.  Therefore, the terminal is periodically restored to
  14.  * normal so that it can be interrupted with Ctrl-C, do Xon/Xoff, etc.
  15.  *
  16.  * Authors: Christine Gianone and Frank da Cruz, CUCCA, March 14, 1989
  17.  */
  18.  
  19. /* Preprocessor includes and defines */
  20.  
  21. #include <stdio.h>            /* Standard i/o */
  22. #include <sgtty.h>            /* Set/Get terminal modes */
  23. #include <signal.h>            /* For keyboard interrupts */
  24. #include <sys/types.h>            /* For stat.h... */
  25. #include <sys/stat.h>            /* For file status queries */
  26. #define BUFL 1000            /* Input buffer length */
  27.  
  28. /* Global Declarations */
  29.  
  30. static struct sgttyb old, new;        /* Terminal modes structure */
  31. static struct stat statbuf;        /* File status structure */
  32. int fd;                    /* Input file descriptor */
  33. int doexit();                /* Forward declaration of doexit() */
  34.  
  35. /* Main function */
  36.  
  37. main(argc,argv) int argc; char *argv[]; {
  38.     int nf;                /* File number from command line. */
  39.     int x;                /* Temporary variable. */
  40.  
  41. /* Find out the current terminal settings from Unix */
  42.  
  43.     gtty(1,&old);            /* For restoring tty to how it was. */
  44.     gtty(1,&new);            /* Plus a new copy, */
  45.     new.sg_flags |= RAW;        /* for putting tty in "raw mode" */
  46.                     /* to allow 8-bit data output */
  47.                     /* with no parity. */
  48.  
  49. /* Send the ANSI "begin transparent print" sequence, "ESC [ 5 i".  This */
  50. /* makes the terminal send its input to the printer instead of the screen. */
  51.  
  52.     printf("%c[5i",'\033');        /* Print the escape sequence. */
  53.     fflush(stdout);            /* Make sure it goes out */
  54.                     /* before we change tty modes. */
  55.  
  56. /* Since programs can be halted by users typing Ctrl-C or other keyboard */
  57. /* interrupt characters, we must catch these interrupts in order to restore */
  58. /* the terminal to normal (non-printing, non-raw) mode before exiting. */
  59.  
  60.     signal(SIGINT,doexit);        /* Control-C */
  61.     signal(SIGQUIT,doexit);        /* Control-\ */
  62.  
  63. /* Input can either be from standard input (redirected stdin, pipe, or MM */
  64. /* PRINT command), or else from a list of files given on the command line. */
  65.  
  66. /* Case 1: Print from Standard Input, e.g. "pcprint < test.txt". */
  67.  
  68.     if (argc == 1) {            /* If printing from standard input */
  69.     fd = 0;                /* File descriptor 0 = stdin */
  70.     dofile();            /* Print until no more data */
  71.     doexit();            /* Done */
  72.     }
  73.  
  74. /* Case 2: Filename(s) specified on command line, e.g. "pcprint x.a x.b". */
  75. /* Each file must be opened, printed, & closed.  Skip over directory files. */
  76.  
  77.     nf = 0;                /* Current file number. */
  78.     while (++nf < argc) {        /* Start with file 1, if any .*/
  79.     if (stat(argv[nf],&statbuf) < 0) /* First see if the file exists. */
  80.       continue;            /* Doesn't exist, try next one. */
  81.     x = statbuf.st_mode & S_IFMT;    /* Check file format. */
  82.     if ((x != 0) && (x != S_IFREG))    /* If not a regular file, */
  83.       continue;            /* try the next file. */
  84.     if ((fd = open(argv[nf],0)) < 0) /* Try to open the file read-only. */
  85.       continue;            /* On failure, try next file. */
  86.     dofile();            /* Opened OK, call printing function */
  87.     close(fd);            /* and after printing close the file */
  88.     }
  89.     doexit();                /* No more files, clean up and exit. */
  90. }
  91.  
  92. /* Function to print one file */
  93.  
  94. dofile() {
  95.     char buf[BUFL];            /* Input buffer */
  96.     int i;                /* Input buffer counter */
  97.     int n;                /* Input character EOF indicator */
  98.     char c;                /* Input character itself */
  99.     int done;                /* Flag for done */
  100.  
  101.     done = 0;                /* Initial condition for loop. */
  102.     while (!done) {            /* While not done... */
  103.     stty(1,&old);            /* Put terminal in normal mode */
  104.                     /* to catch terminal interrupts. */
  105.     for (i = 0; i < BUFL-1; i++) {  /* Loop to fill the input buffer. */
  106.         n = read(fd,&c,1);        /* Read one character. */
  107.         if (n == 0) {        /* If no more, */
  108.         done = 1;        /* we're done! */
  109.         break;            /* Break out of this for-loop. */
  110.         }
  111.         if (c == '\n')         /* If character is a newline, */
  112.           buf[i++] = '\r';        /* supply a carriage return. */
  113.         buf[i] = c;            /* Deposit character in buffer. */
  114.     }
  115.     stty(1,&new);            /* Put tty in raw (no-parity) mode. */
  116.     write(1,buf,i);            /* Request UNIX write the buffer. */
  117.     fflush(stdout);            /* Now make UNIX really do it. */
  118.     }
  119. }
  120.  
  121. /* Exit function.  Leave user's terminal the way it was upon entry. */
  122. /* Turn off transparent print by sending the escape sequence "ESC [ 4 i". */
  123.  
  124. doexit() {                /* Program exit. */
  125.     if (fd != 0) close(fd);        /* Close any open input file. */
  126.     signal(SIGINT,SIG_DFL);        /* Return keyboard interrupts */
  127.     signal(SIGQUIT,SIG_DFL);        /* to normal. */
  128.     printf("%c[4i",'\033');        /* Turn off transparent print. */
  129.     fflush(stdout);            /* Make sure it goes out. */
  130.     stty(1,&old);            /* Restore terminal to normal. */
  131.     exit(0);                /* And exit. */
  132. }
  133.