~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/opts.c -rw-r--r-- 2.2 KiB
f9a8cbadStone Tickle print stdout/stderr of failing test 4 months ago
                                                                                
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "posix.h"

#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "lang/workspace.h"
#include "log.h"
#include "opts.h"

void
print_usage(FILE *f, const struct command *commands,
	const char *pre, const char *opts, const char *post)
{
	uint32_t i;
	fprintf(f, "usage: %s%s%s%s\n",
		pre,
		opts ?  " [opts]" : "",
		commands ?  " [command]" : "",
		post ? post : ""
		);

	if (opts) {
		fprintf(f,
			"opts:\n"
			"%s"
			"  -h - show this message\n",
			opts);
	}

	if (commands) {
		fprintf(f, "commands:\n");

		for (i = 0; commands[i].name; ++i) {
			fprintf(f, "  %-10s", commands[i].name);

			if (commands[i].desc) {
				fprintf(f, "- %s", commands[i].desc);
			}

			fputc('\n', f);
		}
	}
}

bool
find_cmd(const struct command *commands, cmd_func *ret,
	uint32_t argc, uint32_t argi, char *const argv[], bool optional)
{
	uint32_t i;
	const char *cmd;

	if (argi >= argc) {
		if (optional) {
			*ret = NULL;
			return true;
		} else {
			LOG_E("missing command");
			return false;
		}
	} else {
		cmd = argv[argi];
	}

	for (i = 0; commands[i].name; ++i) {
		if (strcmp(commands[i].name, cmd) == 0) {
			*ret = commands[i].cmd;
			return true;
		}
	}

	LOG_E("invalid command '%s'", cmd);
	return false;
}

bool
parse_config_key_value(struct workspace *wk, char *lhs, const char *val)
{
	char *subproj;

	subproj = lhs;
	if ((lhs = strchr(lhs, ':'))) {
		*lhs = 0;
		++lhs;
	} else {
		lhs = subproj;
		subproj = NULL;
	}

	if (!*lhs) {
		LOG_E("'%s%s=%s' missing option name",
			subproj ? subproj : "", subproj ? ":" : "", val);
		return false;
	} else if (subproj && !*subproj) {
		LOG_E("':%s=%s' there is a colon in the option name,"
			"but no subproject was specified", lhs, val);
		return false;
	}

	struct option_override oo = {
		.name = wk_str_push(wk, lhs),
		.val = wk_str_push(wk, val),
	};

	if (subproj) {
		oo.proj = wk_str_push(wk, subproj);
	}

	darr_push(&wk->option_overrides, &oo);

	return true;
}

bool
parse_config_opt(struct workspace *wk, char *lhs)
{
	char *rhs = strchr(lhs, '=');
	if (!rhs) {
		LOG_E("expected '=' in config opt '%s'", lhs);
		return false;
	}
	*rhs = 0;
	++rhs;

	return parse_config_key_value(wk, lhs, rhs);
}