PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's 
Author Message
 PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's


: I need help with what I consider a simple problem but as of yet have
: not found an answer.

: When running a program that was compiled in BP 7.0 or TP 7.0 I need to
: pass long strings to the program from the dos prompt.

: Ex.    TEST C:\AUTOEXEC.BAT "TEXT TO INSERT"

: BP or TP break this command up into 4 diferrent parameters :
:               1<   C:\AUTOEXEC.BAT
:               2<   "TEST
:               3<   TO
:               4<   INSERT"

: Borland C and normal ANSI C would treat this as 2 parmeters with the
: quotation marks combining the proper text. Does any one know how to
: make Pascal treat these as 2 strings. I love pascal and would prefer
: not to have to link in any C libraries if possible.

I think you will have to process the parameters yourself (e.g., by checking
for one that begins with " and then concantenating that and all subsequent
parameters until you reach the next ".

The Chief
---------
Dr. A{*filter*}la A. Olowofoyeku (The African Chief)

Author of: Chief's Installer Pro 2.60 for Win16 and Win32:
           winner of PC PLUS Magazine Gold Award (April 1995 U.K. edition)
           ftp://ftp.demon.co.uk/pub/ibmpc/win3/apps/chief/pro/cinstp26.zip



Wed, 18 Jun 1902 08:00:00 GMT  
 PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's

Quote:

>BP or TP break this command up into 4 diferrent parameters :
>            1<   C:\AUTOEXEC.BAT
>            2<   "TEST
>            3<   TO
>            4<   INSERT"

>Borland C and normal ANSI C would treat this as 2 parmeters with the
>quotation marks combining the proper text. Does any one know how to
>make pascal treat these as 2 strings. I love pascal and would prefer

You'll have to roll your own argument parser and work with the original
command line data.  The DOS command line is stored at PSP:0081 and its
length is at PSP:0080--a normal Pascal string, in other words.

The command line area is shared with the DTA, so you should grab the
command line as soon as possible in your program--and definitely before
any disk I/O takes place.

Here's some rough code that picks off the individual args from an
un{*filter*}erated command line.  Text within " or ' are preserved as a
single argument, much like the argv[] array in C.

You might want to put the parsed args into a linked list stored on the
heap for actual use--this demo simply prints 'em.

Program Argv;
{ Parse your own command line and treat quote-delimited args as a
  single string as in C's argv[] array.  Turbo Pascal v4.0+.

  This is a quick 15-minute hack (and it shows), but it does get the
  point across, so be gentle.  Donated to the public domain.

Quote:
}

FUNCTION IsWhiteSpace(c: char): boolean;
begin
    IsWhiteSpace:=c in [' ',#9, ',', ';']
end;

FUNCTION IsQuoteChar(c: char): boolean;
begin
    IsQuoteChar:=c in ['"','''']
end;

FUNCTION ParseCmdLine(cmdline: string): integer;
{ Splits string (cmdline) into separate args.
  If quote chars (" or ') are imbedded, the text within is counted as
  a single argument much like C's argv[] array. }

    function NextNonWhiteSpace(s: string; i: integer): integer;
    var len: integer;
    begin
        len:=Length(s);
        while IsWhiteSpace(s[i]) and (i<=len) do Inc(i);
        NextNonWhiteSpace:=i
    end;

var s, e, c, len: integer;
    param: string;
    qch: char;
begin
    len:=Length(cmdline);
    s:=NextNonWhiteSpace(cmdline,1);   { 1st nonwhitespace char }
    e:=s; c:=0;
    while (e<=len) do
    begin
        if IsQuoteChar(cmdline[s]) then
        begin
            Inc(e);
            qch:=cmdline[s];    { Look for matching quote char }
            while (cmdline[e] <> qch) and (e<=len) do Inc(e);
            Inc(e);
        end
        else
            while not(IsWhiteSpace(cmdline[e])) and (e<=len) do
                Inc(e);
        Inc(c);
        param:=Copy(cmdline,s,e-s);   { Get argument }
        writeln(c:2,': |',param,'|'); {  s=',s,' e=',e);}
        e:=NextNonWhiteSpace(cmdline,e);
        s:=e
    end; {while}
    ParseCmdLine:=c
end; {ParseCmdLine}

VAR cmdlinePtr: ^string;
    x: integer;

BEGIN
    cmdlinePtr:=Ptr(PrefixSeg,$0080);    { Command line at PSP:0080 }
    writeln(#10#13'The un{*filter*}erated command line is:');
    writeln('|',cmdlinePtr^,'|');
    writeln('The length of the command line is ',
        Length(cmdlinePtr^));
    writeln(#10#13'Assuming space, tab, comma or semi-colon as');
    writeln('argument delimiters, the command line args are:'#10#13);
    x:=ParseCmdLine(cmdlinePtr^);
    writeln(#10#13'There were ',x,' command line parameters.')
END.

Running this program as

        ARGV These are separate args.  'But "this" is a single string arg.'
And these are more singles.

yields as output:

The un{*filter*}erated command line is:
        | These are separate args. 'But "this" is a single string arg.' And
these are more singles.|
The length of the command line is 90

Assuming space, tab, comma or semi-colon as
argument delimiters, the command line args are:

 1: |These|
 2: |are|
 3: |separate|
 4: |args.|
 5: |'But "this" is a single string arg.'|
 6: |And|
 7: |these|
 8: |are|
 9: |more|
10: |singles.|

There were 10 command line parameters.

--

"Be wary of strong spirits.  It can make you shoot at tax collectors...
and miss." --RAH



Wed, 18 Jun 1902 08:00:00 GMT  
 PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's

Quote:
>Borland C and normal ANSI C would treat this as 2 parmeters with the
>quotation marks combining the proper text. Does any one know how to
>make pascal treat these as 2 strings. I love pascal and would prefer
>not to have to link in any C libraries if possible.

You need to parse the command line yourself.  It's stored as a
Pascal-style 127 byte string in the PSP at offset $80; you can get it
like this:

var
  cmdline : PString;
begin
  cmdline := Ptr(PrefixSeg, $80);

Then you can do anything you like with the string cmdline^.

Duncan Murdoch



Wed, 18 Jun 1902 08:00:00 GMT  
 PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's
I need help with what I consider a simple problem but as of yet have
not found an answer.

When running a program that was compiled in BP 7.0 or TP 7.0 I need to
pass long strings to the program from the dos prompt.

Ex.    TEST C:\AUTOEXEC.BAT "TEXT TO INSERT"

BP or TP break this command up into 4 diferrent parameters :
                1<   C:\AUTOEXEC.BAT
                2<   "TEST
                3<   TO
                4<   INSERT"

Borland C and normal ANSI C would treat this as 2 parmeters with the
quotation marks combining the proper text. Does any one know how to
make pascal treat these as 2 strings. I love pascal and would prefer
not to have to link in any C libraries if possible.

Any suggestions or hint on where to look for the answer I would be
very greatful.




Wed, 18 Jun 1902 08:00:00 GMT  
 PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's

Christopher, there is a way, You'll have to play with it a little before
you can get it working exactly they way you want, and I'll look to see if
I have some code to post later, ( I think I do ), but basically what
you'll need to do is read ParamCount, and find out how many you have, it
would list 4, next you would read each ParamStr(), character by character.
If your ParamStr(2) starts with a double quote " then Var TotalString :=
ParamStr(2) + ParamStr(3) + ParamStr(4), or until you don't have any more
ParamStrings.  Then send it through a simple routine to cut the first and
last characters which would be the acctual (double quote) character and
your ready to rumble....  

:)

Robert


: I need help with what I consider a simple problem but as of yet have
: not found an answer.

: When running a program that was compiled in BP 7.0 or TP 7.0 I need to
: pass long strings to the program from the dos prompt.

: Ex.    TEST C:\AUTOEXEC.BAT "TEXT TO INSERT"

: BP or TP break this command up into 4 diferrent parameters :
:               1<   C:\AUTOEXEC.BAT
:               2<   "TEST
:               3<   TO
:               4<   INSERT"

: Borland C and normal ANSI C would treat this as 2 parmeters with the
: quotation marks combining the proper text. Does any one know how to
: make pascal treat these as 2 strings. I love pascal and would prefer
: not to have to link in any C libraries if possible.

: Any suggestions or hint on where to look for the answer I would be
: very greatful.




Wed, 18 Jun 1902 08:00:00 GMT  
 PARAMSTR Versus ARGV - Pascal handling of Parameters to EXE's

Quote:


>>Borland C and normal ANSI C would treat this as 2 parmeters with the
>>quotation marks combining the proper text. Does any one know how to
>>make pascal treat these as 2 strings. I love pascal and would prefer
>>not to have to link in any C libraries if possible.

> You need to parse the command line yourself.  It's stored as a
> Pascal-style 127 byte string in the PSP at offset $80; you can get it
> like this:

> var
>   cmdline : PString;
> begin
>   cmdline := Ptr(PrefixSeg, $80);

> Then you can do anything you like with the string cmdline^.

Just to be complete:  under Windows 3.1, the command line is stored (at
offset 80h of PSP) as a C zero-terminated string.  Under Windows 95,
it's stored somewhere else if longer than 126 bytes  --  then the last
half of PSP is just padded out with spaces or ascii 0's (don't rememb.).

- Alf



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. testing command line parameters with paramstr

2. testing command line parameters with paramstr

3. V.C++ versus V.Basic versus Delphi

4. Pascal exe's without dos ints?

5. Can TQuery handle table names as parameters?

6. 'Error creating cursor handle'

7. Parameter passing to othetr EXE file, please help...

8. "commandline" parameters myprogram.exe -hello

9. InfoPower QBE: 'Error creating cursor handle'

10. IS Express can't find BDE's DATAPUMP.EXE

11. GPF's when running multiple exe's against a DB

12. Pascal versus C, Source Code, and More...

 

 
Powered by phpBB® Forum Software