File: cfg.c

package info (click to toggle)
latex2rtf 1.8aa-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,300 kB
  • ctags: 535
  • sloc: ansic: 5,434; makefile: 231; sh: 4
file content (461 lines) | stat: -rw-r--r-- 13,265 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * $Id: cfg.c,v 1.7 1998/11/12 15:15:42 glehner Exp $
 * History:
 * $Log: cfg.c,v $
 * Revision 1.7  1998/11/12 15:15:42  glehner
 * Cleaned up includes, moved from .h file to .c
 * added #ifndef __CFG_H ....
 *
 * Revision 1.6  1998/07/03 07:01:28  glehner
 * added ReadLg() for language.cfg files
 * fixed open_cfg search path parsing
 *
 * Revision 1.5  1997/02/15 21:17:42  ralf
 * Created default for environment separator.
 *
 * Revision 1.4  1997/02/15 21:10:45  ralf
 * Added environment separator ENVSEP (uses ';' for DOS)
 *
 * Revision 1.3  1997/02/15 20:36:29  ralf
 * Almost complete rewrite of config file reading.
 * The interface was made cleaner, there are no external functions
 * that access internal data structures now.
 * The opening of config files was also cleaned up.
 * There was a bug fix for parsing of the environment settings
 * that prevented some directories from being found on second
 * parsing. This was reported by L. Mugnier and there was a proposed fix
 * by V. Menkov.
 *
 * Revision 1.2  1995/05/24 11:54:03  ralf
 * Removed an off-by-one malloc error
 *
 * Revision 1.1  1995/03/23  16:09:01  ralf
 * Initial revision
 *
 */
/*****************************************************************************
     name : cfg.c
    autor : Ralf Schlatterbeck
  purpose : Read config files and provide lookup routines
 *****************************************************************************/


/****************************** includes *************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "cfg.h"
#include "util.h"
#include "main.h"
/****************************************************************************/



/******************************* typedefs & structures **********************/
typedef struct ConfigInfoT
{
    /*@observer@*/        char          *filename;
    /*@owned@*//*@null@*/ ConfigEntryT **config_info;
                          size_t         config_info_size;
			  bool           remove_leading_backslash;
} ConfigInfoT;
/****************************************************************************/


/********************************* global variables *************************/
static ConfigInfoT configinfo [] =
{
    { "direct.cfg", NULL, 0, FALSE },
    {  "fonts.cfg", NULL, 0, FALSE },
    { "ignore.cfg", NULL, 0, FALSE },
    { "english.cfg", NULL, 0, FALSE },

};
#define CONFIG_SIZE (sizeof(configinfo) / sizeof(ConfigInfoT))
/* LEG200698 I would have prefered to make the reading of the language
 * file apart, since the language is known some steps after reading
 * the configuration files. Since the search functions rely on the
 * index into configinfo this is not trivial. So I reread the language
 * file to the array at the moment the name is known.
 */

/****************************************************************************/

/********************************* functions ********************************/

/***/
static int cfg_compare (ConfigEntryT **el1, ConfigEntryT **el2)
/****************************************************************************
 * purpose:  compare-function for bsearch
 * params:   el1, el2: Config Entries to be compared
 ****************************************************************************/
{
   return strcmp ((*el1)->TexCommand, (*el2)->TexCommand);
}


/* Default to ':' for environment separator */
#ifndef ENVSEP
#define ENVSEP ':'
#endif
/* Defaults to '/' for unices, maybe defined '\\' for MS-ices */
#ifndef PATHSEP
#define PATHSEP '/'
#endif

/*
 * LEG240698
 * Tries to open the config file NAME. First all of the paths
 * specified in RTFPATH are searched. Then the precompiled default
 * path is searched.
 * At compiletime the defaults for the pathseparator end the separator
 * between directory names in the path can be overridden to match
 * e.g. MS-DOS naming conventions: ENVSEP = ';' PATHSEP = '\\'
 * leading or trailing ENVSEP's are allowed and ignored, trailing
 * PATHSEP's are allowed and ignored too.
 */
FILE *open_cfg (const char *name)
/****************************************************************************
purpose: open config files specified in name
params:  name: config-file-name
 ****************************************************************************/
{
    char *cfg_path = getenv("RTFPATH");
    /*@only@*/ static char *path = NULL;
    static size_t size = BUFFER_INCREMENT;
    size_t len;
    FILE *fp;

    diagnostics(4, "RTFPATH=`%s'", cfg_path);
    if(path == NULL && (path = (char*)malloc(size)) == NULL)
    {
	fprintf(stderr, "%s: Fatal Error: Cannot allocate memory\n", progname);
	exit(EXIT_FAILURE);
    }
    if(cfg_path != NULL)
    {
	char *s, *t;
	s = cfg_path;
	while(s != NULL && *s != '\0')
	{
	    size_t pathlen = 0;
	    t = s;
	    s = strchr(s, ENVSEP);
	    if(s) /* found */
	    {
		pathlen = (size_t)(s - t);
		s++;
	    }
	    else /* ENVSEP not found */
	      {  /* could be last path in string */
		if(strlen(t) != 0)
		  pathlen = strlen(t);
	      }
	    if((len = (pathlen + strlen(name) + 2)) > size)
	    {
		size = len;
		if((path = realloc(path, size)) == NULL)
		{
		    Fatal ("Cannot allocate memory for cfg filename");
		}
	    }
	    strncpy(path, t, pathlen);

	    /* now fix up string ending */
	    t = &path[pathlen-1];
	    if(*t++ != PATHSEP)
	      *t++ = PATHSEP;
	    *t   = '\0';

	    strcat(path, name);
	    diagnostics(4, "Trying to open config: %s", path);
	    if((fp = fopen(path,"r")) != NULL)
	      {
		diagnostics(4, "Opened config file %s", path);
		return(fp);
	      }
	}
    }
    if((len = (strlen(LIBDIR) + strlen(name) + 2)) > size)
    {
	size = len;
	if((path = (char*)realloc(path, size)) == NULL)
	{
	    fprintf(stderr, "%s: Fatal Error: Cannot allocate memory\n",
		progname);
	    exit(EXIT_FAILURE);
	}
    }
    strcpy(path, LIBDIR);
    strcat(path, "/");
    strcat(path, name);
    if((fp = fopen(path,"r")) == NULL)
    {
	fprintf(stderr, "\n%s: ERROR: cannot open file '%s'.",progname,name);
	fprintf(stderr,"\nprogram aborted\n");
	diagnostics(4, "Path: %s", path);
	diagnostics(4, "cfg-Path: %s", cfg_path);

	exit(EXIT_FAILURE);
    }
    diagnostics(4, "Opened default config file %s", path);

    return(fp);
}


/***/
static size_t read_cfg( FILE *cfgfile
                      , ConfigEntryT ***pointer_array
	              , bool do_remove_backslash
	              )
/****************************************************************************
 * purpose: Read config file and provide sorted lookup table
 ****************************************************************************/
/*@modifies pointer_array@*/
{
    size_t bufindex = 0, bufsize = 0;
    char *line, *cmdend;

    if(*pointer_array == NULL)
    {
        if((*pointer_array = malloc(BUFFER_INCREMENT * sizeof(char *))) == NULL)
        {
            Fatal("Cannot allocate memory for pointer list\n");
        }
        bufsize = BUFFER_INCREMENT;
    }
    
    while((line = ReadUptoMatch (cfgfile, "\n")) != NULL)
    {
	(void)getc(cfgfile); /* skip newline */
	/* Skip leading white space */
	while (isspace((unsigned char) *line))
	{
	    line++;
	}
	if(*line == '#' || *line == '\0')
	{
	    continue;
	}
	cmdend = strchr (line, '.');
	if (cmdend == NULL)
	{
	    ParseError("Illegal format, expected '.', got\n\"%s\"", line);
	}
	*cmdend = '\0';
	if(do_remove_backslash)
	{
	    if(*line != '\\')
	    {
		ParseError("Illegal format, expected '\\', got\n\"%s\"", line);
	    }
	    else
	    {
		line++;
	    }
	}
	if(bufindex >= bufsize)
	{
	  /*LEG210698*** Here we know, that pointer array is not null!
	   * What to do with the second allocation? */
	    if ((*pointer_array
		= malloc((bufsize += BUFFER_INCREMENT) * sizeof(char *))
		) == NULL
	       )
	    {
		Fatal("Cannot allocate memory for pointer list\n");
	    }
	}
	line = StrSave(line);
	cmdend = strchr (line, ',');
	if (cmdend == NULL)
	{
	    ParseError("Illegal format, expected ',', got\n\"%s\"", line);
	}
	*cmdend++ = '\0';

	if(((*pointer_array)[bufindex] = malloc(sizeof(ConfigEntryT))) == NULL)
	{
	    Fatal("Cannot allocate memory for config entry\n");
	}
	(*pointer_array)[bufindex]->TexCommand = line;
	(*pointer_array)[bufindex]->RtfCommand = cmdend;
	bufindex++;
    }
    qsort ( *pointer_array
          , bufindex
	  , sizeof(**pointer_array)
	  , (fptr)cfg_compare
	  );
    return bufindex;
}



/***/
void ReadCfg (void)
/****************************************************************************
 * purpose: opens config-files & reads them
 * globals: Direct-, Font- IgnoreArray[Size/Root]
 ****************************************************************************/
{
    size_t i;
    FILE *fp;

    for(i=0; i < CONFIG_SIZE; i++)
    {
	linenumber = 1;
	currfile = configinfo[i].filename;
	fp = open_cfg (configinfo[i].filename);
	configinfo[i].config_info_size
	    = read_cfg ( fp
	               , &(configinfo[i].config_info)
		       , configinfo[i].remove_leading_backslash
		       );
	(void) fclose (fp);
    }
}

/***/
/*@null@*/
static ConfigEntryT **search_rtf (const char *theTexCommand, int WhichCfg)
/****************************************************************************
 * purpose:  search theTexCommand in specified config data and return
 *           pointer to the data
 ****************************************************************************/
{
    const ConfigEntryT compare_item =
    {
	  theTexCommand
	, ""
    };
    const ConfigEntryT * const compare_ptr = &compare_item;

    assert (WhichCfg >= 0 && (size_t) WhichCfg < CONFIG_SIZE);
    assert (configinfo[WhichCfg].config_info != NULL);

    return (ConfigEntryT **) bsearch
	( &compare_ptr
	, configinfo[WhichCfg].config_info
	, configinfo[WhichCfg].config_info_size
	, sizeof(compare_ptr)
	, (fptr)cfg_compare
	); 
}

/***/
size_t SearchRtfIndex (const char *theTexCommand, int WhichCfg)
/****************************************************************************
 * purpose:  search theTexCommand in a specified config data and return
 *           index
 ****************************************************************************/
{
    ConfigEntryT **help = search_rtf(theTexCommand, WhichCfg);
    if(help == NULL)
    {
	return 0;
    }
    /*LEG210698*** subtraction of two ConfigEntryT pointers */
    return help - configinfo[WhichCfg].config_info;
}

/***/
const char *SearchRtfCmd (const char *theTexCommand, int WhichCfg)
/****************************************************************************
 * purpose:  search theTexCommand in a specified config data and return
 *           pointer to the data
 ****************************************************************************/
{
    ConfigEntryT **help = search_rtf(theTexCommand, WhichCfg);
    return help == NULL ? NULL : (*help)->RtfCommand;
}

/***/
/*@null@*/
const ConfigEntryT **CfgStartIterate (/*@unused@*/ int WhichCfg)
/****************************************************************************
 * purpose:  Start iterating of configuration data
 ****************************************************************************/
{
    return NULL;
}

/***/
/*@null@*/
const ConfigEntryT **CfgNext (                  int            WhichCfg
                             , /*@null@*/ const ConfigEntryT **last
			     )
/****************************************************************************
 * purpose:  Get the next entry from specified configuration data
 ****************************************************************************/
{
    if (last == NULL)
    {
	return (const ConfigEntryT **)configinfo[WhichCfg].config_info;
    }
    last++;
    if(   last
        > (const ConfigEntryT **)  configinfo[WhichCfg].config_info
	                         + configinfo[WhichCfg].config_info_size
				 - 1
      )
    {
	return NULL;
    }
    return last;
}



/****************************************************************************
 * opens and reads the language configuration file named in lang

Opens language file & builds a search tree for the translation of
"Hardcoded" latex headings like "Part", "References", ...
The file format is:
LATEXTOKEN,Translation.

 ****************************************************************************/
void
ReadLg (char *lang)
{
  FILE *fp;
  char *langfn;
  
  langfn = malloc(strlen(lang) + strlen(".cfg"));
  if (langfn == NULL)
    diagnostics(ERROR, "Could not allocate memory for language filename.");
    /*LEG210698*** lclint -  diagnostics exits on ERROR */
  strcpy(langfn, lang);
  strcat(langfn, ".cfg");

  fp = open_cfg (langfn);
  free(langfn);

  configinfo[LANGUAGE_A].config_info_size 
    = read_cfg ( fp,
		 &(configinfo[LANGUAGE_A].config_info),
		 configinfo[LANGUAGE_A].remove_leading_backslash);
  
  (void) fclose (fp);
}



/****************************************************************************
 *LEG030598 

 purpose : returns a pointer to the Printout name of a Heading, since
           this is read from a language file it provides translation
           capability.
 params  : name, name of heading.

 ****************************************************************************/
const char *
TranslateName(char *name)
{
  return SearchRtfCmd (name, LANGUAGE_A);
}