1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: aitcli.h,v 1.1.1.1.2.5 2010/04/20 12:09:48 misho Exp $
7: *
8: *************************************************************************/
9: #ifndef __AITCLI_H
10: #define __AITCLI_H
11:
12:
13: struct tagCLICmd {
14: const char *cmd_name;
15: int (*cmd_func)(void *, int, FILE *, char **);
16: const char *cmd_doc;
17: const char *cmd_help;
18: char *(*cmd_comp)(const char *, int);
19: };
20:
21: typedef struct tagCLICmd cliCommands_t;
22: typedef char *cli_CompEntry_t(const char *, int);
23: typedef char **cli_Completion_t(const char *, int, int);
24:
25:
26: // cli_GetErrno() Get error code of last operation
27: inline int cli_GetErrno();
28: // cli_GetError() Get error text of last operation
29: inline const char *cli_GetError();
30:
31: /*
32: * cli_Printf() Printf CLI features
33: * @out = Output stream
34: * @csFormat = Printf format string
35: * return: -1 error, != -1 printed chars
36: */
37: inline int cli_Printf(FILE *out, const char *csFormat, ...);
38:
39: /*
40: * cliInit() Initialize Readline
41: * @csProg = program name
42: * return: none
43: */
44: inline void cliInit(const char *csProg);
45: /*
46: * cliNetInit() Initialize Readline if CLI bind to socket
47: * @csProg = program name
48: * @pty = Master pty
49: * @term = stdin termios
50: * return: none
51: */
52: void cliNetInit(const char *csProg, int pty, struct termios *term);
53: /*
54: * cliTTY() Initialize I/O TTY CLI features
55: * @term = terminal name
56: * @inp = input handle
57: * @out = output handle
58: * @win = window size
59: * return: -1 error, != -1 ok
60: */
61: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win);
62: /*
63: * cliComp() Initialize completion CLI features
64: * @cmdComplete = Completion function
65: * @cmdEntry = Compentry function
66: * return: none
67: */
68: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry);
69: /*
70: * cliExec() Execute CLI main loop
71: * @cmdList = Commands list
72: * @csPrompt = Prompt text
73: * return: -1 error, 0 = exit w/^+D, 1 done.
74: */
75: int cliExec(cliCommands_t *cmdList, const char *csPrompt);
76:
77: /*
78: * cli_ReadHistory() Read CLI History from file
79: * @csFile = history file name, if NULL default history name is ".aitcli.history"
80: * return: -1 error; != -1 readed ok
81: */
82: inline int cli_ReadHistory(const char *csFile);
83: /*
84: * cli_WriteHistory() Write CLI History to file
85: * @csFile = history file name, if NULL default history name is ".aitcli.history"
86: * return: -1 error; != -1 readed ok
87: */
88: inline int cli_WriteHistory(const char *csFile);
89:
90:
91: /*
92: * cli_PrintHelp() Helper print for missing command arguments
93: * @out = Output stream
94: * @cmds = Commands list
95: * @idx = Selected command ID
96: * return: -1 error, !=-1 ok
97: * return: none
98: */
99: inline int cli_PrintHelp(FILE *out, void *cmds, int idx);
100:
101: /*
102: * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
103: * @cmds = Commands list
104: * @idx = Selected command ID
105: * @out = Output handle
106: * @args = Parsed arguments array
107: * return: -1 error, 0 = ok, 1 exit from Cli!
108: */
109: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
110: /*
111: * cli_Cmd_Help() Builtin helper function for Help screen
112: * @cmds = Commands list
113: * @idx = Selected command ID
114: * @out = Output handle
115: * @args = Parsed arguments array
116: * return: -1 error, 0 = ok
117: */
118: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
119: /*
120: * cli_Cmd_Exit() Builtin helper function for Exit from Cli
121: * @cmds = Commands list
122: * @idx = Selected command ID
123: * @out = Output handle
124: * @args = Parsed arguments array
125: * return: 1 exit from Cli!
126: */
127: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
128:
129:
130: /*
131: * cli_Register_Commands - Declare helper function for register and export Commands variable
132: */
133: #define CLI_REGISTER_COMMANDS(CMDS) \
134: extern cliCommands_t CMDS[];
135: /*
136: * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
137: */
138: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS) \
139: char *FUNC(const char *text, int state) \
140: { \
141: register int i; \
142: int len = strlen(text); \
143: for (i = state; CMDS[i].cmd_name; i++) { \
144: if (strncmp(CMDS[i].cmd_name, "---", 3) && \
145: !strncmp(CMDS[i].cmd_name, text, len)) \
146: return strdup(CMDS[i].cmd_name); \
147: } \
148: return NULL; \
149: }
150:
151: /*
152: * cli_Make_Comp_Args - Declare helper function for Arguments completion
153: */
154: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
155: char *FUNC(const char *text __attribute__((unused)), int state) \
156: { \
157: while (ARGS[state]) \
158: return strdup(ARGS[state]); \
159: return NULL; \
160: }
161:
162: /*
163: * cli_Comp_Filename() Builtin helper function for filename completion arguments
164: * @text = Text line
165: * @state = Position state
166: * return: NULL not found filename, != NULL filename
167: */
168: char *cli_Comp_Filename(const char *text, int state);
169:
170:
171: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>