]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control-pxmc.git/blob - src/libs4c/cmdproc/cmd_io_line.c
Raspberry Pi 3-phase motor control educational system based on PXMC.
[fpga/rpi-motor-control-pxmc.git] / src / libs4c / cmdproc / cmd_io_line.c
1 /*******************************************************************
2   Components for embedded applications builded for
3   laboratory and medical instruments firmware  
4  
5   cmd_proc.h - text line command processor
6                designed for instruments control and setup
7                over RS-232 line
8  
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>
12
13   This file can be used and copied according to next
14   license alternatives
15    - MPL - Mozilla Public License
16    - GPL - GNU Public License
17    - other license provided by project originators
18  *******************************************************************/
19
20 #include <cmd_proc.h>
21 #include "cmd_proc_priv.h"
22 #include <string.h>
23
24 /* cmd_io line editor */
25
26 /** 
27  * Adds new characters to an edit line buffer.
28  * 
29  * @param elb Edit line buffer.
30  * @param ch character to add.
31  * 
32  * @return 1 in case of end of line, 0 otherwise.
33  */
34 int cmd_ed_line_buf(ed_line_buf_t *elb, int ch)
35 {
36   int lastch=elb->lastch;
37   if (elb->flg&FL_ELB_INSEND)
38     return -1;
39   elb->lastch=ch;
40   if(!lastch){
41     elb->inbuf=0;               /* Start new line */
42   }
43   if((!(elb->flg&FL_ELB_NOCRLF))&&((ch=='\n')||(ch=='\r'))){
44     if((lastch=='\n')&&(ch=='\r')) /* Empty line, ignore it. */
45       return 0;
46     elb->lastch=0;               /* End the string */
47     elb->buf[elb->inbuf]=0;
48     return 1;
49   }
50   if(elb->inbuf>=elb->alloc-1){ 
51     /* try to reallocate buffer len not implemented */
52     return 0;
53   }
54   elb->buf[elb->inbuf++]=ch;  
55   return 0;
56 }
57
58 int cmd_io_line_putc(cmd_io_t *cmd_io,int ch)
59 {
60   return cmd_ed_line_buf(cmd_io->priv.ed_line.out,ch);
61 }
62
63 /* Process pending output */
64 int cmd_io_line_out(cmd_io_t *cmd_io)
65 {
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;
68   
69   if(!ed_line_out->inbuf) return 0;
70   if(!io_stack)
71     return -1;
72   
73   if(!(ed_line_out->flg&FL_ELB_INSEND)){
74     ed_line_out->flg|=FL_ELB_INSEND;
75     ed_line_out->lastch=0;
76   }
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;
80       ed_line_out->inbuf=0;
81       ed_line_out->flg&=~FL_ELB_INSEND;
82       return 0;
83     }
84   }
85   return 1;
86 }
87
88 /* process input */
89 int cmd_io_line_in(cmd_io_t *cmd_io)
90 {
91   int ch;
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;
94
95   if(!io_stack)
96       return -1;
97
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);
101     if(eol){
102       if(ed_line_in->flg&FL_ELB_ECHO){
103         while(cmd_io_putc(io_stack,'\r')<0);
104         while(cmd_io_putc(io_stack,'\n')<0);
105       }
106       return 1;
107     }
108     else 
109       if(ed_line_in->flg&FL_ELB_ECHO) {
110         while(cmd_io_putc(io_stack,ch)<0);
111       }
112   }
113   return 0;
114 }
115
116 /* The possibly blocking read of one line, should be used only
117    when other options fails */
118 char *cmd_io_line_rdline(cmd_io_t *cmd_io, int mode)
119 {
120   int ret;
121   while((ret=cmd_io_line_in(cmd_io))==0)
122     if(!mode) break;
123   if(ret<=0) return NULL;
124   return cmd_io->priv.ed_line.in->buf;
125 }
126
127 /* Local Variables: */
128 /* c-basic-offset: 2 */
129 /* End */