home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / TPOWER54.ZIP / DEMOSRC.ARC / TPCMDLIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-10  |  7.3 KB  |  281 lines

  1. {$S-,R-,V-,I-,B-}
  2.  
  3. {$IFDEF Ver40}
  4. {$F-}
  5. {$ELSE}
  6.   {$F+}
  7.   {$I OPLUS.INC}
  8. {$ENDIF}
  9.  
  10. {*********************************************************}
  11. {*                  TPCMDLIN.PAS 5.07                    *}
  12. {*        Copyright (c) TurboPower Software 1987.        *}
  13. {* Portions copyright (c) Sunny Hill Software 1985, 1986 *}
  14. {*     and used under license to TurboPower Software     *}
  15. {*                 All rights reserved.                  *}
  16. {*********************************************************}
  17.  
  18. unit TPCmdLin;
  19.   {-Accessing the command line}
  20.  
  21. interface
  22.  
  23. function CmdLineError : Integer;
  24.   {-Return 0 for success,
  25.     1 for missing argument,
  26.     2 for invalid argument,
  27.     3 for internal error}
  28.  
  29. function ParamCnt(P : string) : Integer;
  30.   {-Return the number of parameters in the string P}
  31.  
  32. function ParamStrPos(P : string; Param : Integer; var StartPos : Integer) : string;
  33.   {-Return the specified parameter and its position in the string at P^}
  34.  
  35. function ConvertArg(InStr : string; ConvertAsc : Boolean) : string;
  36.   {-Return a string with asciis (#nnn) converted to characters}
  37.  
  38. function GetArgString(var I : Integer;
  39.                       TakeQuoted : Boolean;
  40.                       ConvertAscii : Boolean) : string;
  41.   {-Get string argument following parameter i}
  42.  
  43. function GetArgNumber(var I : Integer) : Integer;
  44.   {-Return number following argument i}
  45.  
  46.   {==========================================================================}
  47.  
  48. implementation
  49.  
  50. const
  51.   Quote = #39;               {Single quote '}
  52.   Dquote = #34;              {Double quote "}
  53. const
  54.   Numbers : set of Char = ['0'..'9'];
  55. var
  56.   CmdLineErrorLast : Integer;
  57.  
  58.   function CmdLineError : Integer;
  59.     {-Return 0 for success,
  60.     1 for missing argument,
  61.     2 for invalid argument}
  62.   begin
  63.     CmdLineError := CmdLineErrorLast;
  64.     CmdLineErrorLast := 0;
  65.   end;
  66.  
  67.   function ParamCnt(P : string) : Integer;
  68.     {-Return the number of parameters in the string P}
  69.   var
  70.     InWhite : Boolean;
  71.     ParamNum : Integer;
  72.     StartPos : Integer;
  73.   begin
  74.     StartPos := 0;
  75.     InWhite := True;
  76.     ParamNum := 0;
  77.  
  78.     {Count the parameters}
  79.     repeat
  80.       Inc(StartPos);
  81.       if StartPos <= Length(P) then
  82.         case P[StartPos] of
  83.           #9, #32 : InWhite := True;
  84.         else
  85.           if InWhite then begin
  86.             {Next parameter}
  87.             Inc(ParamNum);
  88.             InWhite := False;
  89.           end;
  90.         end;
  91.     until (StartPos > Length(P));
  92.     ParamCnt := ParamNum;
  93.   end;
  94.  
  95.   function ParamStrPos(P : string; Param : Integer; var StartPos : Integer) : string;
  96.     {-Return the specified parameter and its position in the string P}
  97.   var
  98.     InWhite : Boolean;
  99.     ParamNum : Integer;
  100.     EndPos : Integer;
  101.   begin
  102.     StartPos := 0;
  103.     InWhite := True;
  104.     ParamNum := 0;
  105.  
  106.     {Scan to find start of param'th parameter}
  107.     repeat
  108.       Inc(StartPos);
  109.       if StartPos <= Length(P) then
  110.         case P[StartPos] of
  111.           #9, #32 : InWhite := True;
  112.         else
  113.           if InWhite then begin
  114.             {Next parameter}
  115.             Inc(ParamNum);
  116.             InWhite := False;
  117.           end;
  118.         end;
  119.     until (StartPos > Length(P)) or (ParamNum = Param);
  120.  
  121.     if ParamNum = Param then begin
  122.       {Scan to find end of parameter}
  123.       EndPos := StartPos;
  124.       repeat
  125.         Inc(EndPos);
  126.         case P[EndPos] of
  127.           #9, #32 : InWhite := True;
  128.         end;
  129.       until InWhite or (EndPos > Length(P));
  130.  
  131.       {Return the parameter}
  132.       ParamStrPos := Copy(P, StartPos, EndPos-StartPos);
  133.  
  134.     end else
  135.       {No such parameter}
  136.       ParamStrPos := '';
  137.   end;
  138.  
  139.   function ConvertAscii(InStr : string; var InPos : Word) : Char;
  140.     {-Return character corresponding to numeric sequence starting at inpos}
  141.   var
  142.     Num : string;
  143.     NumVal : Integer;
  144.     Code : Word;
  145.   begin
  146.     Num := '';
  147.     {Collect the numeric string}
  148.     while (InStr[InPos] in Numbers) and (Length(Num) <= 3) do begin
  149.       Num := Num+InStr[InPos];
  150.       Inc(InPos);
  151.     end;
  152.     {Convert it to number}
  153.     Val(Num, NumVal, Code);
  154.     if Code <> 0 then begin
  155.       CmdLineErrorLast := 2;
  156.       Exit;
  157.     end;
  158.     ConvertAscii := Char(lo(NumVal));
  159.   end;
  160.  
  161.   function ConvertArg(InStr : string; ConvertAsc : Boolean) : string;
  162.     {-Analyze the argument, returning a string with escapes and asciis expanded}
  163.   const
  164.     Ascii = '#';
  165.   var
  166.     OutStr : string;
  167.     InPos : Word;
  168.  
  169.     procedure AppendC(var OutStr : string; Ch : Char; var InPos : Word);
  170.       {-Append a character to outstr}
  171.     begin
  172.       OutStr := OutStr+Ch;
  173.       Inc(InPos);
  174.     end;
  175.  
  176.   begin
  177.     {Initialize position}
  178.     InPos := 1;
  179.     OutStr := '';
  180.  
  181.     {Scan the argument}
  182.     while InPos <= Length(InStr) do
  183.       case InStr[InPos] of
  184.         Ascii :
  185.           if not(ConvertAsc) then
  186.             AppendC(OutStr, InStr[InPos], InPos)
  187.           else if (InPos < Length(InStr)) and (InStr[Succ(InPos)] in Numbers) then begin
  188.             Inc(InPos);
  189.             OutStr := OutStr+ConvertAscii(InStr, InPos);
  190.           end else
  191.             AppendC(OutStr, InStr[InPos], InPos);
  192.       else
  193.         AppendC(OutStr, InStr[InPos], InPos);
  194.       end;
  195.  
  196.     ConvertArg := OutStr;
  197.   end;
  198.  
  199.   function GetQuoted(var I : Integer) : string;
  200.     {-Return quoted region of command line, starting at parameter I.
  201.       Return I pointing to last word parameter used.}
  202.   var
  203.     PS : ^string;
  204.     QuoteChar : Char;
  205.     Posn : Integer;
  206.     SPosn : Integer;
  207.     InWhite : Boolean;
  208.   begin
  209.     {Define pointer to command line string}
  210.     PS := Ptr(PrefixSeg, $80);
  211.  
  212.     {Find position of start of quote}
  213.     if ParamStrPos(PS^, I, Posn) = '' then begin
  214.       CmdLineErrorLast := 2;
  215.       Exit;
  216.     end;
  217.  
  218.     {Store starting quote character}
  219.     QuoteChar := PS^[Posn];
  220.     SPosn := Posn;
  221.  
  222.     {Scan till end of quote}
  223.     InWhite := False;
  224.     repeat
  225.       Inc(Posn);
  226.       if Posn <= Length(PS^) then
  227.         case PS^[Posn] of
  228.           #9, #32 : InWhite := True;
  229.         else
  230.           if InWhite then begin
  231.             {Next parameter}
  232.             Inc(I);
  233.             InWhite := False;
  234.           end;
  235.         end;
  236.     until (Posn > Length(PS^)) or (PS^[Posn] = QuoteChar);
  237.  
  238.     {Return the quoted string}
  239.     GetQuoted := Copy(PS^, Succ(SPosn), Pred(Posn-SPosn));
  240.   end;
  241.  
  242.   function GetArgString(var I : Integer; TakeQuoted : Boolean;
  243.                         ConvertAscii : Boolean) : string;
  244.     {-Get string argument following parameter i}
  245.   var
  246.     Arg : string;
  247.   begin
  248.     if I >= ParamCount then begin
  249.       CmdLineErrorLast := 2;
  250.       Exit;
  251.     end;
  252.     Inc(I);
  253.     Arg := ParamStr(I);
  254.     if TakeQuoted then
  255.       if (Arg[1] = Quote) or (Arg[1] = Dquote) then
  256.         {Take entire command line until end quote}
  257.         Arg := GetQuoted(I);
  258.     GetArgString := ConvertArg(Arg, ConvertAscii);
  259.   end;                       {GetArgString}
  260.  
  261.   function GetArgNumber(var I : Integer) : Integer;
  262.     {-Return number following argument i}
  263.   var
  264.     Code : Word;
  265.     Num : Integer;
  266.   begin
  267.     if I >= ParamCount then begin
  268.       CmdLineErrorLast := 1;
  269.       Exit;
  270.     end;
  271.     Inc(I);
  272.     Val(ParamStr(I), Num, Code);
  273.     if Code <> 0 then
  274.       CmdLineErrorLast := 2;
  275.     GetArgNumber := Num;
  276.   end;
  277.  
  278. begin
  279.   CmdLineErrorLast := 0;
  280. end.
  281.