comparison www/code.html @ 432:01473712c9fe

Document that optflags is always an int (so 32 bit and 64 bit platforms behave the same).
author Rob Landley <rob@landley.net>
date Mon, 06 Feb 2012 21:15:19 -0600
parents 2cbbd4c5eaaa
children 8abb9e307d0c
comparison
equal deleted inserted replaced
431:87edfe8ae99e 432:01473712c9fe
526 <p><b>long toys.optflags</b></p> 526 <p><b>long toys.optflags</b></p>
527 527
528 <p>Each option in the optflags string corresponds to a bit position in 528 <p>Each option in the optflags string corresponds to a bit position in
529 toys.optflags, with the same value as a corresponding binary digit. The 529 toys.optflags, with the same value as a corresponding binary digit. The
530 rightmost argument is (1<<0), the next to last is (1<<1) and so on. If 530 rightmost argument is (1<<0), the next to last is (1<<1) and so on. If
531 the option isn't encountered while parsing argv[], its bit remains 0. 531 the option isn't encountered while parsing argv[], its bit remains 0.</p>
532 (Since toys.optflags is a long, it's only guaranteed to store 32 bits.) 532
533 For example, 533 <p>For example,
534 the optflags string "abcd" would parse the command line argument "-c" to set 534 the optflags string "abcd" would parse the command line argument "-c" to set
535 optflags to 2, "-a" would set optflags to 8, "-bd" would set optflags to 535 optflags to 2, "-a" would set optflags to 8, "-bd" would set optflags to
536 6 (I.E. 4|2), and "-a -c" would set optflags to 10 (2|8).</p> 536 6 (I.E. 4|2), and "-a -c" would set optflags to 10 (2|8).</p>
537 537
538 <p>Only letters are relevant to optflags, punctuation is skipped: in the 538 <p>Only letters are relevant to optflags, punctuation is skipped: in the
539 string "a*b:c#d", d=1, c=2, b=4, a=8. The punctuation after a letter 539 string "a*b:c#d", d=1, c=2, b=4, a=8. The punctuation after a letter
540 usually indicate that the option takes an argument.</p> 540 usually indicate that the option takes an argument.</p>
541
542 <p>Since toys.optflags is an unsigned int, it only stores 32 bits. (Which is
543 the amount a long would have on 32-bit platforms anyway; 64 bit code on
544 32 bit platforms is too expensive to require in common code used by almost
545 all commands.) Bit positions beyond the 1<<31 aren't recorded, but
546 parsing higher options can still set global variables.</p>
541 547
542 <p><b>Automatically setting global variables from arguments (union this)</b></p> 548 <p><b>Automatically setting global variables from arguments (union this)</b></p>
543 549
544 <p>The following punctuation characters may be appended to an optflags 550 <p>The following punctuation characters may be appended to an optflags
545 argument letter, indicating the option takes an additional argument:</p> 551 argument letter, indicating the option takes an additional argument:</p>
574 with the rightmost saved in this[0]. Again using "a*b:c#d", "-c 42" would set 580 with the rightmost saved in this[0]. Again using "a*b:c#d", "-c 42" would set
575 this[0]=42; and "-b 42" would set this[1]="42"; each slot is left NULL if 581 this[0]=42; and "-b 42" would set this[1]="42"; each slot is left NULL if
576 the corresponding argument is not encountered.</p> 582 the corresponding argument is not encountered.</p>
577 583
578 <p>This behavior is useful because the LP64 standard ensures long and pointer 584 <p>This behavior is useful because the LP64 standard ensures long and pointer
579 are the same size, and C99 guarantees structure members will occur in memory 585 are the same size. C99 guarantees structure members will occur in memory
580 in the 586 in the same order they're declared, and that padding won't be inserted between
581 same order they're declared, and that padding won't be inserted between
582 consecutive variables of register size. Thus the first few entries can 587 consecutive variables of register size. Thus the first few entries can
583 be longs or pointers corresponding to the saved arguments.</p> 588 be longs or pointers corresponding to the saved arguments.</p>
584 589
585 <p><b>char *toys.optargs[]</b></p> 590 <p><b>char *toys.optargs[]</b></p>
586 591