forked from cqctlang/l1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
74 lines (64 loc) · 1.86 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdlib.h>
#include <string.h>
#include "cqct.h"
#include "sys.h"
#include "util.h"
#include "syscqct.h"
#include "vm-options.h"
static void free_loadpath(struct vm_options* options) {
char** entry;
for (entry = options->loadpath; *entry != 0; ++entry) {
free(*entry);
}
free(options->loadpath);
options->loadpath = 0;
}
void cqct_options_init(struct vm_options* options) {
memset(options, 0, sizeof(*options));
}
void cqct_options_set_memfile(struct vm_options* options, const char* memfile) {
if (options->memfile) {
free(options->memfile);
options->memfile = 0;
}
if (memfile) {
options->memfile = strdup(memfile);
}
}
void cqct_options_set_loadpath(struct vm_options* options,
const char** loadpath) {
const char** entry;
char **cur;
int nentries = 0;
if (options->loadpath) {
free_loadpath(options);
}
for (entry = loadpath; *entry != 0; ++entry) {
++nentries;
}
options->loadpath = (char**)malloc((nentries + 1) * sizeof(char*));
cur = options->loadpath;
for (entry = loadpath; *entry != 0; ++entry) {
*(cur++) = strdup(*entry);
}
*cur = 0;
}
void cqct_options_release(struct vm_options* options) {
free(options->memfile);
free_loadpath(options);
memset(options, 0, sizeof(*options));
}
static void set_or_clear_xfd(const Xfd* src, Xfd* dst, char* is_set) {
if (src) {
*dst = *src;
} else {
memset(dst, 0, sizeof(*dst));
}
*is_set = !!src;
}
void cqct_options_set_io(struct vm_options* options,
const Xfd* in, const Xfd* out, const Xfd* err) {
set_or_clear_xfd(in, &options->io.in, &options->io.has_in);
set_or_clear_xfd(out, &options->io.out, &options->io.has_out);
set_or_clear_xfd(err, &options->io.err, &options->io.has_err);
}