📄 std_cmdl.c
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company */
/* All rights reserved */
/* File: "std_cmdl.c" */
/* Description: Standard implementation of the command-line processing */
/* object, `cmdl'. */
/* Author: David Taubman */
/* Affiliation: Hewlett-Packard and */
/* The University of New South Wales, Australia */
/* Version: VM6.0 */
/* Last Revised: 19 January, 2000 */
/*****************************************************************************/
#include <local_services.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <ifc.h>
#include "std_cmdl_local.h"
/*****************************************************************************/
/* STATIC __initialize */
/*****************************************************************************/
static void
__initialize(cmdl_ref base, int argc, char *argv[])
{
std_cmdl_ref self = (std_cmdl_ref) base;
cmd_list_ptr elt;
char *arg, *cp;
assert(argc >= 1);
self->prog_name = argv[0];
argc--; argv++;
while (argc > 0)
{
arg = *argv;
if ((arg[0] != '-') || !isalpha(arg[1]))
local_error("Illegal command-line argument \"%s\"! Arguments must "
"commence with the `-' character, followed by an "
"alphabetical character!",arg);
elt = (cmd_list_ptr) local_malloc(CMDL_MEM_KEY,sizeof(cmd_list));
elt->next = self->list;
self->list = elt;
elt->arg = arg;
elt->arg_length = strlen(arg);
elt->tnum = -1;
if ((cp=strchr(arg,'+')) != NULL)
{
int j, tnum;
char num_buf[24];
elt->arg_length = cp - arg;
for (j=0, cp++; *cp != '\0'; j++, cp++)
{
num_buf[j] = *cp;
if (j >= 23)
local_error("Argument has an invalid or excessively "
"long numeric tile-index suffix, \"%s\"!",
arg);
}
num_buf[j] = '\0';
if ((j == 0) ||
(sscanf(num_buf,"%d",&tnum) == 0) || (tnum < 0))
local_error("Malformed tile-specifier, \"+<tnum>\", encountered "
"while parsing command-line argument, \"%s\"!",arg);
elt->tnum = tnum;
}
argc--; argv++;
elt->params = argv;
elt->num_params = 0;
while ((argc > 0) && (((*argv)[0] != '-') || !isalpha((*argv)[1])))
{
elt->num_params++;
argc--; argv++;
}
}
}
/*****************************************************************************/
/* STATIC __extract */
/*****************************************************************************/
static int
__extract(cmdl_ref base, char *arg, int tnum, char ***params)
{
std_cmdl_ref self = (std_cmdl_ref) base;
cmd_list_ptr scan, global_match, tile_match, match;
int len;
global_match = tile_match = NULL;
len = strlen(arg);
for (scan=self->list; scan != NULL; scan = scan->next)
if ((len == scan->arg_length) &&
(strncmp(arg,scan->arg,(size_t) len) == 0))
{ /* Found a potential match. */
if (scan->tnum < 0)
{
global_match = scan;
if (tnum < 0)
break;
}
else
{
if (scan->tnum == tnum)
tile_match = scan;
}
}
if (global_match != NULL)
global_match->recognized = 1;
if (tile_match != NULL)
{
tile_match->recognized = 1;
match = tile_match;
}
else
match = global_match;
if (match == NULL)
return(-1);
if (params != NULL)
*params = match->params;
return(match->num_params);
}
/*****************************************************************************/
/* STATIC __extract_next */
/*****************************************************************************/
static int
__extract_next(cmdl_ref base, char *arg,
int min_tnum, int *actual_tnum, char ***params)
{
std_cmdl_ref self = (std_cmdl_ref) base;
cmd_list_ptr scan, match;
int len;
assert(min_tnum >= 0);
len = strlen(arg);
match = NULL;
for (scan=self->list; scan != NULL; scan = scan->next)
if ((len == scan->arg_length) &&
(strncmp(arg,scan->arg,(size_t) len) == 0))
{ /* Found a potential match. */
if (scan->tnum < min_tnum)
continue;
if ((match == NULL) || (match->tnum > scan->tnum))
match = scan;
}
if (match == NULL)
return(-1);
match->recognized = 1;
if (actual_tnum != NULL)
*actual_tnum = match->tnum;
if (params != NULL)
*params = match->params;
return(match->num_params);
}
/*****************************************************************************/
/* STATIC __have_tile_specific_args */
/*****************************************************************************/
static int
__have_tile_specific_args(cmdl_ref base, int tnum)
{
std_cmdl_ref self = (std_cmdl_ref) base;
cmd_list_ptr elt;
assert(tnum >= 0);
for (elt=self->list; elt != NULL; elt = elt->next)
if (elt->tnum == tnum)
return(1);
return(0);
}
/*****************************************************************************/
/* STATIC __terminate */
/*****************************************************************************/
static void
__terminate(cmdl_ref base, int check_args)
{
std_cmdl_ref self = (std_cmdl_ref) base;
cmd_list_ptr elt;
if (check_args)
{
for (elt=self->list; elt != NULL; elt = elt->next)
if (!elt->recognized)
break;
if (elt != NULL)
{
local_printf(0,70,"Error encountered while parsing command line "
"arguments! The following arguments and any "
"associated parameters were not recognized:\n");
for (; elt != NULL; elt = elt->next)
if (!elt->recognized)
local_printf(6,70,"\"%s\"\n",elt->arg);
local_exit(-1);
}
}
while ((elt=self->list) != NULL)
{
self->list = elt->next;
local_free(elt);
}
local_free(self);
}
/*****************************************************************************/
/* EXTERN create_std_cmdl */
/*****************************************************************************/
cmdl_ref
create_std_cmdl(void)
{
std_cmdl_ref result;
result = (std_cmdl_ref)
local_malloc(CMDL_MEM_KEY,sizeof(std_cmdl_obj));
result->base.initialize = __initialize;
result->base.extract = __extract;
result->base.extract_next = __extract_next;
result->base.have_tile_specific_args = __have_tile_specific_args;
result->base.terminate = __terminate;
return((cmdl_ref) result);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -