1 /*******************************************************************
2 Components for embedded applications builded for
3 laboratory and medical instruments firmware
5 cmd_proc.h - text line command processor
6 designed for instruments control and setup
9 Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
10 (C) 2002 by PiKRON Ltd. http://www.pikron.com
11 (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
13 This file can be used and copied according to next
15 - MPL - Mozilla Public License
16 - GPL - GNU Public License
17 - other license provided by project originators
18 *******************************************************************/
21 #include "cmd_proc_priv.h"
24 /* cmd_io line editor */
27 * Adds new characters to an edit line buffer.
29 * @param elb Edit line buffer.
30 * @param ch character to add.
32 * @return 1 in case of end of line, -1 if called at inaproprate time, 0 otherwise.
34 int cmd_ed_line_buf(ed_line_buf_t *elb, int ch)
36 int lastch=elb->lastch;
37 if (elb->flg&FL_ELB_INSEND)
41 elb->inbuf=0; /* Start new line */
43 if((!(elb->flg&FL_ELB_NOCRLF))&&((ch=='\n')||(ch=='\r'))){
44 if((lastch=='\n')&&(ch=='\r')) /* Empty line, ignore it. */
46 elb->lastch=0; /* End the string */
47 elb->buf[elb->inbuf]=0;
50 if(elb->inbuf>=elb->alloc-1){
51 /* try to reallocate buffer len not implemented */
54 elb->buf[elb->inbuf++]=ch;
58 int cmd_io_line_putc(cmd_io_t *cmd_io,int ch)
60 return cmd_ed_line_buf(cmd_io->priv.ed_line.out,ch);
63 /* Process pending output */
64 int cmd_io_line_out(cmd_io_t *cmd_io)
66 cmd_io_t* io_stack=cmd_io->priv.ed_line.io_stack;
67 ed_line_buf_t* ed_line_out=cmd_io->priv.ed_line.out;
69 if(!ed_line_out->inbuf) return 0;
73 if(!(ed_line_out->flg&FL_ELB_INSEND)){
74 ed_line_out->flg|=FL_ELB_INSEND;
75 ed_line_out->lastch=0;
77 while(cmd_io_putc(io_stack, ed_line_out->buf[ed_line_out->lastch])>=0){
78 if(++ed_line_out->lastch >= ed_line_out->inbuf){
79 ed_line_out->lastch=0;
81 ed_line_out->flg&=~FL_ELB_INSEND;
89 int cmd_io_line_in(cmd_io_t *cmd_io)
92 cmd_io_t* io_stack = cmd_io->priv.ed_line.io_stack;
93 ed_line_buf_t *ed_line_in = cmd_io->priv.ed_line.in;
98 while((ch=cmd_io_getc(io_stack))>=0){
99 // DPRINT("Added %c (%d)\n", ch, ch);
100 int eol = cmd_ed_line_buf(ed_line_in,ch);
104 if(ed_line_in->flg&FL_ELB_ECHO){
105 while(cmd_io_putc(io_stack,'\r')<0);
106 while(cmd_io_putc(io_stack,'\n')<0);
111 if(ed_line_in->flg&FL_ELB_ECHO) {
112 while(cmd_io_putc(io_stack,ch)<0);
118 /* The possibly blocking read of one line, should be used only
119 when other options fails */
120 char *cmd_io_line_rdline(cmd_io_t *cmd_io, int mode)
123 while((ret=cmd_io_line_in(cmd_io))==0)
125 if(ret<=0) return NULL;
126 return cmd_io->priv.ed_line.in->buf;
129 /* Local Variables: */
130 /* c-basic-offset: 2 */