-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
152 lines (131 loc) · 3.92 KB
/
input.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
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "basics.h"
#include "builtin.h"
#include "prompt.h"
#include "lash.h"
#include "history.h"
#include "lib/string/strlib.h"
/*
* brief Read a line of input from stdin.
* return The line from stdin.
*/
char *lash_read_line() {
int bufsize = LASH_RL_BUFSIZE, c;
int position = 0;
char *buffer = malloc(sizeof(char) * bufsize);
// If we hit EOF, replace it with a null character and return.
while (TRUE) {
c = getchar();
if (c == EOF || c == EOL) {
buffer[position++] = '\0';
// add to history
add_to_history(buffer,20);
return buffer;
} else
buffer[position++] = c;
}
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += LASH_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
}
}
/*
* brief Split a line into tokens (very naively).
* param line The line.
* return Null-terminated array of tokens.
*/
char **lash_split_line(char *line,int *count) {
int bufsize = LASH_TOK_BUFSIZE, position = 0;
char **tokens = malloc(bufsize * sizeof(char *));
char *token;
char *special;
int specialcharindex;
token = strtok(line,LASH_TOK_DELIM);
/*
* if token has special char
* will find the index of special char
* if that was at the begining of the token will add special to our list
* if that was and the end of token , will remove that from the end and add both token and special char
* and if that was in middle , will make token to 2 different pieces and both to our tokends
*/
while (token != NULL) {
special = find_special_char(token);
if (special != NULL) {
specialcharindex = indexof(token,special[0]);
if (specialcharindex == -1) {
tokens[position++] = token;
}
else if (specialcharindex == 0) {
tokens[position++] = special;
token++;
} else if(specialcharindex == strlen(token) - 1) {
token[strlen(token)-1] = '\0';
tokens[position++] = token;
tokens[position++] = special;
}
else {
char* firsthalf = malloc(LASH_TOK_BUFSIZE * sizeof(char*));
char* secondhalf = malloc(LASH_TOK_BUFSIZE* sizeof(char*));
strcpy(firsthalf,token);
strcpy(secondhalf,token);
// Get First Half
firsthalf[specialcharindex] = '\0';
tokens[position++] = firsthalf;
// Add in special
tokens[position++] = special;
// Get Second Half
secondhalf = strstr(secondhalf,special);
secondhalf++;
tokens[position++] = secondhalf;
token = secondhalf;
}
}
else{
tokens[position++] = token;
}
token = strtok(NULL,LASH_TOK_DELIM);
if (position >= bufsize) {
bufsize += LASH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char *));
}
}
tokens[position] = NULL;
*count = position;
return tokens;
}
/*
* Utility Function
* brief find special chracter in line like > < | and return that
*/
char *find_special_char(char *line) {
while (*line++) {
switch (*line) {
case '>':
return ">\0";
case '<':
return "<\0";
case '|':
return "|\0";
case ';':
return ";\0";
}
}
return NULL;
}
bool isspecial(char *c) {
switch (*c) {
case '>':
case '<':
case '|':
case ';':
return TRUE;
default:
return FALSE;
}
}