-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.c
220 lines (205 loc) · 5.45 KB
/
library.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include "tm4c123gh6pm.h"
#include "clock.h"
#include "gpio.h"
#include "wait.h"
#include "library.h"
void getsUart0(USER_DATA* data)
{
uint8_t count = 0;
while(count <= MAX_CHARS)
{
if(count == MAX_CHARS)
{
data->buffer[count+1] = '\0';
}
char input = getcUart0();
if(input >= 32 && input != 127)
{
data->buffer[count] = input;
count++;
}
if((input == 8 || input == 127) && count > 0)
{
count--;
}
if(input == 13)
{
data->buffer[count] = '\0';
return;
}
}
return;
}
int isAlpha(char c)
{
if((c > 57 && c < 127) || (c > 32 && c < 48))
return 1;
return 0;
}
int isNum(char c)
{
if(c > 47 && c < 57)
return 1;
return 0;
}
void parseFields(USER_DATA* data)
{
//loop until null terminator
//bool firstSeen Var = true when buffer[i] != num | alpha
uint8_t i = 0;
int seen = 0;
data->fieldCount = 0;
while(data->buffer[i] != '\0') //read until null terminator. Don't read the whole buffer
{
if(isAlpha(data->buffer[i]) || isNum(data->buffer[i]))
{
if(seen == 0) //new word or number
{
data->fieldPosition[data->fieldCount] = i;
if(isNum(data->buffer[i]))
{
data->fieldType[data->fieldCount] = 'n';
}
else
{
data->fieldType[data->fieldCount] = 'a';
}
data->fieldCount++;
}
seen = 1;
}
else
{
seen = 0;
data->buffer[i] = '\0';
}
if(data->fieldCount == MAX_FIELDS)
return;
i++;
}
}
bool stringsEqual(char *str1, char *str2) {
uint8_t str1Length = 0;
uint8_t str2Length = 0;
//kill
//str1Length = 0,1,2,3,4
while(str1[str1Length] != '\0')
str1Length++;
while(str2[str2Length] != '\0')
str2Length++;
if(str1Length != str2Length)
return false;
uint8_t i = 0;
for(i = 0; i < str1Length; i++)
{
if(str1[i] != str2[i])
return false;
}
return true;
}
bool isCommand(USER_DATA* data, const char strCommand[], uint8_t minArguments)
{
uint8_t i = 0;
char * temp = &data->buffer[data->fieldPosition[0]];
for(i = 0; temp[i] != '\0'; i++)
{
if(strCommand[i] != temp[i])
return false;
}
if(data->fieldCount <= minArguments)
return false;
return true;
}
bool isValidCommand(USER_DATA* data)
{
char commands[][10] = {"reboot", "ps", "ipcs", "kill", "pi", "preempt", "sched", "pidof"};
char * argumentOne = &data->buffer[data->fieldPosition[0]];
uint8_t argumentNumber = data->fieldCount;
uint8_t i = 0;
for(i = 0; i < 9; i++) {
if(stringsEqual(commands[i], argumentOne))
return true;
}
if(argumentNumber == 2) {
char * argumentTwo = &data->buffer[data->fieldPosition[1]];
if(stringsEqual(argumentTwo, "&"))
return true;
}
return false;
}
char* getFieldString(USER_DATA* data, uint8_t fieldNumber)
{
if(data->fieldType[fieldNumber] == 'a')
return &data->buffer[data->fieldPosition[fieldNumber]];
return 0;
}
int32_t getFieldInteger(USER_DATA* data, uint8_t fieldNumber)
{
if(data->fieldType[fieldNumber] == 'n')
{
char * temp = &data->buffer[data->fieldPosition[fieldNumber]];
int32_t sum = 0;
int i = 0;
while(temp[i] != '\0')
{
int32_t currentInteger = temp[i] - 48;
sum = (sum * 10) + currentInteger;
i++;
}
return sum;
}
return 0;
}
int myAtoi(char s[])
{
//discard all white spaces: Loop over the string until current character is not space
//go to the next char & check if the character is a negative, positive or a number.
//create an int sign = 1. if the current char== neg -> sign = -1 and incr iterator; if current char==pos -> sign = 1 and incr iterator. if s[0] is not an int or a sign, return 0;
//loop over the string while s[i] is a number, break if the current sum> INT_MAX or sum < INT_MIN.
// putsUart0(&s);
// putsUart0("\n");
// putcUart0('\n');
// char s[] = data->buffer;
// putcUart0(s[0]);
// putcUart0(s[1]);
char temp[50];
int i = 0;
int sign = 1;
long sum = 0;
while(s[i] == ' ') {
i++;
}
if(s[i] == '-'|| s[i] =='+'|| (s[i] > 47 && s[i] < 58)) {
if(s[i] == '-') {
sign = -1;
i++;
putsUart0("neg\n");
} else if(s[i] == '+') {
sign = 1;
i++;
}
} else {
return 0;
}
// putcUart0(s[1]);
while(s[i] > 47 && s[i] < 58) {
int currentInteger = s[i] - 48;
sum = (sum * 10) + currentInteger;
// sprintf(temp, "%d",sum);
// putsUart0(s);
// if(sum > 2147483647)
// break;
// else if (sum < -2147483648)
// break;
i++;
}
sum = sum * sign;
// if(sum > 2147483647)
// return 2147483647;
// else if (sum < -2147483648)
// return -2147483648;
return sum;
}