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(ch == '\b') { /* backspace */
48 if((!(elb->flg&FL_ELB_NOCRLF))&&((ch=='\n')||(ch=='\r'))){
49 if((lastch=='\n')&&(ch=='\r')) /* Empty line, ignore it. */
51 elb->lastch=0; /* End the string */
52 elb->buf[elb->inbuf]=0;
55 if(elb->inbuf>=elb->alloc-1){
56 /* try to reallocate buffer len not implemented */
59 elb->buf[elb->inbuf++]=ch;
63 int cmd_io_line_putc(cmd_io_t *cmd_io,int ch)
65 return cmd_ed_line_buf(cmd_io->priv.ed_line.out,ch);
68 /* Process pending output */
69 int cmd_io_line_out(cmd_io_t *cmd_io)
71 cmd_io_t* io_stack=cmd_io->priv.ed_line.io_stack;
72 ed_line_buf_t* ed_line_out=cmd_io->priv.ed_line.out;
74 if(!ed_line_out->inbuf) return 0;
78 if(!(ed_line_out->flg&FL_ELB_INSEND)){
79 ed_line_out->flg|=FL_ELB_INSEND;
80 ed_line_out->lastch=0;
82 while(cmd_io_putc(io_stack, ed_line_out->buf[ed_line_out->lastch])>=0){
83 if(++ed_line_out->lastch >= ed_line_out->inbuf){
84 ed_line_out->lastch=0;
86 ed_line_out->flg&=~FL_ELB_INSEND;
93 /* process input & perform echo if requested */
94 int cmd_io_line_in(cmd_io_t *cmd_io)
97 cmd_io_t* io_stack = cmd_io->priv.ed_line.io_stack;
98 ed_line_buf_t *el = cmd_io->priv.ed_line.in;
103 while((ch=cmd_io_getc(io_stack))>=0){
104 // DPRINT("Added %c (%d)\n", ch, ch);
105 int eol = cmd_ed_line_buf(el,ch);
109 if(el->flg&FL_ELB_ECHO){
110 while(cmd_io_putc(io_stack,'\r')<0);
111 while(cmd_io_putc(io_stack,'\n')<0);
116 if(el->flg&FL_ELB_ECHO) {
117 while(cmd_io_putc(io_stack,ch)<0);
119 cmd_io_puts(io_stack, "\033[K"); /* Erase End of Line */
125 /* The possibly blocking read of one line, should be used only
126 when other options fails */
127 char *cmd_io_line_rdline(cmd_io_t *cmd_io, int mode)
130 while((ret=cmd_io_line_in(cmd_io))==0)
132 if(ret<=0) return NULL;
133 return cmd_io->priv.ed_line.in->buf;
136 /* Local Variables: */
137 /* c-basic-offset: 2 */