/*******************************************************************
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, 0 otherwise.
+ *
+ * @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;
}
-/* process input */
+/* process input & perform echo if requested */
int cmd_io_line_in(cmd_io_t *cmd_io)
{
int ch;
while((ch=cmd_io_getc(io_stack))>=0){
// DPRINT("Added %c (%d)\n", ch, ch);
int eol = cmd_ed_line_buf(ed_line_in,ch);
+ if (eol == -1)
+ return -1;
if(eol){
if(ed_line_in->flg&FL_ELB_ECHO){
while(cmd_io_putc(io_stack,'\r')<0);
}
return 1;
}
- else
+ else
if(ed_line_in->flg&FL_ELB_ECHO) {
while(cmd_io_putc(io_stack,ch)<0);
}
return 0;
}
-/* char *cmd_io_line_rdline(cmd_io_t *cmd_io, int mode) */
-/* { */
-/* int ret; */
-/* while((ret=cmd_rs232_line_in(cmd_io))==0) */
-/* if(!mode) break; */
-/* if(ret<=0) return NULL; */
-/* return cmd_io->priv.ed_line.in->buf; */
-/* } */
+/* The possibly blocking read of one line, should be used only
+ when other options fails */
+char *cmd_io_line_rdline(cmd_io_t *cmd_io, int mode)
+{
+ int ret;
+ while((ret=cmd_io_line_in(cmd_io))==0)
+ if(!mode) break;
+ if(ret<=0) return NULL;
+ return cmd_io->priv.ed_line.in->buf;
+}
/* Local Variables: */
/* c-basic-offset: 2 */
-/* End */
+/* End: */