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
|
/*
* Copyright 1993, 1995 Christopher Seiwald.
*
* This file is part of Jam - see jam.c for Copyright information.
*/
# include "jam.h"
# include "lists.h"
# include "variable.h"
# include "expand.h"
# include "filesys.h"
# include "newstr.h"
/*
* expand.c - expand a buffer, given variable values
*
* External routines:
*
* var_expand() - variable-expand input string into list of strings
*
* Internal routines:
*
* var_edit() - copy input target name to output, performing : modifiers
* var_mods() - parse : modifiers into FILENAME structure
*
* 01/25/94 (seiwald) - $(X)$(UNDEF) was expanding like plain $(X)
* 04/13/94 (seiwald) - added shorthand L0 for null list pointer
*/
typedef struct {
char downshift; /* :L -- downshift result */
char upshift; /* :U -- upshift result */
char parent; /* :P -- go to parent directory */
char to_slashes; /* :T -- convert "\" to "/" */
} VAR_ACTS ;
static void var_edit( char *in, char *mods, char *out );
static void var_mods( char *mods, FILENAME *f, VAR_ACTS *acts );
# define MAGIC_COLON '\001'
# define MAGIC_LEFT '\002'
# define MAGIC_RIGHT '\003'
/*
* var_expand() - variable-expand input string into list of strings
*
* Would just copy input to output, performing variable expansion,
* except that since variables can contain multiple values the result
* of variable expansion may contain multiple values (a list). Properly
* performs "product" operations that occur in "$(var1)xxx$(var2)" or
* even "$($(var2))".
*
* Returns a newly created list.
*/
LIST *
var_expand(
LIST *l,
char *in,
char *end,
LOL *lol,
int cancopyin )
{
char out_buf[ MAXSYM ];
char *out = out_buf;
char *inp = in;
char *ov; /* for temp copy of variable in outbuf */
int depth;
if( DEBUG_VAREXP )
printf( "expand '%.*s'\n", end - in, in );
/* This gets alot of cases: $(<) and $(>) */
if( in[0] == '$' && in[1] == '(' && in[3] == ')' && !in[4] )
{
switch( in[2] )
{
case '1':
case '<':
return list_copy( l, lol_get( lol, 0 ) );
case '2':
case '>':
return list_copy( l, lol_get( lol, 1 ) );
}
}
/* Just try simple copy of in to out. */
while( in < end )
if( ( *out++ = *in++ ) == '$' && *in == '(' )
goto expand;
/* No variables expanded - just add copy of input string to list. */
/* Cancopyin is an optimization: if the input was already a list */
/* item, we can use the copystr() to put it on the new list. */
/* Otherwise, we use the slower newstr(). */
*out = '\0';
if( cancopyin )
return list_new( l, copystr( inp ) );
else
return list_new( l, newstr( out_buf ) );
expand:
/*
* Input so far (ignore blanks):
*
* stuff-in-outbuf $(variable) remainder
* ^ ^
* in end
* Output so far:
*
* stuff-in-outbuf $
* ^ ^
* out_buf out
*
*
* We just copied the $ of $(...), so back up one on the output.
* We now find the matching close paren, copying the variable and
* modifiers between the $( and ) temporarily into out_buf, so that
* we can replace :'s with MAGIC_COLON. This is necessary to avoid
* being confused by modifier values that are variables containing
* :'s. Ugly.
*/
depth = 1;
out--, in++;
ov = out;
while( in < end && depth )
{
switch( *ov++ = *in++ )
{
case '(': depth++; break;
case ')': depth--; break;
case ':': ov[-1] = MAGIC_COLON; break;
case '[': ov[-1] = MAGIC_LEFT; break;
case ']': ov[-1] = MAGIC_RIGHT; break;
}
}
/* Copied ) - back up. */
ov--;
/*
* Input so far (ignore blanks):
*
* stuff-in-outbuf $(variable) remainder
* ^ ^
* in end
* Output so far:
*
* stuff-in-outbuf variable
* ^ ^ ^
* out_buf out ov
*
* Later we will overwrite 'variable' in out_buf, but we'll be
* done with it by then. 'variable' may be a multi-element list,
* so may each value for '$(variable element)', and so may 'remainder'.
* Thus we produce a product of three lists.
*/
{
LIST *variables = 0;
LIST *remainder = 0;
LIST *vars;
/* Recursively expand variable name & rest of input */
if( out < ov )
variables = var_expand( L0, out, ov, lol, 0 );
if( in < end )
remainder = var_expand( L0, in, end, lol, 0 );
/* Now produce the result chain */
/* For each variable name */
for( vars = variables; vars; vars = list_next( vars ) )
{
LIST *value;
char *colon;
char *bracket;
char varname[ MAXSYM ];
int i, sub1, sub2;
/* Look for a : modifier in the variable name */
/* Must copy into varname so we can modify it */
strcpy( varname, vars->string );
if( colon = strchr( varname, MAGIC_COLON ) )
*colon = '\0';
if( bracket = strchr( varname, MAGIC_LEFT ) )
{
char *dash;
if( dash = strchr( bracket + 1, '-' ) )
{
*dash = '\0';
sub1 = atoi( bracket + 1 );
sub2 = atoi( dash + 1 );
}
else
{
sub1 = sub2 = atoi( bracket + 1 );
}
*bracket = '\0';
}
else
{
sub1 = sub2 = 0; /* not needed */
}
/* Get variable value, specially handling $(<), $(>), $(n) */
if( varname[0] == '<' && !varname[1] )
{
value = lol_get( lol, 0 );
}
else if( varname[0] == '>' && !varname[1] )
{
value = lol_get( lol, 1 );
}
else if( varname[0] >= '1' && varname[0] <= '9' && !varname[1] )
{
value = lol_get( lol, varname[0] - '1' );
}
else
{
value = var_get( varname );
}
/* The fast path: $(x) - just copy the variable value. */
if( out == out_buf && !bracket && !colon && in == end )
{
l = list_copy( l, value );
continue;
}
/* For each variable value */
for( i = 1; value; i++, value = list_next( value ) )
{
LIST *rem;
char *out1;
/* Skip members not in subscript */
if( bracket && ( i < sub1 || sub2 && i > sub2 ) )
continue;
/* Apply : mods, if present */
if( colon )
var_edit( value->string, colon + 1, out );
else
strcpy( out, value->string );
/* If no remainder, append result to output chain. */
if( in == end )
{
l = list_new( l, newstr( out_buf ) );
continue;
}
/* Remember the end of the variable expansion so */
/* we can just tack on each instance of 'remainder' */
out1 = out + strlen( out );
/* For each remainder, or just once if no remainder, */
/* append the complete string to the output chain */
for( rem = remainder; rem; rem = list_next( rem ) )
{
strcpy( out1, rem->string );
l = list_new( l, newstr( out_buf ) );
}
}
}
/* variables & remainder were gifts from var_expand */
/* and must be freed */
if( variables )
list_free( variables );
if( remainder)
list_free( remainder );
if( DEBUG_VAREXP )
{
printf( "expanded to " );
list_print( l );
printf( "\n" );
}
return l;
}
}
/*
* var_edit() - copy input target name to output, performing : modifiers
*/
static void
var_edit(
char *in,
char *mods,
char *out )
{
FILENAME oldf, newf;
VAR_ACTS acts;
/* Parse apart original filename, putting parts into "oldf" */
file_parse( in, &oldf );
/* Parse apart modifiers, putting them into "newf" */
var_mods( mods, &newf, &acts );
/* Replace any oldf with newf */
if( newf.f_grist.ptr )
oldf.f_grist = newf.f_grist;
if( newf.f_root.ptr )
oldf.f_root = newf.f_root;
if( newf.f_dir.ptr )
oldf.f_dir = newf.f_dir;
if( newf.f_base.ptr )
oldf.f_base = newf.f_base;
if( newf.f_suffix.ptr )
oldf.f_suffix = newf.f_suffix;
if( newf.f_member.ptr )
oldf.f_member = newf.f_member;
/* If requested, modify oldf to point to parent */
if( acts.parent )
file_parent( &oldf );
/* Put filename back together */
file_build( &oldf, out, 0 );
/* Handle upshifting, downshifting now */
if( acts.upshift )
{
for( ; *out; ++out )
*out = toupper( *out );
}
else if( acts.downshift )
{
for( ; *out; ++out )
*out = tolower( *out );
}
/* Handle conversion of "\" to "/" */
else if ( acts.to_slashes )
{
for ( ; *out; ++out )
if ( *out == '\\' )
*out = '/';
}
}
/*
* var_mods() - parse : modifiers into FILENAME structure
*
* The : modifiers in a $(varname:modifier) currently support replacing
* or omitting elements of a filename, and so they are parsed into a
* FILENAME structure (which contains pointers into the original string).
*
* Modifiers of the form "X=value" replace the component X with
* the given value. Modifiers without the "=value" cause everything
* but the component X to be omitted. X is one of:
*
* G <grist>
* D directory name
* B base name
* S .suffix
* M (member)
* R root directory - prepended to whole path
*
* This routine sets:
*
* f->f_xxx.ptr = 0
* f->f_xxx.len = 0
* -> leave the original component xxx
*
* f->f_xxx.ptr = string
* f->f_xxx.len = strlen( string )
* -> replace component xxx with string
*
* f->f_xxx.ptr = ""
* f->f_xxx.len = 0
* -> omit component xxx
*
* var_edit() above and file_build() obligingly follow this convention.
*/
static void
var_mods(
char *mods,
FILENAME *f,
VAR_ACTS *acts )
{
char *flags = "GRDBSMT";
int havezeroed = 0;
memset( (char *)f, 0, sizeof( *f ) );
memset( (char *)acts, 0, sizeof( *acts ) );
while( *mods )
{
char *fl;
FILEPART *fp;
/* First take care of :U or :L (upshift, downshift) */
if( *mods == 'L' )
{
acts->downshift = 1;
++mods;
continue;
}
else if( *mods == 'U' )
{
acts->upshift = 1;
++mods;
continue;
}
else if( *mods == 'P' )
{
acts->parent = 1;
++mods;
continue;
}
else if ( *mods == 'T' )
{
acts->to_slashes = 1;
++mods;
continue;
}
/* Now handle the file component flags */
if( !( fl = strchr( flags, *mods++ ) ) )
break; /* should complain, but so what... */
fp = &f->part[ fl - flags ];
if( *mods++ != '=' )
{
/* :X - turn everything but X off */
int i;
mods--;
if( !havezeroed++ )
for( i = 0; i < 6; i++ )
{
f->part[ i ].len = 0;
f->part[ i ].ptr = "";
}
fp->ptr = 0;
}
else
{
/* :X=value - set X to value */
char *p;
if( p = strchr( mods, MAGIC_COLON ) )
{
fp->ptr = mods;
fp->len = p - mods;
mods = p + 1;
}
else
{
fp->ptr = mods;
fp->len = strlen( mods );
mods += fp->len;
}
}
}
}
|