-
Notifications
You must be signed in to change notification settings - Fork 0
/
ush.h
97 lines (84 loc) · 3.49 KB
/
ush.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
/**
* @author Neo Xu ([email protected])
* @license The MIT License (MIT)
*
* Copyright (c) 2019 Neo Xu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @brief ush - micro-shell is a shell for embedded devices.
*/
#ifndef _USH_H_
#define _USH_H_
#include "stdint.h"
#include "ush_conf.h"
#define USH_VER_NUMBER 0x011 //0.1.1
typedef enum{
ush_error_ok = 0, /**< all ok */
ush_error_nullp = -1, /**< null pointer */
ush_error_nocmd = -2, /**< no valid command found */
ush_error_maxarg = -3, /**< reached to maximum arguments allowed */
ush_error_buffsz = -4, /**< line buffer size is limted. */
ush_error_str = -5, /**< the string parameter is incompleted. */
ush_error_nullstr = -6, /**< input string is null. */
ush_error_strillegal = -7,/**<illegal input string for number */
}ush_error_def;
typedef int32_t (*ush_func_def)(int32_t argc, char ** argv);
typedef struct _ush_cmd{
void *func; /**< the function to deal with this command. */
const char *cmd; /**< the command string that calls this function. */
const char *desc; /**< description of this command. */
}ush_cmd_def;
typedef struct _ush_list{
struct _ush_list *next;
const ush_cmd_def *const item;
}ush_list_def;
#define CONCAT_(x,y) x##y
#define CONCAT(x,y) CONCAT_(x,y)
#define uniquename(func) CONCAT(func, __LINE__)
#define USH_REGISTER(func, name, desc) \
const ush_cmd_def uniquename(func) __attribute__((section("ushtable"))) __attribute__((used))= \
{ \
(void *)&func, \
#name, \
#desc, \
}
typedef enum{
ush_state_normal = 0,
ush_state_in_str, /**< ush is dealing with input string. */
ush_state_escaping, /**< there is a '\' input in a string. */
}ush_state_def;
typedef enum{
ush_num_uint32 = 0, /**< number is unsigned */
ush_num_int32,
ush_num_float,
}ush_num_def;
typedef struct _ush{
char *linebuff; /**< input line buffer*/
uint32_t buffsz; /**< line buffer size. */
uint32_t w_pos; /**< current position in line buffer to write. */
uint32_t argc; /**< the arguements found in linebuffer till now.*/
char *argv[USH_MAX_ARG]; /**< pointers point to the arugments. */
ush_state_def state;
}ush_def;
ush_error_def ush_init(ush_def *ush, char *pbuff, uint32_t len);
ush_error_def ush_cmdlist_append(ush_list_def *pitem);
ush_error_def ush_process_input(ush_def *ush, const char *pbuff, uint32_t len);
ush_error_def ush_str2num(const char *pstr, uint32_t len, ush_num_def* num_type, void *value);
#endif