changing circuitry to disable RTC, update initialization to match
[fw/openalt] / monitor / args.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <ctype.h>
6
7 //
8 // Scheduler includes
9 //
10 #include "FreeRTOS.h"
11 #include "args.h"
12
13 //
14 //
15 //
16 static char *strtrim (char *s)
17 {
18   char *t = s + strlen (s) - 1;
19
20   while (t >= s && *t && isspace (*t))
21     *t-- = '\0';
22
23   while (*s && isspace (*s))
24     s++;
25
26   return s;
27 }
28
29 //
30 //  bufferLength includes the space reserved for the \0
31 //
32 int argsGetLine (int fd __attribute__ ((unused)), U8 *buffer, int bufferLength)
33 {
34   U8 *p;
35
36   p = buffer;
37   *p = '\0';
38
39   while (1)
40   {
41     U8 c;
42     
43     fflush (stdout);
44
45     if (read (fd, &c, sizeof (c)) == sizeof (c))
46     {
47       switch (c)
48       {
49         case '\n' :
50         case '\r' :
51           printf ("\n");
52           return strlen ((char *) buffer);
53
54         case '\b' :
55           if (p > buffer)
56             *--p = '\0';
57           printf ("\b \b");
58           break;
59
60         case 0x15 : // CTRL-U
61           while (p != buffer)
62           {
63             printf ("\b \b");
64             --p;
65           }
66           *p = '\0';
67           break;
68
69         case 0xfe :
70         case 0xff :
71           *buffer++ = c;
72           *buffer = '\0';
73           return 1;
74
75         default : 
76           if (p < buffer + bufferLength - 1 && c >= ' ' && c < 0x7f)
77           { 
78             *p++ = c;
79             *p = '\0';
80             printf ("%c", c); 
81           }
82           else
83             printf ("%c", c); 
84
85           break;
86       }
87     }
88   }
89
90   return 0;
91 }
92
93 //
94 //
95 //
96 typedef enum
97 {
98   P_EATWHITESPACE = 0,
99   P_GETCHARFIRST,
100   P_GETCHAR,
101   P_QUOTEDGETCHAR
102 }
103 PSTATE;
104
105 int argsParse (char *cmd, char **argv, int sizeofArgv, int *argc)
106 {
107   int maxArgs = (sizeofArgv / sizeof (argv [0])) - 1;
108   char *s = strtrim (cmd);
109   PSTATE pstate = P_EATWHITESPACE;
110
111   *argc = 0;
112   memset (argv, 0, sizeofArgv);
113
114   while (*s)
115   {
116     switch (pstate)
117     {
118       case P_EATWHITESPACE :
119         {
120           if (!isspace (*s))
121             pstate = P_GETCHARFIRST;
122           else
123             s++;
124         }
125         break;
126
127       case P_GETCHARFIRST :
128         {
129           *argv++ = s;
130
131           if (++*argc == maxArgs)
132             return 1;
133           if (*s == '"')
134             pstate = P_QUOTEDGETCHAR;
135           else
136             pstate = P_GETCHAR;
137
138           s++;
139         }
140         break;
141
142       case P_GETCHAR :
143         {
144           if (isspace (*s)) {
145             pstate = P_EATWHITESPACE;
146             *s = '\0';
147           } 
148           else if (*s == '"')
149             pstate = P_QUOTEDGETCHAR;
150
151           s++;
152         }
153         break;
154
155       case P_QUOTEDGETCHAR :
156         {
157           if (*s == '"')
158             pstate = P_GETCHAR;
159
160           s++;
161         }
162         break;
163     }
164   }
165
166   return 0;
167 }