]> rtime.felk.cvut.cz Git - sysless.git/blob - libs4c/cmdproc/cmd_proc.h
cmdproc: Add more comments
[sysless.git] / libs4c / cmdproc / cmd_proc.h
1 /*******************************************************************
2   Components for embedded applications builded for
3   laboratory and medical instruments firmware
4
5   cmd_proc.h - text line command processor
6                designed for instruments control and setup
7                over RS-232 line
8
9   Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
10             (C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
11             (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
12
13   This file can be used and copied according to next
14   license alternatives
15    - MPL - Mozilla Public License
16    - GPL - GNU Public License
17    - other license provided by project originators
18  *******************************************************************/
19
20 #ifndef _CMD_PROC_H_
21 #define _CMD_PROC_H_
22
23 #define CMD_PROC_WITH_FILE
24
25 #ifdef CMD_PROC_WITH_FILE
26 #include <stdio.h>
27 #endif
28
29 #define FL_ELB_ECHO   0x10      /* Read characters are echoed back */
30 #define FL_ELB_INSEND 0x20      /* The line is currently being sent */
31 #define FL_ELB_NOCRLF 0x40      /* CR and/or LF will not start a new
32                                  * line (used for out buffers to store
33                                  * more than a single line) */
34 #define CMD_DES_CONTINUE_AT_ID ((cmd_des_t*)1)
35 #define CMD_DES_INCLUDE_SUBLIST_ID ((cmd_des_t*)2)
36 #define CMD_DES_CONTINUE_AT(list)     CMD_DES_CONTINUE_AT_ID,((cmd_des_t*)list)
37 #define CMD_DES_INCLUDE_SUBLIST(list) CMD_DES_INCLUDE_SUBLIST_ID,((cmd_des_t*)list)
38
39 /* generic cmd_io structure */
40
41 /* buffer for in/out line collection */
42 typedef struct{
43   int  flg;                     /* FL_ELB_xxx */
44   int  inbuf;                   /* Index to store new characters */
45   int  alloc;                   /* Size of the buffer pointed by buf */
46   int  maxlen;
47   int  lastch;                  /* Last characted added to the buffer.
48                                  * If FL_ELB_INSEND is set, lastch is
49                                  * index of last sent char. */
50   char *buf;
51 } ed_line_buf_t;
52
53 /* Structure for character input output. It is used either for direct
54  * access to IO device or for buffered IO. In the later case,
55  * priv.edline is used to store buffer information. */
56 typedef struct cmd_io{
57   int (*putc)(struct cmd_io *cmd_io,int ch);
58   int (*getc)(struct cmd_io *cmd_io);
59   int (*write)(struct cmd_io *cmd_io,const void *buf,int count);
60   int (*read)(struct cmd_io *cmd_io,void *buf,int count);
61   union {
62     struct {
63       ed_line_buf_t *in;  /* Buffer for storing the read command line */
64       ed_line_buf_t *out; /* Temporarily stores characters to be sent
65                            * out (until the send operation is
66                            * finished). This is not used for echo
67                            * handling. */
68       struct cmd_io *io_stack;
69     } ed_line;
70     struct {
71       long pos;
72       void *ptr;
73     } device;
74 #ifdef CMD_PROC_WITH_FILE
75     struct {
76       FILE *in;
77       FILE *out;
78     } file;
79 #endif /*CMD_PROC_WITH_FILE*/
80     struct {
81       int uartch;
82     } uart;
83   } priv;
84 } cmd_io_t;
85
86 static inline int cmd_io_putc(struct cmd_io *cmd_io,int ch)
87 { if(!cmd_io->putc) return -1;
88   return (*cmd_io->putc)(cmd_io,ch);
89 }
90
91 static inline int cmd_io_getc(struct cmd_io *cmd_io)
92 { if(!cmd_io->getc) return -1;
93   return (*cmd_io->getc)(cmd_io);
94 }
95
96 static inline int cmd_io_write(struct cmd_io *cmd_io,const void *buf,int count)
97 { if(!cmd_io->write) return -1;
98   return (*cmd_io->write)(cmd_io,buf,count);
99 }
100
101 static inline int cmd_io_read(struct cmd_io *cmd_io,void *buf,int count)
102 { if(!cmd_io->read) return -1;
103   return (*cmd_io->read)(cmd_io,buf,count);
104 }
105
106 int cmd_io_puts(cmd_io_t *cmd_io, const char *str);
107
108 int cmd_io_line_putc(cmd_io_t *cmd_io,int ch);
109
110 int cmd_io_write_bychar(cmd_io_t *cmd_io,const void *buf,int count);
111
112 int cmd_io_read_bychar(cmd_io_t *cmd_io,void *buf,int count);
113
114 /* command descriptions */
115
116 #define CDESM_OPCHR     0x10    /* Command uses operation character */
117 #define CDESM_RD        0x01    /* Value read is possible */
118 #define CDESM_WR        0x02    /* Value write is possible */
119 #define CDESM_RW        (CDESM_RD|CDESM_WR) /* Both */
120 #define CDESM_SPACE_SEP 0x20    /* Whitespace separated params - replaces space with '\0' */
121
122 /**
123  * Command descriptor.
124  *
125  * Command name may contain the following wildcards:
126  * - '?' stands for one alphanumeric character
127  * - '#' stands for one digit
128  * - '*' stands for any number of alphanumeric characters at the end of the command
129  *
130  * The fnc() function is passed the following information as the param array:
131  *   - param[0] points to the command (the longest sequence of alphanumeric characters)
132  *   If CDESM_OPCHR is specified in the command descriptor then
133  *   - param[1] points to the first wildcard in the command or is NULL for commands without wildcard in the name
134  *   - param[2] points to the operation character (':' or '?') and
135  *   - param[3] points to the string after the operation character (after skipping whitespace)
136  *   If CDESM_OPCHR is not specified then
137  *     If the command name contains a wildcard,
138  *     - param[1] points to the first wildcard in the command and
139  *     If the command has any parameters
140  *     - param[1] or param[2] (depending on the presence of a wildcard)
141  *       points to the beginning of those parameters (after skipping
142  *       whitespace)
143  *
144  *   If CDESM_SPACE_SEP is specified in the command descriptor, then
145  *   the command arguments are split at whitespace by '\0'
146  *   characters and stored in the next unused param elements. Param
147  *   array can contain up to 9 elements.
148  *
149  *   The param elements are always terminated by NULL element.
150  */
151 typedef struct cmd_des{
152   int code;                     /* Application specific code */
153   int mode;                     /* CDESM_* constants */
154   char *name;                   /* Name of the command with possible wildcards */
155   char *help;
156   int (*fnc)(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
157   char *info[];
158 } cmd_des_t;
159
160 #define CMDERR_BADCMD 2
161 #define CMDERR_OPCHAR 10
162 #define CMDERR_WRPERM 11
163 #define CMDERR_RDPERM 12
164 #define CMDERR_GARBAG 13
165 #define CMDERR_BADSEP 14
166 #define CMDERR_BADCFG 15
167 #define CMDERR_NOMEM  16
168 #define CMDERR_BADSUF 17
169 #define CMDERR_BADPAR 20
170 #define CMDERR_VALOOR 21
171 #define CMDERR_BADREG 40
172 #define CMDERR_BSYREG 41
173 #define CMDERR_BADDIO 50
174 #define CMDERR_NODEV  60
175 #define CMDERR_TIMEOUT 61
176 #define CMDERR_EIO    62
177
178 int cmd_opchar_check(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
179
180 int cmd_num_suffix(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[], unsigned *val);
181
182 int proc_cmd_line(cmd_io_t *cmd_io, cmd_des_t const **des_arr, char *line);
183
184 int i2str(char *s,long val,int len,int form);
185
186 int cmd_do_stamp(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
187
188 int cmd_do_help(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
189
190 int cmd_do_rw_short(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
191
192 int cmd_do_rw_int(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
193
194 int cmd_do_rw_long(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
195
196 int cmd_do_rw_bitflag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
197
198 int cmd_opchar_replong(cmd_io_t *cmd_io, char *param[], long val,int len,int form);
199
200 int cmd_processor_run(cmd_io_t *cmd_io, cmd_des_t const **commands);
201
202 #endif /* _CMD_PROC_H_ */
203
204 /* Local Variables: */
205 /* c-basic-offset: 2 */
206 /* End: */