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