Annotation of libaitcli/inc/aitcli.h, revision 1.2.2.9
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.9 ! misho 6: * $Id: aitcli.h,v 1.2.2.8 2010/06/07 12:30:28 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #ifndef __AITCLI_H
10: #define __AITCLI_H
11:
12:
1.2.2.5 misho 13: #include <termios.h>
14: #include <sys/queue.h>
15:
16:
1.2.2.2 misho 17: #define STRSIZ 256
18:
1.2.2.1 misho 19: /* Key definitions */
20:
21: #define K_F1 "\x1b\x4f\x50"
22: #define K_F2 "\x1b\x4f\x51"
23: #define K_F3 "\x1b\x4f\x52"
24: #define K_F4 "\x1b\x4f\x53"
25: #define K_F5 "\x1b\x5b\x31\x35\x7e"
26: #define K_F6 "\x1b\x5b\x31\x37\x7e"
27: #define K_F7 "\x1b\x5b\x31\x38\x7e"
28: #define K_F8 "\x1b\x5b\x31\x39\x7e"
29: #define K_F9 "\x1b\x5b\x32\x30\x7e"
30: #define K_F10 "\x1b\x5b\x32\x31\x7e"
31: #define K_F11 "\x1b\x5b\x32\x33\x7e"
32: #define K_F12 "\x1b\x5b\x32\x34\x7e"
33: #define K_CTRL_F1 "\x1b\x5b\x6b"
34: #define K_CTRL_F2 "\x1b\x5b\x6c"
35: #define K_CTRL_F3 "\x1b\x5b\x6d"
36: #define K_CTRL_F4 "\x1b\x5b\x6e"
37: #define K_CTRL_F5 "\x1b\x5b\x6f"
38: #define K_CTRL_F6 "\x1b\x5b\x70"
39: #define K_CTRL_F7 "\x1b\x5b\x71"
40: #define K_CTRL_F8 "\x1b\x5b\x72"
41: #define K_CTRL_F9 "\x1b\x5b\x73"
42: #define K_CTRL_F10 "\x1b\x5b\x74"
43: #define K_CTRL_F11 "\x1b\x5b\x75"
44: #define K_CTRL_F12 "\x1b\x5b\x76"
45: #define K_CTRL_SH_F1 "\x1b\x5b\x77"
46: #define K_CTRL_SH_F2 "\x1b\x5b\x78"
47: #define K_CTRL_SH_F3 "\x1b\x5b\x79"
48: #define K_CTRL_SH_F4 "\x1b\x5b\x7a"
49: #define K_CTRL_SH_F5 "\x1b\x5b\x40"
50: #define K_CTRL_SH_F6 "\x1b\x5b\x5b"
51: #define K_CTRL_SH_F7 "\x1b\x5b\x5c"
52: #define K_CTRL_SH_F8 "\x1b\x5b\x5d"
53: #define K_CTRL_SH_F9 "\x1b\x5b\x5e"
54: #define K_CTRL_SH_F10 "\x1b\x5b\x5f"
55: #define K_CTRL_SH_F11 "\x1b\x5b\x60"
56: #define K_CTRL_SH_F12 "\x1b\x5b\x7b"
57:
58: #define K_INS "\x1b\x5b\x32\x7e"
59: #define K_DEL "\x1b\x5b\x33\x7e"
60: #define K_PGUP "\x1b\x5b\x35\x7e"
61: #define K_PGDN "\x1b\x5b\x36\x7e"
62: #define K_HOME "\x1b\x5b\x48"
63: #define K_END "\x1b\x5b\x46"
64: #define K_UP "\x1b\x5b\x41"
65: #define K_DOWN "\x1b\x5b\x42"
66: #define K_RIGHT "\x1b\x5b\x43"
67: #define K_LEFT "\x1b\x5b\x44"
68:
69: #define K_NULL "\x0"
70: #define K_CR "\xd"
71: #define K_BTAB "\x1b\x5b\x5a"
72: #define K_TAB "\x9"
73: #define K_ENTER "\xa"
74: #define K_ESC "\x1b"
75: #define K_BACKSPACE "\x7f"
76: #define K_SPACE "\x20"
77:
78: #define K_CTRL_SPACE K_NULL
79: #define K_CTRL_2 K_NULL
80: #define K_CTRL_A "\x1"
81: #define K_CTRL_B "\x2"
82: #define K_CTRL_C "\x3"
83: #define K_CTRL_D "\x4"
84: #define K_CTRL_E "\x5"
85: #define K_CTRL_F "\x6"
86: #define K_CTRL_G "\x7"
87: #define K_CTRL_H "\x8"
88: #define K_CTRL_I K_TAB
89: #define K_CTRL_J K_ENTER
90: #define K_CTRL_K "\xb"
91: #define K_CTRL_L "\xc"
92: #define K_CTRL_M K_ENTER // K_CR
93: #define K_CTRL_N "\xe"
94: #define K_CTRL_O "\xf"
95: #define K_CTRL_P "\x10"
96: #define K_CTRL_Q "\x11"
97: #define K_CTRL_R "\x12"
98: #define K_CTRL_S "\x13"
99: #define K_CTRL_T "\x14"
100: #define K_CTRL_U "\x15"
101: #define K_CTRL_V "\x16"
102: #define K_CTRL_W "\x17"
103: #define K_CTRL_X "\x18"
104: #define K_CTRL_Y "\x19"
105: #define K_CTRL_Z "\x1a"
106: #define K_CTRL_LBRACE "\x1b"
107: #define K_CTRL_PIPE "\x1c"
108: #define K_CTRL_RBRACE "\x1d"
109: #define K_CTRL_6 "\x1e"
110: #define K_CTRL__ "\x1f"
111:
112:
113: #define K_X_CTRL_INS "\x1b\x5b\x32\x3b\x35\x7e"
114: #define K_X_CTRL_DEL "\x1b\x5b\x33\x3b\x35\x7e"
115: #define K_X_CTRL_PGUP "\x1b\x5b\x35\x3b\x35\x7e"
116: #define K_X_CTRL_PGDN "\x1b\x5b\x36\x3b\x35\x7e"
117: #define K_X_CTRL_HOME "\x1b\x5b\x31\x3b\x35\x48"
118: #define K_X_CTRL_END "\x1b\x5b\x31\x3b\x35\x46"
119: #define K_X_CTRL_UP "\x1b\x5b\x31\x3b\x35\x41"
120: #define K_X_CTRL_DOWN "\x1b\x5b\x31\x3b\x35\x42"
121: #define K_X_CTRL_RIGHT "\x1b\x5b\x31\x3b\x35\x43"
122: #define K_X_CTRL_LEFT "\x1b\x5b\x31\x3b\x35\x44"
123:
124: #define K_X_ALT_INS "\x1b\x5b\x32\x3b\x33\x7e"
125: #define K_X_ALT_DEL "\x1b\x5b\x33\x3b\x33\x7e"
126: #define K_X_ALT_PGUP "\x1b\x5b\x35\x3b\x33\x7e"
127: #define K_X_ALT_PGDN "\x1b\x5b\x36\x3b\x33\x7e"
128: #define K_X_ALT_HOME "\x1b\x5b\x31\x3b\x33\x48"
129: #define K_X_ALT_END "\x1b\x5b\x31\x3b\x33\x46"
130: #define K_X_ALT_UP "\x1b\x5b\x31\x3b\x33\x41"
131: #define K_X_ALT_DOWN "\x1b\x5b\x31\x3b\x33\x42"
132: #define K_X_ALT_RIGHT "\x1b\x5b\x31\x3b\x33\x43"
133: #define K_X_ALT_LEFT "\x1b\x5b\x31\x3b\x33\x44"
134:
135: #define K_X_CTL_A_INS "\x1b\x5b\x32\x3b\x37\x7e"
136: #define K_X_CTL_A_DEL "\x1b\x5b\x33\x3b\x37\x7e"
137: #define K_X_CTL_A_PGUP "\x1b\x5b\x35\x3b\x37\x7e"
138: #define K_X_CTL_A_PGDN "\x1b\x5b\x36\x3b\x37\x7e"
139: #define K_X_CTL_A_HOME "\x1b\x5b\x31\x3b\x37\x48"
140: #define K_X_CTL_A_END "\x1b\x5b\x31\x3b\x37\x46"
141: #define K_X_CTL_A_UP "\x1b\x5b\x31\x3b\x37\x41"
142: #define K_X_CTL_A_DOWN "\x1b\x5b\x31\x3b\x37\x42"
143: #define K_X_CTL_A_RIGHT "\x1b\x5b\x31\x3b\x37\x43"
144: #define K_X_CTL_A_LEFT "\x1b\x5b\x31\x3b\x37\x44"
145:
146:
147: /* History types */
148:
149: struct tagHistory {
150: int hist_len;
151: char hist_line[BUFSIZ];
152:
153: TAILQ_ENTRY(tagHistory) hist_next;
1.1 misho 154: };
1.2.2.1 misho 155: typedef TAILQ_HEAD(tqHistoryHead, tagHistory) history_t;
156:
157: /* Bind keys structure types */
158:
159: typedef int (*bindkey_func_t)(int idx, /*linebuffer_t **/ void * __restrict buffer);
160: typedef struct {
161: int key_len;
162: unsigned char key_ch[8];
163: bindkey_func_t key_func;
164: } bindkey_t;
165:
166:
1.2.2.4 misho 167: /* Commands structure for CLI */
168:
1.2.2.8 misho 169: typedef int (*cmd_func_t)(/*linebuffer_t **/ void * __restrict buffer, int idx, char ** __restrict args);
1.2.2.4 misho 170: struct tagCommand {
171: int cmd_level;
172:
173: int cmd_len;
174: char cmd_name[STRSIZ];
175:
176: char cmd_info[STRSIZ];
177: char cmd_help[STRSIZ];
178:
179: cmd_func_t cmd_func;
180:
1.2.2.9 ! misho 181: char **cmd_comp;
! 182:
1.2.2.4 misho 183: SLIST_ENTRY(tagCommand) cmd_next;
184: };
185: typedef SLIST_HEAD(slCommandHead, tagCommand) commands_t;
186:
187:
1.2.2.1 misho 188: /* Main structure, Buffer for CLI work with thread models ;-) special designed by M.Punov */
189:
190: typedef struct {
191: char line_mode;
192:
193: char *line_prompt;
194:
195: int line_bol;
196: int line_eol;
197: int line_len;
198: char *line_buf;
199:
200: const struct tagHistory *line_h;
201: history_t line_history;
1.1 misho 202:
1.2.2.1 misho 203: bindkey_t *line_keys;
1.1 misho 204:
1.2.2.8 misho 205: int line_level;
1.2.2.4 misho 206: commands_t line_cmds;
207:
1.2.2.1 misho 208: int line_in;
209: int line_out;
210: } linebuffer_t;
211:
212:
213: /* Error support functions */
1.1 misho 214:
215: // cli_GetErrno() Get error code of last operation
216: inline int cli_GetErrno();
217: // cli_GetError() Get error text of last operation
218: inline const char *cli_GetError();
219:
220:
1.2.2.9 ! misho 221: /* CLI Helper functions */
! 222:
! 223: /*
! 224: * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
! 225: * @buffer = CLI buffer
! 226: * @idx = Selected command ID
! 227: * @args = Parsed arguments array
! 228: * return: RETCODE_OK ok
! 229: */
! 230: int cli_Cmd_Unsupported(void * __restrict buffer, int idx, char ** __restrict args);
! 231:
! 232:
1.2.2.1 misho 233: /* CLI Functions */
234:
1.1 misho 235: /*
1.2.2.3 misho 236: * cli_BindKey() Bind function to key
237: * @key = key structure
238: * @buffer = CLI buffer
239: * return: RETCODE_ERR error, RETCODE_OK ok, >0 bind at position
240: */
241: int cli_BindKey(bindkey_t * __restrict key, linebuffer_t * __restrict buffer);
242:
1.2.2.6 misho 243:
244: /*
245: * cli_addCommand() Add command to CLI session
246: * @buffer = CLI buffer
247: * @csCmd = Command name
248: * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
249: * @funcCmd = Callback function when user call command
250: * @csInfo = Inline information for command
251: * @csHelp = Help line when call help
1.2.2.9 ! misho 252: * @anComp = Completion array terminated with NULL element, -1 complete commands, NULL nothing
1.2.2.6 misho 253: * return: RETCODE_ERR error, RETCODE_OK ok
254: */
255: int
256: cli_addCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd,
1.2.2.9 ! misho 257: const char *csInfo, const char *csHelp, const char **anComp);
1.2.2.6 misho 258: /*
259: * cli_delCommand() Delete command from CLI session
260: * @buffer = CLI buffer
261: * @csCmd = Command name
262: * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
263: * return: RETCODE_ERR error, RETCODE_OK ok
264: */
265: int
266: cli_delCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel);
267: /*
268: * cli_updCommand() Update command in CLI session
269: * @buffer = CLI buffer
270: * @csCmd = Command name
271: * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
272: * @funcCmd = Callback function when user call command
273: * @csInfo = Inline information for command
274: * @csHelp = Help line when call help
1.2.2.9 ! misho 275: * @anComp = Completion array terminated with NULL element, -1 complete commands, NULL nothing,
! 276: * update only if funcCmd is not NULL
1.2.2.6 misho 277: * return: RETCODE_ERR error, RETCODE_OK ok
278: */
279: int
280: cli_updCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd,
1.2.2.9 ! misho 281: const char *csInfo, const char *csHelp, const char **anComp);
1.2.2.6 misho 282:
283:
1.2.2.3 misho 284: /*
285: * cli_addHistory() Add line to history
286: * @buffer = CLI buffer
1.2.2.4 misho 287: * @str = Add custom text or if NULL use readed line from CLI buffer
1.2.2.3 misho 288: * return: RETCODE_ERR error, RETCODE_OK ok
289: */
290: int cli_addHistory(linebuffer_t * __restrict buffer, const char * __restrict str);
291: /*
292: * cli_saveHistory() Save history to file
293: * @buffer = CLI buffer
294: * @histfile = History filename, if NULL will be use default name
295: * @lines = Maximum history lines to save
296: * return: RETCODE_ERR error, RETCODE_OK ok
297: */
298: int cli_saveHistory(linebuffer_t * __restrict buffer, const char *histfile, int lines);
299: /*
300: * cli_loadHistory() Load history from file
301: * @buffer = CLI buffer
302: * @histfile = History filename, if NULL will be use default name
303: * return: RETCODE_ERR error, RETCODE_OK ok
304: */
305: int cli_loadHistory(linebuffer_t * __restrict buffer, const char *histfile);
306: /*
307: * cli_resetHistory() Reset history search in CLI session
308: * @buffer = CLI buffer
309: * return: none
310: */
311: inline void cli_resetHistory(linebuffer_t * __restrict buffer);
312:
313: /*
314: * cli_freeLine() Clear entire line
315: * @buffer = CLI buffer
316: * return: RETCODE_ERR error, RETCODE_OK ok
317: */
318: inline int cli_freeLine(linebuffer_t * __restrict buffer);
319: /*
320: * cli_setPrompt() Set new prompt for CLI session
321: * @buffer = CLI buffer
322: * @prompt = new text for prompt or if NULL disable prompt
323: * return: none
324: */
325: inline void cli_setPrompt(linebuffer_t * __restrict buffer, const char *prompt);
1.2.2.4 misho 326: /*
327: * cli_Printf() Send message to CLI session
328: * @buffer = CLI buffer
329: * @fmt = printf format string
330: * @... = arguments defined in fmt
331: * return: none
332: */
333: inline void cli_Printf(linebuffer_t * __restrict buffer, char *fmt, ...);
1.2.2.3 misho 334:
335: /*
336: * cliEnd() Clear data, Free resources and close CLI session
337: * @buffer = CLI buffer
338: * return: RETCODE_ERR error, RETCODE_OK ok
339: */
340: void cliEnd(linebuffer_t * __restrict buffer);
341: /*
342: * cliInit() Start CLI session, allocate memory for resources and bind keys
343: * @fin = Input device handle
344: * @fout = Output device handle
345: * @prompt = text for prompt, if NULL disable prompt
346: * return: NULL if error or !=NULL CLI buffer
347: */
348: linebuffer_t *cliInit(int fin, int fout, const char *prompt);
349:
350: /*
1.2.2.4 misho 351: * cliReadLine() Read line from opened CLI session
352: * @buffer = CLI buffer
353: * return: NULL if error or !=NULL readed line, must be free after use!
354: */
355: char *cliReadLine(linebuffer_t * __restrict buffer);
356: /*
357: * cliLoop() CLI main loop
358: * @buffer = CLI buffer
359: * @csHistFile = History file name
360: * return: RETCODE_ERR error, RETCODE_OK ok
361: */
362: int cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile);
363:
364: /*
365: * cliLoop() CLI main loop
366: * @buffer = CLI buffer
367: * @csHistFile = History file name
368: * return: RETCODE_ERR error, RETCODE_OK ok
369: */
370: int cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile);
371: /*
372: * cliNetLoop() CLI network main loop binded to socket
373: * @buffer = CLI buffer
374: * @csHistFile = History file name
1.2 misho 375: * @sock = client socket
376: * @term = stdin termios
377: * @win = window size of tty
1.2.2.4 misho 378: * return: RETCODE_ERR error, RETCODE_OK ok
1.2 misho 379: */
1.2.2.4 misho 380: int cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock,
381: struct termios *term, struct winsize *win);
1.2 misho 382:
1.1 misho 383:
384:
385: /*
386: * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
387: */
388: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS) \
389: char *FUNC(const char *text, int state) \
390: { \
391: register int i; \
392: int len = strlen(text); \
393: for (i = state; CMDS[i].cmd_name; i++) { \
394: if (strncmp(CMDS[i].cmd_name, "---", 3) && \
395: !strncmp(CMDS[i].cmd_name, text, len)) \
396: return strdup(CMDS[i].cmd_name); \
397: } \
398: return NULL; \
399: }
400:
401: /*
402: * cli_Make_Comp_Args - Declare helper function for Arguments completion
403: */
404: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
405: char *FUNC(const char *text __attribute__((unused)), int state) \
406: { \
407: while (ARGS[state]) \
408: return strdup(ARGS[state]); \
409: return NULL; \
410: }
411:
412:
413: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>