Skip to content

Commit

Permalink
treewide: deduplicate token parameter parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
f00b4r0 committed Sep 11, 2024
1 parent 6217da2 commit d909c31
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 67 deletions.
38 changes: 38 additions & 0 deletions acarsdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,41 @@ int main(int argc, char **argv)

exit(res);
}

/**
* Parse a parameter string.
* Parameter string formatted like "param1=foo,param2=blah,param3=42"
* @paramsp pointer to params string input
* @sp pointer to struct containing expected parameters, the #valp member will be updated for each match
* @np array size of #sp
* @return NULL if the input has been succesfully exhausted (or was NULL), pointer to first unmatched token otherwise
* @note Behavior matching that of strsep(): *paramsp is updated to point to next token group or NULL if EOL
* If an unidentified parameter is found in the string, it is returned by the function, with the '=' separator restored
*/
char * parse_params(char **paramsp, struct params_s *sp, const int np)
{
char *param, *sep;
int i;

while ((param = strsep(paramsp, ","))) {
sep = strchr(param, '=');
if (!sep)
continue;

*sep++ = '\0';

for (i = 0; i < np; i++) {
if (!strcmp(sp[i].name, param)) {
*sp[i].valp = sep;
break;
}
}

if (np == i) { // unknown param
*--sep = '='; // restore key-value separator for external processing
return param;
}
}

return NULL;
}
7 changes: 7 additions & 0 deletions acarsdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ typedef struct {

extern runtime_t R;

struct params_s {
const char *const name;
char **valp;
};

char * parse_params(char **, struct params_s *, const int);

#endif /* acarsdec_h */
24 changes: 10 additions & 14 deletions fileout.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,18 @@ static FILE *open_outfile(fileout_t *fout)
// optional "rotate=" parameter followed by "none" (default), "hourly", "daily"
fileout_t *Fileoutinit(char *params)
{
char *param, *sep, *path = NULL, *rotate = NULL;
char *path = NULL, *rotate = NULL;
struct params_s filep[] = {
{ .name = "path", .valp = &path, },
{ .name = "rotate", .valp = &rotate, },
};
char *retp;
fileout_t *fout;

while ((param = strsep(&params, ","))) {
sep = strchr(param, '=');
if (!sep)
continue;
*sep++ = '\0';
if (!strcmp("path", param))
path = sep;
if (!strcmp("rotate", param))
rotate = sep;
else {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", param);
return NULL;
}
retp = parse_params(&params, filep, ARRAY_SIZE(filep));
if (retp) {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", retp);
return NULL;
}

fout = calloc(1, sizeof(*fout));
Expand Down
46 changes: 24 additions & 22 deletions mqttout.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,36 @@ mqttout_t *MQTTinit(char *params)
mqttout_t *mqpriv;
char *urls[15] = {};
char **url, *topic = NULL, *user = NULL, *passwd = NULL, *msgtopic = NULL;
char *param, *sep;
struct params_s mqttp[] = {
{ .name = "topic", .valp = &topic, },
{ .name = "user", .valp = &user, },
{ .name = "passwd", .valp = &passwd, },
};
char *retp, *sep;
int rc;
MQTTAsync_createOptions create_opts = MQTTAsync_createOptions_initializer;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;

url = urls;
while ((param = strsep(&params, ","))) {
sep = strchr(param, '=');
if (!sep)
continue;
*sep++ = '\0';
if (!strcmp("topic", param))
topic = sep;
else if (!strcmp("user", param))
user = sep;
else if (!strcmp("passwd", param))
passwd = sep;
else if (!strcmp("uri", param)) {
if (url > &urls[14])
fprintf(stderr, WARNPFX "too many urls provided, ignoring '%s'\n", sep);
else
*url++ = sep;
}
else {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", param);
return NULL;

do {
retp = parse_params(&params, mqttp, ARRAY_SIZE(mqttp));
if (retp) {
sep = strchr(retp, '='); // guaranteed to exist due to parse_params()
*sep++ = '\0';
if (!strcmp("uri", retp)) {
if (url > &urls[14])
fprintf(stderr, WARNPFX "too many urls provided, ignoring '%s'\n", sep);
else
*url++ = sep;
}
else {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", retp);
return NULL;

}
}
}
} while (retp);

if (!urls[0]) {
fprintf(stderr, ERRPFX "no URI provided\n");
Expand Down
23 changes: 9 additions & 14 deletions netout.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,21 @@
// params is "host=xxx,port=yyy"
netout_t *Netoutinit(char *params)
{
char *param, *sep;
char *addr = NULL;
char *port = NULL;
struct params_s netp[] = {
{ .name = "host", .valp = &addr, },
{ .name = "port", .valp = &port, },
};
char *retp;
struct addrinfo hints, *servinfo, *p;
int sockfd, rv;
netout_t *netpriv = NULL;

while ((param = strsep(&params, ","))) {
sep = strchr(param, '=');
if (!sep)
continue;
*sep++ = '\0';
if (!strcmp("host", param))
addr = sep;
else if (!strcmp("port", param))
port = sep;
else {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", param);
return NULL;
}
retp = parse_params(&params, netp, ARRAY_SIZE(netp));
if (retp) {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", retp);
return NULL;
}

if (!addr) {
Expand Down
28 changes: 11 additions & 17 deletions statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,20 @@ static struct {
*/
int statsd_init(char *params, const char *idstation)
{
const char *host = NULL, *port = NULL;
char *param, *sep;
char *host = NULL, *port = NULL, *retp;
struct params_s statsdp[] = {
{ .name = "host", .valp = &host, },
{ .name = "port", .valp = &port, },
};
int sockfd;
struct addrinfo hints;
struct addrinfo *result, *rp;
int ret;

while ((param = strsep(&params, ","))) {
sep = strchr(param, '=');
if (!sep)
continue;
*sep++ = '\0';
if (!strcmp("host", param))
host = sep;
else if (!strcmp("port", param))
port = sep;
else {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", param);
return -1;
}
retp = parse_params(&params, statsdp, ARRAY_SIZE(statsdp));
if (retp) {
fprintf(stderr, ERRPFX "unknown parameter '%s'\n", retp);
return -1;
}

if (!host || !port) {
Expand Down Expand Up @@ -111,9 +105,9 @@ int statsd_init(char *params, const char *idstation)
perror("statsd");
goto cleanup;
}
sep = stpcpy(statsd_runtime.namespace, STATSD_NAMESPACE);
retp = stpcpy(statsd_runtime.namespace, STATSD_NAMESPACE);
if (idstation) {
strcpy(sep, idstation);
strcpy(retp, idstation);
statsd_runtime.namespace[ret++] = '.';
statsd_runtime.namespace[ret] = '\0';
}
Expand Down

0 comments on commit d909c31

Please sign in to comment.