mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-07 16:01:30 +00:00
Add config file processing
This commit is contained in:
1 parent
a4c9d1ecb7
commit
596c70d3dc
2 files changed
+59
-19
No files matched your search
@@ -7,9 +7,9 @@ VC98 = docker run --rm -e WINEDEBUG=-all -v $(ROOT):$(ROOT) -w $(CURDIR) docker.
|
|||||||
GCC = docker run --rm -v $(ROOT):$(ROOT) -w $(CURDIR) mdashnet/cc2
|
GCC = docker run --rm -v $(ROOT):$(ROOT) -w $(CURDIR) mdashnet/cc2
|
||||||
|
|
||||||
all: $(PROG)
|
all: $(PROG)
|
||||||
$(DEBUGGER) ./$(PROG)
|
$(DEBUGGER) ./$(PROG) $(ARGS)
|
||||||
|
|
||||||
$(PROG):
|
$(PROG): main.c Makefile
|
||||||
$(CC) ../../mongoose.c main.c -I../.. $(CFLAGS) $(EXTRA) -o $@
|
$(CC) ../../mongoose.c main.c -I../.. $(CFLAGS) $(EXTRA) -o $@
|
||||||
|
|
||||||
mongoose_mac: $(PROG)
|
mongoose_mac: $(PROG)
|
||||||
|
|||||||
@@ -31,27 +31,66 @@ static void usage(const char *prog) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cfg(const char *progname, const char *optname, const char *value) {
|
||||||
|
if (value == NULL) {
|
||||||
|
usage(progname);
|
||||||
|
} else if (strcmp(optname, "-D") == 0) {
|
||||||
|
s_debug_level = value;
|
||||||
|
} else if (strcmp(optname, "-H") == 0) {
|
||||||
|
s_enable_hexdump = value;
|
||||||
|
} else if (strcmp(optname, "-S") == 0) {
|
||||||
|
s_ssi_pattern = value;
|
||||||
|
} else if (strcmp(optname, "-d") == 0) {
|
||||||
|
s_root_dir = value;
|
||||||
|
} else if (strcmp(optname, "-l") == 0) {
|
||||||
|
s_listening_address = value;
|
||||||
|
} else {
|
||||||
|
usage(progname);
|
||||||
|
}
|
||||||
|
LOG(LL_INFO, ("Setting option [%s] to [%s]", optname, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void parse_config_file(const char *exe) {
|
||||||
|
char path[1024];
|
||||||
|
const char *p = strrchr(exe, MG_DIRSEP);
|
||||||
|
FILE *fp;
|
||||||
|
if (p == NULL) p = strrchr(exe, '/');
|
||||||
|
snprintf(path, sizeof(path), "%.*s%c%s", p == NULL ? 1 : (int) (p - exe),
|
||||||
|
p == NULL ? "." : exe, MG_DIRSEP, "mongoose.conf");
|
||||||
|
if ((fp = fopen(path, "rb")) != NULL) {
|
||||||
|
char line[8192], *name;
|
||||||
|
int i, line_no = 0;
|
||||||
|
LOG(LL_INFO, ("Processing config file %s", path));
|
||||||
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
||||||
|
line_no++;
|
||||||
|
// Trim whitespaces at the end
|
||||||
|
for (i = strlen(line); i > 0 && isspace(line[i - 1]);) line[--i] = '\0';
|
||||||
|
// Ignore empty lines and comments
|
||||||
|
for (i = 0; isspace(line[i]);) i++;
|
||||||
|
if (line[i] == '#' || line[i] == '\0') continue;
|
||||||
|
name = &line[i];
|
||||||
|
while (line[i] != '\0' && !isspace(line[i])) i++;
|
||||||
|
if (line[i] != ' ') {
|
||||||
|
LOG(LL_ERROR,
|
||||||
|
("%s: ignoring invalid line %d: %s", path, line_no, line));
|
||||||
|
} else {
|
||||||
|
line[i++] = '\0'; // nul-terminate name
|
||||||
|
while (isspace(line[i])) i++;
|
||||||
|
cfg(exe, name, &line[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
struct mg_mgr mgr;
|
struct mg_mgr mgr;
|
||||||
struct mg_connection *c;
|
struct mg_connection *c;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// Parse command-line flags
|
// Configure options from a command-line flags and configuration file
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i += 2) cfg(argv[0], argv[i], argv[i + 1]);
|
||||||
if (strcmp(argv[i], "-d") == 0) {
|
parse_config_file(argv[0]);
|
||||||
s_root_dir = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-D") == 0) {
|
|
||||||
s_debug_level = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-H") == 0) {
|
|
||||||
s_enable_hexdump = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-S") == 0) {
|
|
||||||
s_ssi_pattern = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-l") == 0) {
|
|
||||||
s_listening_address = argv[++i];
|
|
||||||
} else {
|
|
||||||
usage(argv[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialise stuff
|
// Initialise stuff
|
||||||
mg_log_set(s_debug_level);
|
mg_log_set(s_debug_level);
|
||||||
@@ -64,7 +103,8 @@ int main(int argc, char *argv[]) {
|
|||||||
if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1;
|
if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1;
|
||||||
|
|
||||||
// Start infinite event loop
|
// Start infinite event loop
|
||||||
LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION));
|
LOG(LL_INFO, ("Starting Mongoose v%s, serving %s", MG_VERSION,
|
||||||
|
realpath(s_root_dir, NULL)));
|
||||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in new issue
Block a user