X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=x5%2Fkhatus.c;h=db800ea36b0c4026ce746dfa0dd0c36ecc8e8baf;hb=574a4bff980c2e56c603c81e8c2773e42b4baf71;hp=37cbccca13a5d23fe703d2cb20083bd5e6fd6795;hpb=b6316e94783d975f109c9901e4061ba041f0cb3f;p=khatus.git diff --git a/x5/khatus.c b/x5/khatus.c index 37cbccc..db800ea 100644 --- a/x5/khatus.c +++ b/x5/khatus.c @@ -69,24 +69,23 @@ struct Config { void fifo_print_one(Fifo *f, Config *cfg) { - info( - "Fifo " - "{" - " name = %s," - " fd = %d," - " width = %d," - " last_read = %d," - " ttl = %d," - " pos = %d," - " next = %p," - " }\n", - f->name, - f->fd, - f->width, - f->last_read, - f->ttl, - f->pos, - f->next + info("Fifo " + "{" + " name = %s," + " fd = %d," + " width = %d," + " last_read = %d," + " ttl = %d," + " pos = %d," + " next = %p," + " }\n", + f->name, + f->fd, + f->width, + f->last_read, + f->ttl, + f->pos, + f->next ); } @@ -102,20 +101,20 @@ void config_print(Config *cfg) { info( - "Config " - "{" - " interval = %d," - " separator = %s," - " fifo_count = %d," - " total_width = %d," - " log_level = %d," - " fifos = ..." - " }\n", - cfg->interval, - cfg->separator, - cfg->fifo_count, - cfg->total_width, - cfg->log_level + "Config " + "{" + " interval = %d," + " separator = %s," + " fifo_count = %d," + " total_width = %d," + " log_level = %d," + " fifos = ..." + " }\n", + cfg->interval, + cfg->separator, + cfg->fifo_count, + cfg->total_width, + cfg->log_level ); fifo_print_all(cfg->fifos, cfg); } @@ -134,31 +133,31 @@ print_usage() { assert(argv0); fprintf( - stderr, - "\n" - "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" - "\n" - " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" - " FILE_PATH = string\n" - " DATA_WIDTH = int (* (positive) number of characters *)\n" - " DATA_TTL = int (* (positive) number of seconds *)\n" - " OPTION = -i INTERVAL\n" - " | -s SEPARATOR\n" - " | -x (* Output to X root window *)\n" - " | -l LOG_LEVEL\n" - " SEPARATOR = string\n" - " INTERVAL = int (* (positive) number of seconds *)\n" - " LOG_LEVEL = int (* %d through %d *)\n" - "\n", - argv0, - Nothing, - Debug + stderr, + "\n" + "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" + "\n" + " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" + " FILE_PATH = string\n" + " DATA_WIDTH = int (* (positive) number of characters *)\n" + " DATA_TTL = int (* (positive) number of seconds *)\n" + " OPTION = -i INTERVAL\n" + " | -s SEPARATOR\n" + " | -x (* Output to X root window *)\n" + " | -l LOG_LEVEL\n" + " SEPARATOR = string\n" + " INTERVAL = int (* (positive) number of seconds *)\n" + " LOG_LEVEL = int (* %d through %d *)\n" + "\n", + argv0, + Nothing, + Debug ); fprintf( - stderr, - "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" - "\n", - argv0 + stderr, + "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" + "\n", + argv0 ); } @@ -167,78 +166,67 @@ void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive ca void parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) { - if (i < argc) { - char *param = argv[i++]; + char *param; - if (is_pos_num(param)) { - cfg->interval = atoi(param); - opts_parse_any(cfg, argc, argv, i); - } else { - usage("Option -i parameter is invalid: \"%s\"\n", param); - } - } else { + if (i >= argc) usage("Option -i parameter is missing.\n"); - } + param = argv[i++]; + if (!is_pos_num(param)) + usage("Option -i parameter is invalid: \"%s\"\n", param); + cfg->interval = atoi(param); + opts_parse_any(cfg, argc, argv, i); } void parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) { - if (i < argc) { - cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); - strcpy(cfg->separator, argv[i]); - opts_parse_any(cfg, argc, argv, ++i); - } else { + if (i >= argc) usage("Option -s parameter is missing.\n"); - } + cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); + strcpy(cfg->separator, argv[i]); + opts_parse_any(cfg, argc, argv, ++i); } void parse_opts_opt_l(Config *cfg, int argc, char *argv[], int i) { + char *param; int log_level; - if (i < argc) { - char *param = argv[i++]; - - if (is_pos_num(param)) { - log_level = atoi(param); - if (log_level <= Debug) { - cfg->log_level = log_level; - opts_parse_any(cfg, argc, argv, i); - } else { - usage("Option -l value (%d) exceeds maximum (%d)\n", log_level, Debug); - } - } else { - usage("Option -l parameter is invalid: \"%s\"\n", param); - } - } else { + if (i >= argc) usage("Option -l parameter is missing.\n"); - } + param = argv[i++]; + if (!is_pos_num(param)) + usage("Option -l parameter is invalid: \"%s\"\n", param); + log_level = atoi(param); + if (log_level > Debug) + usage("Option -l value (%d) exceeds maximum (%d)\n", log_level, Debug); + cfg->log_level = log_level; + opts_parse_any(cfg, argc, argv, i); } void parse_opts_opt(Config *cfg, int argc, char *argv[], int i) { switch (argv[i][1]) { - case 'i': - /* TODO: Generic set_int */ - parse_opts_opt_i(cfg, argc, argv, ++i); - break; - case 's': - /* TODO: Generic set_str */ - parse_opts_opt_s(cfg, argc, argv, ++i); - break; - case 'x': - cfg->output_to_x_root_window = 1; - opts_parse_any(cfg, argc, argv, ++i); - break; - case 'l': - /* TODO: Generic set_int */ - parse_opts_opt_l(cfg, argc, argv, ++i); - break; - default : - usage("Option \"%s\" is invalid\n", argv[i]); + case 'i': + /* TODO: Generic set_int */ + parse_opts_opt_i(cfg, argc, argv, ++i); + break; + case 's': + /* TODO: Generic set_str */ + parse_opts_opt_s(cfg, argc, argv, ++i); + break; + case 'x': + cfg->output_to_x_root_window = 1; + opts_parse_any(cfg, argc, argv, ++i); + break; + case 'l': + /* TODO: Generic set_int */ + parse_opts_opt_l(cfg, argc, argv, ++i); + break; + default : + usage("Option \"%s\" is invalid\n", argv[i]); } } @@ -280,11 +268,11 @@ opts_parse_any(Config *cfg, int argc, char *argv[], int i) { if (i < argc) { switch (argv[i][0]) { - case '-': - parse_opts_opt(cfg, argc, argv, i); - break; - default : - parse_opts_spec(cfg, argc, argv, i); + case '-': + parse_opts_opt(cfg, argc, argv, i); + break; + default : + parse_opts_spec(cfg, argc, argv, i); } } } @@ -337,9 +325,10 @@ fifo_read_one(Fifo *f, char *buf, Config *cfg) if (current == -1) { error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno)); fifo_read_error(f, buf); - } else + } else { while (total++ < f->width) *b++ = ' '; + } /* TODO Record timestamp read */ close(f->fd); f->fd = -1; @@ -404,9 +393,9 @@ snooze(struct timespec *t, Config *cfg) if (result < 0) { if (errno == EINTR) { info( - "nanosleep interrupted. Remainder: " - "{ tv_sec = %ld, tv_nsec = %ld }", - remainder.tv_sec, remainder.tv_nsec); + "nanosleep interrupted. Remainder: " + "{ tv_sec = %ld, tv_nsec = %ld }", + remainder.tv_sec, remainder.tv_nsec); /* No big deal if we occasionally sleep less, * so not attempting to correct after an interruption. */ @@ -518,8 +507,9 @@ main(int argc, char *argv[]) timespecsub(&ti, &td, &tc); debug("snooze YES\n"); snooze(&tc, cfg); - } else + } else { debug("snooze NO\n"); + } } return EXIT_SUCCESS;