2019-01-17 19:07:47 +11:00
|
|
|
/*
|
2019-01-18 00:00:17 +11:00
|
|
|
* core/parseconf.c
|
2019-01-17 19:07:47 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2019-01-23 03:23:11 +11:00
|
|
|
#include <stdlib.h>
|
2019-01-17 19:07:47 +11:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
extern int verbosity;
|
|
|
|
extern Configuration config;
|
|
|
|
|
2019-01-19 03:09:15 +11:00
|
|
|
#define CMP(a) (!strcmp(keyptr, a))
|
2019-01-18 00:00:17 +11:00
|
|
|
|
2019-01-17 19:07:47 +11:00
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
int parse_config(char *fname, int flags)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2019-01-19 03:09:15 +11:00
|
|
|
char line[SZ_STRINGS+1],
|
|
|
|
*keyptr, *typeptr, *cptr;
|
2019-01-18 00:00:17 +11:00
|
|
|
int numligne;
|
2019-01-17 19:07:47 +11:00
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, ">>> %s ( '%s' $%x )\n", fname, flags);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
config.valid = 49;
|
|
|
|
|
|
|
|
if (NULL==(fp=fopen(fname, "r"))) {
|
|
|
|
perror(fname);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:00:17 +11:00
|
|
|
numligne = 0;
|
|
|
|
|
2019-01-17 19:07:47 +11:00
|
|
|
while (fgets(line, SZ_STRINGS, fp))
|
|
|
|
{
|
2019-01-18 00:00:17 +11:00
|
|
|
numligne++;
|
|
|
|
if ('\0'==line[0]) {
|
|
|
|
fprintf(stderr, "%s : short read line %d\n",
|
|
|
|
fname, numligne);
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:07:47 +11:00
|
|
|
/* massage the end of line */
|
2019-01-18 00:00:17 +11:00
|
|
|
line[strlen(line)-1] = '\0'; /* kill EOL */
|
|
|
|
if (verbosity) {
|
|
|
|
fprintf(stderr, "%3d :\t%s\n", numligne, line);
|
|
|
|
}
|
2019-01-17 19:07:47 +11:00
|
|
|
|
2019-01-18 00:00:17 +11:00
|
|
|
/* seek for the first token in this line */
|
2019-01-19 03:09:15 +11:00
|
|
|
if (NULL==(keyptr = strtok(line, " \t"))) {
|
2019-01-18 00:00:17 +11:00
|
|
|
/* Got an empty line */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* skip comments */
|
2019-01-19 03:09:15 +11:00
|
|
|
if ('#'==*keyptr) continue;
|
|
|
|
/* seek for the type field */
|
|
|
|
if (NULL==(typeptr = strtok(NULL, " \t"))) {
|
|
|
|
/* we can(t get a type flag ? wtf ? */
|
|
|
|
fprintf(stderr, "ERROR line %d : no type\n", numligne);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-17 19:07:47 +11:00
|
|
|
|
2019-01-19 03:09:15 +11:00
|
|
|
if(verbosity)
|
|
|
|
fprintf(stderr, "[%s] type %s\n", keyptr, typeptr);
|
2019-01-17 19:07:47 +11:00
|
|
|
|
2019-01-19 03:09:15 +11:00
|
|
|
if CMP("abort") {
|
|
|
|
fprintf(stderr, "abort in config file\n");
|
|
|
|
}
|
2019-01-17 19:07:47 +11:00
|
|
|
|
2019-01-19 03:09:15 +11:00
|
|
|
if (CMP("input_device")) {
|
|
|
|
config.input_device = strdup(strtok(NULL, " \t"));
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-23 03:23:11 +11:00
|
|
|
|
|
|
|
|
|
|
|
if (CMP("input_speed")) {
|
|
|
|
config.input_speed = atoi(strtok(NULL, " \t"));
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, "input speed = %d\n", config.input_speed);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:00:17 +11:00
|
|
|
}
|
2019-01-17 19:07:47 +11:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2019-01-19 03:09:15 +11:00
|
|
|
return 0;
|
2019-01-17 19:07:47 +11:00
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
int show_config(char *title)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (verbosity) {
|
|
|
|
printf("********** %s **********\n", title);
|
|
|
|
}
|
|
|
|
|
2019-01-19 03:09:15 +11:00
|
|
|
printf("valid : %d\n", config.valid);
|
|
|
|
printf("input device : %s\n", config.input_device);
|
2019-01-23 03:23:11 +11:00
|
|
|
printf("input speed : %d\n", config.input_speed);
|
2019-01-17 19:07:47 +11:00
|
|
|
|
|
|
|
puts("");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|