From: Tom Balzer Date: Wed, 27 Jun 2018 08:51:07 +0000 (-0500) Subject: Added dumb option detection to tomc X-Git-Url: http://git.hcoop.net/tlb/tomd.git/commitdiff_plain/805d0ff4aa57dabe5cec496f6d7de975ac74f759 Added dumb option detection to tomc --- diff --git a/include/macros.h b/include/macros.h index aabab76..75a38b8 100644 --- a/include/macros.h +++ b/include/macros.h @@ -1,6 +1,7 @@ #ifndef _MACROS_H #define _MACROS_H +#define tomc_p(...) {printf("[tomc] "); printf(__VA_ARGS__); printf("\n");} #define tomd_p(...) {printf("[tomd] "); printf(__VA_ARGS__); printf("\n");} #define SCM_ARR(arr, index) (scm_list_ref(arr, scm_from_int(index))) #define SCM_LIST_LEN(list) (scm_to_int(scm_length(list))) diff --git a/src/tomc/main.c b/src/tomc/main.c index c0325ee..6fb6030 100644 --- a/src/tomc/main.c +++ b/src/tomc/main.c @@ -25,6 +25,12 @@ static int sfd; +static void header(void) +{ + tomc_p("Tom's Client, Copyright (C) 2018 Thomas Balzer"); + tomc_p("GPL v3 or later license."); +} + static void init(void) { /* init socket connection */ @@ -57,17 +63,63 @@ void write_hallo(void) printf("wrote %d bytes.\n", wrote); } -static void header(void) +static char *default_name = "no_name"; +static struct{ + char status, stop, kill, start; + char *name; +} options; + +static void extract_options(int argc, char **argv) +{ + /* -status Get status - if we thought we ran it, current real status */ + /* -stop Run provided command to gently stop */ + /* -kill Aggressive killing of process (signal 15) */ + /* -start Run name */ + if(argc == 1){ + /* only tomc given */ + tomc_p("no args given."); + exit(EXIT_SUCCESS); + } + + options.name = NULL; + + for(int i = 1; + i < argc; + i++){ + char *arg = argv[i]; +#define X(op) {if(strcmp("-" #op, arg) == 0){options.op = 1; continue;}} + X(status); + X(stop); + X(kill); + X(start); +#undef X + /* assume last non option was the name */ + options.name = arg; + } + + if(options.name == NULL){ + options.name = default_name; + } +} + +static void print_options() { - tomd_p("Tom's Client, Copyright (C) 2018 Thomas Balzer"); - tomd_p("GPL v3 or later license."); + tomc_p("---------"); + tomc_p(" name: %s", options.name); + tomc_p(" kill: %d", options.kill); + tomc_p("status: %d", options.status); + tomc_p(" stop: %d", options.stop); + tomc_p(" start: %d", options.start); + tomc_p("---------"); } int main(int argc, char **argv) { header(); + extract_options(argc, argv); + print_options(); init(); - write_hallo(); + return EXIT_SUCCESS; }