home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TURBARG.ZIP / TURBARG.PAS
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  5.0 KB  |  161 lines

  1. {     The tail of a command is everything after the name of a program.
  2. TURBARG.PAS contains two TURBO PASCAL, VERSION 2.0 functions, targinit,
  3. which parses the tail into arguments, and targget, which gets an
  4. argument and its terminator.
  5.  
  6.      More specifically, define the following classes of characters:
  7.           soft separators: [BLANK (or SPACE) and TAB]
  8.           hard separators: [COMMA, SEMICOLON and endtail (CHR(0))].
  9. If the tail is not empty, targinit adds an endtail to it.  Each argument
  10. starts with zero or more soft separators, then zero or more characters which
  11. are not separators, then one separator.  Note that if the original tail ends
  12. with a separator, the last argument will be empty.
  13.  
  14.      INTEGER FUNCTION targinit returns the number of arguments unless it is
  15. clear that the tail has been destroyed.  In the latter case, it returns -1.
  16. Since it has no sure way to check whether the tail has been destroyed, the
  17. safest thing is to use it at the start of the program, before any Turbo
  18. Pascal standard procedures or functions are called.
  19.      targinit also sets the tail with an endtail added to it in the strtail
  20. variable tail, which is available to you.
  21.  
  22.      INTEGER FUNCTION targget(iarg, arg, term) returns argument iarg in arg
  23. and the separator which terminates it in term.  If iarg does not refer to an
  24. argument, the function returns -1; otherwise, it returns the length of arg.
  25. Note that the TYPE of arg is the user defined TYPE strtail.
  26.  
  27.      This design lets you use the tail in many different ways.  It allows
  28. empty arguments so that you can have default positional values, as in
  29. MICROSOFT's compilers.  Similarly, it gives you the terminator so that you
  30. can have different meanings, as MICROSOFT uses COMMA and SEMICOLON.  It strips
  31. leading soft separators to be a little bit forgiving to the operator.
  32.  
  33.      The other externals which you might want to use are:
  34.           length_tail: the maximum number of characters in TYPE strtail.
  35.           strtail:     the type of arg.
  36.           endtail:     the hard separator which represents the end of the tail.
  37.  
  38.      The parsing scheme uses the names tail_narg, tail_arg_index and
  39. tail_arg_table.  Do not use these names in your program.
  40.  
  41.  
  42. AUTHOR: Lew Paper
  43. DATE WRITTEN: 10/3/84
  44. VERSION 1.0}
  45.  
  46. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  47.  
  48. CONST
  49.   length_tail = 128;
  50.   endtail = 0;
  51.  
  52. TYPE
  53.   strtail = STRING[length_tail];
  54.   tail_arg_index = RECORD
  55.                      ndx: 1..length_tail;
  56.                      len: 0..length_tail;
  57.                    END;
  58.  
  59. VAR
  60.   tail: strtail;
  61.   tail_arg_table: ARRAY[1..63] of tail_arg_index;
  62.   tail_narg: INTEGER;
  63.  
  64. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  65.  
  66. FUNCTION targinit: INTEGER;
  67.  
  68.   TYPE
  69.     orders = SET OF 0..127;
  70.  
  71.   CONST
  72.     blank = 32;
  73.     tab = 9;
  74.     comma = 44;
  75.     semicolon = 59;
  76.     soft_separator: orders = [blank, tab];
  77.     hard_separator: orders = [comma, semicolon];
  78.  
  79.   VAR
  80.     nchar, i, istart, baseseg: INTEGER;
  81.     separator: orders;
  82.  
  83.   BEGIN
  84.     separator := soft_separator + hard_separator;
  85.  
  86.     baseseg := CSEG + 8; {CSEG for a .COM file is 0.
  87.                               80H is offset of tail count}
  88.     nchar := MEM[baseseg: 0000]; {MEM predefined byte at [segment:offset]}
  89.     tail := '';
  90.  
  91.     IF nchar <= length_tail THEN
  92.       BEGIN
  93.  
  94.         tail_narg := 0;
  95.  
  96.         IF nchar > 0 THEN
  97.           BEGIN
  98.  
  99.             i := 0;
  100.  
  101.             REPEAT
  102.  
  103.               tail_narg := tail_narg + 1;
  104.               i := i + 1;
  105.  
  106.               {Concatenate leading soft separators}
  107.               WHILE (INTEGER(MEM[baseseg:i]) IN soft_separator)
  108.                 AND (i <= nchar)  DO
  109.                 BEGIN
  110.                   tail := tail + CHR(MEM[baseseg:i]);
  111.                   i := i + 1
  112.                 END;
  113.               istart := i;
  114.               tail_arg_table[tail_narg].ndx := i;
  115.  
  116.  
  117.               WHILE (NOT (INTEGER(MEM[baseseg:i]) IN separator))
  118.                 AND (i <= nchar) DO
  119.                 BEGIN
  120.                   tail := tail + CHR(MEM[baseseg:i]);
  121.                   i := i + 1
  122.                 END;
  123.  
  124.             {At terminator}
  125.               tail_arg_table[tail_narg].len := i - istart;
  126.               IF i <= nchar THEN
  127.                 tail := tail + CHR(MEM[baseseg:i])
  128.               ELSE
  129.                 tail := tail + CHR(endtail);
  130.  
  131.  
  132.             UNTIL i > nchar;
  133.  
  134.           END; {nchar > 0}
  135.  
  136.       END
  137.  
  138.     ELSE {nchar > length_tail}
  139.       tail_narg := -1;
  140.  
  141.     targinit := tail_narg;
  142.  
  143.   END;
  144.  
  145. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  146.  
  147. FUNCTION targget(iarg: INTEGER; VAR arg:strtail; VAR term: CHAR):
  148.          INTEGER;
  149.  
  150.   BEGIN
  151.     IF (tail_narg >= 0) AND (iarg > 0) AND (iarg <= tail_narg) THEN
  152.       BEGIN
  153.         arg := COPY(tail, tail_arg_table[iarg].ndx, tail_arg_table[iarg].len);
  154.         term := tail[tail_arg_table[iarg].ndx + tail_arg_table[iarg].len];
  155.         targget := tail_arg_table[iarg].len
  156.       END
  157.     ELSE
  158.       targget := -1;
  159.  
  160.   END;
  161.