home *** CD-ROM | disk | FTP | other *** search
- { The tail of a command is everything after the name of a program.
- TURBARG.PAS contains two TURBO PASCAL, VERSION 2.0 functions, targinit,
- which parses the tail into arguments, and targget, which gets an
- argument and its terminator.
-
- More specifically, define the following classes of characters:
- soft separators: [BLANK (or SPACE) and TAB]
- hard separators: [COMMA, SEMICOLON and endtail (CHR(0))].
- If the tail is not empty, targinit adds an endtail to it. Each argument
- starts with zero or more soft separators, then zero or more characters which
- are not separators, then one separator. Note that if the original tail ends
- with a separator, the last argument will be empty.
-
- INTEGER FUNCTION targinit returns the number of arguments unless it is
- clear that the tail has been destroyed. In the latter case, it returns -1.
- Since it has no sure way to check whether the tail has been destroyed, the
- safest thing is to use it at the start of the program, before any Turbo
- Pascal standard procedures or functions are called.
- targinit also sets the tail with an endtail added to it in the strtail
- variable tail, which is available to you.
-
- INTEGER FUNCTION targget(iarg, arg, term) returns argument iarg in arg
- and the separator which terminates it in term. If iarg does not refer to an
- argument, the function returns -1; otherwise, it returns the length of arg.
- Note that the TYPE of arg is the user defined TYPE strtail.
-
- This design lets you use the tail in many different ways. It allows
- empty arguments so that you can have default positional values, as in
- MICROSOFT's compilers. Similarly, it gives you the terminator so that you
- can have different meanings, as MICROSOFT uses COMMA and SEMICOLON. It strips
- leading soft separators to be a little bit forgiving to the operator.
-
- The other externals which you might want to use are:
- length_tail: the maximum number of characters in TYPE strtail.
- strtail: the type of arg.
- endtail: the hard separator which represents the end of the tail.
-
- The parsing scheme uses the names tail_narg, tail_arg_index and
- tail_arg_table. Do not use these names in your program.
-
-
- AUTHOR: Lew Paper
- DATE WRITTEN: 10/3/84
- VERSION 1.0}
-
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
-
- CONST
- length_tail = 128;
- endtail = 0;
-
- TYPE
- strtail = STRING[length_tail];
- tail_arg_index = RECORD
- ndx: 1..length_tail;
- len: 0..length_tail;
- END;
-
- VAR
- tail: strtail;
- tail_arg_table: ARRAY[1..63] of tail_arg_index;
- tail_narg: INTEGER;
-
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
-
- FUNCTION targinit: INTEGER;
-
- TYPE
- orders = SET OF 0..127;
-
- CONST
- blank = 32;
- tab = 9;
- comma = 44;
- semicolon = 59;
- soft_separator: orders = [blank, tab];
- hard_separator: orders = [comma, semicolon];
-
- VAR
- nchar, i, istart, baseseg: INTEGER;
- separator: orders;
-
- BEGIN
- separator := soft_separator + hard_separator;
-
- baseseg := CSEG + 8; {CSEG for a .COM file is 0.
- 80H is offset of tail count}
- nchar := MEM[baseseg: 0000]; {MEM predefined byte at [segment:offset]}
- tail := '';
-
- IF nchar <= length_tail THEN
- BEGIN
-
- tail_narg := 0;
-
- IF nchar > 0 THEN
- BEGIN
-
- i := 0;
-
- REPEAT
-
- tail_narg := tail_narg + 1;
- i := i + 1;
-
- {Concatenate leading soft separators}
- WHILE (INTEGER(MEM[baseseg:i]) IN soft_separator)
- AND (i <= nchar) DO
- BEGIN
- tail := tail + CHR(MEM[baseseg:i]);
- i := i + 1
- END;
- istart := i;
- tail_arg_table[tail_narg].ndx := i;
-
-
- WHILE (NOT (INTEGER(MEM[baseseg:i]) IN separator))
- AND (i <= nchar) DO
- BEGIN
- tail := tail + CHR(MEM[baseseg:i]);
- i := i + 1
- END;
-
- {At terminator}
- tail_arg_table[tail_narg].len := i - istart;
- IF i <= nchar THEN
- tail := tail + CHR(MEM[baseseg:i])
- ELSE
- tail := tail + CHR(endtail);
-
-
- UNTIL i > nchar;
-
- END; {nchar > 0}
-
- END
-
- ELSE {nchar > length_tail}
- tail_narg := -1;
-
- targinit := tail_narg;
-
- END;
-
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
-
- FUNCTION targget(iarg: INTEGER; VAR arg:strtail; VAR term: CHAR):
- INTEGER;
-
- BEGIN
- IF (tail_narg >= 0) AND (iarg > 0) AND (iarg <= tail_narg) THEN
- BEGIN
- arg := COPY(tail, tail_arg_table[iarg].ndx, tail_arg_table[iarg].len);
- term := tail[tail_arg_table[iarg].ndx + tail_arg_table[iarg].len];
- targget := tail_arg_table[iarg].len
- END
- ELSE
- targget := -1;
-
- END;