v / vlib / os
Raw file | 81 loc (75 sloc) | 2.38 KB | Latest commit hash 017ace6ea
1module os
2
3// - ProcessState.not_started - the process has not yet started
4// - ProcessState.running - the process is currently running
5// - ProcessState.stopped - the process was running, but was stopped temporarily
6// - ProcessState.exited - the process has finished/exited
7// - ProcessState.aborted - the process was terminated by a signal
8// - ProcessState.closed - the process resources like opened file descriptors were freed/discarded, final state.
9pub enum ProcessState {
10 not_started
11 running
12 stopped
13 exited
14 aborted
15 closed
16}
17
18[heap]
19pub struct Process {
20pub mut:
21 filename string // the process's command file path
22 pid int // the PID of the process
23 code int = -1
24 // the exit code of the process, != -1 *only* when status is .exited *and* the process was not aborted
25 status ProcessState = .not_started
26 // the current status of the process
27 err string // if the process fails, contains the reason why
28 args []string // the arguments that the command takes
29 work_folder string // the initial working folder of the process. When '', reuse the same folder as the parent process.
30 env_is_custom bool // true, when the environment was customized with .set_environment
31 env []string // the environment with which the process was started (list of 'var=val')
32 use_stdio_ctl bool // when true, then you can use p.stdin_write(), p.stdout_slurp() and p.stderr_slurp()
33 use_pgroup bool // when true, the process will create a new process group, enabling .signal_pgkill()
34 stdio_fd [3]int // the stdio file descriptors for the child process, used only by the nix implementation
35 wdata voidptr // the WProcess; used only by the windows implementation
36 create_no_window bool // sets a value indicating whether to start the process in a new window, The default is false; used only by the windows implementation
37}
38
39// new_process - create a new process descriptor
40// Note: new does NOT start the new process.
41// That is done because you may want to customize it first,
42// by calling different set_ methods on it.
43// In order to start it, call p.run() or p.wait()
44pub fn new_process(filename string) &Process {
45 return &Process{
46 filename: filename
47 stdio_fd: [-1, -1, -1]!
48 }
49}
50
51// set_args - set the arguments for the new process
52pub fn (mut p Process) set_args(pargs []string) {
53 if p.status != .not_started {
54 return
55 }
56 p.args = pargs
57 return
58}
59
60// set_work_folder - set the initial working folder for the new process
61// If you do not set it, it will reuse the current working folder of the parent process.
62pub fn (mut p Process) set_work_folder(path string) {
63 if p.status != .not_started {
64 return
65 }
66 p.work_folder = real_path(path)
67 return
68}
69
70// set_environment - set a custom environment variable mapping for the new process
71pub fn (mut p Process) set_environment(envs map[string]string) {
72 if p.status != .not_started {
73 return
74 }
75 p.env_is_custom = true
76 p.env = []string{}
77 for k, v in envs {
78 p.env << '${k}=${v}'
79 }
80 return
81}