-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
435 lines (359 loc) · 11.3 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <linux/limits.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <texted/edit.h>
#include <texted/print.h>
#include <texted/insert.h>
#include <texted/fileio.h>
#include <texted/texted.h>
#include <credits/credits.h>
// max digit for 64bit value
#define DIGITS64 25
// extra prompt size
#define PROMPT_EXTRA 30
// max size of prompt
#define PROMPT_MAX (PATH_MAX + USER_PERMISSION_COLOR_SIZE + DIGITS64 + PROMPT_EXTRA)
int main(int argc, char* argv[])
{
char* Buffer = NULL; // Continuous buffer
char* Prompt = NULL; // Prompt
char* Filename; // Name of open file
LineBuffer_s* LineBuffer; // Array of lines
size_t Line = 1; // Selected row
commans_s Command; // Command
size_t counter; // Global counter
int status = 0; // Return status
usr_perm_e permissions; // Operations we can perform on this file
// LOADING
if (argc <= 1) {
Filename = strdup("a.txt");
} else if(streq(argv[1], "--help", 7) || streq(argv[1], "-h", 3)) {
display_help();
return 0;
} else if(streq(argv[1], "-v", 3) || streq(argv[1], "--version", 10) ) {
fputs(BOLD RED " / \\--------------, \n"
RED " \\_,| | \t" BLUE "TextEd\n"
RED " | TextEd | \t" BLUE "Version: " VERSION "\n"
RED " | ,------------\n"
RED " \\_/___________/\n" RESET, stdout);
return 0;
} else if(streq(argv[1], "--credits", 10)) {
return credits();
} else {
Filename = path_resolve(argv[1]);
}
// Try to create file
permissions = get_user_permissions(Filename);
if(permissions == ERR_PERM &&
(status = createFile(Filename, permissions)))
{
return status;
}
if(!(permissions & RD_PERM)) {
fputs(RED "Failed to read the file: Permission denied!\n" RESET, stderr);
return -1;
}
// Load the file in the LineBuffer
LineBuffer = lbLoadFile(Filename);
if(errno)
perror( RED "Something went wrong when trying to read the file!" RESET );
// Initialize command handler
Command.args = calloc(ARGS_NUM, sizeof(char*));
//Initialize Prompt
Prompt = malloc(PROMPT_MAX);
fputs(BOLD YELLOW"Welcome in Texted - " RELEASE RESET"\n", stdout);
// MAIN LOOP
for(;;)
{
snprintf(Prompt, PROMPT_MAX, BOLD "%s%s %lu> "RESET,
get_temp() ? get_user_permission_color(TMP_PATH) : get_user_permission_color(Filename),
Filename,
Line
);
Command.raw_command = readline(Prompt);
if(Command.raw_command && *Command.raw_command)
add_history(Command.raw_command);
else
continue;
Command.command = Command.raw_command[0];
switch (Command.command)
{
case 'p': // PRINT MODE
// Read arguments
Command.args[0] = strdup(Command.raw_command + 1);
// Interpet arguments
if (streq(Command.args[0], "", 1)) {
ed_print(LineBuffer, 0);
} else if (streq(Command.args[0], "n", 2)) {
ed_print(LineBuffer, 1);
} else if (streq(Command.args[0], "l", 2)) {
fputs(getLine(LineBuffer, Line), stdout);
// Newline coherence
if(LineBuffer && Line != LineBuffer->LB_Size)
goto exit_print;
} else if (streq(Command.args[0], "ln", 3)) {
if(LineBuffer)
printf("%lu %s", Line, getLine(LineBuffer, Line));
// Newline coherence
if(LineBuffer && Line != LineBuffer->LB_Size)
goto exit_print;
} else if(Command.args[0][0] == 'l') {
// Print a line without changing the current line
// "counter" as temporary variable
counter = strtoul(Command.args[0] + 1, NULL, 10);
if(!(counter != 0 && counter < ULONG_MAX && LineBuffer && counter <= LineBuffer->LB_Size)) {
fprintf(stderr, RED"Wrong line number\n"RESET);
goto exit_print;
}
if(LineBuffer)
printf("%lu %s", counter, getLine(LineBuffer, counter));
// Newline coherence
if(LineBuffer && counter != LineBuffer->LB_Size)
goto exit_print;
} else if(streq(Command.args[0], " -p", 4)) {
ed_print_permissions(Filename);
goto exit_print;
} else if(streq(Command.args[0], "s", 2)) {
if(!(LineBuffer && *(LineBuffer->LineBuffer)[0]))
goto exit_print;
if(get_temp()) {
Buffer = getBuffer(LineBuffer);
if(!Buffer)
goto exit_print;
if(save(TMP_PATH, Buffer)) {
free(Buffer);
goto exit_print;
} else {
free(Buffer);
}
}
ed_print_highlight(Filename);
goto exit_print;
} else {
fprintf(stderr, RED "Wrong syntax for the print command\n" RESET);
goto exit_print;
}
if((LineBuffer) && (LineBuffer->LineBuffer))
putchar('\n');
exit_print:
free(Command.args[0]);
break;
case 'i': // INSERT MODE
// Permission handling
if(!(permissions & WR_PERM)) {
fputs(RED "You don't have write permissions on this file!\n" RESET, stderr);
break;
}
Command.args[0] = strdup(Command.raw_command + 1);
if (streq(Command.args[0], "", 1)) // Write in RAM
{
// Load Buffer
fputs("--INSERT MODE--\n", stdout);
Buffer = insert();
// Make LineBuffer from Buffer
if(!LineBuffer)
LineBuffer = getLineBuffer(Buffer);
else
LineBuffer = concatenateBuffer(LineBuffer, Buffer);
PAUSE();
}
else if (streq(Command.args[0], "w", 2)) // Write directly to file
{
// Load Buffer
fputs("--INSERT MODE--\n", stdout);
Buffer = insert();
// Append to file
app_save(Filename, Buffer);
printf("Added %lu bytes\n", strlen(Buffer));
freeLineBuffer(LineBuffer);
free(Buffer);
Buffer = NULL;
// Reload LineBuffer
LineBuffer = lbLoadFile(Filename);
if(errno)
perror( RED "Something went wrong when trying to read the file!" RESET );
PAUSE();
}
else
{
Buffer = NULL;
fputs(RED"Wrong syntax for the insert (i) command\n"RESET, stderr);
}
if(Buffer) {
free(Buffer);
Buffer = NULL;
}
free(Command.args[0]);
break;
case 'w': // SAVE
case 'x': // SAVE AND EXIT
// Permission handling
if(!(permissions & WR_PERM)) {
fputs(RED "You don't have write permissions on this file!\n" RESET, stderr);
break;
}
Buffer = getBuffer(LineBuffer);
status = save(Filename, Buffer);
if(status == ED_NULL_FILE_PTR || status == ED_NULL_PTR) {
if(errno)
perror(RED"Failed to write to the file"RESET);
else
fputs(RED"Failed to write to the file\n"RESET, stderr);
if(Buffer)
free(Buffer);
Buffer = NULL;
break;
} else {
printf("Written %lu bytes\n", Buffer ? strlen(Buffer) : 0U);
if(Buffer)
free(Buffer);
Buffer = NULL;
if (Command.command == 'x')
goto loop_exit;
}
break;
case 's': // SUBSTITUTE WORD
case 'm': // ADD WORD AFTER
// Permission handling
if(!(permissions & WR_PERM)) {
fputs(RED "You don't have write permissions on this file!\n" RESET, stderr);
break;
}
// Handle NULL LineBuffer
if(!LineBuffer){
fputs(RED "File is empty!\n" RESET, stderr);
break;
}
// Read arguments
status = argumentParser(Command.raw_command + 1, 2, &(Command.args));
// Error handling
if(status == ED_ERRNO) {
perror(RED"Failed to read arguments"RESET);
Command.command = '\0';
} else if(status) {
fprintf(stderr, RED"Wrong syntax for the %s command\n"RESET,
Command.command == 's' ? "substitute (s)" : "embed (m)");
Command.command = '\0';
}
if (Command.command == 's') {
if(substitute(getLinePtr(LineBuffer, Line), Command.args[0], Command.args[1]))
fprintf(stderr, RED "Failed to substitute\n" RESET);
} else if (Command.command == 'm') {
if(putstr(getLinePtr(LineBuffer, Line), Command.args[0], Command.args[1]))
fprintf(stderr, RED "Failed to embed new token\n" RESET);
}
break;
case 'a': // ADD WORD AT LINE END
// Permission handling
if(!(permissions & WR_PERM)) {
fputs(RED "You don't have write permissions on this file!\n" RESET, stderr);
break;
}
Command.args[1] = NULL;
// Read and interpret argument
status = argumentParser(Command.raw_command + 1, 1, &(Command.args));
// Error Handling
if(status == ED_ERRNO) {
perror(RED "Failed to add new word at line end");
break;
} else if(status) {
fprintf(stderr, RED"Wrong syntax for the append (a) command\n"RESET);
break;
}
if(putstr(getLinePtr(LineBuffer, Line), ADD_MODE, Command.args[0]))
fputs(RED "Failed to append string\n" RESET, stderr);
break;
case 'l': // SET LINE
Command.args[0] = strdup(Command.raw_command + 1);
// "counter" as temporary variable
counter = strtoul(Command.args[0], NULL, 10);
if (counter != 0 && counter < ULONG_MAX && LineBuffer && counter <= LineBuffer->LB_Size)
Line = counter;
else
fprintf(stderr, RED"Wrong line number\n"RESET);
free(Command.args[0]);
break;
case 'q': // EXIT
goto loop_exit;
break;
case 'b': // GET BACKUP
if(!backup(Filename))
printf(ITALIC CYAN "Backup file generated: %s\n" RESET, genBackupName(Filename));
else
fprintf(stderr, RED "Failed to create a backup of %s: No such file or directory\n", Filename);
break;
case 'h': // PRINT HELP
if(!Command.raw_command[1])
display_help();
else
fputs(RED "Wrong syntax for the help (h) command.\n"
"Type h for help\n" RESET, stderr);
break;
case 'n': // NEW LINE
// Permission handling
if(!(permissions & WR_PERM)) {
fputs(RED "You don't have write permissions on this file!\n" RESET, stderr);
break;
}
Command.args[1] = NULL;
// Read and interpret argument
status = argumentParser(Command.raw_command + 1, 1, &(Command.args));
// Error Handling
if(status == ED_ERRNO) {
perror(RED "Failed to add new word at line end");
break;
} else if(status) {
fprintf(stderr, RED"Wrong syntax for the new line (n) command\n"RESET);
break;
}
// Add new line
if((status = addLine(&LineBuffer, Command.args[0], Line)))
fprintf(stderr, RED"An error occured while trying to add a new line\n"
ITALIC "Error code: %d\n"RESET, status);
break;
case 'd': // DELETE LINE
// Permission handling
if(!(permissions & WR_PERM)) {
fputs(RED "You don't have write permissions on this file!\n" RESET, stderr);
break;
}
if(delLine(&LineBuffer, Line))
fprintf(stderr, RED"An error occured while trying to remove line no. %lu\n"
"Error code: %d\n"RESET, Line, status);
else {
// branchless if(Line != 1) Line--;
Line -= (Line > 1);
printf(CYAN ITALIC "New working line set to %lu\n" RESET, Line);
}
break;
case '!':
if((status = system(Command.raw_command + 1)))
fprintf(stderr, ITALIC CYAN "Returned %d\n" RESET, status);
status = 0;
break;
default:
if(Command.command)
fprintf(stderr, RED "Invalid command\n" RESET);
break;
}
free(Command.raw_command); // Jumped when exiting with q
}
loop_exit:
free(Filename);
free(Command.raw_command); // Free the command
remove(TMP_PATH); // Remove the temporary file
free(Prompt); // Free the prompt
rl_clear_history(); // Clean extra info written to the history by readline
clear_history(); // Clear the history of readline
caller_user_info_free(); // Free the informations about the user needed for permission checks
free(Command.args); // Free the array of arguments
freeLineBuffer(LineBuffer); // Free the elements of LineBuffer
free(LineBuffer); // Free LineBuffer
return 0;
}