home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 443.lha / pcl2english_v2.0 / main.c < prev    next >
C/C++ Source or Header  |  1990-12-02  |  5KB  |  241 lines

  1. /* $RCSfile: main.c,v $  $Revision: 2.0 $ */
  2.  
  3. /*  
  4.  
  5. Program: pcl2english
  6.  
  7. Author: Allen Norskog
  8.  
  9. Revision history:
  10.     V1.x:           - (unreleased development versions)
  11.     V2.0:   9/16/90 - First release 
  12.  
  13. This program released into the Public Domain.
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "gdefs.h"
  20. #include "externals.h"
  21. #include "protos.h"
  22. FILE *fopen(),*ifile,*ofile;
  23.  
  24. #define ERR_USAGE        1
  25. #define ERR_INFILE        2
  26. #define ERR_OUTFILE        3
  27.  
  28. #define MODE_CHAR        0
  29. #define MODE_ESC        1
  30.  
  31. extern int optind;
  32. static char version[]="\npcl2english V2.0\n";
  33.  
  34. void init(void)
  35. {
  36.     opt_e = FALSE;
  37.     opt_v = FALSE;
  38.     opt_x = FALSE;
  39.     first_col = current_col = 0;
  40.     last_col = 79;
  41.     num_lines = 0;
  42.     raster_mode = NO_RASTER;
  43.     raster_count = 0;
  44. }
  45.  
  46. void main(int argc, char *argv[])
  47. {
  48.     int c;
  49.     int done;
  50.  
  51.  
  52.     init();
  53.     /* Decode the options.  */
  54.  
  55.     while ((c = getopt (argc, argv, "evx")) != EOF) {
  56.         switch (c) {
  57.             case 'e':
  58.                 /* Print escape sequences only.  */
  59.                 opt_e = TRUE;
  60.                 break;
  61.             case 'v':
  62.                 /* Print version number.  */
  63.                 opt_v = TRUE;
  64.                 break;
  65.             case 'x':
  66.                 /* Print hexadecimal equivalents.  */
  67.                 opt_x = TRUE;
  68.                 break;
  69.             case '?':
  70.                 /* Illegal option -- print usage message */
  71.                 err_exit(ERR_USAGE);
  72.                 break;
  73.         }
  74.     }
  75.     if (optind > argc - 1) {
  76.         if (opt_v) {
  77.             printf("%s\n\n",version);
  78.             exit(0);
  79.         }
  80.         else {
  81.             err_exit(ERR_USAGE);
  82.         }
  83.     }
  84.     ifile=fopen(argv[optind],"r");
  85.     if (!ifile) {
  86.         err_exit(ERR_INFILE);
  87.     }
  88.     if (optind <= argc - 2) {
  89.         ofile=fopen(argv[optind + 1],"w");
  90.         if (!ofile) {
  91.             err_exit(ERR_OUTFILE);
  92.         }
  93.     }
  94.     else ofile=stdout;
  95.     if (opt_v) {
  96.         fprintf(ofile, "%s\n\n",version);
  97.     }
  98.     mode = MODE_CHAR;
  99.     done = FALSE;
  100.     while (done == FALSE) {
  101.         c = getc(ifile);
  102.         if (c == EOF) {
  103.             done = TRUE;
  104.         }
  105.         switch (mode) {
  106.             case MODE_CHAR:
  107.                 switch (c) {
  108.                     case ESC:
  109.                         if (char_count > 0) {
  110.                             print_chars();  
  111.                             /* print_chars will make sure raster_mode 
  112.                                is cleared */
  113.                         }
  114.                         else {
  115.                         }
  116.                         mode = MODE_ESC;
  117.                         esc_count = 0;
  118.                         esc_string[esc_count] = c;
  119.                         break;
  120.                     case FORM_FEED:
  121.                         print_chars();
  122.                         fprintf(ofile,
  123.                             "\n<Form feed>  Move to first line at top of next page\n");
  124.                         if (opt_x) {
  125.                             fprintf(ofile,"0C");
  126.                         }
  127.                         else {
  128.                             fprintf(ofile,"  ");
  129.                         }
  130.                         fprintf(ofile,
  131.                             "           while maintaining current column ");
  132.                         fprintf(ofile,"at %d.\n", current_col);
  133.                         break;
  134.                     case SHIFT_IN:
  135.                         print_chars();
  136.                         fprintf(ofile,
  137.                             "\n<Shift In>  Select characters that follow from the\n");
  138.                         if (opt_x) {
  139.                             fprintf(ofile,"0F");
  140.                         }
  141.                         else {
  142.                             fprintf(ofile,"  ");
  143.                         }
  144.                         fprintf(ofile,"          current primary font.\n");
  145.                         break;
  146.                     case SHIFT_OUT:
  147.                         print_chars();
  148.                         fprintf(ofile,
  149.                             "\n<Shift Out> Select characters that follow from the\n");
  150.                         if (opt_x) {
  151.                             fprintf(ofile,"0E");
  152.                         }
  153.                         else {
  154.                             fprintf(ofile,"  ");
  155.                         }
  156.                         fprintf(ofile,"          current secondary font.\n");
  157.                         break;
  158.                     case EOF:
  159.                         print_chars();
  160.                         fprintf(ofile,"\n");
  161.                         break;
  162.                     case CARRIAGE_RETURN:
  163.                         current_col = first_col;
  164.                         add_char(c);
  165.                         break;
  166.                     case LINE_FEED:
  167.                         num_lines++;
  168.                         add_char(c);
  169.                         break;
  170.                     case BACK_SPACE:
  171.                         if (current_col > first_col) {
  172.                             current_col--;
  173.                         }
  174.                         add_char(c);
  175.                         break;
  176.                     case TAB:
  177.                         {
  178.                             int i;
  179.                             i = (current_col - first_col) / 8;
  180.                             current_col = (i+1)*8 + first_col;
  181.                             if (current_col > last_col) {
  182.                                 current_col = last_col;
  183.                             }
  184.                             add_char(c);
  185.                         }
  186.                         break;
  187.                     default:
  188.                         add_char(c);
  189.                         if (((c >= 32) && (c <= 127)) ||
  190.                             ((c >= 161) && (c <= 254))) {
  191.                             /* Ignore non-printing characters in
  192.                                 Roman-8 character set */
  193.                             if (current_col < last_col) {
  194.                                 current_col++;
  195.                             }
  196.                         }
  197.                         break;
  198.                 }
  199.                 break;
  200.             case MODE_ESC:
  201.                 if (c == EOF) {
  202.                     fprintf(ofile,
  203.                         "\nEOF reached before completing last escape sequence.\n");
  204.                 }
  205.                 else {
  206.                     esc_count++;
  207.                     esc_string[esc_count] = c;
  208.                     if (((esc_count == 1) && (c >= '0') && (c <= '~')) ||
  209.                         /* A two character escape string */
  210.                         ((c >= '@') && (c <= 'Z'))) {    
  211.                         /* Look for terminating character.
  212.                             @ is just before A */
  213.  
  214.                             esc_string[esc_count + 1] = 0;
  215.                         if (raster_mode == NO_RASTER) {
  216.                             print_esc_string();
  217.                         }
  218.                         else {
  219.                             if (strncmp(esc_string, raster_header, 
  220.                                 strlen(raster_header)) != 0) {
  221.  
  222.                                 print_extra_rasters();
  223.                                 raster_mode = NO_RASTER;
  224.                                 print_esc_string();
  225.                             }
  226.                         }
  227.                         decode_esc_string();
  228.                         mode = MODE_CHAR;
  229.                         char_count = 0;
  230.                     }
  231.                 }
  232.                 break;
  233.             default:
  234.                 break;
  235.         }
  236.     }
  237.     fclose(ifile);
  238.     fclose(ofile);
  239.     exit(0);
  240. }
  241.