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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
/****h* ROBODoc/Configuration
* FUNCTION
* Functions to access the ROBODoc configuration and configuration
* file (robodoc.rc) or the file specified with the --rc option.
*
* The robodoc.rc file consists of a number of blocks. Each
* block starts with a line of the form
*
* <block name>:
*
* This is followed by a number of lines of data. Each line starts
* with at least one space followed by the actual data.
*
* This module parses this data and stores it in the global
* configuration.
*
* NOTES
* Is missing a lot of documentation.
*
******
* $Id: roboconfig.c,v 1.23 2003/12/30 17:39:36 gumpu Exp $
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "headertypes.h"
#include "util.h"
#include "roboconfig.h"
#ifdef DMALLOC
#include <dmalloc.h>
#endif
/****v* Configuration/item_names
* NAME
* item_names -- default item names
* SYNOPSIS
* char *default_item_names[]
* FUNCTION
* Defines the names of items that ROBODoc recognized as
* items by default if none are specified in the
* robodoc.rc file.
* SOURCE
*/
char *default_item_names[] = {
"SOURCE", /* source code inclusion */
"NAME", /* Item name + short description */
"COPYRIGHT", /* who own the copyright : "(c) <year>-<year> by <company/person>" */
"SYNOPSIS", "USAGE", /* how to use it */
"FUNCTION", "DESCRIPTION", "PURPOSE", /* what does it */
"AUTHOR", /* who wrote it */
"CREATION DATE", /* when did the work start */
"MODIFICATION HISTORY", "HISTORY", /* who done what changes when */
"INPUTS", "ARGUMENTS", "OPTIONS", "PARAMETERS", "SWITCHES", /* what can we feed into it */
"OUTPUT", "SIDE EFFECTS", /* what output will be made */
"RESULT", "RETURN VALUE", /* what do we get returned */
"EXAMPLE", /* a clear example of the items use */
"NOTES", /* any annotations */
"DIAGNOSTICS", /* diagnostical output */
"WARNINGS", "ERRORS", /* warning & error-messages */
"BUGS", /* known bugs */
"TODO", "IDEAS", /* what to implement next & ideas */
"PORTABILITY", /* where does it come from, where will it work */
"SEE ALSO", /* references */
"METHODS", "NEW METHODS", /* oop methods */
"ATTRIBUTES", "NEW ATTRIBUTES", /* oop attributes */
"TAGS", /* tagitem description */
"COMMANDS", /* command description */
"DERIVED FROM", /* oop super class */
"DERIVED BY", /* oop sub class */
"USES", "CHILDREN", /* what modules are used by this one */
"USED BY", "PARENTS", /* which modules do use this */
NULL, /* don't delete, so we can count how many there are... */
};
/***********/
/* Maximum length of a line in the configuration file */
#define BUFFER_LENGTH 2048
/****v* Configuration/configuration
* FUNCTION
* This global stores all the configuration parameters specified on
* the command line and in the robodoc.rc file.
* SOURCE
*/
struct RB_Configuration configuration;
/*****/
static void RB_AllocItemNames( void );
static void RB_AllocOptions( unsigned int argc, char **argv );
static T_Block_Kind RB_BlockKind( char *line );
static T_Line_Kind RB_ConfigLineKind( char *line );
static void RB_FirstScan( FILE * f );
static void RB_SecondScan( FILE * f );
static void RB_AllocIgnoreItems( void );
static void RB_Alloc_Parameters( struct Parameters* parameters, unsigned int size );
static void RB_AddParameter( char* name, struct Parameters* parameters );
static void RB_GetParameters( char* line, struct Parameters* parameters );
static void RB_Install_Custom_HeaderTypes( void );
/****f* Configuration/RB_ReadConfiguration
* FUNCTION
* Read the robodoc configuration file, and create
* a RB_Configuration structure.
*
* The configuration is scanned twice. The first scan
* is to determine the number of elements in each section,
* in the second scan the elements are read and stored.
*
* SYNOPSIS
* void RB_ReadConfiguration( unsigned int argc, char **argv, char* filename )
* INPUTS
* o argc -- the arg count as received by main()
* o argv -- the arg valules as received by main()
* o filename -- an optional filename. If none is given,
* "robodoc.rc" is used.
* RESULT
* An initialized configuration.
* SOURCE
*/
void
RB_ReadConfiguration( unsigned int argc, char **argv, char* filename )
{
FILE *f;
if ( filename )
{
f = fopen( filename, "r" );
if ( !f )
{
RB_Panic( "Can't open %s\n", filename );
}
}
else
{
f = fopen( "robodoc.rc", "r" );
/* TODO look at other locations. */
}
if ( f )
{
RB_FirstScan( f );
}
RB_AllocItemNames();
RB_AllocOptions( argc, argv );
RB_AllocIgnoreItems();
RB_Alloc_Parameters( &( configuration.custom_headertypes ), 10 );
RB_Alloc_Parameters( &( configuration.ignore_files ), 10 );
if ( f )
{
rewind( f );
RB_SecondScan( f );
fclose( f );
}
RB_Install_Custom_HeaderTypes();
}
/******/
/****if* Config/RB_ConfigLineKind
* FUNCTION
* Deterimine the kind of line we a currently processing.
* SYNOPSIS
* static T_Line_Kind RB_ConfigLineKind( char* line )
* INPUTS
* line -- the current line.
* RETURN
* The kind of line.
* SOURCE
*/
static T_Line_Kind
RB_ConfigLineKind( char *line )
{
T_Line_Kind kind = CFL_UNKNOWN;
if ( *line == '\0' )
{
kind = CFL_EMPTYLINE;
}
else if ( *line == '#' )
{
kind = CFL_REMARK;
}
else if ( isspace( *line ) )
{
char *cur_char = line;
for ( ; *cur_char && isspace( *cur_char ); ++cur_char )
{
/* Empty */
}
if ( *cur_char == '\0' )
{
kind = CFL_EMPTYLINE;
}
else
{
/* There is atleast one non-space character */
kind = CFL_PARAMETER;
}
}
else
{
kind = CFL_SECTION;
}
return kind;
}
/********/
static T_Block_Kind
RB_BlockKind( char *line )
{
T_Block_Kind section_kind = SK_UNKNOWN;
if ( strcmp( line, "items:" ) == 0 )
{
section_kind = SK_ITEMS;
}
else if ( strcmp( line, "options:" ) == 0 )
{
section_kind = SK_OPTIONS;
}
else if ( strcmp( line, "extensions:" ) == 0 )
{
section_kind = SK_EXTENSIONS;
printf("Warning: the 'extensions:' section is obsolete, use 'ignore files:' instead\n");
}
else if ( strcmp( line, "ignore items:" ) == 0 )
{
section_kind = SK_IGNOREITEMS;
}
else if ( strcmp( line, "headertypes:" ) == 0 )
{
section_kind = SK_HEADERTYPES;
}
else if ( strcmp( line, "ignore files:" ) == 0 )
{
section_kind = SK_IGNORE_FILES;
}
else
{
RB_Panic( "unknown section kind \"%s\"\n", line );
}
return section_kind;
}
static void RB_Install_Custom_HeaderTypes( void )
{
unsigned int i;
for ( i = 0; i < configuration.custom_headertypes.number; i += 3 )
{
char* typeCharString;
char* indexName;
char* fileName;
typeCharString = configuration.custom_headertypes.names[ i ];
indexName = configuration.custom_headertypes.names[ i + 1 ];
fileName = configuration.custom_headertypes.names[ i + 2 ];
if ( strlen( typeCharString ) > 1 )
{
RB_Panic( "Trying to add a new headertype with parameters"
"%s %s %s.\nHowever the typecharacter %s is more "
"than one character long\n",
typeCharString, indexName, fileName,
typeCharString );
}
RB_AddHeaderType( typeCharString[ 0 ], indexName, fileName );
}
}
static void RB_Alloc_Parameters( struct Parameters* parameters, unsigned int size )
{
parameters->size = size;
parameters->number = 0;
parameters->names = calloc( size, sizeof( char* ) );
}
/* TODO Documentation */
static void RB_AddParameter( char* name, struct Parameters* parameters )
{
parameters->names[ parameters->number ] = RB_StrDup( name );
parameters->number++;
if ( parameters->number >= parameters->size )
{
parameters->size *= 2;
parameters->names = realloc( parameters->names, parameters->size * sizeof( char* ) );
}
}
/* TODO Documentation */
static void RB_GetParameters( char* line, struct Parameters* parameters )
{
int i;
int n = strlen( line );
/* Remove any spaces at the end of the line */
for ( i = n - 1; i >= 0 && isspace( line[i] ); --i )
{
line[i] = '\0';
}
assert( i > 0 ); /* If i <= 0 then the line was empty
and that cannot be, because this
is supposed to be a parameter */
/* Skip any white space at the begin of the line. */
n = strlen( line );
for ( i = 0; i < n && isspace( line[i] ); ++i )
{
/* Empty */
}
line += i;
n = strlen( line );
for ( i = 0; i < n; /* empty */)
{
char* name = line;
if ( line[ i ] == '"' )
{
/* It is quoted string, fetch everything until
* the next quote */
++name; /* skip the double quote */
for ( ++i; ( i < n ) && ( line[ i ] != '"') ; ++i )
{
/* empty */
}
if ( i == n )
{
RB_Panic( "Missing quote in your .rc file in line:\n %s\n", line );
}
else
{
#if defined(__APPLE__)
/* hacked because of error when compiling on Mac OS X */
assert ( line[ i ] == 34 );
#else
assert ( line[ i ] == '"' );
#endif
line[ i ] = '\0';
RB_AddParameter( name, parameters );
}
}
else
{
/* a single word, find the next space */
for ( ; ( i < n ) && !isspace( line[ i ] ); ++i )
{
/* empty */
}
if ( i < n )
{
line[ i ] = '\0';
}
RB_AddParameter( name, parameters );
}
/* Is there anything left? */
if ( i < n )
{
/* skip any spaces until the next parameter */
++i; /* first skip the nul character */
line += i;
n = strlen( line );
for ( i = 0 ; ( i < n ) && isspace( line[ i ] ); ++i )
{
/* empty */
}
line += i;
n = strlen( line );
i = 0;
}
}
}
char *
RB_GetParameter( char *line )
{
int i;
int n = strlen( line );
/* Remove any spaces at the end of the line */
for ( i = n - 1; i >= 0 && isspace( line[i] ); --i )
{
line[i] = '\0';
}
assert( i > 0 ); /* If i <= 0 then the line was empty
and that cannot be, because this
is supposed to be a parameter */
/* Skip any white space at the begin of the line. */
n = strlen( line );
for ( i = 0; i < n && isspace( line[i] ); ++i )
{
/* Empty */
}
line += i;
return RB_StrDup( line );
}
void
RB_StripCR( char *line )
{
char *c;
for ( c = line; *c; ++c )
{
if ( *c == '\n' )
{
*c = '\0';
}
}
}
void
RB_Free_Configuration( void )
{
unsigned int i;
for ( i = 0; i < configuration.no_items; ++i )
{
free( configuration.item_names[i] );
}
free( configuration.item_names );
for ( i = 0; i < configuration.no_ignore_items; ++i )
{
free( configuration.ignore_items[i] );
}
free( configuration.ignore_items );
/* TODO Deallocate custom_headertypes */
}
/* TODO Documentation */
static void
RB_AllocIgnoreItems( void )
{
if ( configuration.no_ignore_items )
{
configuration.ignore_items =
( char ** ) calloc( configuration.no_ignore_items,
sizeof( char * ) );
}
else
{
configuration.ignore_items = NULL;
}
}
/* TODO Documentation */
static void
RB_AllocOptions( unsigned int argc, char **argv )
{
unsigned int i;
RB_Alloc_Parameters( &( configuration.options ), argc );
for ( i = 0; i < argc; ++i )
{
RB_AddParameter( argv[i], &( configuration.options ) );
}
}
/* TODO Documentation */
static void
RB_AllocItemNames( void )
{
if ( configuration.no_items )
{
configuration.no_items++;
configuration.item_names =
( char ** ) calloc( configuration.no_items, sizeof( char * ) );
assert( configuration.item_names );
/* The SOURCE item is always included */
configuration.item_names[0] = RB_StrDup( "SOURCE" );
}
else
{
/* No item names were defined, so we use the default ones */
unsigned int i;
for ( ; default_item_names[configuration.no_items];
++configuration.no_items )
{
/* empty */
}
configuration.item_names =
( char ** ) calloc( configuration.no_items, sizeof( char * ) );
for ( i = 0; i < configuration.no_items; ++i )
{
configuration.item_names[i] = RB_StrDup( default_item_names[i] );
}
}
RB_Say( "There are %d item names\n", configuration.no_items );
}
/* Count number of paramters and options */
static void
RB_FirstScan( FILE * f )
{
char line_buffer[BUFFER_LENGTH];
T_Block_Kind section_kind = SK_UNKNOWN;
T_Line_Kind line_kind = CFL_UNKNOWN;
while ( fgets( line_buffer, BUFFER_LENGTH, f ) )
{
RB_StripCR( line_buffer );
line_kind = RB_ConfigLineKind( line_buffer );
switch ( line_kind )
{
case CFL_REMARK:
case CFL_EMPTYLINE: /* fall through */
break;
case CFL_SECTION:
section_kind = RB_BlockKind( line_buffer );
break;
case CFL_PARAMETER:
{
switch ( section_kind )
{
case SK_ITEMS:
++configuration.no_items;
break;
case SK_EXTENSIONS:
/* Obsolete */
break;
case SK_IGNOREITEMS:
++configuration.no_ignore_items;
break;
case SK_OPTIONS: /* fall through */
case SK_HEADERTYPES:
case SK_IGNORE_FILES:
break;
case SK_UNKNOWN:
assert( 0 );
}
}
break;
case CFL_UNKNOWN:
default:
assert( 0 );
}
}
}
/* TODO Documentation */
static void
RB_SecondScan( FILE * f )
{
char line_buffer[BUFFER_LENGTH];
T_Block_Kind section_kind = SK_UNKNOWN;
T_Line_Kind line_kind = CFL_UNKNOWN;
int itemname_nr = 1; /* 0 if for the SOURCE_ITEM */
int ignore_nr = 0;
while ( fgets( line_buffer, BUFFER_LENGTH, f ) )
{
RB_StripCR( line_buffer );
line_kind = RB_ConfigLineKind( line_buffer );
switch ( line_kind )
{
case CFL_REMARK:
case CFL_EMPTYLINE: /* fall through */
/* Do nothing */
break;
case CFL_SECTION:
section_kind = RB_BlockKind( line_buffer );
break;
case CFL_PARAMETER:
{
switch ( section_kind )
{
case SK_ITEMS:
configuration.item_names[itemname_nr] =
RB_GetParameter( line_buffer );
++itemname_nr;
break;
case SK_OPTIONS:
RB_GetParameters( line_buffer, &( configuration.options ) );
break;
case SK_EXTENSIONS:
/* Obsolete */
break;
case SK_IGNOREITEMS:
configuration.ignore_items[ignore_nr] =
RB_GetParameter( line_buffer );
++ignore_nr;
break;
case SK_HEADERTYPES:
RB_GetParameters( line_buffer, &( configuration.custom_headertypes ) );
break;
case SK_IGNORE_FILES:
RB_GetParameters( line_buffer, &( configuration.ignore_files ) );
break;
case SK_UNKNOWN:
break;
default:
assert( 0 );
}
}
break;
case CFL_UNKNOWN:
default:
assert( 0 );
}
}
}
|