Advertisement
Jack2

Untitled

Oct 11th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int, char **);
  6.  
  7. static int toolbox_main(int argc, char **argv)
  8. {
  9.     // "toolbox foo ..." is equivalent to "foo ..."
  10.     if (argc > 1) {
  11.         return main(argc - 1, argv + 1);
  12.     } else {
  13.         printf("Toolbox!\n");
  14.         return 0;
  15.     }
  16. }
  17.  
  18. #define TOOL(name) int name##_main(int, char**);
  19. #include "tools.h"
  20. #undef TOOL
  21.  
  22. static struct
  23. {
  24.     const char *name;
  25.     int (*func)(int, char**);
  26. } tools[] = {
  27.     { "toolbox", toolbox_main },
  28. #define TOOL(name) { #name, name##_main },
  29. #include "tools.h"
  30. #undef TOOL
  31.     { 0, 0 },
  32. };
  33.  
  34. int main(int argc, char **argv)
  35. {
  36.     int i;
  37.     char *name = argv[0];
  38.  
  39.     if((argc > 1) && (argv[1][0] == '@')) {
  40.         name = argv[1] + 1;
  41.         argc--;
  42.         argv++;
  43.     } else {
  44.         char *cmd = strrchr(argv[0], '/');
  45.         if (cmd)
  46.             name = cmd + 1;
  47.     }
  48.  
  49.     for(i = 0; tools[i].name; i++){
  50.         if(!strcmp(tools[i].name, name)){
  51.             return tools[i].func(argc, argv);
  52.         }
  53.     }
  54.  
  55.     printf("%s: no such tool\n", argv[0]);
  56.     return -1;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement