home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / desqview / qupie11.arc / QP11.C < prev    next >
C/C++ Source or Header  |  1988-11-06  |  5KB  |  165 lines

  1. /****************************************************************************
  2.  
  3.             Print program that bypasses DOS and BIOS print routines
  4.                       to allow good background printing
  5.  
  6. *****************************************************************************/
  7. /* version 1.1
  8.       Tweaked to reduce size of executable
  9.  
  10.       Ralf Brown   1:129/31
  11.       11/6/88
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <ctype.h>
  17. #ifdef __TURBOC__
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. void prchar(unsigned char) ;
  22. int prfile(char *name) ;
  23. int prind(char *name) ;
  24.  
  25. /* tweaks to let program run in minimal memory */
  26. unsigned int _stklen = 768 ;    /* don't need 4K stack */
  27. unsigned int _heaplen = 128 ;   /* don't need much heap */
  28. void _setenvp(void) {}          /* don't need environment array */
  29. #endif __TURBOC__
  30.  
  31. int port;    /*  Port address  */
  32. int auto_ff;    /*  Set nonzero if FF is to be printed after each file  */
  33.  
  34. /*****  Routine to print one character to parallel port  *****/
  35.  
  36. void prchar(c)
  37. unsigned char c;
  38. {
  39.   unsigned char status;
  40.  
  41.   while ((inportb(port+1)&0xb8)!=0x98) ;    /*  Wait for printer ready  */
  42.   status=inportb(port+2);            /*  Get current status  */
  43.   outportb(port,c);                /*  Output data  */
  44.   outportb(port+2,status|1);            /*  Strobe STR line  */
  45.   outportb(port+2,status&~1);
  46. }
  47.  
  48. /*****  Routine to print file specified by fname.  Returns 0 if file  *****/
  49. /*****  printed o.k., -1 if there was an error opening the file.      *****/
  50.  
  51. int prfile(fname)
  52. char *fname;
  53. {
  54.   static unsigned char buff[512];
  55.   int i,n,fd;
  56.  
  57.   fd=open(fname,O_RDONLY|O_BINARY);
  58.   if (fd<0) return(-1);
  59.   while ((n=read(fd,buff,sizeof(buff)))>0) {
  60.     for (i=0; i<n; i++) prchar(buff[i]);
  61.   }
  62.   close(fd);
  63.   if (auto_ff) prchar(12);    /*  Print FF after file if requested  */
  64.   return(0);
  65. }
  66.  
  67. /*****  Routine to print each file listed in file fname.  Handles  *****/
  68. /*****  nested "@" files.  Returns -1 on error, 0 otherwise.       *****/
  69.  
  70. int prind(fname)
  71. char *fname;
  72. {
  73.   extern char *fgets();
  74.   static char buff[80];
  75.   char *cp,*cp2;
  76.   FILE *fp;
  77.  
  78.   fp=fopen(fname,"r");
  79.   if (!fp) return(-1);
  80.   while (fgets(buff,sizeof(buff),fp)) {
  81.     cp=buff;
  82.     while (*cp && isspace(*cp)) cp++;
  83.     if (!(*cp)) continue;
  84.     if (*cp=='@') {        /*  Handle nested @filename recursively  */
  85.       cp++;
  86.       while (*cp && isspace(*cp)) cp++;
  87.       cp2=cp;
  88.       while (*cp && !isspace(*cp)) cp++;
  89.       *cp=0;
  90.       prind(cp2);
  91.     } else {            /*  Print file  */
  92.       cp2=cp;
  93.       while (*cp && !isspace(*cp)) cp++;
  94.       *cp=0;
  95.       prfile(cp2);
  96.     }
  97.   }
  98.   fclose(fp);
  99.   return(0);
  100. }
  101.  
  102. /*****  Here we go!  *****/
  103.  
  104. main(ac,av)
  105. int ac;
  106. char *av[];
  107. {
  108.   int i,portnum;
  109.   char c;
  110.   char name[80] ;
  111.  
  112.   if (ac<2) {
  113.     puts("Qupie print program   by Rob Epps\n\tv1.1 tweaked by Ralf Brown");
  114.     puts("Usages:\n");
  115.     puts("\tQP [options] filename");
  116.     puts("\t    prints one file.");
  117.     puts("\tQP [options] @filename");
  118.     puts("\t    prints files whose names are in specified file.");
  119.     puts("\t    The file is a simple text file containing one file name per line.");
  120.     puts("\tQP [options] =") ;
  121.     puts("\t    prompts for files to print") ;
  122.     puts("Options:\n");
  123.     puts("\t-f   print form feed after each file.");
  124.     puts("\t-l#  print to printer # (i.e. -l2 prints to LPT2).\n");
  125.     exit(1);
  126.   }
  127.   portnum=0;
  128.   if (ac>2) {        /*  Process options  */
  129.     for (i=1; i<ac; i++) if (*av[i]=='-') {
  130.       c=*(av[i]+1);
  131.       if (c=='f' || c=='F') auto_ff=1;
  132.       if (c=='l' || c=='L') {
  133.         portnum = atoi(av[i]+2);
  134.         if (portnum<1 || portnum>4) {
  135.           puts("Sorry, can't deal with that printer port selection!\n");
  136.           fflush(stdout) ;
  137.           exit(1);
  138.         }
  139.         portnum--;
  140.       }
  141.     }
  142.   }
  143.   port=peek(0x40,8+portnum+portnum);
  144.   if (!port) {
  145.     fputs("There's no LPT",stdout) ;
  146.     putchar(portnum+'1') ;
  147.     puts(" entry in BIOS table!");
  148.     exit(1);
  149.   }
  150.   if (*av[ac-1]=='=' && av[ac-1][1]=='\0')  /* if filename is '=', prompt for name */
  151.      {
  152.      puts("Enter filename or just RETURN when done") ;
  153.      do {
  154.         fputs("File: ",stdout) ;
  155.         gets(name) ;
  156.         if (name[0] == '@')
  157.            prind(name+1) ;
  158.         else if (*name)
  159.            prfile(name) ;
  160.         } while (*name != '\0') ;
  161.       }
  162.   if (*av[ac-1]=='@') prind(av[ac-1]+1); else prfile(av[ac-1]);
  163.   exit(0);
  164. }
  165.