Added dumb option detection to tomc
authorTom Balzer <niebieskitrociny@gmail.com>
Wed, 27 Jun 2018 08:51:07 +0000 (03:51 -0500)
committerTom Balzer <niebieskitrociny@gmail.com>
Wed, 27 Jun 2018 08:51:07 +0000 (03:51 -0500)
include/macros.h
src/tomc/main.c

index aabab76..75a38b8 100644 (file)
@@ -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)))
index c0325ee..6fb6030 100644 (file)
 
 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 <name>  Get status - if we thought we ran it, current real status */
+  /* -stop <name>    Run provided command to gently stop */
+  /* -kill <name>    Aggressive killing of process (signal 15) */
+  /* -start <name>   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;
 }