forked from biot/tokenline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokenline.h
114 lines (100 loc) · 2.98 KB
/
tokenline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (C) 2014 Bert Vermeulen <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TOKENLINE_H
#define TOKENLINE_H
#include <stdint.h>
#define TL_MAX_LINE_LEN 128
#define TL_MAX_ESCAPE_LEN 8
#define TL_MAX_WORDS 64
#define TL_MAX_TOKEN_LEVELS 8
#define TL_MAX_HISTORY_SIZE 512
#define TL_TOKEN_DELIMITER ':'
#define TL_ONE_COMMAND_PER_LINE FALSE
enum {
/* Token can be optionally suffixed by delimiter and integer. */
T_FLAG_SUFFIX_TOKEN_DELIM_INT = (1 << 0),
};
typedef struct token_dict {
int token;
char *tokenstr;
} t_token_dict;
typedef const struct token {
int token;
uint16_t arg_type;
uint16_t flags;
const struct token *subtokens;
char *help;
char *help_full;
} t_token;
typedef struct tokenline_parsed {
int tokens[TL_MAX_WORDS];
char buf[TL_MAX_LINE_LEN];
t_token *last_token_entry;
} t_tokenline_parsed;
typedef void (*tl_printfunc)(void *user, const char *str);
typedef void (*tl_callback)(void *user, t_tokenline_parsed *p);
typedef struct tokenline {
t_token *token_levels[TL_MAX_TOKEN_LEVELS];
int token_level;
t_token_dict *token_dict;
tl_printfunc print;
void *user;
char buf[TL_MAX_LINE_LEN];
int buf_len;
char escape[TL_MAX_ESCAPE_LEN];
int escape_len;
char *prompt;
tl_callback callback;
int pos;
int one_command_per_line;
t_tokenline_parsed parsed;
int hist_step;
int hist_begin;
int hist_end;
/* Need a terminating zero to print last chunk in ring buffer. */
char hist_buf[TL_MAX_HISTORY_SIZE + 1];
} t_tokenline;
/* These share a number space with the tokens. */
enum token_argtypes {
/* Optionally followed by k (kilo), m (mega) or g (giga). */
T_ARG_UINT = 10000,
/* Optionally followed by k (kilo), m (mega) or g (giga). */
T_ARG_FLOAT,
T_ARG_STRING,
/* Argument is one of the tokens in subtokens. */
T_ARG_TOKEN,
/* Integer argument suffixed to token with delimiter and integer. */
T_ARG_TOKEN_SUFFIX_INT,
T_ARG_HELP,
};
void tl_init(t_tokenline *tl, t_token *tokens_top, t_token_dict *token_dict,
tl_printfunc printfunc, void *user);
void tl_set_prompt(t_tokenline *tl, char *prompt);
void tl_set_callback(t_tokenline *tl, tl_callback callback);
int tl_mode_push(t_tokenline *tl, t_token *tokens_mode);
int tl_mode_pop(t_tokenline *tl);
int tl_input(t_tokenline *tl, uint8_t c);
#ifndef NULL
#define NULL 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#endif