home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / LJPLOT10.ZIP / LJPLOT.C next >
C/C++ Source or Header  |  1990-04-30  |  9KB  |  239 lines

  1. /* LJPLOT */
  2. /* this program takes an AUTOCAD HPGL plot file sends it to an  */
  3. /* HP LaserJet III printer.  The printer is prepared as:        */
  4. /* 1 - reset                                                    */
  5. /* 2 - HPGL mode                                                */
  6. /* the file has all escape sequences before the HPGL "IN"       */
  7. /* command stripped out.  Then, the "IN" command is replaced    */
  8. /* by "INRO90", basically doing a landscape plot                */
  9. /* The AUTOCAD setup is:                                        */
  10. /* Plotter:                                                     */
  11. /* Pens don't seem to matter                                    */
  12.  
  13.  
  14. /* This software is provided as is without any waranty or gaurantee */
  15. /* It is Copyright (c) 1990, SIRIUS Systems Co., Albuquerque, NM    */
  16.  
  17. /*  This is free software  and you may make and use and give away   */
  18. /* as many coppies as you like.  You may not sell this software in  */
  19. /* any form, alone or bundled.    */
  20. /* Exit codes:  1 - no args               2 - command line error    */
  21. /*              3 - plot file open error  4 - printer error         */
  22. /*                  5 - plot file not HPGL ?                                      */
  23.  
  24. #define ERR_ARGS 1
  25. #define ERR_COMMAND_LINE 2
  26. #define ERR_FILE 3
  27. #define ERR_PRINTER 4
  28. #define ERR_HPGL 5
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <ctype.h>
  33. #include <fcntl.h>
  34. #include <string.h>
  35.  
  36. #include <sys/stat.h>
  37.  
  38. #define BUFSIZE 16384
  39.  
  40.  
  41. int inpath,outpath; /* input and output file pointers */
  42. int port = 0;       /* flag for portrait mode on command line */
  43. char *width = 0;    /* value (string in mm) of pen width from command line */
  44. int errabort = 0;   /* flag to abort on error or not */
  45. int overplot = 0;   /* flag is set to prevent a reset between plots */
  46.                     /* this prevents the form feed and allows two plots */
  47.                     /* to be overlaid in memery before actually printing */
  48. char buffer[BUFSIZE];  /* plotfile Read/Write buffer */
  49. int plotnum = 0;    /* count the number of plots here */
  50. int final_reset = 1;    /* default to reset at the end, also the beginning */
  51.  
  52. main(argc,argv)
  53. int argc;
  54. char *argv[];
  55. {
  56.  
  57.      char *file_of_names = 0; /* pointer to a file containing file names to plot */
  58.      FILE *names_fp;       /* FILE pointer to file for reading names from */
  59. /*    char *defext = "plt"; */
  60.     char *def_width = ".25";
  61.      int i,j,k;
  62.     char linebuf[128];   /* buffer for reading filenames */
  63.  
  64.  
  65.     /* process files as they are encountered */
  66.     /* For menu help, -? or /? must be the first arg (or no args at all) */
  67.     /* Check for help */
  68.  
  69.     if(argc==1 || !strcmp(argv[1],"/?") || !strcmp(argv[2],"-?") ) {
  70.         prusage();
  71.         exit((argc>1)?0:ERR_ARGS);
  72.     }
  73.     if ( (outpath=open("PRN",O_WRONLY)) == -1 ) {
  74.         printf("can't open printer\n");     /* exit with printer error */
  75.         exit(ERR_PRINTER);
  76.     }
  77.  
  78.  
  79.     for( i=1; i<argc; i++ ) {
  80.         if( argv[i][0] == '-' || argv[i][0] == '/' ) { /* then it's an option */
  81.             switch(tolower(argv[i][1])){
  82.             case 'p':       /* use portrait mode */
  83.                             /* assume printer is in portrait and don't rotate */
  84.                             /* this toggles each time it is encountered */
  85.                 port = port?0:1;
  86.                 break;
  87.             case 'w':       /* set pen width */
  88.                 width = &argv[i][2];
  89.                 if( *width == '\0' )
  90.                     width = def_width;
  91.                 break;
  92.             case 'f':       /* get filenames from this file */
  93.                      file_of_names = &argv[i][2]; /* point to the name */
  94.                 /* try to open */
  95.                      if( (names_fp = fopen(file_of_names,"rt")) == NULL ) {
  96.                           printf("LJPLOT: Can't open file list %s\n",file_of_names);
  97.                     exit(ERR_FILE);
  98.                      }
  99.                      else {
  100.                         printf("Plotting files from: %s\n",file_of_names);
  101.                      }
  102.                      while( fgets(linebuf,128,names_fp) != NULL )
  103.                           plotfile(linebuf);
  104.                      fclose(names_fp);
  105.                      printf("End of %s\n",file_of_names);
  106.                 break;
  107.             case 'o':       /* toggle overplot */
  108.                             /* if overplot was enabled, send reset */
  109.                             /* and set overplot to 0 */
  110.                 if( overplot ) {
  111.                     write(outpath,"\033E",2);
  112.                     overplot = 0;
  113.                 }
  114.                 else
  115.                     overplot = 1;
  116.                 break;
  117.             default:
  118.                 printf("LJPLOT: Unrecognized argument '%s'\n",argv[i]);
  119.                 exit(ERR_COMMAND_LINE);
  120.                 break;
  121.             }
  122.         }
  123.         else {  /* not an option, must be a file */
  124.             plotfile(argv[i]);      /* call the file copy function */
  125.         }
  126.      }
  127.      if( final_reset ) {
  128.          write(outpath,"\033E",2);            /* always reset at the end */
  129.      }
  130.      else {
  131.         printf("Final reset disabled, Check printer for paper\n");
  132.      }
  133. }
  134.  
  135. int plotfile(char *filename)
  136. {
  137.     unsigned char_count;
  138.     char header[32];    /* buffer for revised buffer */
  139.     char *p;
  140.      p = filename;
  141.     /* scan filename.  Throw out trailing spaces */
  142.      while( *p && *p != 0x0d && *p != 0x0a && *p != 0x20 )
  143.         p++;
  144.      *p = '\0';   /* null terminate */
  145.  
  146.      /* check for a NULL file name, just return 0 if passed */
  147.      if( !*filename ) {
  148.         return(0);
  149.      }
  150.  
  151.     if( (inpath = open(filename,O_RDONLY|O_BINARY)) == -1 ) {
  152.           printf("LJPLOT: Can't open plot file %s",filename);
  153.         if( errabort ) {
  154.             exit(3);
  155.         }
  156.         else {
  157.             return(-1);
  158.         }
  159.      }
  160.      else {
  161.             printf("Plotting: %s",filename);
  162.      }
  163.     /* infile is open now.  1) reset plotter if first plot
  164.                             2) HPGL mode if overplot = 0 or 1
  165.                             3) read filename, find "IN;" chars in the buffer
  166.                                Report a file format error if not found.
  167.                             4) send IN, rotate and pen width if required
  168.                             5) send plot data
  169.                             6) reset if not in overplot mode, this will print
  170.                             7) increment overplot if in overplot mode
  171.                                this will leave the plotter in HPGL mode for
  172.                                the next file
  173.     */
  174.     /* step 1 */
  175.      if( plotnum == 0 && final_reset ) { /* this is the first plot, need to perform a reset */
  176.           write(outpath,"\033E",2);
  177.     }
  178.     /* step 2 */
  179.     if( overplot == 0 || overplot == 1 ) {
  180.         write(outpath,"\033%0B",4);     /* go into HPGL mode */
  181.     }
  182.     if( overplot > 1 ) {
  183.         printf(" as overlay");
  184.     }
  185.     printf("\n");     /* start new file name line */
  186.     /* step 3 */
  187.     char_count = read(inpath,buffer,BUFSIZE);
  188.     p = buffer;
  189.      while( strncmp(p,"IN;",3) && char_count ) {   /* make p point to HPGL INIT */
  190.         p++;
  191.         char_count--;   /* keep track of skipped garbage */
  192.      }
  193.      if( !char_count ) {
  194.         printf("NO PLOT INITIALIZATION FOUND - CHECK FILE\n");
  195.         return(ERR_HPGL);
  196.      }
  197.     p+=3;               /* pass it */
  198.     char_count-=3;      /* and send that many less chars */
  199.     /* step 4 */
  200.     strcpy(header,"IN");
  201.     if( !port )
  202.         strcat(header,"RO90");
  203.     if( width ) {
  204.         strcat(header,"PW");
  205.         strcat(header,width);
  206.         strcat(header,";");
  207.         itoa(width,&header[strlen(header)],10);
  208.     }
  209.     write(outpath,header,strlen(header));
  210.     /* step 5 */
  211.     write(outpath,p,char_count);    /* send what's in the buffer */
  212.      while( (char_count=read(inpath,buffer,BUFSIZE)) >0 ) { /* send the rest */
  213.      /* any processing of file params goes here */
  214.         write(outpath,buffer,char_count);
  215.     }
  216.     /* steps 6 and 7 */
  217.     if( ! overplot )
  218.         write(outpath,"\033E",2);
  219.     else
  220.         overplot++;
  221.     plotnum++;
  222.     close(inpath);
  223.     return(0);
  224. }
  225.  
  226. prusage()
  227. {
  228.     printf("LJPLOT: Send AUTOCAD HPGL plot files to HP LaserJet III, V1.0\n");
  229.     printf("Copyright (c) 1990, SIRIUS Systems Co.\n");
  230.     printf("Syntax: [switches] files [switches] files\n");
  231.     printf("Command line processed left to right\n");
  232.     printf("Switches:\n");
  233.     printf("         /o - overplot following files (toggle)\n");
  234.     printf("         /p - use portrait mode (don't rotate plot) (toggle)\n");
  235.     printf("         /ffilename - read file list\n");
  236.     printf("         /wnum - set pen width\n");
  237. }
  238.  
  239.