home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / other / cled122s / source / lprintdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-14  |  15.9 KB  |  460 lines

  1. #include "define.h"
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include "rpcfg.h"
  5.  
  6. /* GLOBAL variables, naughty but nice */
  7.  
  8. extern struct COMMAND print_codes[11];     /* Array to hold printer commands */
  9. extern char print_name[80];                /* Printers name */ 
  10. extern int coloura,quality;                 /* Have we got colour, nlq? 0=No */
  11. extern int driver_loaded;
  12.  
  13. #ifdef NO_ISXDIGIT
  14. int isxdigit(c)
  15. /**************************************************************************
  16.  Emulates ANSII standard function isxdigit, returns 0 if character is not
  17.  a valid hexidecimal digit, non-zero if it is valid.
  18. **************************************************************************/
  19. char c;
  20. {
  21.    if ( (isdigit(c)==0) && ((tolower(c)<'a')||(tolower(c)>'f')) ) return 0;
  22.    return 1;
  23. }
  24. #endif
  25.   
  26. void display_error(err,line)
  27. /****************************************************************************
  28. SUBROUTINE  :  display_error           VERSION  : 1.0   
  29. AUTHOR      :  ROBERT ALLEY            DATE     : 5/7/90  
  30. DESCRIPTION :  Display an error message
  31. LOGIC USED  :  STEP 1.  Based on the error code, build an error string,
  32.                         display it and return.
  33. RETURNS     :  none.  
  34. PARAMETERS  :  err      I           Error number (As defined in RPCFG.H)
  35.                line     I           Line error occured on (Start at 0)
  36. VARIABLES   :  mess[80] Char        String to hold error message
  37. CONSTANTS   :  None.
  38. REQD. FILES :  None.
  39. REQD. SUBPG.:  None.
  40. NOTES       :  None.
  41. USAGE       :  display_error(err_num,line_num);
  42. ****************************************************************************/
  43. int err,line;
  44. {
  45.    char mess[80];
  46.    
  47.    switch (err) {
  48.       case TOOLONG:
  49.          sprintf(mess,"[3][Error :Line too long|in line %d][OK]",line);
  50.          break;
  51.       case RANOFFE:
  52.          sprintf(mess,"[3][Error :Ran off the end|of file at line %d][OK]",line);
  53.          break;
  54.       case TOOBIGN:
  55.          sprintf(mess,"[3][Error :To many characters|in # at line %d][OK]",line);
  56.          break;
  57.       case NOMEMOR:
  58.          sprintf(mess,"[3][Error :Ran out of memory][OK]");
  59.          break;
  60.       case SYNTAXE:
  61.          sprintf(mess,"[3][Error :Syntax error|at line %d][OK]",line);
  62.          break;
  63.       default     :
  64.          sprintf(mess,"[3][Error in line %d][OK]",line);
  65.          break;
  66.    }
  67.    form_alert(1,mess);
  68. }
  69.  
  70.  
  71. int convert(line,commands)
  72. /****************************************************************************
  73. SUBROUTINE  :  convert                 VERSION  : 1.0   
  74. AUTHOR      :  ROBERT ALLEY            DATE     : 5/7/90 
  75. DESCRIPTION :  Convert a string of commands into a list of numbers and place
  76.                them in a command structure.
  77. LOGIC USED  :  STEP 1.  Get the next character in the string.  if its a $,
  78.                         go into hex mode, ignore if a space.
  79.                STEP 2.  If its a comma, put a 0 teminator on the end of the
  80.                         temporary string.  Convert the string to a number
  81.                         (either hex or decimal coversion).  if conversion
  82.                         didnt work or we have got too many commands, return 
  83.                         an error.  Zero hex flag and temporary counter.
  84.                STEP 3.  All other characters, if not in hex mode, check the 
  85.                         character is a digit, if not return an error.  If in
  86.                         hex mode check its a valid hex digit, if not return
  87.                         an error.  Stick character in temporary string, if
  88.                         string is too long, return an error.
  89.                STEP 4.  Repeat 1-3 until the end of the line is reached.
  90.                         Allocate enough space for the command list.  If 
  91.                         space cant be allocated, return an error.
  92.                         Copy the commands to the allocated space, fill in
  93.                         the length and return.
  94. RETURNS     :  Integer error status (Error codes defined in RPCFG.H) 0=OK.
  95. PARAMETERS  :  line      Char *      Line to parse
  96.                commands  COMMAND *   Structure to hold commands read
  97. VARIABLES   :  character Int         Current character being processed
  98.                count     Int         Current character number
  99.                hex       Int         Flag for hex mode, 1=Hex #
  100.                num       Int         Number of values converted
  101.                values[5] Int         array of command numbers
  102.                tcount    I           Number of characters in temp string
  103.                temp[80]  Char        Temporary string
  104.                error     I           Error status returned by sscanf.
  105. CONSTANTS   :  None.
  106. REQD. FILES :  None.
  107. REQD. SUBPG.:  None. 
  108. NOTES       :  None.
  109. USAGE       :  error_status=convert(line_to_convert,commands_found);
  110. ****************************************************************************/
  111. char *line;
  112. struct COMMAND *commands;
  113. {
  114.    int character;       /* char being processed */
  115.    int count=0;         /* current char number */
  116.    int hex=0;           /* flag for hexi number */
  117.    int num=0;           /* number of values stroed */
  118.    int values[MAXLEN];  /* holds converted numbers */
  119.    int tcount=0;        /* number of chars in temp */
  120.    char temp[MAXCHA];   /* temp holder for numbers (as text) */
  121.    int error;           /* error status returned by sscanf */
  122.  
  123.    
  124.    
  125. /* STEP 1. */
  126.  
  127.    do {
  128.       character=line[count++];
  129.       switch (character) {
  130.          case '$' :
  131.             hex=1;
  132.             break;
  133.          case ' ' :
  134.             break;
  135.          case 0   :
  136.  
  137. /* STEP 2. */
  138.  
  139.          case ',' :
  140.             temp[tcount]=0;         /* Put terminator on end */
  141.             if (hex==1) {
  142.                error=sscanf(temp,"%x",&values[num++]);
  143.             }
  144.             else {
  145.                error=sscanf(temp,"%d",&values[num++]);
  146.             }
  147.             if (error!=1)  return(SYNTAXE);
  148.             if (num >= MAXLEN) return(TOOLONG);
  149.             hex=tcount=0;
  150.             break;
  151.  
  152. /* STEP 3. */
  153.  
  154.          default :
  155.             if (hex==0)
  156.                if ( (isdigit(character))==0)   return(SYNTAXE);
  157.             if (hex==1)
  158.                if ( (isxdigit(character))==0)  return(SYNTAXE);
  159.             temp[tcount++]=character;
  160.             if (tcount >= MAXCHA) return(TOOBIGN);
  161.             break;
  162.        }
  163.        
  164. /* STEP 4. */
  165.  
  166.    } while (character != 0);
  167.    if ( (commands->list=(int *)malloc(num*sizeof(int)))==NULL) 
  168.       return(NOMEMOR);
  169.    else 
  170.       memcpy(commands->list,values,num*sizeof(int));
  171.    commands->length=num;
  172.    return(0);
  173. }
  174.  
  175. read_line(infile,line,linen)
  176. /****************************************************************************
  177. SUBROUTINE  :  read_line               VERSION  : 1.0   
  178. AUTHOR      :  ROBERT ALLEY            DATE     :  
  179. DESCRIPTION :  Read a line from a printer config file.  Ignores characters 
  180.                after a semi-colon (;) (Comments).  Ignores blank lines.  
  181.                Leading spaces not placed in string. 
  182. LOGIC USED  :  STEP 1.   While the finished flag hasnt been set.
  183.                            Get a character from the file stream.
  184.                STEP 2.     If its a ';', read in the rest of the line,
  185.                            increment line counter.
  186.                            If its a space, ignore it.
  187.                            If its a new line, increment line number.
  188.                            if its the EOF, return error status.
  189.                STEP 3.     Put the character in the string.
  190.                            While the characters not EOL, ';' or newline:
  191.                               put it in the string,
  192.                               read in a new character.
  193.                            If last character was ';', read in the rest of the
  194.                            line.
  195.                            Increment line counter, set finished flag.
  196.                STEP 4.   Put 0 at end of string, return 0.
  197. RETURNS     :  Integer error string, 0 for no error.
  198. PARAMETERS  :  infile   FILE *      File to read from (assumed open)
  199.                line     char *      Pointer to string to fill
  200.                linen    I *         Line number
  201. VARIABLES   :  character I          Current character being processed
  202.                finished I           Flag for finished reading 1=YES
  203.                count    I           Number of characters put in string.
  204. CONSTANTS   :  None.
  205. REQD. FILES :  Whatever infile refers to.
  206. REQD. SUBPG.:  None.
  207. NOTES       :  None.
  208. USAGE       :  error=readline(FILE *infile,char *line,int *linen);
  209. ****************************************************************************/
  210. FILE *infile;
  211. char *line;
  212. int *linen;
  213.  
  214. {
  215.    int character;       /* current character */
  216.    int finished=0;      /*  finished yet ??? */
  217.    int count=0;
  218.    
  219. /* STEP 1. */
  220.  
  221.    while (finished==0) {
  222.  
  223.       character=getc(infile);
  224.       switch (character) {
  225.  
  226. /* STEP 2. */
  227.             
  228.          case ';':
  229.             while (character != '\n') 
  230.                character=getc(infile); 
  231.             (*linen)++;
  232.             break;
  233.  
  234.          case ' ':      /* Basically ignore it ! */
  235.             break;
  236.  
  237.          case '\n':
  238.             (*linen)++;
  239.             break;
  240.             
  241.          case EOF :
  242.             return(RANOFFE);
  243.             break;
  244.             
  245. /* STEP 3. */
  246.  
  247.          default :
  248.             while ((character !=';') && (character !='\n') && (character != EOF)) {
  249.                line[count++]=character;
  250.                if (count >= MAXLEN) return (TOOLONG);
  251.                character=getc(infile);
  252.             }
  253.             if (character==';')
  254.                while(character != '\n') character=getc(infile);
  255.             (*linen)++;
  256.             finished=1;
  257.             break;
  258.  
  259.       }  
  260.    }    
  261.  
  262. /* STEP 4. */
  263.  
  264.    line[count]=0;                    
  265.    return(0);
  266.    
  267. }
  268.  
  269.  
  270.  
  271. int rpd(name,colour,nlq,driver)
  272. /****************************************************************************
  273. SUBROUTINE  :  rpd                     VERSION  : 1.0   
  274. AUTHOR      :  ROBERT ALLEY            DATE     : 5/7/90 
  275. DESCRIPTION :  Read printer details
  276. LOGIC USED  :  STEP 1.  Open file, if we cant return an error.
  277.                STEP 2.  Get a line, return if an error occurs, check for 
  278.                         valid version number, return error if not.
  279.                STEP 3.  Get the printer name, return if an error occurs.
  280.                STEP 4.  Read a line, returning on error, if its a one weve
  281.                         got a colour printer so read and convert the three
  282.                         colour commands. 
  283.                STEP 5.  Read and convert line 3-4.
  284.                STEP 6.  read a line, if its a one weve got a NLQ printer so
  285.                         read the nlq on/off commands.
  286.                STEP 7.  Read an convert line 7-10.
  287. RETURNS     :  Integer error number, 0=No error.
  288. PARAMETERS  :  name        Char *   String to hold printer name
  289.                colour      Int *    Is printer colour? 0=No 1=Yes
  290.                nlq         int *    Is printer NLq 0=No, 1=Yes
  291. VARIABLES   :  driver      FILE *   File to read from
  292.                line[80]    Char     Current line read
  293.                error       Int      Error code, 0=OK
  294.                linen       Int      Current line number
  295.                comnum      Int      Command number
  296. CONSTANTS   :  None.
  297. REQD. FILES :  None.
  298. REQD. SUBPG.:  read_line
  299.                convert
  300.                display_error
  301. NOTES       :  Line numbers start at 0.
  302. USAGE       :  error_status=rpd(printer_name,is_colour,is_nlq);
  303. ****************************************************************************/
  304. int *colour,*nlq;
  305. char *name;
  306. FILE *driver;
  307. {
  308.    char line[80];                   /* line read */
  309.    int error,linen=0;               /* error code, linenumber */
  310.    int comnum;                      /* command number being processed */
  311.                 
  312. /* STEP 2. */
  313.         
  314.    error=read_line(driver,line,&linen);     /* get first line **/
  315.    if (error != 0) {                           /* if error, display and abort */
  316.       display_error(error,linen);
  317.       return(1);
  318.    }
  319.    if (strcmp(line,"CLED1.2") != 0) {       /* is it a valid version ? */
  320.       form_alert(1,"[3][Unrecognised version| of driver][OK]");
  321.       return(1);
  322.    }
  323.  
  324. /* STEP 3. */
  325.         
  326.    error=read_line(driver,name,&linen);    /* get name of printer */
  327.    if (error != 0) {                          /* if error, display and abort */
  328.       display_error(error,linen);
  329.       return(1);
  330.    }
  331.  
  332. /* STEP 4. */
  333.  
  334.    error=read_line(driver,line,&linen);   /* get 1st command, colour mode */
  335.       if (error != 0) {                      /* if error, display and abort */
  336.          display_error(error,linen);
  337.          return(1);
  338.       }
  339.       
  340.     if (tolower(line[0])=='y') {            /* if printer is colour */
  341.        *colour=1;                             /* set colour flag */
  342.        for (comnum=0;comnum<=2;comnum++) {   /* get next 3 commands */
  343.          error=read_line(driver,line,&linen);
  344.          if (error != 0) {
  345.             display_error(error,linen);
  346.             return(1);
  347.          }
  348.          if ( (error=convert(line,&print_codes[comnum])) != 0) {
  349.             display_error(error,linen);
  350.             return(1);
  351.          }
  352.       }
  353.     }
  354.     else *colour=0;
  355.  
  356. /* STEP 5. */
  357.                     
  358.    for (comnum=3;comnum<=4;comnum++) {       /* get commands 3 and 4 */
  359.       error=read_line(driver,line,&linen);
  360.       if (error != 0) {
  361.          display_error(error,linen);
  362.          return(1);
  363.       }
  364.       if ( (error=convert(line,&print_codes[comnum])) != 0) {
  365.          display_error(error,linen);
  366.          return(1);
  367.       }
  368.    }
  369.  
  370. /* STEP 6. */
  371.    
  372.    error=read_line(driver,line,&linen);      /* get nlq mode */
  373.       if (error != 0) {
  374.          display_error(error,linen);
  375.          return(1);
  376.       }
  377.       
  378.     if (tolower(line[0])=='y') {               /* if it does nlq */
  379.        *nlq=1;                                  /* set the flag */
  380.        for (comnum=5;comnum<=6;comnum++) {      /* read nlq on/off cammands */
  381.          error=read_line(driver,line,&linen);
  382.          if (error != 0) {
  383.             display_error(error,linen);
  384.             return(1);
  385.          }
  386.          if ( (error=convert(line,&print_codes[comnum])) != 0) {
  387.             display_error(error,linen);
  388.             return(1);
  389.          }
  390.       }
  391.     }
  392.     else *nlq=0;
  393.     
  394. /* STEP 7. */
  395.                     
  396.    for (comnum=7;comnum<=10;comnum++) {      /* get commands 7-10 */
  397.       error=read_line(driver,line,&linen);
  398.       if (error != 0) {
  399.          display_error(error,linen);
  400.          return(1);
  401.       }
  402.       if ( (error=convert(line,&print_codes[comnum])) != 0) {
  403.          display_error(error,linen);
  404.          return(1);
  405.       }
  406.    }
  407.  
  408. }
  409.  
  410. load_prn_drv()
  411. {
  412.    FILE *p_driver;
  413.  
  414.    if ( (p_driver=fopen("print.drv","r"))==NULL) {
  415.       form_alert(1,"[3][ ERROR|Unable to load driver|Cant print][OK]");
  416.       driver_loaded=0;
  417.    }
  418.    else {
  419.       if ( (driver_loaded=rpd(print_name,&coloura,&quality,p_driver))!=0) {
  420.          form_alert(1,"[3][ ERROR|Unable to load driver|Cant print][OK]");
  421.          driver_loaded=0;
  422.       }
  423.       else driver_loaded=1;
  424.    fclose(p_driver);
  425.    }
  426. }
  427.  
  428. load_driver(directory)
  429. char *directory;
  430. {
  431.    FILE *read;
  432.    int abort,ok;
  433.    static char file[128]="";
  434.  
  435.    abort=0;
  436.    while (abort==0) {
  437.       abort = select(file,directory);
  438.       if (abort ==0) {
  439.          if ( (read=fopen(file,"r"))==(FILE *)NULL) {
  440.             abort=form_alert(1,"[3][Error opening file][Abort|Continue]");
  441.             if (abort==2) abort=0;
  442.          }
  443.          else
  444.             abort=2;
  445.       }
  446.    }
  447.    if (abort != 1) {
  448.       if ( (ok=rpd(print_name,&coloura,&quality,read))!=0) {
  449.          if (driver_loaded==0)
  450.             form_alert(1,"[3][  ERROR|Unable to load driver| Cant print][OK]");
  451.          else
  452.             form_alert(1,"[3][  ERROR|Unable to load driver| old driver used][OK]");
  453.       }
  454.       else driver_loaded=1;
  455.    fclose(read);
  456.    }
  457. }
  458.  
  459.  
  460.