[cig-commits] r6893 - cs/cigma/trunk/tools
luis at geodynamics.org
luis at geodynamics.org
Wed May 16 10:16:06 PDT 2007
Author: luis
Date: 2007-05-16 10:16:06 -0700 (Wed, 16 May 2007)
New Revision: 6893
Modified:
cs/cigma/trunk/tools/cigma.c
Log:
Improved command dispatcher to use a table of function pointers
instead of relying on execv.
Modified: cs/cigma/trunk/tools/cigma.c
===================================================================
--- cs/cigma/trunk/tools/cigma.c 2007-05-16 17:12:39 UTC (rev 6892)
+++ cs/cigma/trunk/tools/cigma.c 2007-05-16 17:16:06 UTC (rev 6893)
@@ -3,45 +3,92 @@
#include <string.h>
#include <unistd.h>
-void usage(FILE *fp, const char *pgm)
+
+
+/* function prototypes for each command */
+int init_main(int, char **);
+int cube_main(int, char **);
+int skel_main(int, char **);
+int extract_main(int, char **);
+
+
+
+// ----------------------------------------------------------------------------
+
+
+typedef int (*cmd_t)(int, char **);
+
+
+struct table_t {
+ char *name;
+ cmd_t main;
+ int active;
+ char *description;
+} commands[] = {
+ {"skel", skel_main, 0, 0},
+ {"init", init_main, 1, "initialize model file"},
+ {"cube", cube_main, 1, "discretize unit cube"},
+ {"extract", extract_main, 1, "extract quadrature points"},
+ {0,0}
+};
+
+
+
+// ----------------------------------------------------------------------------
+
+
+void cigma_usage(FILE *fp, int verbose)
{
- fprintf(fp, "Usage: %s command\n\n", pgm);
- fprintf(fp, "Commands: \n"
- " help \n"
- " init\n"
- " extract\n"
- " compare\n"
- " project\n"
- );
-}
+ fprintf(fp, "Usage: cigma <subcommand> [options] [args]\n");
-int match(char *arg, const char *command)
-{
- return (strcmp(command, arg) == 0);
+ if (verbose)
+ {
+ int i;
+ printf("\n");
+ for (i = 0; commands[i].name; i++)
+ {
+ if (commands[i].active)
+ {
+ fprintf(fp, " %-15s %s\n",
+ commands[i].name,
+ commands[i].description);
+ }
+ }
+ }
+
+ return;
}
+
int main(int argc, char *argv[])
{
- int status;
+ int status = 0;
if (argc == 1)
{
- usage(stderr, argv[0]);
- return EXIT_FAILURE;
+ cigma_usage(stdout,1);
}
-
- if (match(argv[1], "help"))
+ else
{
- usage(stdout, argv[0]);
- return EXIT_SUCCESS;
- }
+ int i;
+ int found = 0;
- status = execv(argv[1], &argv[1]);
+ /* Simplest dispatcher */
+ for (i = 0; commands[i].name; i++)
+ {
+ if (strcmp(commands[i].name, argv[1]) == 0)
+ {
+ found = 1;
+ status = commands[i].main(argc-1, &argv[1]);
+ break;
+ }
+ }
- if (status < 0)
- {
- fprintf(stderr, "Command not found: %s\n", argv[1]);
-
+ if (!found)
+ {
+ printf("Unkown command: %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
}
return status;
More information about the cig-commits
mailing list