-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.c
146 lines (133 loc) · 4.58 KB
/
config.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Author: Nicu Pavel <[email protected]>
* Copyright (c) 2021 Green Electronics LLC
* The MIT License (MIT)
*
*/
#include "config.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
ph_config_t config = {.port = DEFAULT_SERVER_PORT,
.addr = DEFAULT_SERVER_ADDR,
.timeout = -1,
.output_stdin = 1,
.rate = 0,
.max_lines = DEFAULT_MAX_LINES,
.body_prefix = NULL,
.body_suffix = NULL,
.line_delimiter = NULL};
void config_parse_opts(int argc, char **argv, ph_config_t *config) {
int opt, rc;
if (!config) return;
while ((opt = getopt(argc, argv, "l:p:a:b:s:d:t:r:ohV")) != -1) {
switch (opt) {
case 'l':
rc = sscanf(optarg, "%u", &config->max_lines);
if (rc < 1) {
config->rate = DEFAULT_MAX_LINES;
}
break;
case 'p':
rc = sscanf(optarg, "%hu", &config->port);
if (rc < 1) {
config->port = DEFAULT_SERVER_PORT;
}
break;
case 't':
rc = sscanf(optarg, "%d", &config->timeout);
if (rc < 1) {
config->timeout = -1;
}
break;
case 'r':
rc = sscanf(optarg, "%u", &config->rate);
if (rc < 1) {
config->rate = 0;
}
break;
case 'a':
config->addr = optarg;
break;
case 'b':
config->body_prefix = optarg;
break;
case 's':
config->body_suffix = optarg;
break;
case 'd':
config->line_delimiter = optarg;
break;
case 'o':
config->output_stdin = 0;
break;
case 'V':
case 'h':
default:
config_help();
exit(EXIT_SUCCESS);
break;
}
}
}
int config_set_key(ph_config_t *config, const char *key, const void *val) {
if (key == NULL || val == NULL) return -1;
if (strcmp(key, "rate") == 0) {
config->rate = (*(unsigned int *)val);
} else if (strcmp(key, "max_lines") == 0) {
config->max_lines = (*(unsigned int *)val);
} else if (strcmp(key, "output_stdin") == 0) {
config->output_stdin = (*(unsigned short int *)val);
} else if (strcmp(key, "timeout") == 0) {
config->timeout = (*(int *)val);
} else {
return -1;
}
return 0;
}
void config_print(ph_config_t *config) {
if (!config) return;
fprintf(stderr,
"Configuration: \n"
"\tport: %d\n"
"\taddr: %s\n"
"\ttimeout: %d\n"
"\toutput stdin: %d\n"
"\trate: %d seconds\n"
"\tmax_lines: %d\n"
"\tbody_prefix: %s\n"
"\tbody_suffix: %s\n"
"\tline_delimiter: %s\n",
config->port, config->addr, config->timeout, config->output_stdin,
config->rate, config->max_lines, config->body_prefix,
config->body_suffix, config->line_delimiter);
}
void config_help(void) {
fprintf(stdout, "\nph - %s\n\n", PH_VERSION);
fprintf(
stdout,
"Usage: "
"ph [options...]\n"
"The following options can also be supplied to the command:\n\n"
""
" -a <addr> - The address to bind. Default any\n"
" -p <port> - The port to bind. Default %d\n"
" -l <number> - Max number of lines to hold. Default %d\n"
" -t <seconds> - Inactivity timeout in seconds. Default infinite.\n"
" -b <string> - String to append at the begining of response. "
"Default none.\n"
" -s <string> - String to append at end of response. Default "
"none.\n"
" -d <string> - Line delimiter string to append between lines "
"(except last line). Default none.\n"
" -r <seconds> - Rate limiting incoming lines. Lines comming "
"faster "
"will be ignored. Default no limit.\n"
" -o - Don't output stdin to stdout\n"
" -h - This help.\n"
" -V - Display version information and exit.\n"
"\n\n",
DEFAULT_SERVER_PORT, DEFAULT_MAX_LINES);
}