v2 / vlib / os / process.v
79 lines · 73 sloc · 3.04 KB · 37255767290c243b71f0e78d77c3bd5e875748e6
Raw
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 // the exit code of the process, != -1 *only* when status is .exited *and* the process was not aborted
24 status ProcessState = .not_started // the current status of the process
25 err string // if the process fails, contains the reason why
26 args []string // the arguments that the command takes
27 work_folder string // the initial working folder of the process. When '', reuse the same folder as the parent process.
28 env_is_custom bool // true, when the environment was customized with .set_environment
29 env []string // the environment with which the process was started (list of 'var=val')
30 use_stdio_ctl bool // when true, then you can use p.stdin_write(), p.stdout_slurp() and p.stderr_slurp()
31 use_pgroup bool // when true, the process will create a new process group, enabling .signal_pgkill()
32 stdio_fd [3]int // the stdio file descriptors for the child process, used only by the nix implementation
33 wdata voidptr // the WProcess; used only by the windows implementation
34 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
35}
36
37// new_process - create a new process descriptor.
38// Note: new does NOT start the new process.
39// That is done because you may want to customize it first,
40// by calling different set_ methods on it.
41// In order to start it, call p.run() or p.wait()
42pub fn new_process(filename string) &Process {
43 return &Process{
44 filename: filename
45 stdio_fd: [-1, -1, -1]!
46 }
47}
48
49// set_args - set the arguments for the new process.
50pub fn (mut p Process) set_args(pargs []string) {
51 if p.status != .not_started {
52 return
53 }
54 p.args = pargs
55 return
56}
57
58// set_work_folder - set the initial working folder for the new process.
59// If you do not set it, it will reuse the current working folder of the parent process.
60pub fn (mut p Process) set_work_folder(path string) {
61 if p.status != .not_started {
62 return
63 }
64 p.work_folder = real_path(path)
65 return
66}
67
68// set_environment - set a custom environment variable mapping for the new process.
69pub fn (mut p Process) set_environment(envs map[string]string) {
70 if p.status != .not_started {
71 return
72 }
73 p.env_is_custom = true
74 p.env = []string{}
75 for k, v in envs {
76 p.env << '${k}=${v}'
77 }
78 return
79}
80