Detect Stderr from FBC compiling

DOS specific questions.
Post Reply
lassar
Posts: 306
Joined: Jan 17, 2006 1:35

Detect Stderr from FBC compiling

Post by lassar »

I am working on a program to automate compiling in Freebasic dos.

I need to detect when there is a error in fbc and abort the rest of the program.

Here is what I have so far.

Code: Select all

#lang "FBLITE"

Cmd$ = COMMAND$
SHELL "FBC " + Cmd$

PeriodPos% = Instr(Cmd$,".")
FileName$ = LEFT$(Cmd$,PeriodPos%-1)
ExeName$ = FileName$ + ".exe"


SHELL "exe2coff " + ExeName$
SHELL "copy C:\bin\pmodstub.exe .\"
SHELL "copy /b pmodstub.exe+" + FileName$ + " " + ExeName$


How do I detect when FBC has error?
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Detect Stderr from FBC compiling

Post by dkl »

Hi,

like many programs, fbc returns a non-zero exit code in case of an error. You can check the result of the shell() function to detect this.

Code: Select all

var result = shell("fbc -version")
select case result
case 0
	print "ok, it worked"
case -1
	print "command not found (or other shell() failure)"
case else
	print "command aborted with exit code " & result
end select
By the way, fbc prints error messages to stdout, not stderr (yea, that's rather non-standard).

If you want to capture the error messages, check out Open Pipe.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Detect Stderr from FBC compiling

Post by MrSwiss »

Just a remark on "shelling" from a compiled program:

I've learned "the hard way" that it is usually the better approach, to call a shell script (depending on OS).
The main REASONS are:
- flexibility: can be changed any Time "without touching source code of APPLICATION", recompiling etc.
- script can return Standard Error Codes, or "defined by you" Codes (on exit of script).

Ex. WIN: called "errorlevel" in *.cmd/*.bat (*.cmd = recommended, 32bit Console!)
Ex. DOS: called "errorlevel" in *.bat (16bit Console)
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Detect Stderr from FBC compiling

Post by marcov »

MrSwiss wrote:Just a remark on "shelling" from a compiled program:

I've learned "the hard way" that it is usually the better approach, to call a shell script (depending on OS).
The main REASONS are:
- flexibility: can be changed any Time "without touching source code of APPLICATION", recompiling etc.
- script can return Standard Error Codes, or "defined by you" Codes (on exit of script).

Ex. WIN: called "errorlevel" in *.cmd/*.bat (*.cmd = recommended, 32bit Console!)
Ex. DOS: called "errorlevel" in *.bat (16bit Console)

OTOH, if you use something CreateProcessW based it will actually be unicode. Running over a shell (cmd.exe) is always in the ansi encoding, and it is hard if not impossible to get accents right that are not in your default encoding.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Detect Stderr from FBC compiling

Post by MrSwiss »

marcov wrote:OTOH, if you use something CreateProcessW based it will actually be unicode. Running over a shell (cmd.exe) is always in the ansi encoding, and it is hard if not impossible to get accents right that are not in your default encoding.
Don't understand what you're about ...
This Thread reads DOS = ASCII, not any accenting ("plain old English", *.bat, command.com).
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Detect Stderr from FBC compiling

Post by marcov »

MrSwiss wrote:
marcov wrote:OTOH, if you use something CreateProcessW based it will actually be unicode. Running over a shell (cmd.exe) is always in the ansi encoding, and it is hard if not impossible to get accents right that are not in your default encoding.
Don't understand what you're about ...
This Thread reads DOS = ASCII, not any accenting ("plain old English", *.bat, command.com).
Then I wonder why the .cmd extension was mentioned. That got me off in the wrong direction. To my best knowledge command.com doesn't do .cmd.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Detect Stderr from FBC compiling

Post by MrSwiss »

marcov wrote:Then I wonder why the .cmd extension was mentioned.
A) Clearly marked as Example, by Ex:
B) To show that it's still "the same" (errorlevel) in later OS's
C) Advice for using the "more Modern" Command Processor, if NOT on DOS ...

Maybe a good Example, to illustrate a "quick Shot", that went off, into the wrong Direction ...
BTW: I don't like the "quick Shot" Approach, many here seem to favour (check the Outcome).
Post Reply