tkparse.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:19k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * tkparse.c
  3.  *
  4.  * Eric Youngdale was the original author of xconfig.
  5.  * Michael Elizabeth Chastain (mec@shout.net) is the current maintainer.
  6.  *
  7.  * Parse a config.in file and translate it to a wish script.
  8.  * This task has three parts:
  9.  *
  10.  *   tkparse.c tokenize the input
  11.  *   tkcond.c   transform 'if ...' statements
  12.  *   tkgen.c    generate output
  13.  *
  14.  * Change History
  15.  *
  16.  * 7 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
  17.  * - Teach dep_tristate about a few literals, such as:
  18.  *     dep_tristate 'foo' CONFIG_FOO m
  19.  *   Also have it print an error message and exit on some parse failures.
  20.  *
  21.  * 14 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
  22.  * - Don't fclose stdin.  Thanks to Tony Hoyle for nailing this one.
  23.  *
  24.  * 14 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
  25.  * - Steam-clean this file.  I tested this by generating kconfig.tk for
  26.  *   every architecture and comparing it character-for-character against
  27.  *   the output of the old tkparse.
  28.  *
  29.  * 23 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
  30.  * - Remove bug-compatible code.
  31.  *
  32.  * 07 July 1999, Andrzej M. Krzysztofowicz, <ankry@mif.pg.gda.pl>
  33.  * - Submenus implemented,
  34.  * - plenty of option updating/displaying fixes,
  35.  * - dep_bool, define_hex, define_int, define_string, define_tristate and
  36.  *   undef implemented,
  37.  * - dep_tristate fixed to support multiple dependencies,
  38.  * - handling of variables with an empty value implemented,
  39.  * - value checking for int and hex fields,
  40.  * - more checking during condition parsing; choice variables are treated as
  41.  *   all others now,
  42.  *
  43.  * TO DO:
  44.  * - xconfig is at the end of its life cycle.  Contact <mec@shout.net> if
  45.  *   you are interested in working on the replacement.
  46.  */
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "tkparse.h"
  51. static struct kconfig * config_list = NULL;
  52. static struct kconfig * config_last = NULL;
  53. static const char * current_file = "<unknown file>";
  54. static int lineno = 0;
  55. static void do_source( const char * );
  56. #undef strcmp
  57. int my_strcmp( const char * s1, const char * s2 ) { return strcmp( s1, s2 ); }
  58. #define strcmp my_strcmp
  59. /*
  60.  * Report a syntax error.
  61.  */
  62. static void syntax_error( const char * msg )
  63. {
  64.     fprintf( stderr, "%s: %d: %sn", current_file, lineno, msg );
  65.     exit( 1 );
  66. }
  67. /*
  68.  * Find index of a specific variable in the symbol table.
  69.  * Create a new entry if it does not exist yet.
  70.  */
  71. struct variable *vartable;
  72. int max_varnum = 0;
  73. static int vartable_size = 0;
  74. int get_varnum( char * name )
  75. {
  76.     int i;
  77.     
  78.     for ( i = 1; i <= max_varnum; i++ )
  79. if ( strcmp( vartable[i].name, name ) == 0 )
  80.     return i;
  81.     while (max_varnum+1 >= vartable_size) {
  82. vartable = realloc(vartable, (vartable_size += 1000)*sizeof(*vartable));
  83. if (!vartable) {
  84.     fprintf(stderr, "tkparse realloc vartable failedn");
  85.     exit(1);
  86. }
  87.     }
  88.     vartable[++max_varnum].name = malloc( strlen( name )+1 );
  89.     strcpy( vartable[max_varnum].name, name );
  90.     return max_varnum;
  91. }
  92. /*
  93.  * Get a string.
  94.  */
  95. static const char * get_string( const char * pnt, char ** label )
  96. {
  97.     const char * word;
  98.     word = pnt;
  99.     for ( ; ; )
  100.     {
  101. if ( *pnt == '' || *pnt == ' ' || *pnt == 't' )
  102.     break;
  103. pnt++;
  104.     }
  105.     *label = malloc( pnt - word + 1 );
  106.     memcpy( *label, word, pnt - word );
  107.     (*label)[pnt - word] = '';
  108.     if ( *pnt != '' )
  109. pnt++;
  110.     return pnt;
  111. }
  112. /*
  113.  * Get a quoted string.
  114.  * Insert a '' before any characters that need quoting.
  115.  */
  116. static const char * get_qstring( const char * pnt, char ** label )
  117. {
  118.     char quote_char;
  119.     char newlabel [2048];
  120.     char * pnt1;
  121.     /* advance to the open quote */
  122.     for ( ; ; )
  123.     {
  124. if ( *pnt == '' )
  125.     return pnt;
  126. quote_char = *pnt++;
  127. if ( quote_char == '"' || quote_char == ''' )
  128.     break;
  129.     }
  130.     /* copy into an intermediate buffer */
  131.     pnt1 = newlabel;
  132.     for ( ; ; )
  133.     {
  134. if ( *pnt == '' )
  135.     syntax_error( "unterminated quoted string" );
  136. if ( *pnt == quote_char && pnt[-1] != '\' )
  137.     break;
  138. /* copy the character, quoting if needed */
  139. if ( *pnt == '"' || *pnt == ''' || *pnt == '[' || *pnt == ']' )
  140.     *pnt1++ = '\';
  141. *pnt1++ = *pnt++;
  142.     }
  143.     /* copy the label into a permanent location */
  144.     *pnt1++ = '';
  145.     *label = (char *) malloc( pnt1 - newlabel );
  146.     memcpy( *label, newlabel, pnt1 - newlabel );
  147.     /* skip over last quote and next whitespace */
  148.     pnt++;
  149.     while ( *pnt == ' ' || *pnt == 't' )
  150. pnt++;
  151.     return pnt;
  152. }
  153. /*
  154.  * Get a quoted or unquoted string. It is recognized by the first 
  155.  * non-white character. '"' and '"' are not allowed inside the string.
  156.  */
  157. static const char * get_qnqstring( const char * pnt, char ** label )
  158. {
  159.     char quote_char;
  160.     while ( *pnt == ' ' || *pnt == 't' )
  161. pnt++;
  162.     if ( *pnt == '' )
  163. return pnt;
  164.     quote_char = *pnt;
  165.     if ( quote_char == '"' || quote_char == ''' )
  166. return get_qstring( pnt, label );
  167.     else
  168. return get_string( pnt, label );
  169. }
  170. /*
  171.  * Tokenize an 'if' statement condition.
  172.  */
  173. static struct condition * tokenize_if( const char * pnt )
  174. {
  175.     struct condition * list;
  176.     struct condition * last;
  177.     struct condition * prev;
  178.     /* eat the open bracket */
  179.     while ( *pnt == ' ' || *pnt == 't' )
  180. pnt++;
  181.     if ( *pnt != '[' )
  182. syntax_error( "bad 'if' condition" );
  183.     pnt++;
  184.     list = last = NULL;
  185.     for ( ; ; )
  186.     {
  187. struct condition * cond;
  188. /* advance to the next token */
  189. while ( *pnt == ' ' || *pnt == 't' )
  190.     pnt++;
  191. if ( *pnt == '' )
  192.     syntax_error( "unterminated 'if' condition" );
  193. if ( *pnt == ']' )
  194.     return list;
  195. /* allocate a new token */
  196. cond = malloc( sizeof(*cond) );
  197. memset( cond, 0, sizeof(*cond) );
  198. if ( last == NULL )
  199.     { list = last = cond; prev = NULL; }
  200. else
  201.     { prev = last; last->next = cond; last = cond; }
  202. /* determine the token value */
  203. if ( *pnt == '-' && pnt[1] == 'a' )
  204. {
  205.     if ( ! prev || ( prev->op != op_variable && prev->op != op_constant ) )
  206. syntax_error( "incorrect argument" );
  207.     cond->op = op_and;  pnt += 2; continue;
  208. }
  209. if ( *pnt == '-' && pnt[1] == 'o' )
  210. {
  211.     if ( ! prev || ( prev->op != op_variable && prev->op != op_constant ) )
  212. syntax_error( "incorrect argument" );
  213.     cond->op = op_or;   pnt += 2; continue;
  214. }
  215. if ( *pnt == '!' && pnt[1] == '=' )
  216. {
  217.     if ( ! prev || ( prev->op != op_variable && prev->op != op_constant ) )
  218. syntax_error( "incorrect argument" );
  219.     cond->op = op_neq;  pnt += 2; continue;
  220. }
  221. if ( *pnt == '=' )
  222. {
  223.     if ( ! prev || ( prev->op != op_variable && prev->op != op_constant ) )
  224. syntax_error( "incorrect argument" );
  225.     cond->op = op_eq;   pnt += 1; continue;
  226. }
  227. if ( *pnt == '!' )
  228. {
  229.     if ( prev && ( prev->op != op_and && prev->op != op_or
  230.       && prev->op != op_bang ) )
  231. syntax_error( "incorrect argument" );
  232.     cond->op = op_bang; pnt += 1; continue;
  233. }
  234. if ( *pnt == '"' )
  235. {
  236.     const char * word;
  237.     if ( prev && ( prev->op == op_variable || prev->op == op_constant ) )
  238. syntax_error( "incorrect argument" );
  239.     /* advance to the word */
  240.     pnt++;
  241.     if ( *pnt == '$' )
  242. { cond->op = op_variable; pnt++; }
  243.     else
  244. { cond->op = op_constant; }
  245.     /* find the end of the word */
  246.     word = pnt;
  247.     for ( ; ; )
  248.     {
  249. if ( *pnt == '' )
  250.     syntax_error( "unterminated double quote" );
  251. if ( *pnt == '"' )
  252.     break;
  253. pnt++;
  254.     }
  255.     /* store a copy of this word */
  256.     {
  257. char * str = malloc( pnt - word + 1 );
  258. memcpy( str, word, pnt - word );
  259. str [pnt - word] = '';
  260. if ( cond->op == op_variable )
  261. {
  262.     cond->nameindex = get_varnum( str );
  263.     free( str );
  264. }
  265. else /* op_constant */
  266. {
  267.     cond->str = str;
  268. }
  269.     }
  270.     pnt++;
  271.     continue;
  272. }
  273. /* unknown token */
  274. syntax_error( "bad if condition" );
  275.     }
  276. }
  277. /*
  278.  * Tokenize a choice list.  Choices appear as pairs of strings;
  279.  * note that I am parsing *inside* the double quotes.  Ugh.
  280.  */
  281. static const char * tokenize_choices( struct kconfig * cfg_choose,
  282.     const char * pnt )
  283. {
  284.     int default_checked = 0;
  285.     for ( ; ; )
  286.     {
  287. struct kconfig * cfg;
  288. char * buffer = malloc( 64 );
  289. /* skip whitespace */
  290. while ( *pnt == ' ' || *pnt == 't' )
  291.     pnt++;
  292. if ( *pnt == '' )
  293.     return pnt;
  294. /* allocate a new kconfig line */
  295. cfg = malloc( sizeof(*cfg) );
  296. memset( cfg, 0, sizeof(*cfg) );
  297. if ( config_last == NULL )
  298.     { config_last = config_list = cfg; }
  299. else
  300.     { config_last->next = cfg; config_last = cfg; }
  301. /* fill out the line */
  302. cfg->token      = token_choice_item;
  303. cfg->cfg_parent = cfg_choose;
  304. pnt = get_string( pnt, &cfg->label );
  305. if ( ! default_checked &&
  306.      ! strncmp( cfg->label, cfg_choose->value, strlen( cfg_choose->value ) ) )
  307. {
  308.     default_checked = 1;
  309.     free( cfg_choose->value );
  310.     cfg_choose->value = cfg->label;
  311. }
  312. while ( *pnt == ' ' || *pnt == 't' )
  313.     pnt++;
  314. pnt = get_string( pnt, &buffer );
  315. cfg->nameindex = get_varnum( buffer );
  316.     }
  317.     if ( ! default_checked )
  318. syntax_error( "bad 'choice' default value" );
  319.     return pnt;
  320. }
  321. /*
  322.  * Tokenize one line.
  323.  */
  324. static void tokenize_line( const char * pnt )
  325. {
  326.     static struct kconfig * last_menuoption = NULL;
  327.     enum e_token token;
  328.     struct kconfig * cfg;
  329.     struct dependency ** dep_ptr;
  330.     char * buffer = malloc( 64 );
  331.     /* skip white space */
  332.     while ( *pnt == ' ' || *pnt == 't' )
  333. pnt++;
  334.     /*
  335.      * categorize the next token
  336.      */
  337. #define match_token(t, s) 
  338.     if (strncmp(pnt, s, strlen(s)) == 0) { token = t; pnt += strlen(s); break; }
  339.     token = token_UNKNOWN;
  340.     switch ( *pnt )
  341.     {
  342.     default:
  343. break;
  344.     case '#':
  345.     case '':
  346. return;
  347.     case 'b':
  348. match_token( token_bool, "bool" );
  349. break;
  350.     case 'c':
  351. match_token( token_choice_header, "choice"  );
  352. match_token( token_comment, "comment" );
  353. break;
  354.     case 'd':
  355. match_token( token_define_bool, "define_bool" );
  356. match_token( token_define_hex, "define_hex" );
  357. match_token( token_define_int, "define_int" );
  358. match_token( token_define_string, "define_string" );
  359. match_token( token_define_tristate, "define_tristate" );
  360. match_token( token_dep_bool, "dep_bool" );
  361. match_token( token_dep_mbool, "dep_mbool" );
  362. match_token( token_dep_tristate, "dep_tristate" );
  363. break;
  364.     case 'e':
  365. match_token( token_else, "else" );
  366. match_token( token_endmenu, "endmenu" );
  367. break;
  368.     case 'f':
  369. match_token( token_fi, "fi" );
  370. break;
  371.     case 'h':
  372. match_token( token_hex, "hex" );
  373. break;
  374.     case 'i':
  375. match_token( token_if, "if" );
  376. match_token( token_int, "int" );
  377. break;
  378.     case 'm':
  379. match_token( token_mainmenu_name, "mainmenu_name" );
  380. match_token( token_mainmenu_option, "mainmenu_option" );
  381. break;
  382.     case 's':
  383. match_token( token_source, "source" );
  384. match_token( token_string, "string" );
  385. break;
  386.     case 't':
  387. match_token( token_then, "then" );
  388. match_token( token_tristate, "tristate" );
  389. break;
  390.     case 'u':
  391. match_token( token_unset, "unset" );
  392. break;
  393.     }
  394. #undef match_token
  395.     if ( token == token_source )
  396.     {
  397. while ( *pnt == ' ' || *pnt == 't' )
  398.     pnt++;
  399. do_source( pnt );
  400. return;
  401.     }
  402.     if ( token == token_then )
  403.     {
  404. if ( config_last != NULL && config_last->token == token_if )
  405.     return;
  406. syntax_error( "bogus 'then'" );
  407.     }
  408. #if 0
  409.     if ( token == token_unset )
  410.     {
  411. fprintf( stderr, "Ignoring 'unset' commandn" );
  412. return;
  413.     }
  414. #endif
  415.     if ( token == token_UNKNOWN )
  416. syntax_error( "unknown command" );
  417.     /*
  418.      * Allocate an item.
  419.      */
  420.     cfg = malloc( sizeof(*cfg) );
  421.     memset( cfg, 0, sizeof(*cfg) );
  422.     if ( config_last == NULL )
  423. { config_last = config_list = cfg; }
  424.     else
  425. { config_last->next = cfg; config_last = cfg; }
  426.     /*
  427.      * Tokenize the arguments.
  428.      */
  429.     while ( *pnt == ' ' || *pnt == 't' )
  430. pnt++;
  431.     cfg->token = token;
  432.     switch ( token )
  433.     {
  434.     default:
  435. syntax_error( "unknown token" );
  436.     case token_bool:
  437.     case token_tristate:
  438. pnt = get_qstring ( pnt, &cfg->label );
  439. pnt = get_string  ( pnt, &buffer );
  440. cfg->nameindex = get_varnum( buffer );
  441. break;
  442.     case token_choice_header:
  443. {
  444.     static int choose_number = 0;
  445.     char * choice_list;
  446.     pnt = get_qstring ( pnt, &cfg->label  );
  447.     pnt = get_qstring ( pnt, &choice_list );
  448.     pnt = get_string  ( pnt, &cfg->value  );
  449.     cfg->nameindex = -(choose_number++);
  450.     tokenize_choices( cfg, choice_list );
  451.     free( choice_list );
  452. }
  453. break;
  454.     case token_comment:
  455. pnt = get_qstring(pnt, &cfg->label);
  456. if ( last_menuoption != NULL )
  457. {
  458.     pnt = get_qstring(pnt, &cfg->label);
  459.     if (cfg->label == NULL)
  460. syntax_error( "missing comment text" );
  461.     last_menuoption->label = cfg->label;
  462.     last_menuoption = NULL;
  463. }
  464. break;
  465.     case token_define_bool:
  466.     case token_define_tristate:
  467. pnt = get_string( pnt, &buffer );
  468. cfg->nameindex = get_varnum( buffer );
  469. while ( *pnt == ' ' || *pnt == 't' )
  470.     pnt++;
  471. if ( ( pnt[0] == 'Y'  || pnt[0] == 'M' || pnt[0] == 'N'
  472. ||     pnt[0] == 'y'  || pnt[0] == 'm' || pnt[0] == 'n' )
  473. &&   ( pnt[1] == '' || pnt[1] == ' ' || pnt[1] == 't' ) )
  474. {
  475.     if      ( *pnt == 'n' || *pnt == 'N' ) cfg->value = strdup( "CONSTANT_N" );
  476.     else if ( *pnt == 'y' || *pnt == 'Y' ) cfg->value = strdup( "CONSTANT_Y" );
  477.     else if ( *pnt == 'm' || *pnt == 'M' ) cfg->value = strdup( "CONSTANT_M" );
  478. }
  479. else if ( *pnt == '$' )
  480. {
  481.     pnt++;
  482.     pnt = get_string( pnt, &cfg->value );
  483. }
  484. else
  485. {
  486.     syntax_error( "unknown define_bool value" );
  487. }
  488. get_varnum( cfg->value );
  489. break;
  490.     case token_define_hex:
  491.     case token_define_int:
  492. pnt = get_string( pnt, &buffer );
  493. cfg->nameindex = get_varnum( buffer );
  494. pnt = get_string( pnt, &cfg->value );
  495. break;
  496.     case token_define_string:
  497. pnt = get_string( pnt, &buffer );
  498. cfg->nameindex = get_varnum( buffer );
  499. pnt = get_qnqstring( pnt, &cfg->value );
  500. if (cfg->value == NULL)
  501.     syntax_error( "missing value" );
  502. break;
  503.     case token_dep_bool:
  504.     case token_dep_mbool:
  505.     case token_dep_tristate:
  506. pnt = get_qstring ( pnt, &cfg->label );
  507. pnt = get_string  ( pnt, &buffer );
  508. cfg->nameindex = get_varnum( buffer );
  509. while ( *pnt == ' ' || *pnt == 't' )
  510.     pnt++;
  511. dep_ptr = &(cfg->depend);
  512. do {
  513.     *dep_ptr = (struct dependency *) malloc( sizeof( struct dependency ) );
  514.     (*dep_ptr)->next = NULL;
  515.     if ( ( pnt[0] == 'Y'  || pnt[0] == 'M' || pnt[0] == 'N'
  516.     ||     pnt[0] == 'y'  || pnt[0] == 'm' || pnt[0] == 'n' )
  517.     &&   ( pnt[1] == '' || pnt[1] == ' ' || pnt[1] == 't' ) )
  518.     {
  519. /* dep_tristate 'foo' CONFIG_FOO m */
  520. if      ( pnt[0] == 'Y' || pnt[0] == 'y' )
  521.     (*dep_ptr)->name = strdup( "CONSTANT_Y" );
  522. else if ( pnt[0] == 'N' || pnt[0] == 'n' )
  523.     (*dep_ptr)->name = strdup( "CONSTANT_N" );
  524. else
  525.     (*dep_ptr)->name = strdup( "CONSTANT_M" );
  526. pnt++;
  527. get_varnum( (*dep_ptr)->name );
  528.     }
  529.     else if ( *pnt == '$' )
  530.     {
  531. pnt++;
  532. pnt = get_string( pnt, &(*dep_ptr)->name );
  533. get_varnum( (*dep_ptr)->name );
  534.     }
  535.     else
  536.     {
  537. syntax_error( "can't handle dep_bool/dep_mbool/dep_tristate condition" );
  538.     }
  539.     dep_ptr = &(*dep_ptr)->next;
  540.     while ( *pnt == ' ' || *pnt == 't' )
  541. pnt++;
  542. } while ( *pnt );
  543. /*
  544.  * Create a conditional for this object's dependencies.
  545.  */
  546. {
  547.     char fake_if [1024];
  548.     struct dependency * dep;
  549.     struct condition ** cond_ptr;
  550.     int first = 1;
  551.     cond_ptr = &(cfg->cond);
  552.     for ( dep = cfg->depend; dep; dep = dep->next )
  553.     {
  554. if ( token == token_dep_tristate
  555. && ! strcmp( dep->name, "CONSTANT_M" ) )
  556. {
  557.     continue;
  558. }
  559. if ( first )
  560. {
  561.     first = 0;
  562. }
  563. else
  564. {
  565.     *cond_ptr = malloc( sizeof(struct condition) );
  566.     memset( *cond_ptr, 0, sizeof(struct condition) );
  567.     (*cond_ptr)->op = op_and;
  568.     cond_ptr = &(*cond_ptr)->next;
  569. }
  570. *cond_ptr = malloc( sizeof(struct condition) );
  571. memset( *cond_ptr, 0, sizeof(struct condition) );
  572. (*cond_ptr)->op = op_lparen;
  573. if ( token == token_dep_bool )
  574.     sprintf( fake_if, "[ "$%s" = "y" -o "$%s" = "" ]; then",
  575. dep->name, dep->name );
  576. else
  577.     sprintf( fake_if, "[ "$%s" = "y" -o "$%s" = "m" -o "$%s" = "" ]; then",
  578. dep->name, dep->name, dep->name );
  579. (*cond_ptr)->next = tokenize_if( fake_if );
  580. while ( *cond_ptr )
  581.     cond_ptr = &(*cond_ptr)->next;
  582. *cond_ptr = malloc( sizeof(struct condition) );
  583. memset( *cond_ptr, 0, sizeof(struct condition) );
  584. (*cond_ptr)->op = op_rparen;
  585. cond_ptr = &(*cond_ptr)->next;
  586.     }
  587. }
  588. break;
  589.     case token_else:
  590.     case token_endmenu:
  591.     case token_fi:
  592. break;
  593.     case token_hex:
  594.     case token_int:
  595. pnt = get_qstring ( pnt, &cfg->label );
  596. pnt = get_string  ( pnt, &buffer );
  597. cfg->nameindex = get_varnum( buffer );
  598. pnt = get_string  ( pnt, &cfg->value );
  599. break;
  600.     case token_string:
  601. pnt = get_qstring ( pnt, &cfg->label );
  602. pnt = get_string  ( pnt, &buffer );
  603. cfg->nameindex = get_varnum( buffer );
  604. pnt = get_qnqstring  ( pnt, &cfg->value );
  605. if (cfg->value == NULL)
  606.     syntax_error( "missing initial value" );
  607. break;
  608.     case token_if:
  609. cfg->cond = tokenize_if( pnt );
  610. break;
  611.     case token_mainmenu_name:
  612. pnt = get_qstring( pnt, &cfg->label );
  613. break;
  614.     case token_mainmenu_option:
  615. if ( strncmp( pnt, "next_comment", 12 ) == 0 )
  616.     last_menuoption = cfg;
  617. else
  618.     pnt = get_qstring( pnt, &cfg->label );
  619. break;
  620.     case token_unset:
  621. pnt = get_string( pnt, &buffer );
  622. cfg->nameindex = get_varnum( buffer );
  623. while ( *pnt == ' ' || *pnt == 't' )
  624.     pnt++;
  625. while (*pnt)
  626. {
  627.     cfg->next = (struct kconfig *) malloc( sizeof(struct kconfig) );
  628.     memset( cfg->next, 0, sizeof(struct kconfig) );
  629.     cfg = cfg->next;
  630.     cfg->token = token_unset;
  631.     pnt = get_string( pnt, &buffer );
  632.     cfg->nameindex = get_varnum( buffer );
  633.     while ( *pnt == ' ' || *pnt == 't' )
  634. pnt++;
  635. }
  636. break;
  637.     }
  638.     return;
  639. }
  640. /*
  641.  * Implement the "source" command.
  642.  */
  643. static void do_source( const char * filename )
  644. {
  645.     char buffer [2048];
  646.     FILE * infile;
  647.     const char * old_file;
  648.     int old_lineno;
  649.     int offset;
  650.     /* open the file */
  651.     if ( strcmp( filename, "-" ) == 0 )
  652. infile = stdin;
  653.     else
  654. infile = fopen( filename, "r" );
  655.     /* if that failed, try ../filename */
  656.     if ( infile == NULL )
  657.     {
  658. sprintf( buffer, "../%s", filename );
  659. infile = fopen( buffer, "r" );
  660.     }
  661.     if ( infile == NULL )
  662.     {
  663. sprintf( buffer, "unable to open %s", filename );
  664. syntax_error( buffer );
  665.     }
  666.     /* push the new file name and line number */
  667.     old_file     = current_file;
  668.     old_lineno   = lineno;
  669.     current_file = filename;
  670.     lineno       = 0;
  671.     /* read and process lines */
  672.     for ( offset = 0; ; )
  673.     {
  674. char * pnt;
  675. /* read a line */
  676. fgets( buffer + offset, sizeof(buffer) - offset, infile );
  677. if ( feof( infile ) )
  678.     break;
  679. lineno++;
  680. /* strip the trailing return character */
  681. pnt = buffer + strlen(buffer) - 1;
  682. if ( *pnt == 'n' )
  683.     *pnt-- = '';
  684. /* eat  NL pairs */
  685. if ( *pnt == '\' )
  686. {
  687.     offset = pnt - buffer;
  688.     continue;
  689. }
  690. /* tokenize this line */
  691. tokenize_line( buffer );
  692. offset = 0;
  693.     }
  694.     /* that's all, folks */
  695.     if ( infile != stdin )
  696. fclose( infile );
  697.     current_file = old_file;
  698.     lineno       = old_lineno;
  699.     return;
  700. }
  701. /*
  702.  * Main program.
  703.  */
  704. int main( int argc, const char * argv [] )
  705. {
  706.     do_source        ( "-"         );
  707.     fix_conditionals ( config_list );
  708.     dump_tk_script   ( config_list );
  709.     free(vartable);
  710.     return 0;
  711. }