]> rtime.felk.cvut.cz Git - sysless.git/blobdiff - libs4c/cmdproc/cmd_io_line.c
cmdproc: Add more comments
[sysless.git] / libs4c / cmdproc / cmd_io_line.c
index 6e85cdc2b9afcdd40cff094f1260120dc7584bd1..dd87c2d1cc2adec4fdfcada0aca5cb61547d5040 100644 (file)
@@ -1,11 +1,11 @@
 /*******************************************************************
   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)
 {
@@ -47,11 +47,11 @@ 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;
 }
 
@@ -65,11 +65,11 @@ int cmd_io_line_out(cmd_io_t *cmd_io)
 {
   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;
@@ -85,7 +85,7 @@ int cmd_io_line_out(cmd_io_t *cmd_io)
   return 1;
 }
 
-/* process input */
+/* process input & perform echo if requested */
 int cmd_io_line_in(cmd_io_t *cmd_io)
 {
   int ch;
@@ -98,6 +98,8 @@ int cmd_io_line_in(cmd_io_t *cmd_io)
   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);
@@ -105,7 +107,7 @@ int cmd_io_line_in(cmd_io_t *cmd_io)
       }
       return 1;
     }
-    else 
+    else
       if(ed_line_in->flg&FL_ELB_ECHO) {
         while(cmd_io_putc(io_stack,ch)<0);
       }
@@ -113,15 +115,17 @@ int cmd_io_line_in(cmd_io_t *cmd_io)
   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: */