Annotation of libaitcli/inc/aitcli.h, revision 1.2.2.2
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.2.2.2 ! misho 6: * $Id: aitcli.h,v 1.2.2.1 2010/06/04 12:08:32 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #ifndef __AITCLI_H
10: #define __AITCLI_H
11:
12:
1.2.2.2 ! misho 13: #define STRSIZ 256
! 14:
1.2.2.1 misho 15: /* Key definitions */
16:
17: #define K_F1 "\x1b\x4f\x50"
18: #define K_F2 "\x1b\x4f\x51"
19: #define K_F3 "\x1b\x4f\x52"
20: #define K_F4 "\x1b\x4f\x53"
21: #define K_F5 "\x1b\x5b\x31\x35\x7e"
22: #define K_F6 "\x1b\x5b\x31\x37\x7e"
23: #define K_F7 "\x1b\x5b\x31\x38\x7e"
24: #define K_F8 "\x1b\x5b\x31\x39\x7e"
25: #define K_F9 "\x1b\x5b\x32\x30\x7e"
26: #define K_F10 "\x1b\x5b\x32\x31\x7e"
27: #define K_F11 "\x1b\x5b\x32\x33\x7e"
28: #define K_F12 "\x1b\x5b\x32\x34\x7e"
29: #define K_CTRL_F1 "\x1b\x5b\x6b"
30: #define K_CTRL_F2 "\x1b\x5b\x6c"
31: #define K_CTRL_F3 "\x1b\x5b\x6d"
32: #define K_CTRL_F4 "\x1b\x5b\x6e"
33: #define K_CTRL_F5 "\x1b\x5b\x6f"
34: #define K_CTRL_F6 "\x1b\x5b\x70"
35: #define K_CTRL_F7 "\x1b\x5b\x71"
36: #define K_CTRL_F8 "\x1b\x5b\x72"
37: #define K_CTRL_F9 "\x1b\x5b\x73"
38: #define K_CTRL_F10 "\x1b\x5b\x74"
39: #define K_CTRL_F11 "\x1b\x5b\x75"
40: #define K_CTRL_F12 "\x1b\x5b\x76"
41: #define K_CTRL_SH_F1 "\x1b\x5b\x77"
42: #define K_CTRL_SH_F2 "\x1b\x5b\x78"
43: #define K_CTRL_SH_F3 "\x1b\x5b\x79"
44: #define K_CTRL_SH_F4 "\x1b\x5b\x7a"
45: #define K_CTRL_SH_F5 "\x1b\x5b\x40"
46: #define K_CTRL_SH_F6 "\x1b\x5b\x5b"
47: #define K_CTRL_SH_F7 "\x1b\x5b\x5c"
48: #define K_CTRL_SH_F8 "\x1b\x5b\x5d"
49: #define K_CTRL_SH_F9 "\x1b\x5b\x5e"
50: #define K_CTRL_SH_F10 "\x1b\x5b\x5f"
51: #define K_CTRL_SH_F11 "\x1b\x5b\x60"
52: #define K_CTRL_SH_F12 "\x1b\x5b\x7b"
53:
54: #define K_INS "\x1b\x5b\x32\x7e"
55: #define K_DEL "\x1b\x5b\x33\x7e"
56: #define K_PGUP "\x1b\x5b\x35\x7e"
57: #define K_PGDN "\x1b\x5b\x36\x7e"
58: #define K_HOME "\x1b\x5b\x48"
59: #define K_END "\x1b\x5b\x46"
60: #define K_UP "\x1b\x5b\x41"
61: #define K_DOWN "\x1b\x5b\x42"
62: #define K_RIGHT "\x1b\x5b\x43"
63: #define K_LEFT "\x1b\x5b\x44"
64:
65: #define K_NULL "\x0"
66: #define K_CR "\xd"
67: #define K_BTAB "\x1b\x5b\x5a"
68: #define K_TAB "\x9"
69: #define K_ENTER "\xa"
70: #define K_ESC "\x1b"
71: #define K_BACKSPACE "\x7f"
72: #define K_SPACE "\x20"
73:
74: #define K_CTRL_SPACE K_NULL
75: #define K_CTRL_2 K_NULL
76: #define K_CTRL_A "\x1"
77: #define K_CTRL_B "\x2"
78: #define K_CTRL_C "\x3"
79: #define K_CTRL_D "\x4"
80: #define K_CTRL_E "\x5"
81: #define K_CTRL_F "\x6"
82: #define K_CTRL_G "\x7"
83: #define K_CTRL_H "\x8"
84: #define K_CTRL_I K_TAB
85: #define K_CTRL_J K_ENTER
86: #define K_CTRL_K "\xb"
87: #define K_CTRL_L "\xc"
88: #define K_CTRL_M K_ENTER // K_CR
89: #define K_CTRL_N "\xe"
90: #define K_CTRL_O "\xf"
91: #define K_CTRL_P "\x10"
92: #define K_CTRL_Q "\x11"
93: #define K_CTRL_R "\x12"
94: #define K_CTRL_S "\x13"
95: #define K_CTRL_T "\x14"
96: #define K_CTRL_U "\x15"
97: #define K_CTRL_V "\x16"
98: #define K_CTRL_W "\x17"
99: #define K_CTRL_X "\x18"
100: #define K_CTRL_Y "\x19"
101: #define K_CTRL_Z "\x1a"
102: #define K_CTRL_LBRACE "\x1b"
103: #define K_CTRL_PIPE "\x1c"
104: #define K_CTRL_RBRACE "\x1d"
105: #define K_CTRL_6 "\x1e"
106: #define K_CTRL__ "\x1f"
107:
108:
109: #define K_X_CTRL_INS "\x1b\x5b\x32\x3b\x35\x7e"
110: #define K_X_CTRL_DEL "\x1b\x5b\x33\x3b\x35\x7e"
111: #define K_X_CTRL_PGUP "\x1b\x5b\x35\x3b\x35\x7e"
112: #define K_X_CTRL_PGDN "\x1b\x5b\x36\x3b\x35\x7e"
113: #define K_X_CTRL_HOME "\x1b\x5b\x31\x3b\x35\x48"
114: #define K_X_CTRL_END "\x1b\x5b\x31\x3b\x35\x46"
115: #define K_X_CTRL_UP "\x1b\x5b\x31\x3b\x35\x41"
116: #define K_X_CTRL_DOWN "\x1b\x5b\x31\x3b\x35\x42"
117: #define K_X_CTRL_RIGHT "\x1b\x5b\x31\x3b\x35\x43"
118: #define K_X_CTRL_LEFT "\x1b\x5b\x31\x3b\x35\x44"
119:
120: #define K_X_ALT_INS "\x1b\x5b\x32\x3b\x33\x7e"
121: #define K_X_ALT_DEL "\x1b\x5b\x33\x3b\x33\x7e"
122: #define K_X_ALT_PGUP "\x1b\x5b\x35\x3b\x33\x7e"
123: #define K_X_ALT_PGDN "\x1b\x5b\x36\x3b\x33\x7e"
124: #define K_X_ALT_HOME "\x1b\x5b\x31\x3b\x33\x48"
125: #define K_X_ALT_END "\x1b\x5b\x31\x3b\x33\x46"
126: #define K_X_ALT_UP "\x1b\x5b\x31\x3b\x33\x41"
127: #define K_X_ALT_DOWN "\x1b\x5b\x31\x3b\x33\x42"
128: #define K_X_ALT_RIGHT "\x1b\x5b\x31\x3b\x33\x43"
129: #define K_X_ALT_LEFT "\x1b\x5b\x31\x3b\x33\x44"
130:
131: #define K_X_CTL_A_INS "\x1b\x5b\x32\x3b\x37\x7e"
132: #define K_X_CTL_A_DEL "\x1b\x5b\x33\x3b\x37\x7e"
133: #define K_X_CTL_A_PGUP "\x1b\x5b\x35\x3b\x37\x7e"
134: #define K_X_CTL_A_PGDN "\x1b\x5b\x36\x3b\x37\x7e"
135: #define K_X_CTL_A_HOME "\x1b\x5b\x31\x3b\x37\x48"
136: #define K_X_CTL_A_END "\x1b\x5b\x31\x3b\x37\x46"
137: #define K_X_CTL_A_UP "\x1b\x5b\x31\x3b\x37\x41"
138: #define K_X_CTL_A_DOWN "\x1b\x5b\x31\x3b\x37\x42"
139: #define K_X_CTL_A_RIGHT "\x1b\x5b\x31\x3b\x37\x43"
140: #define K_X_CTL_A_LEFT "\x1b\x5b\x31\x3b\x37\x44"
141:
142:
143: /* History types */
144:
145: struct tagHistory {
146: int hist_len;
147: char hist_line[BUFSIZ];
148:
149: TAILQ_ENTRY(tagHistory) hist_next;
1.1 misho 150: };
1.2.2.1 misho 151: typedef TAILQ_HEAD(tqHistoryHead, tagHistory) history_t;
152:
153: /* Bind keys structure types */
154:
155: typedef int (*bindkey_func_t)(int idx, /*linebuffer_t **/ void * __restrict buffer);
156: typedef struct {
157: int key_len;
158: unsigned char key_ch[8];
159: bindkey_func_t key_func;
160: } bindkey_t;
161:
162:
163: /* Main structure, Buffer for CLI work with thread models ;-) special designed by M.Punov */
164:
165: typedef struct {
166: char line_mode;
167:
168: char *line_prompt;
169:
170: int line_bol;
171: int line_eol;
172: int line_len;
173: char *line_buf;
174:
175: const struct tagHistory *line_h;
176: history_t line_history;
1.1 misho 177:
1.2.2.1 misho 178: bindkey_t *line_keys;
1.1 misho 179:
1.2.2.1 misho 180: int line_in;
181: int line_out;
182: } linebuffer_t;
183:
184: /* Commands structure for CLI */
185:
186: typedef int (*cmd_func_t)(linebuffer_t * __restrict buffer, int argc, char ** __restrict argv);
187: typedef struct {
188: int cmd_level;
189:
190: int cmd_min;
191: int cmd_len;
192: char cmd_name[STRSIZ];
193:
194: char cmd_info[STRSIZ];
195: char cmd_help[STRSIZ];
196:
197: cmd_func_t *cmd_func;
198: } commands_t;
199:
200:
201: /* Error support functions */
1.1 misho 202:
203: // cli_GetErrno() Get error code of last operation
204: inline int cli_GetErrno();
205: // cli_GetError() Get error text of last operation
206: inline const char *cli_GetError();
207:
208:
1.2.2.1 misho 209: /* CLI Functions */
210:
1.1 misho 211: /*
212: * cliExec() Execute CLI main loop
213: * @cmdList = Commands list
214: * @csPrompt = Prompt text
215: * return: -1 error, 0 = exit w/^+D, 1 done.
216: */
1.2.2.2 ! misho 217: int cliExec(commands_t *cmdList, const char *csPrompt);
1.2 misho 218: /*
219: * cliNetExec() Execute net CLI main loop
220: * @cmdList = Commands list
221: * @csPrompt = Prompt text
222: * @sock = client socket
223: * @term = stdin termios
224: * @win = window size of tty
225: * return: -1 error, 0 = exit w/^+D, 1 done.
226: */
1.2.2.2 ! misho 227: int cliNetExec(commands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win);
1.2 misho 228:
1.1 misho 229:
1.2.2.1 misho 230: /* CLI Helper functions */
1.1 misho 231:
232: /*
233: * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
234: * @cmds = Commands list
235: * @idx = Selected command ID
236: * @out = Output handle
237: * @args = Parsed arguments array
238: * return: -1 error, 0 = ok, 1 exit from Cli!
239: */
240: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
241: /*
242: * cli_Cmd_Help() Builtin helper function for Help screen
243: * @cmds = Commands list
244: * @idx = Selected command ID
245: * @out = Output handle
246: * @args = Parsed arguments array
247: * return: -1 error, 0 = ok
248: */
249: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
250: /*
251: * cli_Cmd_Exit() Builtin helper function for Exit from Cli
252: * @cmds = Commands list
253: * @idx = Selected command ID
254: * @out = Output handle
255: * @args = Parsed arguments array
256: * return: 1 exit from Cli!
257: */
258: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
259:
260:
261: /*
262: * cli_Register_Commands - Declare helper function for register and export Commands variable
263: */
264: #define CLI_REGISTER_COMMANDS(CMDS) \
1.2.2.1 misho 265: extern commands_t CMDS[];
1.1 misho 266: /*
267: * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
268: */
269: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS) \
270: char *FUNC(const char *text, int state) \
271: { \
272: register int i; \
273: int len = strlen(text); \
274: for (i = state; CMDS[i].cmd_name; i++) { \
275: if (strncmp(CMDS[i].cmd_name, "---", 3) && \
276: !strncmp(CMDS[i].cmd_name, text, len)) \
277: return strdup(CMDS[i].cmd_name); \
278: } \
279: return NULL; \
280: }
281:
282: /*
283: * cli_Make_Comp_Args - Declare helper function for Arguments completion
284: */
285: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
286: char *FUNC(const char *text __attribute__((unused)), int state) \
287: { \
288: while (ARGS[state]) \
289: return strdup(ARGS[state]); \
290: return NULL; \
291: }
292:
293:
294: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>