From 16584701b5d96b5ef79d06a505816c92d2c50a8d Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 16 Jul 2001 10:32:12 +0000 Subject: [PATCH] More work; command parsing via readline. --- test/test_physfs.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/test/test_physfs.c b/test/test_physfs.c index a55fdc57..4bb346d2 100644 --- a/test/test_physfs.c +++ b/test/test_physfs.c @@ -1,11 +1,14 @@ #include +#include +#include +#include #include "physfs.h" #define TEST_VERSION_MAJOR 0 #define TEST_VERSION_MINOR 1 #define TEST_VERSION_PATCH 0 -void output_versions(void) +static void output_versions(void) { PHYSFS_Version compiled; PHYSFS_Version linked; @@ -22,7 +25,7 @@ void output_versions(void) } /* output_versions */ -void output_archivers(void) +static void output_archivers(void) { const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes(); const PHYSFS_ArchiveInfo **i; @@ -39,11 +42,85 @@ void output_archivers(void) (*i)->author, (*i)->url); } /* for */ } /* else */ + + printf("\n"); } /* output_archivers */ +static int cmd_help(char *cmdstr) +{ + printf("Commands:\n" + " quit - exit this program.\n" + " help - this information.\n"); + return(1); +} /* output_cmd_help */ + + +static int cmd_quit(char *cmdstr) +{ + return(0); +} /* cmd_quit */ + + +typedef struct +{ + const char *cmd; + int (*func)(char *cmdstr); +} command_info; + +static command_info commands[] = +{ + {"quit", cmd_quit}, + {"q", cmd_quit}, + {"help", cmd_help}, + {NULL, NULL} +}; + + +static int process_command(char *complete_cmd) +{ + command_info *i; + char *ptr = strchr(complete_cmd, ' '); + char *cmd = NULL; + int rc = 1; + + if (ptr == NULL) + { + cmd = malloc(strlen(complete_cmd) + 1); + strcpy(cmd, complete_cmd); + } /* if */ + else + { + *ptr = '\0'; + cmd = malloc(strlen(complete_cmd) + 1); + strcpy(cmd, complete_cmd); + *ptr = ' '; + } /* else */ + + for (i = commands; i->cmd != NULL; i++) + { + if (strcmp(i->cmd, cmd) == 0) + { + rc = i->func(complete_cmd); + break; + } /* if */ + } /* for */ + + if (i->cmd == NULL) + printf("Unknown command. Enter \"help\" for instructions.\n"); + + free(cmd); + return(rc); +} /* process_command */ + + int main(int argc, char **argv) { + char *buf = NULL; + int rc = 0; + + printf("\n"); + if (!PHYSFS_init(argv[0])) { printf("PHYSFS_init() failed!\n reason: %s\n", PHYSFS_getLastError()); @@ -53,6 +130,15 @@ int main(int argc, char **argv) output_versions(); output_archivers(); + printf("Enter commands. Enter \"help\" for instructions.\n"); + + do + { + buf = readline("> "); + rc = process_command(buf); + free(buf); + } while (rc); + return(0); } /* main */