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