home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / USCX / TURBO-06.ZIP / ARGLIST2.PAS < prev    next >
Pascal/Delphi Source File  |  1985-02-23  |  3KB  |  120 lines

  1.  
  2. { This provides capabilities similar to argc/argv in C. You can now
  3.   read the argument list from your TURBO Pascal program. `argc' is
  4.   actually a function that returns the number of parameters on the
  5.   command line. Spaces are the separators between parameters. If the
  6.   parameter is enclosed in quotes ('), then any characters can appear.
  7.   If you want a quote, put two in the command line parameter.
  8.  
  9. Example
  10.  
  11.     cmd 'this is the first' and 'this is the ''third'' argument'
  12.  
  13. This will return argc=3.
  14.  
  15. You can retrieve the arguments by calling argv(#)
  16.  
  17. Example
  18.    if the above was the command line, then the program
  19.  
  20.       for i := 1 to argc do
  21.          writeln('argv(',i,') =<',argv(i),'>');
  22.  
  23. would print
  24.  
  25.     argv(1) =<this is the first>
  26.     argv(2) =<and>
  27.     argv(3) =<this is the 'third' argument>
  28.  
  29. }
  30.  
  31. type
  32.     arglist_string = string[80];
  33. const
  34.     arglist_max = 20;
  35.     arglist_number : integer = -1;
  36. var
  37.     argvlist : array[1..arglist_max] of ^arglist_string;
  38. function argv(num : integer) : arglist_string;
  39.  
  40. var
  41.     argument : arglist_string absolute cseg:$80;
  42.     newparm,parmline : arglist_string;
  43.     i,j : integer;
  44.     state : (leading_ws, non_quote, quoted, end_quote);
  45.     inchar : char;
  46.  
  47.     procedure saveparm;
  48.     begin
  49.       if arglist_number < arglist_max then begin
  50.         arglist_number := arglist_number+1;
  51.         new(argvlist[arglist_number]);
  52.         argvlist[arglist_number]^ := newparm;
  53.         newparm := '';
  54.         end;
  55.       end;
  56.  
  57. begin
  58.     if arglist_number = -1 then begin
  59.         arglist_number := 0;
  60.         parmline := argument+' ';
  61.         state := leading_ws;
  62.         newparm := '';
  63.         for i := 1 to length(parmline) do begin
  64.             inchar := parmline[i];
  65.             case state of
  66.  
  67.                 leading_ws: begin
  68.                     if inchar = '''' then state := quoted
  69.                     else if inchar <> ' ' then begin
  70.                         newparm := newparm+inchar;
  71.                         state := non_quote;
  72.                         end;
  73.                     end;
  74.  
  75.                 non_quote: begin
  76.                     if inchar = ' ' then begin
  77.                         saveparm;
  78.                         state := leading_ws;
  79.                         end
  80.                     else newparm := newparm+inchar;
  81.                     end;
  82.  
  83.                 quoted: begin
  84.                     if inchar = '''' then state := end_quote
  85.                     else newparm := newparm+inchar;
  86.                     end;
  87.  
  88.                 end_quote: begin
  89.                     if inchar = '''' then begin
  90.                         newparm := newparm+inchar;
  91.                         state := quoted;
  92.                         end
  93.                     else if inchar <> ' ' then begin
  94.                         newparm := newparm+inchar;
  95.                         state := non_quote;
  96.                         end
  97.                     else begin
  98.                         saveparm;
  99.                         state := leading_ws;
  100.                         end;
  101.                 end;
  102.             end;
  103.         end;
  104.         end;
  105.     if (num > 0) and (num <= arglist_number) then
  106.         argv := argvlist[num]^
  107.     else argv := '';
  108.     end;
  109.  
  110. function argc : integer;
  111. var
  112.     dummy : arglist_string;
  113.  
  114. begin
  115.     if arglist_number = -1 then dummy := argv(1); {force evaluation}
  116.     argc := arglist_number;
  117.     end;
  118. 
  119.  
  120.