parser.c
来自「lustre 1.6.5 source code」· C语言 代码 · 共 643 行 · 第 1/2 页
C
643 行
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: * * Copyright (C) 2001 Cluster File Systems, Inc. * * This file is part of Lustre, http://www.sf.net/projects/lustre/ * * Lustre is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * Lustre is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Lustre; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>#include <stddef.h>#include <unistd.h>#include <sys/param.h>#include <assert.h>#include <lnet/api-support.h>#include "parser.h"static command_t * top_level; /* Top level of commands, initialized by * InitParser */static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */static int done; /* Set to 1 if user types exit or quit *//* static functions */static char *skipwhitespace(char *s);static char *skiptowhitespace(char *s);static command_t *find_cmd(char *name, command_t cmds[], char **next);static int process(char *s, char **next, command_t *lookup, command_t **result, char **prev);static void print_commands(char *str, command_t *table);static char * skipwhitespace(char * s){ char * t; int len; len = (int)strlen(s); for (t = s; t <= s + len && isspace(*t); t++); return(t);}static char * skiptowhitespace(char * s){ char * t; for (t = s; *t && !isspace(*t); t++); return(t);}static int line2args(char *line, char **argv, int maxargs){ char *arg; int i = 0; arg = strtok(line, " \t"); if ( arg ) { argv[i] = arg; i++; } else return 0; while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) { argv[i] = arg; i++; } return i;}/* find a command -- return it if unique otherwise print alternatives */static command_t *Parser_findargcmd(char *name, command_t cmds[]){ command_t *cmd; for (cmd = cmds; cmd->pc_name; cmd++) { if (strcmp(name, cmd->pc_name) == 0) return cmd; } return NULL;}int Parser_execarg(int argc, char **argv, command_t cmds[]){ command_t *cmd; cmd = Parser_findargcmd(argv[0], cmds); if ( cmd ) { int rc = (cmd->pc_func)(argc, argv); if (rc == CMD_HELP) fprintf(stderr, "%s\n", cmd->pc_help); return rc; } else { printf("Try interactive use without arguments or use one of:\n"); for (cmd = cmds; cmd->pc_name; cmd++) printf("\"%s\" ", cmd->pc_name); printf("\nas argument.\n"); } return -1;}/* returns the command_t * (NULL if not found) corresponding to a _partial_ match with the first token in name. It sets *next to point to the following token. Does not modify *name. */static command_t * find_cmd(char * name, command_t cmds[], char ** next){ int i, len; if (!cmds || !name ) return NULL; /* This sets name to point to the first non-white space character, and next to the first whitespace after name, len to the length: do this with strtok*/ name = skipwhitespace(name); *next = skiptowhitespace(name); len = *next - name; if (len == 0) return NULL; for (i = 0; cmds[i].pc_name; i++) { if (strncasecmp(name, cmds[i].pc_name, len) == 0) { *next = skipwhitespace(*next); return(&cmds[i]); } } return NULL;}/* Recursively process a command line string s and find the command corresponding to it. This can be ambiguous, full, incomplete, non-existent. */static int process(char *s, char ** next, command_t *lookup, command_t **result, char **prev){ *result = find_cmd(s, lookup, next); *prev = s; /* non existent */ if ( ! *result ) return CMD_NONE; /* found entry: is it ambigous, i.e. not exact command name and more than one command in the list matches. Note that find_cmd points to the first ambiguous entry */ if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) && find_cmd(s, (*result) + 1, next)) return CMD_AMBIG; /* found a unique command: component or full? */ if ( (*result)->pc_func ) { return CMD_COMPLETE; } else { if ( *next == '\0' ) { return CMD_INCOMPLETE; } else { return process(*next, next, (*result)->pc_sub_cmd, result, prev); } }}#ifdef HAVE_LIBREADLINEstatic command_t * match_tbl; /* Command completion against this table */static char * command_generator(const char * text, int state){ static int index, len; char *name; /* Do we have a match table? */ if (!match_tbl) return NULL; /* If this is the first time called on this word, state is 0 */ if (!state) { index = 0; len = (int)strlen(text); } /* Return next name in the command list that paritally matches test */ while ( (name = (match_tbl + index)->pc_name) ) { index++; if (strncasecmp(name, text, len) == 0) { return(strdup(name)); } } /* No more matches */ return NULL;}/* probably called by readline */static char **command_completion(char * text, int start, int end){ command_t * table; char * pos; match_tbl = top_level; for (table = find_cmd(rl_line_buffer, match_tbl, &pos); table; table = find_cmd(pos, match_tbl, &pos)) { if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd; } return completion_matches(text, command_generator);}#endif/* take a string and execute the function or print help */int execute_line(char * line){ command_t *cmd, *ambig; char *prev; char *next, *tmp; char *argv[MAXARGS]; int i; int rc = 0; switch( process(line, &next, top_level, &cmd, &prev) ) { case CMD_AMBIG: fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line); while( (ambig = find_cmd(prev, cmd, &tmp)) ) { fprintf(stderr, "%s ", ambig->pc_name); cmd = ambig + 1; } fprintf(stderr, "\n"); break; case CMD_NONE: fprintf(stderr, "No such command, type help\n"); break; case CMD_INCOMPLETE: fprintf(stderr, "'%s' incomplete command. Use '%s x' where x is one of:\n", line, line); fprintf(stderr, "\t"); for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) { fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name); } fprintf(stderr, "\n"); break; case CMD_COMPLETE: i = line2args(line, argv, MAXARGS); rc = (cmd->pc_func)(i, argv); if (rc == CMD_HELP) fprintf(stderr, "%s\n", cmd->pc_help); break; } return rc;}intnoop_fn (){ return (0);}/* just in case you're ever in an airplane and discover you forgot to install readline-dev. :) */int init_input() { int interactive = isatty (fileno (stdin));#ifdef HAVE_LIBREADLINE using_history(); stifle_history(HISTORY); if (!interactive) { rl_prep_term_function = (rl_vintfunc_t *)noop_fn; rl_deprep_term_function = (rl_voidfunc_t *)noop_fn; } rl_attempted_completion_function = (CPPFunction *)command_completion; rl_completion_entry_function = (void *)command_generator;#endif return interactive;}#ifndef HAVE_LIBREADLINE#define add_history(s)char * readline(char * prompt) { char line[2048]; int n = 0; if (prompt) printf ("%s", prompt); if (fgets(line, sizeof(line), stdin) == NULL) return (NULL); n = strlen(line); if (n && line[n-1] == '\n') line[n-1] = '\0'; return strdup(line);}#endif/* this is the command execution machine */int Parser_commands(void){ char *line, *s; int rc = 0; int interactive;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?