-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
186 lines (147 loc) · 5.17 KB
/
main.c
File metadata and controls
186 lines (147 loc) · 5.17 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define red "\e[31m"
#define green "\e[32m"
#define yellow "\e[33m"
#define blue "\e[34m"
#define gray "\e[37m"
#define italic "\e[3m"
#define reset "\e[0m"
int runFunction(int index, int cap, char *seperator_name) {
// Read each line of ubf until at index
FILE *ubf = fopen("ubuild", "r");
printf("%s%sRunning Seperator '%s'%s\n\n", gray, italic, seperator_name, reset);
char *subl = NULL;
size_t len = 0;
ssize_t read;
int infunc = 0;
int curline = 0;
// Multiply " " by cap into char pointer
char *space = malloc(sizeof(char) * cap);
memset(space, ' ', cap);
// Start line loop
while ((read = getline(&subl, &len, ubf)) != -1) {
subl[strcspn(subl, "\n")];
char line[1000];
// Set line to the current line
strcpy(line, subl);
line[strcspn(line, "\n")] = 0;
curline++;
for (int i = 0; i < strlen(line); i++) {
if (line[i] == '\t') {
// Set temp char array to %s%s, space, line
char *old = line;
char *temp = malloc(sizeof(char) * (strlen(line) + strlen(space) + 1));
sprintf(temp, "%s%s", space, line + i + 1);
// Set line array to temp
strcpy(line, temp);
for (int j = 0; j < strlen(line); j++) {
if(line[i] == '\t') {
printf("Fatal: Failed to copy tab spaces.\n");
return -1;
}
}
}
}
if (!(curline >= (index + 2)) || strlen(line) == 0) {
continue;
}
char *operation = strndup(line + cap, strlen(line));
if (strcmp(strndup(line, 3), "sep") != 0) {
printf("%s%s%s%s\n", gray, italic, operation, reset);
} else {
printf("%sSeperator Ended.%s\n", yellow, reset);
}
if (strcmp(strndup(line, cap), space) != 0 || strcmp(strndup(line + cap, 1), " ") == 0) {
if(strcmp(strndup(line + cap, strlen(line)), seperator_name) != 0) {
return 0;
} else {
continue;
}
}
system(operation);
}
}
int main(int argc, char **argv) {
FILE *ubf = fopen("ubuild", "r");
if (ubf == NULL) {
printf("Error: ubuild file not found\n");
return 1;
}
char *subl = NULL;
size_t len = 0;
ssize_t read;
int infunc = 0;
int curline = 0;
int chose_first = 0;
int cap = 4;
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-c") == 0) {
if (argc > (i + 1)) {
cap = atoi(argv[i + 1]);
printf("Cap: %d\n", cap);
} else {
printf("Error: -c requires an argument\n");
return 1;
}
}
}
while ((read = getline(&subl, &len, ubf)) != -1) {
subl[strcspn(subl, "\n")];
char line[1000];
// Set line to the current line
strcpy(line, subl);
line[strcspn(line, "\n")] = 0;
// Check if no seperator argument was defined, then use the first option.
char *seperator = strndup(line, 3);
char *seperator_name = strndup(line + 4, strlen(line));
char *space = malloc(sizeof(char) * cap);
memset(space, ' ', cap);
if (strlen(line) == 0)
continue;
for (int i = 0; i < strlen(line); i++) {
if (line[i] == '\t') {
// Set temp char array to %s%s, space, line
char *old = line;
char *temp = malloc(sizeof(char) * (strlen(line) + strlen(space) + 1));
sprintf(temp, "%s%s", space, line + i + 1);
// Set line array to temp
strcpy(line, temp);
for (int j = 0; j < strlen(line); j++) {
if(line[i] == '\t') {
printf("Fatal: Failed to copy tab spaces.\n");
return -1;
}
}
}
}
if (argc < 2 && strcmp(seperator, "sep") == 0 && chose_first == 0) {
runFunction(curline, cap, seperator_name);
chose_first = 1;
infunc = 1;
} else if (strcmp(seperator, "sep") == 0) {
if (!chose_first == 1) {
for (int b = 1; b < argc; b++) {
if (strcmp(seperator_name, argv[b]) == 0) {
infunc = 1;
runFunction(curline, cap, seperator_name);
}
}
}
} else if (strcmp(strndup(line, cap), space) != 0) {
printf("%sInvalid function at: ..%s%s\n", red, line, reset);
printf("%s ^ %s\n", green, reset);
printf("\nUBuild syntax error at line %d\n", curline);
return -1;
}
curline++;
}
if (infunc == 0 && argc >= 2) {
printf("Error: Invalid seperator defined.\n");
return 1;
} else if (infunc == 0) {
printf("Error: No seperators found.\n");
return 1;
}
}