home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / word / text / 024 / cfgfile.c < prev    next >
C/C++ Source or Header  |  1993-06-04  |  32KB  |  939 lines

  1. /*
  2.  * This module contains all the routines needed to redefine key, colors, and
  3.  *   modes via a configuration file.
  4.  *
  5.  * Program Name:  tdecfg
  6.  * Author:        Frank Davis
  7.  * Date:          June 5, 1992
  8.  */
  9.  
  10.  
  11. #include <io.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include "tdecfg.h"
  17. #include "cfgfile.h"
  18.  
  19.  
  20. /*
  21.  * reference the structures in config files.
  22.  */
  23. extern struct vcfg cfg;
  24. extern FILE *tde_exe;                  /* FILE pointer to tde.exe */
  25. extern KEY_FUNC key_func;
  26. extern MACRO macros;
  27. extern COLORS temp_colours;
  28. extern MODE_INFO in_modes;
  29. extern long keys_offset;
  30. extern long two_key_offset;
  31. extern long macro_offset;
  32. extern long color_offset;
  33. extern long mode_offset;
  34. extern long sort_offset;
  35.  
  36.  
  37.  
  38. char line_in[2000];             /* line buffer */
  39. int  stroke_count;              /* global variable for macro strokes */
  40. unsigned int line_no;           /* global variable for line count */
  41. int modes[NUM_MODES];
  42.  
  43.  
  44. TWO_KEY two_key_list;
  45.  
  46. SORT_ORDER sort_order;
  47.  
  48.  
  49. /*
  50.  * Name:    tdecfgfile
  51.  * Date:    June 5, 1992
  52.  * Notes:   read in a configuration file
  53.  */
  54. void tdecfgfile( void )
  55. {
  56. FILE *config;
  57. char fname[80];
  58. int  rc;
  59.  
  60.    /*
  61.     * prompt for the configuration file name.
  62.     */
  63.    cls( );
  64.    xygoto( 0, 3 );
  65.    puts( "Enter configuration file name, e.g. tde.cfg  :" );
  66.    gets( fname );
  67.  
  68.    if (strlen( fname ) != 0) {
  69.       rc = OK;
  70.       if ((rc = access( fname, EXIST )) != 0) {
  71.          puts( "\n\n Error: File not found." );
  72.          getkey( );
  73.          rc = ERROR;
  74.       } else if ((config = fopen( fname, "r" )) == NULL ) {
  75.          puts( "\n\nError: Cannot open configuration file." );
  76.          getkey( );
  77.          rc = ERROR;
  78.       }
  79.  
  80.       /*
  81.        * if everything is everthing so far, get the current editor settings.
  82.        */
  83.       if (rc == OK) {
  84.          fseek( tde_exe, keys_offset, SEEK_SET );
  85.          fread( (void *)&key_func, sizeof(KEY_FUNC), 1, tde_exe );
  86.          fseek( tde_exe, two_key_offset, SEEK_SET );
  87.          fread( (void *)&two_key_list, sizeof(TWO_KEY), 1, tde_exe );
  88.          fseek( tde_exe, macro_offset, SEEK_SET );
  89.          fread( (void *)¯os, sizeof(MACRO), 1, tde_exe );
  90.          fseek( tde_exe, color_offset, SEEK_SET );
  91.          fread( (void *)&temp_colours, sizeof(COLORS), 1, tde_exe );
  92.          fseek( tde_exe, mode_offset, SEEK_SET );
  93.          fread( (void *)&in_modes, sizeof( MODE_INFO ), 1, tde_exe );
  94.          fseek( tde_exe, sort_offset, SEEK_SET );
  95.          fread( (void *)&sort_order, sizeof( SORT_ORDER ), 1, tde_exe );
  96.  
  97.          stroke_count = get_stroke_count( );
  98.  
  99.          /*
  100.           * put the current modes into an array.  it's easier to work
  101.           *   with them in an array.  This is from cfgmode.c.
  102.           */
  103.          modes[Ins]         = in_modes.insert;
  104.          modes[Ind]         = in_modes.indent;
  105.          modes[PTAB]        = in_modes.ptab_size;
  106.          modes[LTAB]        = in_modes.ltab_size;
  107.          modes[Smart]       = in_modes.smart_tab;
  108.          modes[Write_Z]     = in_modes.control_z;
  109.          modes[Crlf]        = in_modes.crlf;
  110.          modes[Trim]        = in_modes.trailing;
  111.          modes[Eol]         = in_modes.show_eol;
  112.          modes[WW]          = in_modes.word_wrap;
  113.          modes[Left]        = in_modes.left_margin;
  114.          modes[Para]        = in_modes.parg_margin;
  115.          modes[Right]       = in_modes.right_margin;
  116.          modes[Size]        = in_modes.cursor_size;
  117.          modes[Backup]      = in_modes.do_backups;
  118.          modes[Ruler]       = in_modes.ruler;
  119.          modes[Date]        = in_modes.date_style;
  120.          modes[Time]        = in_modes.time_style;
  121.          modes[InflateTabs] = in_modes.inflate_tabs;
  122.          modes[Initcase]    = in_modes.search_case;
  123.          modes[JustRM]      = in_modes.right_justify;
  124.  
  125.          line_no = 1;
  126.          while (!feof( config )) {
  127.             if (fgets( line_in, 1500, config ) == NULL)
  128.                break;
  129.             parse_line( line_in );
  130.             ++line_no;
  131.          }
  132.  
  133.          /*
  134.           *  if we changed any modes, reset them b4 we write them to tde.exe.
  135.           */
  136.          in_modes.insert        = modes[Ins];
  137.          in_modes.indent        = modes[Ind];
  138.          in_modes.ptab_size     = modes[PTAB];
  139.          in_modes.ltab_size     = modes[LTAB];
  140.          in_modes.smart_tab     = modes[Smart];
  141.          in_modes.control_z     = modes[Write_Z];
  142.          in_modes.crlf          = modes[Crlf];
  143.          in_modes.trailing      = modes[Trim];
  144.          in_modes.show_eol      = modes[Eol];
  145.          in_modes.word_wrap     = modes[WW];
  146.          in_modes.left_margin   = modes[Left];
  147.          in_modes.parg_margin   = modes[Para];
  148.          in_modes.right_margin  = modes[Right];
  149.          in_modes.cursor_size   = modes[Size];
  150.          in_modes.do_backups    = modes[Backup];
  151.          in_modes.ruler         = modes[Ruler];
  152.          in_modes.date_style    = modes[Date];
  153.          in_modes.time_style    = modes[Time];
  154.          in_modes.inflate_tabs  = modes[InflateTabs];
  155.          in_modes.search_case   = modes[Initcase];
  156.          in_modes.right_justify = modes[JustRM];
  157.  
  158.          fseek( tde_exe, keys_offset, SEEK_SET );
  159.          fwrite( (void *)&key_func, sizeof(KEY_FUNC), 1, tde_exe );
  160.          fseek( tde_exe, two_key_offset, SEEK_SET );
  161.          fwrite( (void *)&two_key_list, sizeof(TWO_KEY), 1, tde_exe );
  162.          fseek( tde_exe, macro_offset, SEEK_SET );
  163.          fwrite( (void *)¯os, sizeof(MACRO), 1, tde_exe );
  164.          fseek( tde_exe, color_offset, SEEK_SET );
  165.          fwrite( (void *)&temp_colours, sizeof(COLORS), 1, tde_exe );
  166.          fseek( tde_exe, mode_offset, SEEK_SET );
  167.          fwrite( (void *)&in_modes, sizeof( MODE_INFO ), 1, tde_exe );
  168.          fseek( tde_exe, sort_offset, SEEK_SET );
  169.          fwrite( (void *)&sort_order, sizeof( SORT_ORDER ), 1, tde_exe );
  170.          fclose( config );
  171.          printf( "\n\n    Configuration file read.  Press a key to continue :" );
  172.          getkey( );
  173.       }
  174.    }
  175.    cls( );
  176. }
  177.  
  178.  
  179. /*
  180.  * Name:    parse_line
  181.  * Purpose: real work horse of the configuration utility, figure out what
  182.  *          we need to do with each line of the config file.
  183.  * Date:    June 5, 1992
  184.  * Passed:  line:  line that contains the text to parse
  185.  */
  186. void parse_line( char *line )
  187. {
  188. char key[1042];         /* buffer to hold any token that we parse */
  189. char *residue;          /* pointer to next item in line, if it exists */
  190. int key_no;             /* index into key array */
  191. int parent_key;         /* 1st of two-combination keys */
  192. int color;              /* color field */
  193. int mode_index;         /* index in mode array */
  194. int func_no;            /* function number we want to assign to a key */
  195. int color_no;           /* attribute we want to assign to a color field */
  196. int mode_no;            /* mode number we want to assign to a mode */
  197. int found;              /* boolean, did we find a valid key, color, or mode? */
  198. int i;
  199.  
  200.    /*
  201.     * find the first token and put it in key.  residue points to next token.
  202.     */
  203.    residue = parse_token( line, key );
  204.    if (*key != '\0' && *key != ';') {
  205.       if (strlen( key ) > 1) {
  206.          /*
  207.           * try to find a valid key
  208.           */
  209.          found = FALSE;
  210.          key_no = search( key, valid_keys, AVAIL_KEYS-1 );
  211.          if (key_no != ERROR) {
  212.             /*
  213.              * find the function assignment
  214.              */
  215.             found = TRUE;
  216.             if (residue != NULL) {
  217.                residue = parse_token( residue, key );
  218.  
  219.                /*
  220.                 * if this is not a comment, find the function to assign
  221.                 *   to key.  clear any previous macro or key assignment.
  222.                 */
  223.                if (*key != '\0' && *key != ';') {
  224.                   func_no = search( key, valid_func, NUM_FUNC );
  225.                   if (func_no != ERROR) {
  226.                      clear_previous_twokey( key_no );
  227.                      clear_previous_macro( key_no );
  228.                      key_func.key[key_no] = func_no;
  229.