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
|
/*
* cook - file construction tool
* Copyright (C) 1993, 1994, 1996-1999, 2001, 2003 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manage command line options
*
* The options may be set at various levels. The level with the highest
* precedence which has actually been set is used to determine the option
* value at any given time.
*
* Each level of an option is represented by 2 bits in the flag word. One bit
* is used to indicate that the option has been set for that level, the other
* bit indicates the state. Determining the least set bit in an expression is
* cheap (x&-x) so highest priority is the lowest numbered level.
*
* The COOK enviroment variable is basically a replacement for the defaults,
* so that users can change the default behaviour. The command line overrides
* almost everything. The error level is the only level with higher
* precedence than the command line, and it is used to prevent disasters
* after parse errors or interrupts have happened.
*
* -------------------------------------------------------------------------
*
*
* If you are going to add a new recipe flag (set by the "set" statement,
* or the "set" clause of a recipe) you need to change all of the
* following places:
*
* cook/option.h
* to define the OPTION_ value
* cook/option.c
* option_tidyup()
* if the option defaults to true
* option_set_errors()
* if the option should be turned off once cookbook errors
* are encountered.
* option_number_name()
* for the name of the option
* cook/flag.h
* to define the RF_ values (RF stands for Recipe Flag)
* cook/flag.c
* to define the RF_ names
* langu.flags.so
* to document the recipe flag
*
* If you choose to make it a command line option,
* you must also update these files:
*
* cook/main.c
* to define the new command line option and process it
* (only if it should also be a command line option)
* cook/builtin/options.c
* to access the option from within the cookbook (typically used
* for recursive cook invokations)
* lib/en/man1/cook.1
* to document it, if you added a new command line option
*/
#include <ac/ctype.h>
#include <ac/limits.h>
#include <ac/stddef.h>
#include <ac/stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/time.h>
#include <libdir.h>
#include <mem.h>
#include <option.h>
#include <os_interface.h>
#include <progname.h>
#include <trace.h>
typedef struct flag_ty flag_ty;
struct flag_ty
{
unsigned long o_flag[OPTION_max];
};
option_ty option;
static flag_ty flag;
/*
* NAME
* option_set - set an option
*
* SYNOPSIS
* void option_set(option_number_ty num, option_level_ty lvl, int state);
*
* DESCRIPTION
* The option_set function is used to set the given option at the given
* level to the given state.
*
* RETURNS
* void
*/
void
option_set(o, level, state)
option_number_ty o;
option_level_ty level;
int state;
{
trace(("option_set(o = %s, level = %s, state = %d)\n{\n",
option_number_name(o), option_level_name(level), state));
assert((int)o >= 0 && (int)o < (int)OPTION_max);
if (state)
flag.o_flag[(size_t)o] |= 3L << (2 * (int)level);
else
flag.o_flag[(size_t)o] |= 1L << (2 * (int)level);
trace(("}\n"));
}
/*
* NAME
* option_already - see if an option is already set
*
* SYNOPSIS
* int option_already(option_number_ty num, option_level_ty lvl);
*
* DESCRIPTION
* The option_already function is used to test if a given option at a
* given level has been set.
*
* RETURNS
* int: zero if the option has not been set, nonzero if it has.
*/
int
option_already(o, level)
option_number_ty o;
option_level_ty level;
{
int result;
trace(("option_already(o = %s, level = %s)\n{\n",
option_number_name(o), option_level_name(level)));
assert((int)o >= 0 && (int)o < (int)OPTION_max);
result = (((flag.o_flag[(size_t)o] >> (2 * (int)level)) & 1) != 0);
trace(("return %d;\n", result));
trace(("}\n"));
return result;
}
/*
* NAME
* option_undo - remove option setting
*
* SYNOPSIS
* void option_undo(option_number_ty num, option_level_ty lvl);
*
* DESCRIPTION
* The option_undo function is used to is used to remove the option
* setting for the given option at the given level.
*
* RETURNS
* void
*/
void
option_undo(o, level)
option_number_ty o;
option_level_ty level;
{
trace(("option_undo(o = %s, level = %s)\n{\n", option_number_name(o),
option_level_name(level)));
assert((int)o >= 0 && (int)o < (int)OPTION_max);
flag.o_flag[(size_t)o] &= ~(3L << (2 * (int)level));
trace(("}\n"));
}
/*
* NAME
* option_undo_level - remove options settings
*
* SYNOPSIS
* void option_undo_level(option_level_ty lvl);
*
* DESCRIPTION
* The option_undo_level function is used to remove the settings for all
* options at a given level.
*
* RETURNS
* void
*/
void
option_undo_level(level)
option_level_ty level;
{
int o;
trace(("option_undo_level(level = %s)\n{\n", option_level_name(level)));
for (o = 0; o < (int)OPTION_max; ++o)
option_undo((option_number_ty)o, level);
trace(("}\n"));
}
/*
* NAME
* option_test - test an option
*
* SYNOPSIS
* int option_test(option_number_ty num);
*
* DESCRIPTION
* The option_test function is used to test the setting of an option.
* The level of highest precedence which hash been set is used.
*
* RETURNS
* int: zero if the option is off, nonzero if the option is on.
*/
int
option_test(o)
option_number_ty o;
{
unsigned long *op;
unsigned long mask;
int result;
trace(("option_test(o = %s)\n{\n", option_number_name(o)));
assert((int)o >= 0 && (int)o < (int)OPTION_max);
op = &flag.o_flag[(size_t)o];
mask = *op & 0x55555555;
mask &= -mask; /* get LSB */
result = ((*op & (mask << 1)) != 0);
trace(("return %d;\n", result));
trace(("}\n"));
return result;
}
static string_ty *Capitalize _((string_ty *));
static string_ty *
Capitalize(s)
string_ty *s;
{
if (s->str_length < 1 || !islower((unsigned char)s->str_text[0]))
return str_copy(s);
return
str_format
(
"%c%s",
toupper((unsigned char)s->str_text[0]),
s->str_text + 1
);
}
/*
* NAME
* option_tidy_up - mother hen
*
* SYNOPSIS
* void option_tidy_up(void);
*
* DESCRIPTION
* The option_tidy_up function is used to set a few defaults, and tidy up
* after the command line.
*
* RETURNS
* void
*
* CAVEAT
* Must be called after the command line has been parsed.
*/
void
option_tidy_up()
{
string_ty *s;
string_ty *s1;
/*
* Set the defaults before we do anything else,
* the rest of tidy_up depends on them.
*/
trace(("option_tidy_up()\n{\n"));
option_set(OPTION_ACTION, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_CASCADE, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_FINGERPRINT_WRITE, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_INCLUDE_COOKED, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_INCLUDE_COOKED_WARNING, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_LOGGING, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_STRIP_DOT, OPTION_LEVEL_DEFAULT, 1);
option_set(OPTION_TERMINAL, OPTION_LEVEL_DEFAULT, 1);
/*
* user's library
*/
s = os_accdir();
assert(s);
s1 = str_format("%S/.%s", s, progname_get());
str_free(s);
string_list_append_unique(&option.o_search_path, s1);
str_free(s1);
/*
* cook's library
* architecture-specific, then architecture-neutral
*/
s = str_from_c(library_directory_get());
string_list_append_unique(&option.o_search_path, s);
str_free(s);
s = str_from_c(data_directory_get());
string_list_append_unique(&option.o_search_path, s);
str_free(s);
if (!option.o_book)
{
static char *name[] =
{
".how.to.%s",
".howto.%s",
"how.to.%s",
"howto.%s",
"%s.file",
"%sfile",
"%s.book",
"%sbook",
".%s.rc",
".%src",
};
int j;
/*
* A huge range of alternative default names is given.
* The first found will be used.
*/
for (j = 0; j < SIZEOF(name); j++)
{
s = str_format(name[j], progname_get());
switch (os_exists(s))
{
case -1:
exit(1);
case 0:
s1 = Capitalize(s);
str_free(s);
s = s1;
switch (os_exists(s))
{
case -1:
exit(1);
case 0:
str_free(s);
continue;
case 1:
option.o_book = s;
break;
}
break;
case 1:
option.o_book = s;
break;
}
break;
}
}
if (!option.o_logfile && option.o_book)
{
char *sp;
char *cp;
sp = option.o_book->str_text;
/* skip first char in case it's a '.' */
cp = strrchr(sp + 1, '.');
if (cp)
s = str_n_from_c(sp, cp - sp);
else
s = str_copy(option.o_book);
sp = (option_test(OPTION_CMDFILE) ? "sh" : "list");
option.o_logfile = str_format("%S.%s", s, sp);
str_free(s);
}
trace(("}\n"));
}
/*
* NAME
* option_set_errors - set error flags
*
* SYNOPSIS
* void option_set_errors(void);
*
* DESCRIPTION
* The option_set_errors function is used to set the appropriate options
* to prevent undesirable side effects when errors occur.
*
* RETURNS
* void
*/
void
option_set_errors()
{
trace(("option_set_errors()\n{\n"));
option_set(OPTION_SILENT, OPTION_LEVEL_ERROR, 0);
option_set(OPTION_ACTION, OPTION_LEVEL_ERROR, 0);
option_set(OPTION_ERROK, OPTION_LEVEL_ERROR, 0);
option_set(OPTION_METER, OPTION_LEVEL_ERROR, 0);
option_set(OPTION_PERSEVERE, OPTION_LEVEL_ERROR, 0);
trace(("}\n"));
}
void *
option_flag_state_get()
{
flag_ty *fp;
fp = mem_alloc(sizeof(flag_ty));
*fp = flag;
return fp;
}
void
option_flag_state_set(p)
void *p;
{
flag_ty *fp;
fp = p;
flag = *fp;
mem_free(p);
}
const char *
option_level_name(lvl)
option_level_ty lvl;
{
switch (lvl)
{
case OPTION_LEVEL_ERROR: return "OPTION_LEVEL_ERROR";
case OPTION_LEVEL_AUTO: return "OPTION_LEVEL_AUTO";
case OPTION_LEVEL_COMMAND_LINE: return "OPTION_LEVEL_COMMAND_LINE";
case OPTION_LEVEL_EXECUTE: return "OPTION_LEVEL_EXECUTE";
case OPTION_LEVEL_RECIPE: return "OPTION_LEVEL_RECIPE";
case OPTION_LEVEL_COOKBOOK: return "OPTION_LEVEL_COOKBOOK";
case OPTION_LEVEL_ENVIRONMENT: return "OPTION_LEVEL_ENVIRONMENT";
case OPTION_LEVEL_DEFAULT: return "OPTION_LEVEL_DEFAULT";
}
return "option level unknown";
}
const char *
option_number_name(o)
option_number_ty o;
{
switch (o)
{
case OPTION_ACTION:
return "OPTION_ACTION";
case OPTION_BOOK:
return "OPTION_BOOK";
case OPTION_CASCADE:
return "OPTION_CASCADE";
case OPTION_CMDFILE:
return "OPTION_CMDFILE";
case OPTION_DISASSEMBLE:
return "OPTION_DISASSEMBLE";
case OPTION_ERROK:
return "OPTION_ERROK";
case OPTION_FINGERPRINT:
return "OPTION_FINGERPRINT";
case OPTION_FINGERPRINT_WRITE:
return "OPTION_FINGERPRINT_WRITE";
case OPTION_FORCE:
return "OPTION_FORCE";
case OPTION_GATEFIRST:
return "OPTION_GATEFIRST";
case OPTION_IMPLICIT_ALLOWED:
return "OPTION_IMPLICIT_ALLOWED";
case OPTION_INCLUDE_COOKED:
return "OPTION_INCLUDE_COOKED";
case OPTION_INCLUDE_COOKED_WARNING:
return "OPTION_INCLUDE_COOKED_WARNING";
case OPTION_INGREDIENTS_FINGERPRINT:
return "OPTION_INGREDIENTS_FINGERPRINT";
case OPTION_INVALIDATE_STAT_CACHE:
return "OPTION_INVALIDATE_STAT_CACHE";
case OPTION_LOGGING:
return "OPTION_LOGGING";
case OPTION_METER:
return "OPTION_METER";
case OPTION_MKDIR:
return "OPTION_MKDIR";
case OPTION_PERSEVERE:
return "OPTION_PERSEVERE";
case OPTION_PRECIOUS:
return "OPTION_PRECIOUS";
case OPTION_REASON:
return "OPTION_REASON";
case OPTION_RECURSE:
return "OPTION_RECURSE";
case OPTION_SHALLOW:
return "OPTION_SHALLOW";
case OPTION_SILENT:
return "OPTION_SILENT";
case OPTION_STAR:
return "OPTION_STAR";
case OPTION_STRIP_DOT:
return "OPTION_STRIP_DOT";
case OPTION_TERMINAL:
return "OPTION_TERMINAL";
case OPTION_TOUCH:
return "OPTION_TOUCH";
case OPTION_UNLINK:
return "OPTION_UNLINK";
case OPTION_UPDATE:
return "OPTION_UPDATE";
case OPTION_UPDATE_MAX:
return "OPTION_UPDATE_MAX";
case OPTION_MATCH_MODE_REGEX:
return "OPTION_MATCH_MODE_REGEX";
case OPTION_TELL_POSITION:
return "OPTION_TELL_POSITION";
case OPTION_max:
break;
}
return "option number unknown";
}
|