Annotation of libaitcli/inc/aitcli.h, revision 1.2.2.4
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.4 ! misho 6: * $Id: aitcli.h,v 1.2.2.3 2010/06/04 13:51:21 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:
1.2.2.4 ! misho 163: /* Commands structure for CLI */
! 164:
! 165: typedef int (*cmd_func_t)(/*linebuffer_t **/ void * __restrict buffer, int argc, char ** __restrict argv);
! 166: struct tagCommand {
! 167: int cmd_level;
! 168:
! 169: int cmd_min;
! 170: int cmd_len;
! 171: char cmd_name[STRSIZ];
! 172:
! 173: char cmd_info[STRSIZ];
! 174: char cmd_help[STRSIZ];
! 175:
! 176: cmd_func_t cmd_func;
! 177:
! 178: SLIST_ENTRY(tagCommand) cmd_next;
! 179: };
! 180: typedef SLIST_HEAD(slCommandHead, tagCommand) commands_t;
! 181:
! 182:
1.2.2.1 misho 183: /* Main structure, Buffer for CLI work with thread models ;-) special designed by M.Punov */
184:
185: typedef struct {
186: char line_mode;
187:
188: char *line_prompt;
189:
190: int line_bol;
191: int line_eol;
192: int line_len;
193: char *line_buf;
194:
195: const struct tagHistory *line_h;
196: history_t line_history;
1.1 misho 197:
1.2.2.1 misho 198: bindkey_t *line_keys;
1.1 misho 199:
1.2.2.4 ! misho 200: commands_t line_cmds;
! 201:
1.2.2.1 misho 202: int line_in;
203: int line_out;
204: } linebuffer_t;
205:
206:
207: /* Error support functions */
1.1 misho 208:
209: // cli_GetErrno() Get error code of last operation
210: inline int cli_GetErrno();
211: // cli_GetError() Get error text of last operation
212: inline const char *cli_GetError();
213:
214:
1.2.2.1 misho 215: /* CLI Functions */
216:
1.1 misho 217: /*
1.2.2.3 misho 218: * cli_BindKey() Bind function to key
219: * @key = key structure
220: * @buffer = CLI buffer
221: * return: RETCODE_ERR error, RETCODE_OK ok, >0 bind at position
222: */
223: int cli_BindKey(bindkey_t * __restrict key, linebuffer_t * __restrict buffer);
224:
225: /*
226: * cli_addHistory() Add line to history
227: * @buffer = CLI buffer
1.2.2.4 ! misho 228: * @str = Add custom text or if NULL use readed line from CLI buffer
1.2.2.3 misho 229: * return: RETCODE_ERR error, RETCODE_OK ok
230: */
231: int cli_addHistory(linebuffer_t * __restrict buffer, const char * __restrict str);
232: /*
233: * cli_saveHistory() Save history to file
234: * @buffer = CLI buffer
235: * @histfile = History filename, if NULL will be use default name
236: * @lines = Maximum history lines to save
237: * return: RETCODE_ERR error, RETCODE_OK ok
238: */
239: int cli_saveHistory(linebuffer_t * __restrict buffer, const char *histfile, int lines);
240: /*
241: * cli_loadHistory() Load history from file
242: * @buffer = CLI buffer
243: * @histfile = History filename, if NULL will be use default name
244: * return: RETCODE_ERR error, RETCODE_OK ok
245: */
246: int cli_loadHistory(linebuffer_t * __restrict buffer, const char *histfile);
247: /*
248: * cli_resetHistory() Reset history search in CLI session
249: * @buffer = CLI buffer
250: * return: none
251: */
252: inline void cli_resetHistory(linebuffer_t * __restrict buffer);
253:
254: /*
255: * cli_freeLine() Clear entire line
256: * @buffer = CLI buffer
257: * return: RETCODE_ERR error, RETCODE_OK ok
258: */
259: inline int cli_freeLine(linebuffer_t * __restrict buffer);
260: /*
261: * cli_setPrompt() Set new prompt for CLI session
262: * @buffer = CLI buffer
263: * @prompt = new text for prompt or if NULL disable prompt
264: * return: none
265: */
266: inline void cli_setPrompt(linebuffer_t * __restrict buffer, const char *prompt);
1.2.2.4 ! misho 267: /*
! 268: * cli_Printf() Send message to CLI session
! 269: * @buffer = CLI buffer
! 270: * @fmt = printf format string
! 271: * @... = arguments defined in fmt
! 272: * return: none
! 273: */
! 274: inline void cli_Printf(linebuffer_t * __restrict buffer, char *fmt, ...);
1.2.2.3 misho 275:
276: /*
277: * cliEnd() Clear data, Free resources and close CLI session
278: * @buffer = CLI buffer
279: * return: RETCODE_ERR error, RETCODE_OK ok
280: */
281: void cliEnd(linebuffer_t * __restrict buffer);
282: /*
283: * cliInit() Start CLI session, allocate memory for resources and bind keys
284: * @fin = Input device handle
285: * @fout = Output device handle
286: * @prompt = text for prompt, if NULL disable prompt
287: * return: NULL if error or !=NULL CLI buffer
288: */
289: linebuffer_t *cliInit(int fin, int fout, const char *prompt);
290:
291: /*
1.2.2.4 ! misho 292: * cliReadLine() Read line from opened CLI session
! 293: * @buffer = CLI buffer
! 294: * return: NULL if error or !=NULL readed line, must be free after use!
! 295: */
! 296: char *cliReadLine(linebuffer_t * __restrict buffer);
! 297: /*
! 298: * cliLoop() CLI main loop
! 299: * @buffer = CLI buffer
! 300: * @csHistFile = History file name
! 301: * return: RETCODE_ERR error, RETCODE_OK ok
! 302: */
! 303: int cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile);
! 304:
! 305: /*
! 306: * cliLoop() CLI main loop
! 307: * @buffer = CLI buffer
! 308: * @csHistFile = History file name
! 309: * return: RETCODE_ERR error, RETCODE_OK ok
! 310: */
! 311: int cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile);
! 312: /*
! 313: * cliNetLoop() CLI network main loop binded to socket
! 314: * @buffer = CLI buffer
! 315: * @csHistFile = History file name
1.2 misho 316: * @sock = client socket
317: * @term = stdin termios
318: * @win = window size of tty
1.2.2.4 ! misho 319: * return: RETCODE_ERR error, RETCODE_OK ok
1.2 misho 320: */
1.2.2.4 ! misho 321: int cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock,
! 322: struct termios *term, struct winsize *win);
1.2 misho 323:
1.1 misho 324:
1.2.2.1 misho 325: /* CLI Helper functions */
1.1 misho 326:
327: /*
328: * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
329: * @cmds = Commands list
330: * @idx = Selected command ID
331: * @out = Output handle
332: * @args = Parsed arguments array
333: * return: -1 error, 0 = ok, 1 exit from Cli!
334: */
335: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
336: /*
337: * cli_Cmd_Help() Builtin helper function for Help screen
338: * @cmds = Commands list
339: * @idx = Selected command ID
340: * @out = Output handle
341: * @args = Parsed arguments array
342: * return: -1 error, 0 = ok
343: */
344: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
345: /*
346: * cli_Cmd_Exit() Builtin helper function for Exit from Cli
347: * @cmds = Commands list
348: * @idx = Selected command ID
349: * @out = Output handle
350: * @args = Parsed arguments array
351: * return: 1 exit from Cli!
352: */
353: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
354:
355:
356: /*
357: * cli_Register_Commands - Declare helper function for register and export Commands variable
358: */
359: #define CLI_REGISTER_COMMANDS(CMDS) \
1.2.2.1 misho 360: extern commands_t CMDS[];
1.1 misho 361: /*
362: * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
363: */
364: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS) \
365: char *FUNC(const char *text, int state) \
366: { \
367: register int i; \
368: int len = strlen(text); \
369: for (i = state; CMDS[i].cmd_name; i++) { \
370: if (strncmp(CMDS[i].cmd_name, "---", 3) && \
371: !strncmp(CMDS[i].cmd_name, text, len)) \
372: return strdup(CMDS[i].cmd_name); \
373: } \
374: return NULL; \
375: }
376:
377: /*
378: * cli_Make_Comp_Args - Declare helper function for Arguments completion
379: */
380: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
381: char *FUNC(const char *text __attribute__((unused)), int state) \
382: { \
383: while (ARGS[state]) \
384: return strdup(ARGS[state]); \
385: return NULL; \
386: }
387:
388:
389: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>