Process

The running program, other programs it starts, and the environment.

CountProgramParameters ✅

Result = CountProgramParameters()

The number of parameters the program was started with (not counting the program's own name).

ProgramParameter ✅

Parameter$ = ProgramParameter([Index])

A parameter, by Index (0 = the first). Without Index, each call gives the next parameter, starting with the first. Returns "" when there are no more.

Quoted parameters are one parameter, without the quotes: prog "a b" c has the parameters a b and c.

ProgramFilename ✅

Filename$ = ProgramFilename()

The full path of the running executable.

OpenConsole()
PrintN(Str(CountProgramParameters()))
PrintN(LCase(GetExtensionPart(ProgramFilename())))

Output:

0
exe

GetEnvironmentVariable ✅

Result$ = GetEnvironmentVariable(Name$)

Returns the value of an environment variable, or "" if it isn't set.

OpenConsole()
PrintN(Str(Bool(GetEnvironmentVariable("PATH") <> "")) + "[" + GetEnvironmentVariable("X_SURELY_NOT_SET") + "]")

Output:

1[]

SetEnvironmentVariable ✅

SetEnvironmentVariable(Name$, Value$)

Sets an environment variable of this program (making it, or changing its value). Programs started afterwards with RunProgram get it too. On Windows names are the same in any case (Path is PATH); on Linux they are not.

RemoveEnvironmentVariable ✅

RemoveEnvironmentVariable(Name$)

Removes an environment variable.

OpenConsole()
SetEnvironmentVariable("X_DEMO", "one two")
PrintN("[" + GetEnvironmentVariable("X_DEMO") + "]")
SetEnvironmentVariable("X_DEMO", "changed")
PrintN("[" + GetEnvironmentVariable("X_DEMO") + "]")
RemoveEnvironmentVariable("X_DEMO")
PrintN("[" + GetEnvironmentVariable("X_DEMO") + "]")

Output:

[one two]
[changed]
[]

ExamineEnvironmentVariables ✅

Result = ExamineEnvironmentVariables()

Starts going through all the environment variables (it takes a copy of them as they are now). Returns nonzero.

NextEnvironmentVariable ✅

Result = NextEnvironmentVariable()

Moves to the next variable. Returns zero when there are no more.

EnvironmentVariableName, EnvironmentVariableValue ✅

Name$ = EnvironmentVariableName()
Value$ = EnvironmentVariableValue()

The current variable's name and value. On Windows the list also has a few hidden entries such as =C:=C:\Work (the current folder of each drive): their name is "", as in PureBasic.

OpenConsole()
SetEnvironmentVariable("X_DEMO", "42")
If ExamineEnvironmentVariables()
  While NextEnvironmentVariable()
    If EnvironmentVariableName() = "X_DEMO"
      PrintN(EnvironmentVariableName() + " = " + EnvironmentVariableValue())
    EndIf
  Wend
EndIf

Output:

X_DEMO = 42

Running programs

RunProgram ✅

Result = RunProgram(Filename$ [, Parameter$, WorkingDirectory$ [, Flags [, SenderProgram]]])

Starts a program. Without a path it is looked for the way the system does (the PATH; on Windows .exe may be left off). On Windows, a file that isn't a program ("notes.txt", "https://...") is opened with whatever opens it.

Parameter$ is split at spaces; "..." keeps spaces in one parameter and \" is a quote. Linux splits them by the same rules, so a program gets the same parameters on both.

Flags (combine with |):

Flag
#PB_Program_Wait wait until it ends
#PB_Program_Hide start it hidden (no window)
#PB_Program_Open keep it open, to use with the functions below; close it with CloseProgram
#PB_Program_Read read its output (stdout)
#PB_Program_Write write to its input (stdin)
#PB_Program_Error read its error output (stderr)
#PB_Program_Connect its input is the output of SenderProgram (opened with Read)
#PB_Program_NoInput Linux: its input is empty, instead of this program's
#PB_Program_Ascii / #PB_Program_Unicode / #PB_Program_UTF8 the string format for reading and writing (UTF-8 if none)

Read, Write, Error and Connect need Open too.

Returns zero if it couldn't be started. With #PB_Program_Open it returns the program, for the functions below; otherwise nonzero.

The examples here start themselves (ProgramFilename()) with a parameter, so that they run the same everywhere.

OpenConsole()
If ProgramParameter(0) = "child"
  PrintN("hello from the child")
  PrintN("bye")
  End 3
EndIf

p = RunProgram(ProgramFilename(), "child", "", #PB_Program_Open | #PB_Program_Read)
If p
  PrintN("IsProgram: " + Str(IsProgram(p)) + ", has a process id: " + Str(Bool(ProgramID(p) > 0)))
  While ProgramRunning(p)
    If AvailableProgramOutput(p)
      PrintN("read: " + ReadProgramString(p))
    EndIf
  Wend
  PrintN("exit code " + Str(ProgramExitCode(p)))
  CloseProgram(p)
EndIf

Output:

IsProgram: 1, has a process id: 1
read: hello from the child
read: bye
exit code 3

IsProgram ✅

Result = IsProgram(Program)

Nonzero if Program is a program opened with RunProgram (and not yet closed). Any value may be given.

ProgramID ✅

Result = ProgramID(Program)

Its process id (as shown by the task manager or ps). On Windows -1 if a file was opened with another program.

ProgramRunning ✅

Result = ProgramRunning(Program)

Nonzero while it runs - and, if it was started with #PB_Program_Read, while there is still output to read after it has ended. So a While ProgramRunning(p) loop reads all of it.

WaitProgram ✅

Result = WaitProgram(Program [, Timeout])

Waits until it ends, or for at most Timeout milliseconds. Returns nonzero if it has ended, zero if the time ran out.

KillProgram ✅

KillProgram(Program)

Ends it at once. ProgramExitCode then returns -1. It still needs CloseProgram.

ProgramExitCode ✅

Result = ProgramExitCode(Program)

The code it ended with (End 3 gives 3). On Linux, 0 to 255.

OpenConsole()
If ProgramParameter(0) = "slow"
  Delay(5000)
  End
EndIf

p = RunProgram(ProgramFilename(), "slow", "", #PB_Program_Open)
PrintN("ended after 100 ms: " + Str(WaitProgram(p, 100)))
KillProgram(p)
PrintN("running: " + Str(ProgramRunning(p)) + ", exit code " + Str(ProgramExitCode(p)))
CloseProgram(p)
PrintN("started anything: " + Str(RunProgram("surely-no-such-program")))

Output:

ended after 100 ms: 0
running: 0, exit code -1
started anything: 0

CloseProgram ✅

CloseProgram(Program)

Closes the connection (it doesn't end the program) and frees it. If it was started with #PB_Program_Write, its input ends.

AvailableProgramOutput ✅

Result = AvailableProgramOutput(Program)

The number of bytes of its output that can be read now without waiting.

ReadProgramString ✅

Text$ = ReadProgramString(Program [, Flags])

A line of its output, without the line break (Chr(13) + Chr(10) or Chr(10)). Waits for a whole line; at the end of its output, returns what is left (or ""). Flags: #PB_Ascii, #PB_UTF8 or #PB_Unicode, instead of the format given to RunProgram.

ReadProgramError ✅

Text$ = ReadProgramError(Program [, Flags])

A line of its error output. It doesn't wait: returns "" if no whole line has arrived.

ReadProgramData ✅

Result = ReadProgramData(Program, *Buffer, Size)

Reads up to Size bytes of its output. Waits until there are some; returns how many, or 0 at the end of its output.

OpenConsole()
If ProgramParameter(0) = "child"
  Print("12345")
  End
EndIf

p = RunProgram(ProgramFilename(), "child", "", #PB_Program_Open | #PB_Program_Read)
*buffer = AllocateMemory(100)
total = 0
Repeat
  n = ReadProgramData(p, *buffer + total, 100 - total)
  total + n
Until n = 0
PrintN(Str(total) + " bytes: " + PeekS(*buffer, total, #PB_UTF8))
CloseProgram(p)

Output:

5 bytes: 12345

WriteProgramString, WriteProgramStringN ✅

WriteProgramString(Program, Text$ [, Flags])
WriteProgramStringN(Program, Text$ [, Flags])

Writes text to its input; the N form adds a line break (Chr(13) + Chr(10) on Windows, Chr(10) on Linux).

WriteProgramData ✅

Result = WriteProgramData(Program, *Buffer, Size)

Writes Size bytes to its input. Returns how many were written. WriteProgramData(Program, #PB_Program_Eof, 0) ends its input: it reads no more after what was written.

OpenConsole()
If ProgramParameter(0) = "shout"          ; capitals of every line, until the input ends
  Repeat
    line$ = Input()
    If line$ = #PB_Input_Eof : Break : EndIf
    PrintN(UCase(line$))
    ConsoleError("did " + line$)
  ForEver
  End
EndIf

p = RunProgram(ProgramFilename(), "shout", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error)
WriteProgramStringN(p, "pear")
WriteProgramStringN(p, "fig")
WriteProgramData(p, #PB_Program_Eof, 0)
While ProgramRunning(p)
  If AvailableProgramOutput(p) : PrintN(ReadProgramString(p)) : EndIf
Wend
Repeat
  e$ = ReadProgramError(p)
  If e$ <> "" : PrintN("error output: " + e$) : EndIf
Until e$ = ""
CloseProgram(p)

Output:

PEAR
FIG
error output: did pear
error output: did fig

Programs can be joined, the output of one going to the input of the next, with #PB_Program_Connect:

OpenConsole()
Select ProgramParameter(0)
  Case "count"
    For i = 1 To 3 : PrintN("line " + Str(i)) : Next
    End
  Case "shout"
    Repeat
      line$ = Input()
      If line$ = #PB_Input_Eof : Break : EndIf
      PrintN(UCase(line$))
    ForEver
    End
EndSelect

first = RunProgram(ProgramFilename(), "count", "", #PB_Program_Open | #PB_Program_Read)
second = RunProgram(ProgramFilename(), "shout", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Connect, first)
While ProgramRunning(second)
  If AvailableProgramOutput(second) : PrintN(ReadProgramString(second)) : EndIf
Wend
CloseProgram(first)
CloseProgram(second)

Output:

LINE 1
LINE 2
LINE 3

ProgramOutput (x addition) ✅

Text$ = ProgramOutput(Filename$ [, Parameter$ [, WorkingDirectory$]])

Runs a program (hidden), waits for it to end, and returns everything it wrote to its output, with line breaks as Chr(10). "" if it couldn't be started.

OpenConsole()
If ProgramParameter(0) = "list"
  PrintN("apple") : PrintN("pear")
  End
EndIf

out$ = ProgramOutput(ProgramFilename(), "list")
PrintN(Str(CountString(out$, Chr(10))) + " lines")
PrintN(StringField(out$, 2, Chr(10)))

Output:

2 lines
pear