'===========================================================================
' Subject: COMMAND$ PARSER                    Date: 11-01-96 (12:31)       
'  Author: Tika Carr                          Code: QB, PDS                
'  Origin: FidoNet QUIK_BAS Echo            Packet: DOS.ABC
'===========================================================================
'> Does anyone have an example of how to call SWITCHES from within QB?

'If you mean how to parse the COMMAND$, then here's a routine I added
'to my own QuickLibrary that may be of help:

DECLARE SUB GetArg ()
DECLARE FUNCTION ExePath$ ()
'$INCLUDE: 'qb.bi'

DIM SHARED arg$(0 TO 85)

'GETARG.BAS: COMMAND$ Parser
'-----------------------------------------------------------------------
'Revision History:
'
'  1.0     10/28/96          Created                                tc
'-----------------------------------------------------------------------
'Description/Instructions:
'
'Parses the COMMAND$ (which holds any parameters passed to the program
'from the command line). Returns the full path and filename of the
'program currently running in the global variable arg$(0). arg$(1) to
'arg$(85) holds each the command line parameters passed. Simply look for
'one of the arrays to be blank (ie. arg$(x)="") for the end of the list.
'if arg$(1)="" then there were no parameters passed to the program (here
'you would have it print a usage or help screen).
'-----------------------------------------------------------------------
'**** TEST Program ****
'Set COMMAND$ for something like: test /sw1 /er /x -s Macro
'Then run the program below. After that, try passing no paramteres by
'setting COMMAND$ to "" or nothing. This is done in the IDE from the RUN
'menu, Modify COMMAND$ option.
'
CLS
GetArg
PRINT "The program running is: "; arg$(0)
'Note that the above line may give the full path and file name to the
'QuickBasic 4.5 IDE program if you don't compile this. But when put in
'A library and it is compiled, it will show the correct executable and
'path.

IF arg$(1) = "" THEN
    PRINT "You didn't pass any parameters"
ELSE
    i = 1
    DO UNTIL arg$(i) = ""
        PRINT "Parameter #"; i; " is: "; arg$(i)
        i = i + 1
    LOOP
END IF
PRINT i - 1; " parameters were found, excluding the name of the program."
'-----------------------------------------------------------------------
DEFINT A-Z
SUB GetArg
arg$(0) = ExePath$
IF COMMAND$ = "" THEN EXIT SUB
i = 1: j = 1
DO UNTIL i >= LEN(COMMAND$)
    WHILE MID$(COMMAND$, i, 1) <> " " AND MID$(COMMAND$, i, 1) <> ""
        arg$(j) = arg$(j) + MID$(COMMAND$, i, 1)
        i = i + 1
    WEND
    i = i + 1: j = j + 1
LOOP
END SUB
'-----------------------------------------------------------------------
DECLARE FUNCTION ExePath$ ()
DEFINT A-Z
'$INCLUDE: 'qb.bi'

'EXEPATH$.BAS: Gets the path and filename of the executable running.
'              by Joe Negron. Function name changed by Tika Carr.
'-----------------------------------------------------------------------
'Function Description:
'
'"Uses DOS ISR 21H, Function 51H (Get PSP Address) to return the
'name of the currently executing program.  Note that this FUNCTION
'requires DOS 3.0 or >."
'-----------------------------------------------------------------------
'**** TEST Program ****
'E$ = ExePath$
'PRINT E$
'-----------------------------------------------------------------------
FUNCTION ExePath$ STATIC
   DIM Regs AS RegType                       'Allocate space for TYPE
                                             '  RegType
   Regs.ax = &H5100                          'DOS function 51h
   Interrupt &H21, Regs, Regs                '  Get PSP Address

   DEF SEG = Regs.bx                         'Regs.bx returns PSP sgmnt.
   EnvSeg% = PEEK(&H2C) + PEEK(&H2D) * 256   'Get environment address
   DEF SEG = EnvSeg%                         'Set environment address

   DO
       Byte% = PEEK(Offset%)                  'Take a byte

       IF Byte% = 0 THEN                      'Items are ASCIIZ
           Count% = Count% + 1                 '  terminated

           IF Count% AND EXEFlag% THEN         'EXE also ASCIIZ terminated
           EXIT DO                          'Exit at the end
           ELSEIF Count% = 2 THEN              'Last entry in env. is
           EXEFlag% = -1                    '  terminated with two
           Offset% = Offset% + 2            '  NULs.  Two bytes ahead
       END IF                              '  is the EXE file name.
       ELSE                                   'If Byte% <> 0, reset
           Count% = 0                          '  zero counter

           IF EXEFlag% THEN                    'If EXE name found,
                Temp$ = Temp$ + CHR$(Byte%)      '  build string
           END IF
        END IF

        Offset% = Offset% + 1                  'To grab next byte...
    LOOP                                      'Do it again

   DEF SEG                                   'Reset default segment
   ExePath$ = Temp$                      'Return value
   Temp$ = ""                                'Clean up
END FUNCTION
