From 628d53c6670d40d07d436c5a47b4a3f41f9b7730 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 23 Mar 2005 14:57:35 +0000 Subject: [PATCH] *** empty log message *** --- ase/test/lsp/lisp.c | 71 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/ase/test/lsp/lisp.c b/ase/test/lsp/lisp.c index 7a9b2063..47ea4574 100644 --- a/ase/test/lsp/lisp.c +++ b/ase/test/lsp/lisp.c @@ -1,6 +1,7 @@ #include #include #include +#include #ifdef LINUX #include @@ -35,10 +36,65 @@ int to_int (const xp_char_t* str) #include +int handle_cli_error ( + const xp_cli_t* cli, int code, + const xp_char_t* name, const xp_char_t* value) +{ + xp_printf (XP_TEXT("usage: %s /memory=nnn /increment=nnn\n"), cli->verb); + + if (code == XP_CLI_ERROR_INVALID_OPTNAME) { + xp_printf (XP_TEXT("unknown option - %s\n"), name); + } + else if (code == XP_CLI_ERROR_MISSING_OPTNAME) { + xp_printf (XP_TEXT("missing option - %s\n"), name); + } + else if (code == XP_CLI_ERROR_REDUNDANT_OPTVAL) { + xp_printf (XP_TEXT("redundant value %s for %s\n"), value, name); + } + else if (code == XP_CLI_ERROR_MISSING_OPTVAL) { + xp_printf (XP_TEXT("missing value for %s\n"), name); + } + else if (code == XP_CLI_ERROR_MEMORY) { + xp_printf (XP_TEXT("memory error in processing %s\n"), name); + } + else { + xp_printf (XP_TEXT("error code: %d\n"), code); + } + + return -1; +} +xp_cli_t* parse_cli (int argc, xp_char_t* argv[]) +{ + static const xp_char_t* optsta[] = + { + XP_TEXT("/"), XP_TEXT("--"), XP_NULL + }; + + static xp_cliopt_t opts[] = + { + { XP_TEXT("memory"), XP_CLI_OPTNAME | XP_CLI_OPTVAL }, + { XP_TEXT("increment"), XP_CLI_OPTNAME | XP_CLI_OPTVAL }, + { XP_NULL, 0 } + }; + + static xp_cli_t cli = + { + handle_cli_error, + optsta, + XP_TEXT("="), + opts + }; + + if (xp_parsecli (argc, argv, &cli) == -1) return XP_NULL; + return &cli; +} + int xp_main (int argc, xp_char_t* argv[]) { xp_lisp_t* lisp; xp_lisp_obj_t* obj; + xp_cli_t* cli; + int mem, inc; #ifdef LINUX mtrace (); @@ -46,14 +102,21 @@ int xp_main (int argc, xp_char_t* argv[]) setlocale (LC_ALL, ""); - if (argc != 3) { - xp_fprintf (xp_stderr, XP_TEXT("usage: %s mem_ubound mem_ubound_inc\n"), argv[0]); + if ((cli = parse_cli (argc, argv)) == XP_NULL) return -1; + mem = to_int(xp_getclioptval(cli, XP_TEXT("memory"))); + inc = to_int(xp_getclioptval(cli, XP_TEXT("increment"))); + xp_clearcli (cli); + + if (mem <= 0) { + xp_fprintf (xp_stderr, + XP_TEXT("error: invalid memory size given\n")); return -1; } - lisp = xp_lisp_new (to_int(argv[1]), to_int(argv[2])); + lisp = xp_lisp_new (mem, inc); if (lisp == NULL) { - xp_fprintf (xp_stderr, XP_TEXT("can't create a lisp instance\n")); + xp_fprintf (xp_stderr, + XP_TEXT("error: cannot create a lisp instance\n")); return -1; }