Annotation of libaitio/src/cli.c, revision 1.1.2.2
1.1.2.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.1.2.2 ! misho 6: * $Id: cli.c,v 1.1.2.1 2010/03/09 12:44:40 misho Exp $
1.1.2.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
1.1.2.2 ! misho 12: /*
! 13: * io_Cmd_Exit() Builtin helper function for Exit from Cli
! 14: * @cmds = Commands list
! 15: * @out = Output handle
! 16: * @args = Parsed arguments array
! 17: * return: 1 exit from Cli!
! 18: */
! 19: int io_Cmd_Exit(void *cmds, FILE *out, char ** __restrict args)
1.1.2.1 misho 20: {
21: return 1;
22: }
23:
1.1.2.2 ! misho 24: /*
! 25: * io_Cmd_Help() Builtin helper function for Help screen
! 26: * @cmds = Commands list
! 27: * @out = Output handle
! 28: * @args = Parsed arguments array
! 29: * return: -1 error, 0 = ok
! 30: */
! 31: int io_Cmd_Help(void *cmds, FILE *out, char ** __restrict args)
1.1.2.1 misho 32: {
33: register int i;
34: ioCommands_t *commands = cmds;
35:
36: if (!cmds)
37: return -1;
38:
39: if (!args) {
40: fprintf(out, "\n");
41: for (i = 0; commands[i].cmd_name; i++)
42: fprintf(out, "%s\t\t%s\n", commands[i].cmd_name, commands[i].cmd_doc);
43: } else {
44: if (!args[1])
45: fprintf(out, "Help screen::\n");
46: else
47: if (!strncmp(args[1], "---", 3))
48: return 0;
49:
50: for (i = 0; commands[i].cmd_name; i++) {
51: if (args[1] && strcmp(args[1], commands[i].cmd_name))
52: continue;
53:
54: fprintf(out, "%s%s\t\t%s\n", args[1] ? "Syntax::\n\t" : "", commands[i].cmd_name,
55: args[1] ? commands[i].cmd_help : commands[i].cmd_doc);
56: }
57: }
58:
59: return 0;
60: }
61:
1.1.2.2 ! misho 62: /*
! 63: * io_Cmd_Unsupported() Builtin helper function for unsupported commands
! 64: * @cmds = Commands list
! 65: * @out = Output handle
! 66: * @args = Parsed arguments array
! 67: * return: -1 error, 0 = ok, 1 exit from Cli!
! 68: */
! 69: int io_Cmd_Unsupported(void *cmds, FILE *out, char ** __restrict args)
1.1.2.1 misho 70: {
71: fprintf(out, "Command %s not supported in this version ...\n", args[0]);
72: return 0;
73: }
74:
75: // ------------------------------------------------------------
76:
77: #pragma GCC visibility push(hidden)
78:
79: ioCommands_t io_stdCmds[] = {
1.1.2.2 ! misho 80: { "test", io_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", NULL },
1.1.2.1 misho 81: { "-------", NULL, "---------------------", NULL, NULL },
1.1.2.2 ! misho 82: { "help", io_Cmd_Help, "Help screen", "help [command] <cr>", NULL },
! 83: { "exit", io_Cmd_Exit, "Exit from console", "exit <cr>", NULL },
1.1.2.1 misho 84: { NULL, NULL, NULL, NULL }
85: };
86:
87: #pragma GCC visibility pop
88:
89: // ------------------------------------------------------------
90:
91: /*
1.1.2.2 ! misho 92: * ioCLIComp() Initialize completion CLI features
1.1.2.1 misho 93: * @cmdComplete = Completion function
94: * @cmdEntry = Compentry function
95: * return: none
96: */
1.1.2.2 ! misho 97: inline void ioCLIComp(io_Completion_t *cmdComplete, io_CompEntry_t *cmdEntry)
1.1.2.1 misho 98: {
99: // command completon
100: rl_attempted_completion_function = cmdComplete;
101: rl_completion_entry_function = cmdEntry;
102: }
103:
104: /*
105: * ioCLIExec() Execute CLI main loop
106: * @cmdList = Commands list
107: * @out = Output handle
108: * @csPrompt = Prompt text
109: * return: -1 error, 0 = exit w/^+D, 1 done.
110: */
111: int ioCLIExec(ioCommands_t *cmdList, FILE *out, const char *csPrompt)
112: {
113: char *line, *s, *t, **app, *items[20];
114: int ret = 0;
115: register int i;
116: ioCommands_t *cmd = NULL;
117:
1.1.2.2 ! misho 118: inline int inline_help()
! 119: {
! 120: io_Cmd_Help(cmdList ? cmdList : io_stdCmds, out, NULL);
! 121: rl_on_new_line();
! 122: return 0;
! 123: }
! 124:
! 125: char **io_stdCompletion(const char *text, int start, int end)
! 126: {
! 127: register int i;
! 128: char **matches = NULL;
! 129:
! 130: char *cmdCompGet(const char *text, int state)
! 131: {
! 132: int len = strlen(text);
! 133:
! 134: for (i = state; cmdList[i].cmd_name; i++)
! 135: if (strncmp(cmdList[i].cmd_name, "---", 3) &&
! 136: !strncmp(cmdList[i].cmd_name, text, len))
! 137: return strdup(cmdList[i].cmd_name);
! 138:
! 139: return NULL;
! 140: }
! 141:
! 142: if (!start)
! 143: matches = rl_completion_matches(text, cmdCompGet);
! 144: else
! 145: for (i = 0; cmdList[i].cmd_name; i++)
! 146: if (cmdList[i].cmd_comp && !strncmp(rl_line_buffer,
! 147: cmdList[i].cmd_name, strlen(cmdList[i].cmd_name)))
! 148: matches = rl_completion_matches(text, cmdList[i].cmd_comp);
! 149:
! 150: return matches;
! 151: }
! 152: char *io_stdCompEntry(const char *ignore, int invoking_key)
! 153: {
! 154: return NULL;
! 155: }
! 156:
! 157: rl_bind_key('?', inline_help);
! 158: if (!rl_attempted_completion_function)
! 159: ioCLIComp(io_stdCompletion, io_stdCompEntry);
1.1.2.1 misho 160:
161: do {
162: line = readline(csPrompt);
163: if (!line) { // ^+d
164: fprintf(out, "\n");
165: fflush(out);
166: break;
167: }
168: // clear whitespaces
169: for (s = line; isspace(*s); s++);
170: if (*s) {
171: for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
172: *++t = 0;
173: }
174:
175: if (*s) {
176: add_history(s);
177:
178: memset(items, 0, sizeof(char*) * 20);
179: for (app = items; app < items + 19 && (*app = strsep(&s, " \t")); *app ? app++ : app);
180:
181: /*
182: for (i = 0; i < 20; i++)
183: printf("i=%d %s\n", i, items[i]);
184: */
185:
186: // exec_cmd ...
187: for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
188: if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
189: cmd = &cmdList[i];
190: break;
191: }
192: if (!cmd) {
193: fprintf(out, "Command '%s' not found!\n", items[0]);
194: ret = -1;
195: } else
196: ret = cmd->cmd_func(cmdList, out, items);
197: }
198:
199: free(line);
200: } while (ret < 1);
201:
202: return ret;
203: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>