#include <string.h>
#include <stdint.h>
-/**
+/**
* Blocking call to print a string.
- *
+ *
* @param cmd_io cmd_io structure.
* @param str Zero terminated string to print.
- *
+ *
* @return Upon successful completion, puts() shall return a
* non-negative number. In case of error, negative number is returned.
*/
}
return cn;
}
-
/*******************************************************************
Components for embedded applications builded for
- laboratory and medical instruments firmware
-
+ laboratory and medical instruments firmware
+
cmd_proc.h - text line command processor
designed for instruments control and setup
over RS-232 line
-
+
Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002 by PiKRON Ltd. http://www.pikron.com
(C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
/* cmd_io line editor */
-/**
+/**
* Adds new characters to an edit line buffer.
- *
+ *
* @param elb Edit line buffer.
* @param ch character to add.
- *
+ *
* @return 1 in case of end of line, -1 if called at inaproprate time, 0 otherwise.
*/
int cmd_ed_line_buf(ed_line_buf_t *elb, int ch)
elb->buf[elb->inbuf]=0;
return 1;
}
- if(elb->inbuf>=elb->alloc-1){
+ if(elb->inbuf>=elb->alloc-1){
/* try to reallocate buffer len not implemented */
return 0;
}
- elb->buf[elb->inbuf++]=ch;
+ elb->buf[elb->inbuf++]=ch;
return 0;
}
{
cmd_io_t* io_stack=cmd_io->priv.ed_line.io_stack;
ed_line_buf_t* ed_line_out=cmd_io->priv.ed_line.out;
-
+
if(!ed_line_out->inbuf) return 0;
if(!io_stack)
return -1;
-
+
if(!(ed_line_out->flg&FL_ELB_INSEND)){
ed_line_out->flg|=FL_ELB_INSEND;
ed_line_out->lastch=0;
}
return 1;
}
- else
+ else
if(ed_line_in->flg&FL_ELB_ECHO) {
while(cmd_io_putc(io_stack,ch)<0);
}
/*******************************************************************
Components for embedded applications builded for
- laboratory and medical instruments firmware
-
+ laboratory and medical instruments firmware
+
cmd_proc.c - text command processor
enables to define multilevel tables of commands
which can be received from more inputs and send reply
to respective I/O stream output
-
+
Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
(C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
int arr_stack_sp=0;
char *param[10];
short cmd_len;
- int res, i, parcnt;
+ int res, i, parcnt;
param[0]=p=skip_white(p);
if(!*p) return 0;
/* Determine the name of the command */
cmd_len=p-param[0];
}
param[1]=param[2]=skip_white(p);
-
+
/* Find the command in des_arr */
while(1){
des=*(des_arr++);
break;
}
i=1; /* Signal no match */
- break;
+ break;
}
if(i!=0) continue; /* Try next command */
/* Setup parameters */
* if the opchar is not ':' or '?'.
*/
int cmd_opchar_check(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
-{
+{
int opchar=*param[2];
if(opchar==':'){
if(!(des->mode&CDESM_WR)){
p++;
}while(*p && !strchr(" :?=",*p));
*pval=val;
- return 0;
+ return 0;
}
cmd_io_write(cmd_io,param[0],param[2]-param[0]);
cmd_io_putc(cmd_io,'=');
cmd_io_write(cmd_io,param[3],strlen(param[3]));
- return 0;
+ return 0;
}
/**
}else{
return cmd_opchar_replong(cmd_io, param, (long)*ptr, 0, 0);
}
- return 0;
+ return 0;
}
/**
}else{
return cmd_opchar_replong(cmd_io, param, (long)*ptr, 0, 0);
}
- return 0;
+ return 0;
}
}else{
return cmd_opchar_replong(cmd_io, param, (long)*ptr, 0, 0);
}
- return 0;
+ return 0;
}
/**
cmd_io_putc(cmd_io,'=');
cmd_io_putc(cmd_io,*pval&mask?'1':'0');
}
-
+
return 0;
}
/* FIXME: This should not be there unconditional */
if (cmd_io->priv.ed_line.io_stack)
cmd_io = cmd_io->priv.ed_line.io_stack;
-
+
if(filt) {
filt=skip_white(filt);
if(!*filt) filt=NULL;
/*******************************************************************
Components for embedded applications builded for
- laboratory and medical instruments firmware
-
+ laboratory and medical instruments firmware
+
cmd_proc.h - text line command processor
designed for instruments control and setup
over RS-232 line
-
+
Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
(C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
#define CMD_PROC_PRIV_H
/* The maximal depth of command arrays nesting (see CMD_DES_INCLUDE_SUBLIST). */
-#define CMD_ARR_STACK_SIZE 4
+#define CMD_ARR_STACK_SIZE 4
char *skip_white(char *p);
int cmd_io_line_out(cmd_io_t *cmd_io);
/*******************************************************************
Components for embedded applications builded for
- laboratory and medical instruments firmware
-
+ laboratory and medical instruments firmware
+
cmd_proc.c - text command processor
enables to define multilevel tables of commands
which can be received from more inputs and send reply
to respective I/O stream output
-
+
Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
(C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
if(cmd_io_line_out(cmd_io))
return 1; /* Not all the output has been sent. */
-
+
if(cmd_io_line_in(cmd_io)<=0)
return 0; /* Input line not finished or error. */
/* Definitions of IOs */
-#include <unistd.h>
+#include <unistd.h>
#include <cmd_proc.h>
#include <stdio.h>
-static int std_putc(cmd_io_t *cmd_io, int ch) {
+static int std_putc(cmd_io_t *cmd_io, int ch) {
int r=putchar(ch);
fflush(stdout);
return r;
}
//int _getkey_nb(void);
-static int std_getc(cmd_io_t *cmd_io) {
+static int std_getc(cmd_io_t *cmd_io) {
return getchar(); /* On UNIX, we don't use non-blocking
* variant. */
//return _getkey_nb();
return write(1, buf, count);
}
static int std_read(cmd_io_t *cmd_io, void *buf, int count) {
- return read(0, buf, count);
+ return read(0, buf, count);
}
cmd_io_t cmd_io_std={
write:std_write,
read:std_read
};
-
/*******************************************************************
Components for embedded applications builded for
- laboratory and medical instruments firmware
-
+ laboratory and medical instruments firmware
+
cmdio_std_file.c - interconnection of text command processor
with generic file interface
-
+
Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
(C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
int cmd_proc_forfile(FILE * in,FILE * out, cmd_des_t const **des_arr, char *line)
{
cmd_io_t cmd_io;
-
+
cmd_io.putc=cmd_io_putc_forfile;
cmd_io.getc=cmd_io_getc_forfile;
cmd_io.write=cmd_io_write_forfile;
int res;
size_t len;
char *line, *p;
-
+
if(argc<2){
cmd_proc_forfile(stdin, stdout, cmd_forshell, "help");
printf("\n");
return 1;
}
-
+
for(len=0, i=1; i<argc; i++)
len+=strlen(argv[i])+1;
line=malloc(len);
if(!line)
return 3;
-
+
for(p=line, i=1; i<argc; i++) {
len=strlen(argv[i]);
memcpy(p,argv[i],len);
printf("cmdproc \"%s\" failed with error %d\n",line,res);
res=2;
} else res=0;
-
+
free(line);
-
+
return res;
}
"param[1]=%s\n"
"param[2]=%s\n"
"param[3]=%s\n"
- "opchar=%c\n",
+ "opchar=%c\n",
param[0], param[1], param[2], param[3], opchar);
return 0;