home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / pmod23a.lzh / pmod23a / pmod23.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-19  |  6.7 KB  |  231 lines

  1. /*
  2. ** pmod23.c, Copyright (C) 1992 by John Edward Mosley
  3. ** 
  4. **                        - Amiga Version 2.3a -
  5. **    This program sends codes to the printer like condensed/elite type,
  6. **    line feeds, form feeds, and line spacing. It will also print out
  7. **    given text files listed in the argument list. Code arguments must be
  8. **    preceeded by "-" signs and many may immediately follow.  This is a
  9. **    beta version of this program, which makes use of a nice and friendly
  10. **    requestor to allow the user to make corrections to the command-line
  11. **    arguments.  The support file for this is Request.c.  Written in SAS/C
  12. **    version 5.10 or so.
  13. **    
  14. **    Compile with: lc -L pmod23.c Request.c
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include "pmod23.h"
  20.  
  21. void main(int argc, char *argv[]) {
  22.     if(badargs(argc, argv)) print_usage();
  23.     printcodes(argc, argv);
  24. } /* main() */
  25.  
  26.  
  27. /*
  28. ** Checks the command line for valid data and returns 1 if
  29. ** the data was bad, else a 0.
  30. */
  31. int badargs(int argc, char *argv[]) {
  32.     int i, num;
  33.     char *ptr, exnptr[11];
  34.  
  35.     if(argc < 2 ) {
  36.         printf("%s: Not enough arguments\n", NAME);
  37.         return(1);
  38.     } /* if */
  39.     for(i = 1; i < argc; i++) {
  40.         ptr = argv[i];
  41.         if(*ptr == '-')
  42.             while(*ptr) {
  43.                 *ptr = tolower(*ptr);
  44.                 if(*ptr == 'l') {
  45.                     /* check the number's validity */
  46.                     exnum(ptr, exnptr);
  47.                     if(strlen(exnptr) == 0) {
  48.                         printf("%s: Must supply a number for -l option\n", NAME);
  49.                         return(1);
  50.                     } /* if */
  51.                     num = atoi(exnptr);
  52.                     if(num < 1 || num > 999) {
  53.                         printf("%s: Number for -l option must be from 1 to 999\n", NAME);
  54.                         return(1);
  55.                     } /* if */
  56.                     ptr += strlen(exnptr);
  57.                 } /* if */
  58.                 ptr++;
  59.             } /* while */
  60.     } /* for */
  61.     return(0);
  62. } /* badargs() */
  63.  
  64.  
  65. /* global vars for keeping up with file stuff */
  66. char *source_file_buffer = NULL, *dest_file_buffer = NULL;
  67.  
  68. /*
  69. ** Prints out the corresponding codes in the
  70. ** command string pointed to by pmode.
  71. */
  72. void printcodes(int argc, char *argv[]) {
  73.     FILE *dfptr, *sfptr;
  74.     int i, j, lines;
  75.     char ch, *pmode, exnptr[11], answer[11];
  76.     GRES result;
  77.  
  78.     result.ID = NOTHING;
  79.     if((dfptr = fopen("par:","w")) == NULL) {
  80.         printf("%s: Printer not accessible\n", NAME);
  81.         exit(10);
  82.     } /* if */
  83.     if((dest_file_buffer = (char *)malloc(DFB_SIZE)) == NULL
  84.       || setvbuf(dfptr, dest_file_buffer, _IOFBF, DFB_SIZE) != NULL)
  85.         printf("%s: Cannot set up printer buffer\n", NAME);
  86.     /* loop through each argument */
  87.     for(i = 1; i < argc; i++) {
  88.         pmode = argv[i];
  89.         if(*pmode == '-') {    /* argument contains options */
  90.             /* check each option and print the corresponding printer codes */
  91.             while(*pmode) {
  92.                 switch(*pmode) {
  93.                     case '-':
  94.                         break;
  95.                     case '6':
  96.                         fprintf(dfptr, "\x1b");
  97.                         fprintf(dfptr, "2");
  98.                         break;
  99.                     case '8':
  100.                         fprintf(dfptr, "\x1b");
  101.                         fprintf(dfptr, "0");
  102.                         break;
  103.                     case 'q':
  104.                         fprintf(dfptr, "\x1bx\x01");
  105.                         break;
  106.                     case 'd':
  107.                         fprintf(dfptr, "\x1bx");
  108.                         fputc(0, dfptr);
  109.                         break;
  110.                     case 'c':
  111.                         fprintf(dfptr, "\x1b\x0f");
  112.                         break;
  113.                     case 'n':
  114.                         fprintf(dfptr, "\x12");
  115.                         break;
  116.                     case 'e':
  117.                         fprintf(dfptr, "\x1bM");
  118.                         break;
  119.                     case 'p':
  120.                         fprintf(dfptr, "\x1bP");
  121.                         break;
  122.                     case 'f':
  123.                         fprintf(dfptr, "\x0c");
  124.                         break;
  125.                     case 'l':
  126.                         exnum(pmode, exnptr);
  127.                         lines = atoi(exnptr);
  128.                         pmode += strlen(exnptr);
  129.                         for(j = 0; j < lines; j++)
  130.                             fprintf(dfptr, "\x0a");
  131.                         break;
  132.                     default:
  133.                         /* unknown option - signal to break out of while loop */
  134.                         result.ID = OPTION_REQ;
  135.                 } /* switch */
  136.                 if(result.ID == OPTION_REQ)
  137.                     break;    /* break out of while */
  138.                 else
  139.                     pmode++;
  140.             } /* while */
  141.             if(result.ID == OPTION_REQ) {    /* have found an unknown option */
  142.                 /* this brings up the nifty requestor - check results */
  143.                 Do_Request(pmode, &result, OPTION_REQ);
  144.                 if(result.ID == NOTHING) {    /* ICK!  Intuition failed, use default exit */
  145.                     printf("%s: **FATAL ERROR** Unknown option \"%c\"\n", NAME, *pmode);
  146.                     fclose(dfptr);
  147.                     clean_up();
  148.                     print_usage();
  149.                 } /* if */
  150.                 else if(result.ID == RETRY_UP) {    /* user may have altered remaining argument */
  151.                     argv[i--] = result.string;
  152.                     continue;
  153.                 } /* else if */
  154.                 else if(result.ID == SKIP_UP) continue;    /* skip that argument */
  155.                 else if(result.ID == ABORT_UP) break;    /* abort entire printing session */
  156.                 else printf("%s: Bad gadget return result\n", NAME);
  157.             } /* if */
  158.         } /* if */
  159.         /* if it doesn't start with "-", assume it's a file to print */
  160.         else if((sfptr = fopen(pmode, "r")) == NULL) {    /* give user a chance to act on the file error */
  161.             /* spiffy requester */
  162.             Do_Request(pmode, &result, FILE_REQ);
  163.             if(result.ID == NOTHING) {    /* ICK! Intuition failed!  Use default user input */
  164.                 printf("%s: File \"%s\" not accessible.", NAME, pmode);
  165.                 if(i < argc - 1) {
  166.                     printf("  Continue (Y/N)? ");
  167.                     fgets(answer, 11, stdin);
  168.                     if(tolower(*answer) == 'n') break;
  169.                 } /* if */
  170.                 else puts("");
  171.             } /* if */
  172.             else if(result.ID == RETRY_UP) {    /* user may have retyped the filename */
  173.                 argv[i--] = result.string;
  174.                 continue;
  175.             } /* else if */
  176.             else if(result.ID == SKIP_UP) continue;    /* skip this file - go onto next argument */
  177.             else if(result.ID == ABORT_UP) break;    /* abort entire printing session */
  178.             else printf("%s: Bad gadget return result\n", NAME);
  179.         } /* else */
  180.         else {    /* OK. the file acces succeeded; print th' sukka */
  181.             if((source_file_buffer = (char *)malloc(SFB_SIZE)) == NULL
  182.               || setvbuf(sfptr, source_file_buffer, _IOFBF, SFB_SIZE) != NULL)
  183.                 printf("%s: Cannot set up source file buffer for \"%s\"\n", NAME, pmode);
  184.             while((ch = getc(sfptr)) != EOF) fprintf(dfptr, "%c", ch);
  185.             fclose(sfptr);
  186.         } /* else */
  187.     } /* for */
  188.     fclose(dfptr);
  189.     clean_up();
  190. } /* printcodes() */
  191.  
  192.  
  193. /*
  194. ** Searches down a string to find a valid number and
  195. ** returns the pointer of the string containing that number.
  196. */
  197. void exnum(char *p, char *digits) {
  198.     char *dptr;
  199.  
  200.     dptr = digits;
  201.     p++;
  202.     while(*p>='0' && *p<='9') *(dptr++) = *(p++);
  203.     *dptr = NULL;
  204. } /* exnum() */
  205.  
  206.  
  207. /* Frees the file buffers from memory. */
  208. void clean_up(void) {
  209.     if(source_file_buffer) free(source_file_buffer);
  210.     if(dest_file_buffer) free(dest_file_buffer);
  211. } /* clean_up() */
  212.  
  213.  
  214. /* Prints out error message and gives usage. */
  215. void print_usage(void) {
  216.     puts(VERSION_INFO);
  217.     puts("USAGE: pmod [-{68qdcnepfl###}] [file] [-{68qdcnepfl###}] [file]...");
  218.     puts("\t6 = Use 1/6\" line spacing");
  219.     puts("\t8 = Use 1/8\" line spacing");
  220.     puts("\tq = Near letter quality mode ON");
  221.     puts("\td = Draft printing mode (NLQ OFF)");
  222.     puts("\tc = Condensed printing mode");
  223.     puts("\tn = Cancel condensed printing mode");
  224.     puts("\te = Elite character style");
  225.     puts("\tp = Pica character style");
  226.     puts("\tf = Send Form Feed");
  227.     puts("\tl = Send ### Line Feeds");
  228.     puts("\tfile = Text file to print on printer");
  229.     exit(10);
  230. } /* print_usage() */
  231.