home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / ENGINE / MAP_OUT.C < prev    next >
C/C++ Source or Header  |  1991-12-16  |  12KB  |  271 lines

  1. /*
  2. *    map_out.c
  3. *
  4. *   Output mapping functions for the real screen code
  5. *
  6. *   Quincey Koziol
  7. *
  8. *    Date        Notes
  9. *    --------------------------------------------
  10. *    11/90        Started
  11. */
  12.  
  13. /*
  14. * Includes
  15. */
  16.  
  17. #define OUTPUTMASTER
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <io.h>
  23. #include <ctype.h>
  24. #ifdef MSC
  25. #ifdef __TURBOC__
  26. #include <alloc.h>
  27. #else
  28. #include <malloc.h>
  29. #endif
  30. #endif
  31. #ifdef __TURBOC__
  32. #include "turboc.h"
  33. #endif
  34. #include "vskeys.h"
  35. #include "externs.h"
  36. #include "map_out.h"
  37.  
  38. #define MAX_LINE_LENGTH    160        /* the maximum length of a line in the output mapping file */
  39.  
  40. /* Local functions */
  41. static unsigned int parse_str(char *file_str);
  42. static void add_output(unsigned int search_key,unsigned int key_map_code);
  43. static void del_output(unsigned int search_key);
  44.  
  45. /* Local variables */
  46. static char white_sp[]="\x009\x00a\x00b\x00c\x00d\x020";    /* string which contains all the white space characters */
  47. #ifdef OLD_WAY
  48. static char end_token[]="\x000\x009\x00a\x00b\x00c\x00d\x020};";    /* string which contains all the white space characters, and the right curley brace & the semi-colon */
  49. #endif
  50.  
  51. /**********************************************************************
  52. *  Function :    del_output
  53. *  Purpose    :    delete a node from the list of mapped keys,
  54. *                basicly just replaces the key code in the array with the
  55. *                array index.
  56. *  Parameters    :
  57. *            search_key - the keycode to delete from the list
  58. *  Returns    :    none
  59. *  Calls    :    none
  60. *  Called by    :    read_keyboard_file()
  61. **********************************************************************/
  62. static void del_output(unsigned int search_key)
  63. {
  64.     if(search_key<256)
  65.         outputtable[search_key]=(unsigned char)search_key;
  66. }    /* end del_output() */
  67.  
  68. /**********************************************************************
  69. *  Function :    add_output
  70. *  Purpose    :    add a node to the list of mapped output characters
  71. *  Parameters    :
  72. *            search_key - the character code to add to the list
  73. *            key_map_code - the character code to map the key to
  74. *  Returns    :    none
  75. *  Calls    :    none
  76. *  Called by    :    read_keyboard_file()
  77. **********************************************************************/
  78. static void add_output(unsigned int search_key,unsigned int key_map_code)
  79. {
  80.     if(search_key<256)
  81.         outputtable[search_key]=(unsigned char)key_map_code;
  82. }    /* end add_output() */
  83.  
  84. /**********************************************************************
  85. *  Function    :    parse_str()
  86. *  Purpose    :    parse the string to map a key to.
  87. *  Parameters    :
  88. *            file_str - pointer to a string from the file to parse
  89. *  Returns    :    0xFFFF for error, otherwise, the vt100 code for telbin
  90. *  Calls    :    none
  91. *  Called by    :    read_keyboard_file()
  92. **********************************************************************/
  93. static unsigned int parse_str(char *file_str)
  94. {
  95.     unsigned int ret_code;    /* the code to return from this function */
  96.     int done=0;             /* flag for dropping out of the loop */
  97.     byte *temp_str;         /* pointer to the current place in the string */
  98.  
  99.     ret_code=0xFFFF;       /* mark return code to indicate nothing special to return */
  100.     temp_str=file_str;        /* start at the beginning of the string to parse */
  101.     while((*temp_str)!='\0' && !done) {     /* parse until the end of the string or until the buffer is full */
  102.         if((*temp_str) && (*temp_str)!=';' && (*temp_str)!='\\' && (*temp_str)!='{' && (*temp_str)>' ') {    /* copy regular characters until a special one is hit */
  103.             ret_code=(*temp_str);    /* copy the character */
  104.             done=1;                 /* stop parsing the string */
  105.           }    /* end while */
  106.         if(*temp_str) {        /* check on the various special cases for dropping out of the loop */
  107.             switch(*temp_str) {        /* switch for the special case */
  108.                 case '\\':    /* backslash for escape coding a character */
  109.                     temp_str++;        /* get the character after the backslash */
  110.                     if((*temp_str)=='{') {    /* check for curly brace around numbers */
  111.                         temp_str++;    /* increment to the next character */
  112.                         if((*temp_str)=='o') /* check for octal number */
  113.                             ret_code=octal_to_int(temp_str);   /* get the acii number after the escape code */
  114.                         else if((*temp_str)=='x') /* check for hexadecimal number */
  115.                             ret_code=hex_to_int(temp_str); /* get the acii number after the escape code */
  116.                         else {        /* must be an escape integer */
  117.                             if((*temp_str)=='d')    /* check for redundant decimal number specification */
  118.                                 temp_str++;
  119.                             ret_code=atoi(temp_str);   /* get the ascii number after the escape code */
  120.                           }    /* end else */
  121.                       }    /* end if */
  122.                     else if((*temp_str)=='o') /* check for octal number */
  123.                         ret_code=octal_to_int(temp_str);   /* get the acii number after the escape code */
  124.                     else if((*temp_str)=='x') /* check for hexadecimal number */
  125.                         ret_code=hex_to_int(temp_str); /* get the acii number after the escape code */
  126.                     else {        /* must be an escape integer */
  127.                         if((*temp_str)=='d')    /* check for redundant decimal number specification */
  128.                             temp_str++;
  129.                         ret_code=atoi(temp_str);   /* get the ascii number after the escape flag */
  130.                       }    /* end else */
  131.                     done=1;
  132.                     break;
  133.  
  134.                 case '{':    /* curly brace opens a quoted string */
  135.                     temp_str++;        /* jump over the brace itself */
  136.                     if((*temp_str) && (*temp_str)!='}')    /* copy regular characters until a special one is hit */
  137.                         ret_code=(*temp_str);        /* copy the character */
  138.                     done=1;         /* drop out of the loop */
  139.                     break;
  140.  
  141.                 default:    /* ctrl characters, space, and semi-colon terminate a string */
  142.                     done=1;
  143.                     break;
  144.  
  145.               }    /* end switch */
  146.           }    /* end if */
  147.         else
  148.             done=1;
  149.       }    /* end while */
  150.     return(ret_code);
  151. }    /* end parse_str() */
  152.  
  153. /**********************************************************************
  154. *  Function    :    read_output_file
  155. *  Purpose    :    read in a output mapping file, parse the input and 
  156. *                map the output characters
  157. *  Parameters    :
  158. *            output_file - string containing the name of the output mapping file
  159. *  Returns    :    0 for no error, -1 for any errors which occur
  160. *  Calls    :    parse_str(), & lots of library string functions
  161. *  Called by    :    initoutputfile(), Sconfile()
  162. **********************************************************************/
  163. int read_output_file(char *output_file)
  164. {
  165.     FILE *output_fp;        /* pointer to the keyboard file */
  166.     char output_line[MAX_LINE_LENGTH],    /* static array to store lines read from keyboard file */
  167.         *temp_str;            /* temporary pointer to a string */
  168.     uint line_no=0,            /* what line in the file we are on */
  169.         map_code,            /* the output character to re-map to */
  170.         token_num,            /* the current token we are parsing */
  171.         where,                /* pointer to the beginning of text */
  172.         re_map_key,            /* the key to re-map */
  173.         error=0;            /* error from the file reading */
  174.  
  175.     if((output_fp=fopen(output_file,"rt"))!=NULL) {
  176.         while((temp_str=fgets(output_line,MAX_LINE_LENGTH,output_fp))!=NULL && !error) {    /* get a line of input */
  177.             token_num=0;            /* initialize the token we are on */
  178.             if((temp_str=strtok(output_line,white_sp))!=NULL) {    /* get the first token from the string */
  179.                 if((*temp_str)!=';') {        /* check for a comment line */
  180.                     do {
  181.                         switch(token_num) {        /* switch on which token we are processing */
  182.                             case 0:        /* the 'SET' token (we already know it is not a comment) */
  183.                                 if(stricmp(temp_str,"SET")) {    /* make certain the first token is a SET token */
  184.                                     printf("invalid token #%d:'%s' on line %d\n",token_num,temp_str,line_no);
  185.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  186.                                   }    /* end if */
  187.                                 break;
  188.  
  189.                             case 1:        /* the 'KEY' token */
  190.                                 if(stricmp(temp_str,"KEY")) {    /* make certain the first token is a KEY token */
  191.                                     printf("invalid token #%d:'%s' on line %d\n",token_num,temp_str,line_no);
  192.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  193.                                   }    /* end if */
  194.                                 break;
  195.  
  196.                             case 2:        /* the key to be re-mapped */
  197.                                 if(!stricmp(temp_str,"CLEAR") || !stricmp(temp_str,"OFF") || !stricmp(temp_str,"ON") || (*temp_str)==';') {    /* ignore the rest of the line if the 'key' is one of the tokens we don't support */
  198.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  199.                                   }    /* end if */
  200.                                 else {        /* the 'key' field is not a special command or a comment, must be a valid character */
  201.                                     if(*(temp_str)!='\\') {    /* the key to re-map is not an escape code */
  202.                                         re_map_key=*temp_str;    /* set the re-mapping key */
  203.                                       }    /* end if */
  204.                                     else {
  205.                                         re_map_key=atoi(temp_str+1);    /* get the re-mapping key value from the string */
  206.                                         if(re_map_key==0) {        /* invalid key code */
  207.                                             printf("Error, invalid key code:%s, on line %d\n",temp_str,line_no);
  208.                                             token_num=4;    /* bump the token count up to drop out of the loop */
  209.                                           }    /* end if */
  210.                                       }    /* end else */
  211.                                   }    /* end else */
  212.                                 break;
  213.  
  214.                             case 3:        /* the string to re-map the key to */
  215.                                 if((*temp_str)==';') {    /* ignore the rest of the line if the 'key' is one of the tokens we don't support */
  216.                                     outputtable[re_map_key]=(unsigned char)re_map_key;       /* reset the output mapping definition */
  217.                                     token_num=4;    /* bump the token count up to drop out of the loop */
  218.                                   }    /* end if */
  219.                                 else {
  220.                                     if((map_code=parse_str(temp_str))!=0xFFFF) {      /* parse the re-mapping string */
  221.                                         add_output(re_map_key,map_code);  /* add a regular key string to the key mapping list */
  222.                                       }    /* end if */
  223.                                     else {
  224.                                         del_output(re_map_key);
  225.                                         printf("Error, re-mapping string:%s invalid on line %d\n",temp_str,line_no);
  226.                                         token_num=4;    /* bump the token count up to drop out of the loop */
  227.                                       } /* end else */
  228.                                   }    /* end else */
  229.                                 break;
  230.                           }    /* end switch */
  231.                         token_num++;
  232.                         if(token_num<3)        /* if the next token is not the last one, then grab it */
  233.                             temp_str=strtok(NULL,white_sp);        /* get the next token */
  234.                         else if(token_num==3) {        /* for the last 'token' (after the remapping key to the end of the line), just get the next character in the line */
  235.                             temp_str+=(strlen(temp_str)+1);        /* jump over the previous token */
  236.                             where=strspn(temp_str,white_sp);    /* look for the first non-white space in the line */
  237.                             temp_str+=where;    /* jump to the first position with a character */
  238.                             if(!isgraph(*temp_str)) {        /* not more characters in the line */
  239.                                 del_output(re_map_key);
  240.                                 token_num=4;    /* bump the token count to drop out of the loop */
  241.                               }    /* end if */
  242.                           }    /* end if */
  243.                       }    while(temp_str!=NULL && token_num<4);
  244.                   }    /* end if */
  245.               }    /* end if */
  246.             line_no++;            /* increment current line */
  247.           }    /* end while */
  248.         fclose(output_fp);
  249.       }    /* end if */
  250.     else
  251.         error=(-1);        /* indicate an error */
  252.     return(error);
  253. }   /* end read_output_file() */
  254.  
  255. /**********************************************************************
  256. *  Function    :    initoutputfile()
  257. *  Purpose    :    initialize the default output mappings
  258. *  Parameters    :    none
  259. *  Returns    :    none
  260. *  Calls    :    none
  261. *  Called by    :    main()
  262. **********************************************************************/
  263. void initoutputfile(void )
  264. {
  265.     int i;                /* local counting variable */
  266.  
  267.     for(i=0; i<256; i++)    /* just set the mappings in the table to their one code */
  268.         outputtable[i]=(unsigned char)i;
  269. }    /* end initoutputfile() */
  270.  
  271.