supercop

Basic CLI for supercop
git clone git://git.finwo.net/app/supercop
Log | Files | Refs | README | LICENSE

main.c (1192B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #ifdef __cplusplus
      6 extern "C" {
      7 #endif
      8 
      9 #include "cli/command.h"
     10 #include "cli/common.h"
     11 #include "cli/usage.h"
     12 #include "fmt/common.h"
     13 
     14 struct Format *supercop_formats = NULL;
     15 struct cmd_struct *commands = NULL;
     16 
     17 int main(int argc, const char **argv) {
     18   int show_version = 0;
     19 
     20   argc = supercop_global_options(argc, argv, &show_version);
     21 
     22   /* --version is nothing but another way of calling the version command */
     23   if (show_version) {
     24     static const char *version_argv[] = {"version", NULL};
     25     argv                              = version_argv;
     26     argc                              = 1;
     27   }
     28 
     29   if (argc < 1) {
     30     supercop_print_global_usage();
     31     return 1;
     32   }
     33 
     34   /* Try to run command with args provided. */
     35   struct cmd_struct *cmd = commands;
     36   while (cmd) {
     37     const char **name = cmd->name;
     38     while (*name) {
     39       if (!strcmp(*name, argv[0])) {
     40         goto found;
     41       }
     42       name++;
     43     }
     44     cmd = cmd->next;
     45   }
     46 found:
     47 
     48   if (cmd) {
     49     return cmd->fn(argc, argv);
     50   } else {
     51     fprintf(stderr, "Unknown command: %s\n", argv[0]);
     52     return 1;
     53   }
     54 
     55   return 0;
     56 }
     57 
     58 #ifdef __cplusplus
     59 } // extern "C"
     60 #endif