-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
732 lines (625 loc) · 20.7 KB
/
shell.c
File metadata and controls
732 lines (625 loc) · 20.7 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "shell.h"
static const char *colorName[] = {
"black",
"red",
"green",
"yellow",
"blue",
"purple",
"cyan",
"white",
};
static const char *possible_options[] = {
"root_color",
"user_color",
"display_path",
"path_color",
"display_hostname",
"hostname_color"
};
static short parse_user_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50]);
static short parse_root_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50]);
static short parse_display_path(SHELL_CONF *conf, FILE *fp, char key_buffer[50]);
static short parse_path_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50]);
static short parse_display_hostname(SHELL_CONF *conf, FILE *fp, char key_buffer[50]);
static short parse_hostname_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50]);
static char **getArgs(char **args);
static short parse_user_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50])
{
char value_buffer[50];
int i = 0;
int c;
short flag = 0; // know if a valid value has been found for the "user_color" key.
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
if (i < 49)
value_buffer[i++] = c;
}
value_buffer[i] = 0;
size_t chris = sizeof(colorName) / 8;
for (i = 0; i < chris; i++)
{
if (strcmp(value_buffer, colorName[i]) == 0)
{
conf->user_color = i;
flag = 1;
}
}
if (flag == 0) // If no value has been found then we warn the user that default settings are applied.
{
if (conf->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown value \"%s\" with parameter \"%s\" ! Defaulting to red\n", value_buffer, key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return -1;
}
return 0;
};
static short parse_root_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50])
{
char value_buffer[50];
int i = 0;
int c;
short flag = 0; // know if a valid value has been found for the "root_color" key.
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
if (i < 49)
value_buffer[i++] = c;
}
value_buffer[i] = 0;
for (i = 0; i < sizeof(colorName) / 8; i++)
{
if (strcmp(value_buffer, colorName[i]) == 0)
{
conf->root_color = i;
flag = 1;
}
}
if (flag == 0) // If no value has been found then we warn the user that default settings are applied.
{
if (conf->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown value \"%s\" with parameter \"%s\" ! Defaulting to red\n", value_buffer, key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return -1;
}
return 0;
}
static short parse_display_path(SHELL_CONF *conf, FILE *fp, char key_buffer[50])
{
char value_buffer[50];
int i = 0;
int c;
short flag = 0; // know if a valid value has been found for the "root_color" key.
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
if (i < 49)
value_buffer[i++] = c;
}
value_buffer[i] = 0;
if (strcmp(value_buffer, "true") == 0)
{
conf->show_path = 1;
flag = 1;
}
else if (strcmp(value_buffer, "false") == 0)
{
conf->show_path = 0;
flag = 1;
}
if (flag == 0) // If no value has been found then we warn the user that default settings are applied.
{
if (conf->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown value \"%s\" with parameter \"%s\" ! Defaulting to blue\n", value_buffer, key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return -1;
}
return 0;
}
static short parse_display_hostname(SHELL_CONF *conf, FILE *fp, char key_buffer[50])
{
char value_buffer[50];
int i = 0;
int c;
short flag = 0; // know if a valid value has been found for the "root_color" key.
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
if (i < 49)
value_buffer[i++] = c;
}
value_buffer[i] = 0;
if (strcmp(value_buffer, "true") == 0)
{
conf->show_hostname = 1;
flag = 1;
}
else if (strcmp(value_buffer, "false") == 0)
{
conf->show_hostname = 0;
flag = 1;
}
if (flag == 0) // If no value has been found then we warn the user that default settings are applied.
{
if (conf->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown value \"%s\" with parameter \"%s\" ! Defaulting to green\n", value_buffer, key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return -1;
}
return 0;
}
static short parse_hostname_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50])
{
char value_buffer[50];
int i = 0;
int c;
short flag = 0; // know if a valid value has been found for the "root_color" key.
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
if (i < 49)
value_buffer[i++] = c;
}
value_buffer[i] = 0;
for (i = 0; i < sizeof(colorName) / 8; i++)
{
if (strcmp(value_buffer, colorName[i]) == 0)
{
conf->hostname_color = i;
flag = 1;
}
}
if (flag == 0) // If no value has been found then we warn the user that default settings are applied.
{
if (conf->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown value \"%s\" with parameter \"%s\" ! Defaulting to green\n", value_buffer, key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return -1;
}
return 0;
}
static short parse_path_color(SHELL_CONF *conf, FILE *fp, char key_buffer[50])
{
char value_buffer[50];
int i = 0;
int c;
short flag = 0; // know if a valid value has been found for the "root_color" key.
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
if (i < 49)
value_buffer[i++] = c;
}
value_buffer[i] = 0;
for (i = 0; i < sizeof(colorName) / 8; i++)
{
if (strcmp(value_buffer, colorName[i]) == 0)
{
conf->path_color = i;
flag = 1;
}
}
if (flag == 0) // If no value has been found then we warn the user that default settings are applied.
{
if (conf->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown value \"%s\" with parameter \"%s\" ! Defaulting to red\n", value_buffer, key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return -1;
}
return 0;
}
int init_shell(SHELL_CONF **conf, char **envp)
{
short status = 0;
*conf = (SHELL_CONF*)malloc(sizeof(SHELL_CONF));
if (*conf == NULL)
return -1;
(*conf)->env = NULL;
if (get_shell_owner(&((*conf)->env)) != 0) // Know who launched the shell and his permissions.
return -1;
(*conf)->env->config_path = malloc(sizeof(char)*strlen(CONFIGFILE) + strlen((*conf)->env->home_dir_path) + 2);
if ((*conf)->env->config_path == NULL)
return -1;
strcpy((*conf)->env->config_path, (*conf)->env->home_dir_path);
strcat((*conf)->env->config_path, "/");
strcat((*conf)->env->config_path, CONFIGFILE);
(*conf)->env->envp = (char **)malloc(sizeof(char*));
if ((*conf)->env->envp == NULL)
return -1;
(*conf)->env->envp = envp;
(*conf)->warning_flag = 0; // Set the warning flag to zero to allow at least one time to warn user for config errors.
status = readShellConf(*conf);
if (status == -1) // Read the config file.
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : No config file found. Default has been created\n");
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
else if (status == -2)
{
printf("%s", colorTypes[1]);
printf("BSH ERROR : Could not create a config file. Check your permissions !\n");
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
return 0;
}
short readShellConf(SHELL_CONF *config)
{
assert(config != NULL);
FILE *fp;
int c;
int i;
char key_buffer[50];
int key_found = 0;
int valid_key = 0;
// Flags to control if we have already processed some parameters and avoid overwritting them.
// We warn the user of redundancy in the config file.
int flags[6] = {0, 0, 0, 0, 0, 0};
fp = fopen(config->env->config_path, "r");
if (fp == NULL)
{
config->root_color = 1;
config->user_color = 2;
config->show_path = 0;
config->path_color = 4;
config->show_hostname = 0;
config->hostname_color = 2;
fp = fopen(config->env->config_path, "wr");
if (fp == NULL)
return -2;
else
{
fprintf(fp, "root_color=red\n");
fprintf(fp, "user_color=green\n");
fprintf(fp, "display_path=false\n");
fprintf(fp, "path_color=blue\n");
fprintf(fp, "display_hostname=false\n");
fprintf(fp, "hostname_color=green\n");
fclose(fp);
return -1;
}
}
i = 0;
while ((c = fgetc(fp)) != EOF)
{
if (c == '#' || c == ';') // Ignore commented lines in the config file.
while ((c = fgetc(fp)) != '\n' && c != EOF)
;
if (i != 0 && c == '\n' && config->warning_flag == 0)
{
key_buffer[i] = '\0';
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown parameter \"%s\"\n", key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
else if (c == '=')
{
key_buffer[i] = '\0';
/*
* Check every possible parameters that are in the config file and process them
* We also check every flag to see the config doesn't contains multiple times the same parameter
*/
for (i = 0, valid_key = 1; key_found != 1 && valid_key != 0 && i < (sizeof(possible_options)/8) ;i++)
{
if (strcmp(possible_options[i], key_buffer) == 0)
{
key_found = 1;
if (flags[i] == 1 && config->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Redefinition of parameter \"%s\"\n", key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
valid_key = 0;
}
else
valid_key = 1;
}
}
if (key_found && valid_key)
{
if (strcmp(key_buffer, "user_color") == 0)
{
if (parse_user_color(config, fp, key_buffer) == -1)
config->user_color = 2;
flags[1] = 1;
}
else if (strcmp(key_buffer, "root_color") == 0)
{
if (parse_root_color(config, fp, key_buffer) == -1)
config->user_color = 1;
flags[0] = 1;
}
else if (strcmp(key_buffer, "display_path") == 0)
{
if (parse_display_path(config, fp, key_buffer) == -1)
config->show_path = 0;
flags[2] = 1;
}
else if (strcmp(key_buffer, "path_color") == 0)
{
if (parse_path_color(config, fp, key_buffer) == -1)
config->path_color = 4;
}
else if (strcmp(key_buffer, "display_hostname") == 0)
{
if(parse_display_hostname(config, fp, key_buffer) == -1)
config->show_hostname = 0;
}
else if (strcmp(key_buffer, "hostname_color") == 0)
{
if (parse_hostname_color(config, fp, key_buffer) == -1)
config->hostname_color = 2;
}
}
else if (!key_found && config->warning_flag == 0)
{
printf("%s", colorTypes[3]);
printf("BSH WARNING : Unknown parameter \"%s\"\n", key_buffer);
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]);
}
i = 0;
key_found = 0;
valid_key = 0;
}
else
{
if (i < 49)
key_buffer[i++] = c;
}
}
config->warning_flag = 1; // If user has already been warned, set this flag to disable warnings every time we read config file again.
fclose(fp);
return 0;
}
void show_prompt(SHELL_CONF *config)
{
assert(config != NULL && config->env != NULL && config->env->username != NULL && config->env->permissions >= 0);
if (config->env->permissions != 0) // If user is non-root we set the proper color output
printf("%s", colorTypes[config->user_color]);
else
printf("%s", colorTypes[config->root_color]); // If user is root we set the proper color output
printf("%s", config->env->username);
if (config->env->hostname != NULL && config->show_hostname == 1)
{
putchar('@');
printf("%s", colorTypes[config->hostname_color]);
printf("%s", config->env->hostname);
}
if (config->env->curr_path != NULL && config->show_path == 1)
{
putchar(':');
printf("%s", colorTypes[config->path_color]);
printf("%s", config->env->curr_path);
}
printf("%s", colorTypes[sizeof(colorTypes) / 8 - 1]); // Reset the color.
printf("~$ ");
}
char *readCommandInput(void)
{
int i, step, c;
char *buffer;
char *buffer_pt;
buffer = NULL;
i = 0;
step = 2;
buffer = (char*)malloc(sizeof(char)*SHELL_INPUT_BUFFER_SIZE); // First alocate some memory by the ma buffer size
if (buffer == NULL)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Memory Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
return NULL;
}
buffer_pt = buffer;
while((c = getchar()) != EOF && c != '\n') // Get input char by char till user presses Enter
{
if ((i + 1)% SHELL_INPUT_BUFFER_SIZE == 0) // if buffer is full, we reallocate it by step*buffer_size.
{
buffer = (char*) realloc(buffer, sizeof(char*)*SHELL_INPUT_BUFFER_SIZE *step);
if (buffer == NULL)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Memory Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
return NULL;
}
/*
* avoid having buffer_pt pointing to the beginning of buffer.
* We continue where we left by pointing it at the end of buffer and continue to read the input
*/
buffer_pt = buffer + (i *(step - 1));
step++; //increment the step for the next realloc
i = 0;
}
buffer_pt[i++] = c;
}
if (i != 0) // We the input is not empty.
{
buffer_pt[i] = '\0'; // DON'T FORGET TO PUT A NULL CHAR AT THE END OF THE BUFFER !!
/*
* Since we increment the buffer by step*buffer_size every time it is full , there may be some memory wasting.
* Readjust the whole buffer to the right size of input. Don't waste memory !
* Let's say input is 513 chars, buffer 512 and we reallocate 512 to buffer, we will waste 511 bytes !
*/
buffer = (char*)realloc(buffer, strlen(buffer) + 1);
if (buffer == NULL)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Memory Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
return NULL;
}
return buffer;
}
else
{
free(buffer); // If input is empty, free it for the next call.
return NULL;
}
}
char **splitCommandInput(char *commandInput)
{
int position, step;
char **tokens = NULL;
char *token = NULL;
step = 2;
position = 0;
tokens = (char**) malloc(sizeof(char*)*SHELL_TOK_BUFFER_SIZE);
if (tokens == NULL)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Memory Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
return NULL;
}
token = strtok(commandInput, SHELL_TOK_DELIMITER);
while (token != NULL)
{
tokens[position] = token;
position++;
if ((position+ 1) % SHELL_TOK_BUFFER_SIZE == 0)
{
tokens = realloc(tokens, sizeof(char*)*SHELL_TOK_BUFFER_SIZE *step);
if (tokens == NULL)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Memory Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
return NULL;
}
}
token = strtok(NULL, SHELL_TOK_DELIMITER);
}
tokens[position] = NULL;
return tokens;
}
static char **getArgs(char **args)
{
char **args_tmp = NULL;
int i;
static char **curr_pos_each_call = NULL;
if (args != NULL)
{
if (curr_pos_each_call == NULL)
curr_pos_each_call = args;
for(i = 0; curr_pos_each_call[i] != NULL && strcmp(curr_pos_each_call[i], "|") != 0; i++)
{
args_tmp = (char**)realloc(args_tmp, sizeof(char*)*(i + 2));
args_tmp[i] = curr_pos_each_call[i];
}
if (i != 0)
{
if (curr_pos_each_call[i] != NULL && strcmp(curr_pos_each_call[i], "|") == 0)
curr_pos_each_call += (i + 1);
else
curr_pos_each_call += i;
args_tmp[i] = NULL;
}
}
else
{
curr_pos_each_call = NULL;
}
return args_tmp;
}
int shell_launch(char **args)
{
int p[2];
pid_t pid;
int fd_in = 0;
char **tmp = NULL;
int n = 0;
int i = 1;
int j = 0;
for (n = 0; args[n] != NULL ; n++)
{
if (strcmp(args[n], "|") == 0)
i++;
}
while (j < i)
{
tmp = getArgs(args);
if (pipe(p) < 0)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Pipe Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
}
if ((pid = fork()) == -1)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : Fork Error !\n");
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
}
else if (pid == 0)
{
dup2(fd_in, 0); // fd_in result before pipe
//close(fd_in); // STDIN = fd_in
if (j + 1!= i)
{
dup2(p[1], 1); // If we are last, thus last command atfer pipe --> STDOUT = pipe_write
}
close(p[1]); // Closing pipe_write
close(p[0]); // Closing pipe_read
if(execvp(tmp[0], tmp) < 0)
{
fprintf(stderr, "%s", colorTypes[1]);
fprintf(stderr, "BSH : %s : command not found !\n", tmp[0]);
fprintf(stderr, "%s", colorTypes[sizeof(colorTypes) / 8 -1]);
}
exit(EXIT_FAILURE);
}
else
{
if (j + 1 == i)
{
close(p[0]);
close(p[1]);
}
else
close(p[1]);
wait(NULL);
fd_in = p[0]; // Save the input for the next command
j++;
}
}
getArgs(NULL);
free(args);
return 1;
}
void free_shell(SHELL_CONF *conf)
{
assert(conf != NULL);
free(conf->env->username);
free(conf->env);
free(conf);
}
int shell_execute(char **args, SHELL_CONF *config)
{
if (strcmp(args[0], "cd") == 0)
{
return(cd_command(args, &(config->env)));
}
else if (strcmp(args[0], "exit") == 0)
{
return(exit_command(args));
}
else if (strcmp(args[0], "show-env") == 0)
{
return(show_env_command(args, config->env));
}
return shell_launch(args);
}