Annotation of libaitcli/inc/aitcli.h, revision 1.1.1.1.2.7
1.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.1.1.2.7! misho 6: * $Id: aitcli.h,v 1.1.1.1.2.6 2010/04/20 12:16:52 misho Exp $
1.1 misho 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
1.1.1.1.2.4 misho 35: * return: -1 error, != -1 printed chars
1.1 misho 36: */
37: inline int cli_Printf(FILE *out, const char *csFormat, ...);
38:
39: /*
1.1.1.1.2.4 misho 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: /*
1.1.1.1.2.1 misho 54: * cliTTY() Initialize I/O TTY CLI features
1.1.1.1.2.3 misho 55: * @term = terminal name
1.1.1.1.2.1 misho 56: * @inp = input handle
57: * @out = output handle
1.1.1.1.2.4 misho 58: * @win = window size
59: * return: -1 error, != -1 ok
1.1.1.1.2.1 misho 60: */
1.1.1.1.2.4 misho 61: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win);
1.1.1.1.2.1 misho 62: /*
1.1 misho 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: */
1.1.1.1.2.2 misho 75: int cliExec(cliCommands_t *cmdList, const char *csPrompt);
1.1.1.1.2.7! misho 76: /*
! 77: * cliNetExec() Execute net CLI main loop
! 78: * @cmdList = Commands list
! 79: * @csPrompt = Prompt text
! 80: * @sock = client socket
! 81: * @term = stdin termios
! 82: * @win = window size of tty
! 83: * return: -1 error, 0 = exit w/^+D, 1 done.
! 84: */
! 85: int cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win);
1.1 misho 86:
1.1.1.1.2.5 misho 87: /*
88: * cli_ReadHistory() Read CLI History from file
89: * @csFile = history file name, if NULL default history name is ".aitcli.history"
90: * return: -1 error; != -1 readed ok
91: */
92: inline int cli_ReadHistory(const char *csFile);
93: /*
94: * cli_WriteHistory() Write CLI History to file
95: * @csFile = history file name, if NULL default history name is ".aitcli.history"
1.1.1.1.2.6 misho 96: * @lineNum = save number of history entry lines, if -1 all lines saved without limit
1.1.1.1.2.5 misho 97: * return: -1 error; != -1 readed ok
98: */
1.1.1.1.2.6 misho 99: inline int cli_WriteHistory(const char *csFile, int lineNum);
1.1.1.1.2.5 misho 100:
1.1 misho 101:
102: /*
103: * cli_PrintHelp() Helper print for missing command arguments
104: * @out = Output stream
105: * @cmds = Commands list
106: * @idx = Selected command ID
107: * return: -1 error, !=-1 ok
108: * return: none
109: */
110: inline int cli_PrintHelp(FILE *out, void *cmds, int idx);
111:
112: /*
113: * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
114: * @cmds = Commands list
115: * @idx = Selected command ID
116: * @out = Output handle
117: * @args = Parsed arguments array
118: * return: -1 error, 0 = ok, 1 exit from Cli!
119: */
120: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
121: /*
122: * cli_Cmd_Help() Builtin helper function for Help screen
123: * @cmds = Commands list
124: * @idx = Selected command ID
125: * @out = Output handle
126: * @args = Parsed arguments array
127: * return: -1 error, 0 = ok
128: */
129: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
130: /*
131: * cli_Cmd_Exit() Builtin helper function for Exit from Cli
132: * @cmds = Commands list
133: * @idx = Selected command ID
134: * @out = Output handle
135: * @args = Parsed arguments array
136: * return: 1 exit from Cli!
137: */
138: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
139:
140:
141: /*
142: * cli_Register_Commands - Declare helper function for register and export Commands variable
143: */
144: #define CLI_REGISTER_COMMANDS(CMDS) \
145: extern cliCommands_t CMDS[];
146: /*
147: * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
148: */
149: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS) \
150: char *FUNC(const char *text, int state) \
151: { \
152: register int i; \
153: int len = strlen(text); \
154: for (i = state; CMDS[i].cmd_name; i++) { \
155: if (strncmp(CMDS[i].cmd_name, "---", 3) && \
156: !strncmp(CMDS[i].cmd_name, text, len)) \
157: return strdup(CMDS[i].cmd_name); \
158: } \
159: return NULL; \
160: }
161:
162: /*
163: * cli_Make_Comp_Args - Declare helper function for Arguments completion
164: */
165: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
166: char *FUNC(const char *text __attribute__((unused)), int state) \
167: { \
168: while (ARGS[state]) \
169: return strdup(ARGS[state]); \
170: return NULL; \
171: }
172:
173: /*
174: * cli_Comp_Filename() Builtin helper function for filename completion arguments
175: * @text = Text line
176: * @state = Position state
177: * return: NULL not found filename, != NULL filename
178: */
179: char *cli_Comp_Filename(const char *text, int state);
180:
181:
182: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>