📄 variable.c
字号:
if (out >= oute) return -1; /* Don't increment, intentionally. */ *out= '\0'; /* If a variable encountered, expand it and and embed the */ /* space-separated members of the list in the output. */ if( dollar ) { LIST *l; l = var_expand( L0, lastword, out, lol, 0 ); out = lastword; while ( l ) { int so = strlen( l->string ); if( out + so >= oute ) return -1; strcpy( out, l->string ); out += so; l = list_next( l ); if ( l ) *out++ = ' '; } list_free( l ); } } if( out >= oute ) return -1; *out++ = '\0'; return out - out0;}void var_string_to_file( const char * in, int insize, const char * out, LOL * lol ){ const char * ine = in+insize; FILE * out_file = 0; int out_debug = DEBUG_EXEC ? 1 : 0; if ( globs.noexec ) { /* out_debug = 1; */ } else if ( strcmp( out, "STDOUT" ) == 0 ) { out_file = stdout; } else if ( strcmp( out, "STDERR" ) == 0 ) { out_file = stderr; } else { /* Handle "path to file" filenames. */ string out_name; if ( out[0] == '"' && out[strlen(out) - 1] == '"' ) { string_copy(&out_name,out+1); string_truncate(&out_name,out_name.size-1); } else { string_copy(&out_name,out); } out_file = fopen( out_name.value, "w" ); if (!out_file) { printf( "failed to write output file '%s'!\n", out_name.value ); exit( EXITBAD ); } string_free(&out_name); } if ( out_debug ) printf("\nfile %s\n",out); while( *in && in < ine ) { int dollar = 0; const char * output_0 = in; const char * output_1 = in; /* Copy white space */ while ( output_1 < ine && *output_1 && isspace( *output_1 ) ) { ++output_1; } if ( output_0 < output_1 ) { if ( out_file ) fwrite(output_0,output_1-output_0,1,out_file); if ( out_debug ) fwrite(output_0,output_1-output_0,1,stdout); } output_0 = output_1; /* Copy non-white space, watching for variables */ while( output_1 < ine && *output_1 && !isspace( *output_1 ) ) { if( output_1[0] == '$' && output_1[1] && output_1[1] == '(' ) { dollar++; } ++output_1; } /* If a variable encountered, expand it and embed the */ /* space-separated members of the list in the output. */ if( dollar ) { LIST *l; l = var_expand( L0, (char*)output_0, (char*)output_1, lol, 0 ); while ( l ) { if ( out_file ) fputs( l->string, out_file ); if ( out_debug ) puts( l->string ); l = list_next( l ); if ( l ) { if ( out_file ) fputc( ' ', out_file ); if ( out_debug ) fputc( ' ', stdout ); } } list_free( l ); } else if ( output_0 < output_1 ) { if ( out_file ) fwrite(output_0,output_1-output_0,1,out_file); if ( out_debug ) fwrite(output_0,output_1-output_0,1,stdout); } in = output_1; } if ( out_file && out_file != stdout && out_file != stderr ) { fflush( out_file ); fclose( out_file ); } if ( out_debug ) fputc('\n',stdout);}/* * var_get() - get value of a user defined symbol * * Returns NULL if symbol unset. */LIST *var_get( char *symbol ){ LIST * result = 0; #ifdef OPT_AT_FILES /* Some "fixed" variables... */ if ( strcmp( "TMPDIR", symbol ) == 0 ) { result = list_new( L0, newstr( (char*)path_tmpdir() ) ); } else if ( strcmp( "TMPNAME", symbol ) == 0 ) { result = list_new( L0, newstr( (char*)path_tmpnam() ) ); } else if ( strcmp( "TMPFILE", symbol ) == 0 ) { result = list_new( L0, newstr( (char*)path_tmpfile() ) ); } else if ( strcmp( "STDOUT", symbol ) == 0 ) { result = list_new( L0, newstr( "STDOUT" ) ); } else if ( strcmp( "STDERR", symbol ) == 0 ) { result = list_new( L0, newstr( "STDERR" ) ); } else #endif { VARIABLE var, *v = &var; v->symbol = symbol; if( varhash && hashcheck( varhash, (HASHDATA **)&v ) ) { if( DEBUG_VARGET ) var_dump( v->symbol, v->value, "get" ); result = v->value; } } return result;}/* * var_set() - set a variable in jam's user defined symbol table * * 'flag' controls the relationship between new and old values of * the variable: SET replaces the old with the new; APPEND appends * the new to the old; DEFAULT only uses the new if the variable * was previously unset. * * Copies symbol. Takes ownership of value. */voidvar_set( char *symbol, LIST *value, int flag ){ VARIABLE *v = var_enter( symbol ); if( DEBUG_VARSET ) var_dump( symbol, value, "set" ); switch( flag ) { case VAR_SET: /* Replace value */ list_free( v->value ); v->value = value; break; case VAR_APPEND: /* Append value */ v->value = list_append( v->value, value ); break; case VAR_DEFAULT: /* Set only if unset */ if( !v->value ) v->value = value; else list_free( value ); break; }}/* * var_swap() - swap a variable's value with the given one */LIST *var_swap( char *symbol, LIST *value ){ VARIABLE *v = var_enter( symbol ); LIST *oldvalue = v->value; if( DEBUG_VARSET ) var_dump( symbol, value, "set" ); v->value = value; return oldvalue;}/* * var_enter() - make new var symbol table entry, returning var ptr */static VARIABLE *var_enter( char *symbol ){ VARIABLE var, *v = &var; if( !varhash ) varhash = hashinit( sizeof( VARIABLE ), "variables" ); v->symbol = symbol; v->value = 0; if( hashenter( varhash, (HASHDATA **)&v ) ) v->symbol = newstr( symbol ); /* never freed */ return v;}/* * var_dump() - dump a variable to stdout */static voidvar_dump( char *symbol, LIST *value, char *what ){ printf( "%s %s = ", what, symbol ); list_print( value ); printf( "\n" );}/* * var_done() - free variable tables */static void delete_var_( void* xvar, void* data ){ VARIABLE *v = (VARIABLE*)xvar; freestr( v->symbol ); list_free( v-> value );}voidvar_done(){ hashenumerate( varhash, delete_var_, (void*)0 ); hashdone( varhash );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -