comparison sip-in/readconf.c @ 89:e64d4d44025d

sip-in/readconf.c: first step toward non-trivial settings
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 23 Sep 2022 18:51:42 -0800
parents 709b78a4ebf0
children 736c2d68595f
comparison
equal deleted inserted replaced
88:97317ede320a 89:e64d4d44025d
26 int lineno; 26 int lineno;
27 int set_mask; 27 int set_mask;
28 }; 28 };
29 29
30 static void 30 static void
31 require_one_arg(st, kw, arg)
32 struct parse_state *st;
33 char *kw, *arg;
34 {
35 char *cp;
36
37 if (*arg == '\0' || *arg == '#') {
38 inv_syntax: fprintf(stderr,
39 "%s line %d: %s setting requires one argument\n",
40 config_file_pathname, st->lineno, kw);
41 exit(1);
42 }
43 for (cp = arg; *cp && !isspace(*cp); cp++)
44 ;
45 if (*cp)
46 *cp++ = '\0';
47 while (isspace(*cp))
48 cp++;
49 if (*cp != '\0' && *cp != '#')
50 goto inv_syntax;
51 }
52
53 static void
31 handle_ip(st, kw, var, arg) 54 handle_ip(st, kw, var, arg)
32 struct parse_state *st; 55 struct parse_state *st;
33 char *kw, *arg; 56 char *kw, *arg;
34 struct in_addr *var; 57 struct in_addr *var;
35 { 58 {
59 require_one_arg(st, kw, arg);
36 var->s_addr = inet_addr(arg); 60 var->s_addr = inet_addr(arg);
37 if (var->s_addr == INADDR_NONE) { 61 if (var->s_addr == INADDR_NONE) {
38 fprintf(stderr, 62 fprintf(stderr,
39 "%s line %d: invalid IP address argument \"%s\"\n", 63 "%s line %d: invalid IP address argument \"%s\"\n",
40 config_file_pathname, st->lineno, arg); 64 config_file_pathname, st->lineno, arg);
48 char *kw, *arg; 72 char *kw, *arg;
49 unsigned *var; 73 unsigned *var;
50 { 74 {
51 char *endp; 75 char *endp;
52 76
77 require_one_arg(st, kw, arg);
53 *var = strtoul(arg, &endp, 10); 78 *var = strtoul(arg, &endp, 10);
54 if (*endp) { 79 if (*endp) {
55 fprintf(stderr, "%s line %d: invalid numeric argument \"%s\"\n", 80 fprintf(stderr, "%s line %d: invalid numeric argument \"%s\"\n",
56 config_file_pathname, st->lineno, arg); 81 config_file_pathname, st->lineno, arg);
57 exit(1); 82 exit(1);
62 handle_bool(st, kw, var, arg) 87 handle_bool(st, kw, var, arg)
63 struct parse_state *st; 88 struct parse_state *st;
64 char *kw, *arg; 89 char *kw, *arg;
65 int *var; 90 int *var;
66 { 91 {
92 require_one_arg(st, kw, arg);
67 if (!strcmp(arg, "true") || !strcmp(arg, "on") || !strcmp(arg, "yes") 93 if (!strcmp(arg, "true") || !strcmp(arg, "on") || !strcmp(arg, "yes")
68 || !strcmp(arg, "1")) { 94 || !strcmp(arg, "1")) {
69 *var = 1; 95 *var = 1;
70 return; 96 return;
71 } 97 }
82 static void 108 static void
83 process_line(st, line) 109 process_line(st, line)
84 struct parse_state *st; 110 struct parse_state *st;
85 char *line; 111 char *line;
86 { 112 {
87 char *cp, *np, *arg; 113 char *cp, *kw;
88 void (*handler)(), *var; 114 void (*handler)(), *var;
89 int set_id; 115 int set_id;
90 116
91 if (!index(line, '\n')) { 117 if (!index(line, '\n')) {
92 fprintf(stderr, "%s line %d: too long or missing newline\n", 118 fprintf(stderr, "%s line %d: too long or missing newline\n",
95 } 121 }
96 for (cp = line; isspace(*cp); cp++) 122 for (cp = line; isspace(*cp); cp++)
97 ; 123 ;
98 if (*cp == '\0' || *cp == '#') 124 if (*cp == '\0' || *cp == '#')
99 return; 125 return;
100 for (np = cp; *cp && !isspace(*cp); cp++) 126 for (kw = cp; *cp && !isspace(*cp); cp++)
101 ; 127 ;
102 if (*cp) 128 if (*cp)
103 *cp++ = '\0'; 129 *cp++ = '\0';
104 if (!strcmp(np, "bind-ip")) { 130 if (!strcmp(kw, "bind-ip")) {
105 handler = handle_ip; 131 handler = handle_ip;
106 var = &sip_bind_ip; 132 var = &sip_bind_ip;
107 set_id = 1; 133 set_id = 1;
108 } else if (!strcmp(np, "bind-port")) { 134 } else if (!strcmp(kw, "bind-port")) {
109 handler = handle_num; 135 handler = handle_num;
110 var = &sip_bind_port; 136 var = &sip_bind_port;
111 set_id = 2; 137 set_id = 2;
112 } else if (!strcmp(np, "use-100rel")) { 138 } else if (!strcmp(kw, "use-100rel")) {
113 handler = handle_bool; 139 handler = handle_bool;
114 var = &cfg_use_100rel; 140 var = &cfg_use_100rel;
115 set_id = 0; 141 set_id = 0;
116 } else { 142 } else {
117 fprintf(stderr, "%s line %d: non-understood keyword \"%s\"\n", 143 fprintf(stderr, "%s line %d: non-understood keyword \"%s\"\n",
118 config_file_pathname, st->lineno, np); 144 config_file_pathname, st->lineno, kw);
119 exit(1); 145 exit(1);
120 } 146 }
121 if (st->set_mask & set_id) { 147 if (st->set_mask & set_id) {
122 fprintf(stderr, "%s line %d: duplicate %s setting\n", 148 fprintf(stderr, "%s line %d: duplicate %s setting\n",
123 config_file_pathname, st->lineno, np); 149 config_file_pathname, st->lineno, kw);
124 exit(1); 150 exit(1);
125 } 151 }
126 while (isspace(*cp)) 152 while (isspace(*cp))
127 cp++; 153 cp++;
128 if (*cp == '\0' || *cp == '#') { 154 handler(st, kw, var, cp);
129 inv_syntax: fprintf(stderr,
130 "%s line %d: %s setting requires one argument\n",
131 config_file_pathname, st->lineno, np);
132 exit(1);
133 }
134 for (arg = cp; *cp && !isspace(*cp); cp++)
135 ;
136 if (*cp)
137 *cp++ = '\0';
138 while (isspace(*cp))
139 cp++;
140 if (*cp != '\0' && *cp != '#')
141 goto inv_syntax;
142 handler(st, np, var, arg);
143 st->set_mask |= set_id; 155 st->set_mask |= set_id;
144 } 156 }
145 157
146 read_config_file() 158 read_config_file()
147 { 159 {