A more Powerful SHELL 
Author Message
 A more Powerful SHELL

Stop!!  Hold the Presses!!

I discovered something quite interesting today which everyone should find
very useful.

As you know, when you execute a SHELL statement, powerbasic first launches
whatever command processor is in the environment variable COMSPEC and
passing your command line to the processor.  This is very useful because if
your trying to launch a utility which isn't in the current directory, or
you didn't include a full path, the command processor will search the PATH
for it.  And, of course, you can launch batch files from within your
program as well.

The down side is that if your executing something which *is* in the current
directory, or you do have the complete path and the file is not a batch
file then you have the command processor taking up valuable memory.

On a whim, I decided to try changing the COMSPEC variable from within my
program to something else and then doing a SHELL.  And it worked!
Apparently, PowerBASIC's SHELL statement reads COMSPEC from the 'local'
environment.

My COMSPEC is usually set to C:\4DOS\4DOS.COM, but I copied PKUNZIP.EXE
into the current directory and changed COMSPEC to PKUNZIP.EXE and then did
a SHELL.

Comspec$ = ENVIRON$("COMSPEC")  'save the current COMSPEC
ENVIRON "COMSPEC=PKUNZIP.EXE"
SHELL
ENVIRON "COMSPEC="+Comspec$     'restore the old COMSPEC

I was thrilled to see PKUNZIP's help screen come up.  So then I tried
pointing to the directory where I normally keep PKUNZIP.

Comspec$ = ENVIRON$("COMSPEC")
ENVIRON "COMSPEC=C:\UTIL\PKUNZIP.EXE"
SHELL
ENVIRON "COMSPEC="+Comspec$

It bombed!  I got an error 7 (Out of Memory) because there wasn't enough
room in the local environment to hold the larger variable.  Then it hit me.
Since I *know* where PKUNZIP is, I don't need the PATH.  So I can remove
it, freeing up space for my larger COMSPEC variable.

Path$ = ENVIRON$("PATH")
Comspec$ = ENVIRON$("COMSPEC")
ENVIRON "PATH="
ENVIRON "COMSPEC=C:\ARC\PKUNZIP.EXE"
SHELL
ENVIRON "PATH="+Path$
ENVIRON "COMSPEC="+Comspec$

It worked!!  I can even pass parameters to PKUNZIP:

SHELL "-v MYZIP.ZIP"

And since COMMAND.COM isn't getting in the way, you can call the DOS
interrupt to get the errorlevel set by the program you SHELL to.

The end result is:

PRINT MySHELL("C:\ARC\PKUNZIP.EXE","")

FUNCTION MySHELL(BYVAL Program$, BYVAL Parameters$) AS INTEGER

  Path$    = ENVIRON$("PATH")        ' save PATH
  Comspec$ = ENVIRON$("COMSPEC")     ' save COMSPEC
  ENVIRON "PATH="                    ' temporarily remove the path
  ENVIRON "COMSPEC="+Program$        ' set COMSPEC to our program

  SHELL Parameters$                  ' execute the program

  ENVIRON "COMSPEC="+Comspec$        ' restore the comspec
  ENVIRON "PATH="+Path$              ' restore the path

  REG 1, &H4D00                      ' get the error code
  CALL INTERRUPT &H21                ' by calling DOS
  MySHELL = REG(1) AND &HFF          ' return exit code

END FUNCTION

--Dave
---
 t QMPro 1.53 t The road to success is always under construction



Tue, 02 Dec 1997 03:00:00 GMT  
 A more Powerful SHELL
dave meinte am 16.06.95 zum Thema "A more Powerful SHELL"

Hello Dave, hello all!!

Quote:
> On a whim, I decided ...

I also ... <grin>

Quote:
> It bombed!  I got an error 7 (Out of Memory) because there wasn't enough
> room in the local environment to hold the larger variable.  Then it hit me.
> Since I *know* where PKUNZIP is, I don't need the PATH.  So I can remove
> it, freeing up space for my larger COMSPEC variable.

> Path$ = ENVIRON$("PATH")
> Comspec$ = ENVIRON$("COMSPEC")
> ENVIRON "PATH="
> ENVIRON "COMSPEC=C:\ARC\PKUNZIP.EXE"
> SHELL
> ENVIRON "PATH="+Path$
> ENVIRON "COMSPEC="+Comspec$

This is a very simple and not-effective method to modify the environment.

I've wrote a new function (no more problems with error 7)

'*************************************************************************
'
'   Environment veraendern / Modify the environment
'   fuer/for  PowerBASIC 3.0/3.1
'
'   Frei Kopierbar / Public Domain
'
'   von/by Thomas Gohel, Germany
'
'*************************************************************************

$ERROR NUMERIC OFF

CLS
ENVIRON "PROMPT="               ' kill the old prompt
AddEnviron("PROMPT=This is a demo for a new: " + CHR$(13,10) +"[PowerBASIC-SHELL] $p$g")
SHELL                           ' demonstration
END

FUNCTION AddEnviron(Update$) public
        LOCAL PSPAdresse??, MemorySeg??, Mem16??, MemoryRest??
        Carry% = 0

        ' *** Adresse des PSP ermitteln / Find the PSP ***
        ! mov ax, &h6200
        ! int &h21
        ! mov PSPAdresse??, bx
        DEF SEG = PSPAdresse??
        DEF SEG = CVI(PEEK$(&h2c,2))

        ' *** Environment holen / Read environment ***
        FOR i% = 0 TO 1024
            IF PEEK$(i%, 2) = CHR$(0,0) THEN EXIT FOR
        NEXT i%

        ' *** Environment modifizieren / modify ***
        OldEnv$ = PEEK$(0,i%)
        NewEnv$ = OldEnv$ + CHR$(0) + Update$

        ' *** Holen des Programmnamen/pfad / Read Programname/path ***
        z% = i%
        Programmpath$ = ""
        FOR i% = z% TO 1024
            IF PEEK$(i%, 4) = CHR$(0,0,1,0) THEN EXIT FOR
        NEXT i%
        WHILE PEEK(i% + 4) <> 0
            Programmpath$ = Programmpath$ + CHR$(PEEK(i% + 4))
            INCR i%
        WEND

        ' *** Enviroment fertigstellen / Create new environment ***
        NewEnv$ = NewEnv$ + CHR$(0,0,1,0) + Programmpath$ + CHR$(0)

        ' *** DOS-Speicher anfordern / Alloc DOS-Memory ***
        Mem16?? = CEIL(LEN(NewEnv$)/16)
        x& = SETMEM(-(Mem16??*16)-16)
        ! mov ax, &h4800
        ! mov bx, Mem16??
        ! int &h21
        ! mov MemorySeg??, ax
        ! mov MemoryRest??, bx
        ! jnc MemoryOk
        Carry% = 1
        MemoryOk:
        IF Carry% = 0 THEN

            ' *** Schreiben des Environment in den DOS-Speicher ***
            DEF SEG = MemorySeg??
            POKE$ 0, NewEnv$

            ' *** Patchen des PSP / Patch the PSP ***
            DEF SEG = PSPAdresse??
            POKEI &h2c, MemorySeg??
            DEF SEG
        ELSE
            BEEP
            PRINT "Ups??? nur:"; MemoryRest??*16; "Byte verfuegbar"
        END IF
END FUNCTION

Quote:
> --Dave

Bye
                                      ----------------
                                         /
                                       /  h o m a s

                            PB & Sound: Let the dream come true ...
## CrossPoint v3.02 R ##



Tue, 09 Dec 1997 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. A more Powerful SHELL

2. New Powerful Access App Installer/Packager Now Available

3. Powerful Mass Mailing Software

4. VS.Net on my (once great and powerful) computer

5. AUTOMATION. The Most Powerful Word On The NET

6. Powerful Mass Usenet Posting Software

7. - Powerful Imaging Toolkit with SourceCode

8. Recordset and Data Control : Which is more powerful?

9. More Powerful Forms Generation Software

10. Powerful Mass Posting Software

11. Powerful OCX shareware

12. More powerful dirlistbox

 

 
Powered by phpBB® Forum Software