/
builtin.c
697 lines (581 loc) · 13.7 KB
/
builtin.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
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
/* CMPSC 311, Spring 2013, Project 7
*
* Author: Jacob Jones
* Email: jaj5333@psu.edu
*
* Author: Scott Cheloha
* Email: ssc5145@psu.edu
*/
/* Implementation of the "built-in" shell commands as specified
* in requirement (1) of the Project 7 specs.
*
* Per requirements of project 7, we implement:
=======
* quit -- exit the shell, unless bg processes are active (originally called "exit")
* echo -- print remaining command-line arguments
* dir -- print name of current working directory
* cdir -- change working directory
* penv -- print one or all environment variables
* senv -- set an environment variable
* unsenv -- unset an environment variable
* pjobs -- list currently active bg processes
* help -- list builtin shell commands (commands implemented in builtin.c)
*
* In addition, we implement:
* limits -- needs to be written, print the limit macros from pr7.h
* (set echo -- toggle echo mode on or off) -- replaced by toggle
* (set debug -- toggle debug mode on or off) -- replaced by toggle
* set exec -- toggle exec types (lp,vp,ve)
* (set verbose -- toggle verbose mode on or off) -- replaced by toggle
* toggle -- toggle verbosity, echo, debug modes from within the shell
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/param.h>
#include "builtin.h"
#include "pr7.h" // included for shell_msg()
#include "wrapper.h"
#include "linked.h"
int Builtin(char *Argv[])
{
errno = 0; // reset errno
/*
if (!strcmp(Argv[0], "pr7")) {
return 1;
}
*/
/* quit command */
if (!strcmp(Argv[0], "quit")) {
Quit(); return 1;
}
/* echo command */
if (!strcmp(Argv[0], "echo")) {
Echo(Argv);
return 1;
}
/* dir command */
if (!strcmp(Argv[0], "dir")) {
Dir(); return 1;
}
/* cdir command */
if (!strcmp(Argv[0], "cdir")) {
Cdir(Argv); return 1;
}
/* penv command */
if (!strcmp(Argv[0], "penv")) {
Penv(Argv); return 1;
}
/* senv command */
if (!strcmp(Argv[0], "senv")) {
Senv(Argv); return 1;
}
/* unsenv command */
if (!strcmp(Argv[0], "unsenv")) {
Unsenv(Argv); return 1;
}
/* penv command */
if (!strcmp(Argv[0], "help")) {
Help(); return 1;
}
/* pjobs command */
if (!strcmp(Argv[0], "pjobs")) {
Pjobs(); return 1;
}
/* toggle command */
if (!strcmp(Argv[0], "toggle")) {
Toggle(Argv); return 1;
}
/* options command */
if (!strcmp(Argv[0], "options")) {
Options(); return 1;
}
/* set command -- remove? */
if (!strcmp(Argv[0], "set")) {
Set(Argv); return 1;
}
/* ignore singleton & -- why is this here, Don? */
if (!strcmp(Argv[0], "&")) {
return 1;
}
if (!strcmp(Argv[0], "limits")) {
Limits(); return 1;
}
if (!strcmp(Argv[0], "kill")) {
pr7_Kill(Argv); return 1;
}
/* not a builtin command */
return 0;
}
/*----------------------------------------------------------------------------*/
void Quit(void)
{
if (bg_processes->head != NULL)
{
puts("Background processes are still running.");
Pjobs();
}
else
{
if (v_flag)
{
printf("-%s: See you later, %s!\n", prog, getenv("USER"));
}
exit(0);
}
}
/*----------------------------------------------------------------------------*/
void Echo(char *Argv[])
{
for (int i = 1; Argv[i] != NULL; i++)
{
printf("%s ", Argv[i]);
}
puts("");
}
/*----------------------------------------------------------------------------*/
void Dir(void)
{
char *buf;
if (v_flag)
{
shell_msg("dir", "printing working directory");
}
// Allocate MAXPATHLEN bytes for buf
if ((buf = Malloc(MAXPATHLEN, __func__, __LINE__)) == NULL) {
shell_msg("dir", strerror(errno));
return;
}
// Call getwd()
getwd(buf);
// Check if getwd() failed,
// print a message on failure, print the directory name on success
if (errno != 0) {
shell_msg("dir", strerror(errno));
}
else {
printf("%s\n", getwd(buf));
}
// Free() buf and return
Free(buf, __func__, __LINE__);
return;
}
/*----------------------------------------------------------------------------*/
void Cdir(char *Argv[])
{
const char *target_dir;
// If the directory is unspecified, use env. var. HOME as target directory
if (Argv[1] == NULL) {
target_dir = getenv("HOME");
}
// Otherwise, use the specified directory
else {
target_dir = Argv[1];
}
if (v_flag)
{
fprintf(stderr, "-%s: changing working directory to %s\n",
"cdir", target_dir);
}
// Check that the directory is not NULL
if (target_dir)
{
// attempt to change the working directory to Argv[1]
if (chdir(target_dir) == -1) {
shell_msg("cdir", strerror(errno));
return;
}
// attempt to set env. var. PWD to Argv[1]
if (setenv("PWD", target_dir, 1) == -1) {
shell_msg("cdir", strerror(errno));
return;
}
if (v_flag)
{
fprintf(stderr, "-%s: %s: working directory changed to %s\n",
prog, "cdir", target_dir);
}
}
// Strange circumstance where Argv[1] NULL but not caught,
// or HOME is unspecified by the environment
else {
shell_msg("cdir", "strange error; HOME is not set");
}
}
/*----------------------------------------------------------------------------*/
void Penv(char *Argv[])
{
char *env_var;
// If Argv[1] is not NULL, print the env. var. for it
if (Argv[1])
{
// If getenv() succeeds, print the variable
if ((env_var = getenv(Argv[1])) != NULL)
{
printf("%s\n", env_var);
}
return;
}
// If Argv[1] is NULL, print all available environment variables
env_var = *environ;
for (int i = 0; env_var != NULL; i++)
{
printf("%s\n", env_var);
env_var = *(environ + i);
}
}
/*----------------------------------------------------------------------------*/
void Senv(char *Argv[])
{
// If Argv[1] is NULL, no env. variable or value was specified, so we do nothing
if (!Argv[1])
{
return;
}
// If no '=' is found, the command does not conform to formatting specs,
// so issue a message, Free buf, and return to prompt
char *index_of_equal;
if ((index_of_equal = strpbrk(Argv[1], "=")) == NULL)
{
shell_msg("senv", "format is 'senv name=value'");
return;
}
// Allocate space for name
// If we fail, Free buf and return to prompt
char *name;
int namelength = index_of_equal - Argv[1];
if ((name = Malloc(namelength + 1, __func__, __LINE__)) == NULL)
{
shell_msg("senv", strerror(errno));
return;
}
// Copy from buf to name
strncpy(name, Argv[1], namelength);
name[namelength+1] = '\0';
// Allocate space for value
// If we fail, Free buf, Free name, and return to prompt
char *value;
int valuelength = strlen(Argv[1]) - namelength;
if ((value = Malloc(valuelength, __func__, __LINE__)) == NULL)
{
shell_msg("senv", strerror(errno));
Free(name, __func__, __LINE__);
return;
}
// Copy from buf to value
strncpy(value, Argv[1]+namelength+1, valuelength-1);
value[valuelength] = '\0';
// Having successfully parsed name and value,
// We can tell our verbose users exactly what we're doing
if (v_flag)
{
fprintf(stderr, "-%s: %s: setting environment variable %s to %s\n",
prog, "senv", name, value);
}
// Attempt to set the environment variable
if ((setenv(name, value, 1) == -1))
{
shell_msg("senv", strerror(errno));
Free(name, __func__, __LINE__);
Free(value, __func__, __LINE__);
}
if (v_flag)
{
fprintf(stderr, "-%s: %s: successfully set environment variable %s to %s\n",
prog, "senv", name, value);
}
// Free allocations and return to shell
Free(name, __func__, __LINE__);
Free(value, __func__, __LINE__);
return;
}
/*----------------------------------------------------------------------------*/
void Unsenv(char *Argv[])
{
// If Argv[1] is NULL, we do nothing
if (Argv[1] == NULL)
{
return;
}
// Ensure variable exists before attempting to unset
if (getenv(Argv[1]) == NULL)
{
fprintf(stderr, "-%s: %s: cannot unset %s; variable does not exist\n",
prog, "unsenv", Argv[1]);
return;
}
// Let the verbose user know what we're doing
if (v_flag)
{
fprintf(stderr, "-%s: unsenv: removing environment variable %s\n",
prog, Argv[1]);
}
// Attempt to unset the specified environment variable
if (unsetenv(Argv[1]) == -1)
{
shell_msg("unsenv", strerror(errno));
return;
}
if (v_flag)
{
fprintf(stderr, "-%s: unsenv: successfully removed environment variable %s\n",
prog, Argv[1]);
}
return;
}
/*----------------------------------------------------------------------------*/
void Help(void)
{
puts("built-in commands:");
puts(" help print this list");
puts(" quit terminate pr7 if no background processes are running");
puts(" echo print remaining command-line arguments");
puts(" dir print current working directory");
puts(" cdir change working directory");
puts(" penv print one or all environment variables");
puts(" senv set an environment variable");
puts(" unsenv unset an environment variables");
puts(" toggle toggle a command line argument (e, v, or d)");
puts(" options prints all active command line arguments (e, i, v, d, and s)");
}
void Pjobs(void)
{
process_list_print(bg_processes);
}
/*----------------------------------------------------------------------------*/
void Toggle(char *Argv[])
{
// If Argv[1] is null, nothing was specified,
// so we issue a message and return to prompt
if (Argv[1] == NULL)
{
fprintf(stderr, "-%s: error: nothing to toggle\n", prog);
return;
}
if (!strcmp(Argv[1], "e"))
{
if (e_flag)
{
e_flag = 0;
}
else
{
e_flag = 1;
}
printf("-%s: echo mode is now ", prog);
if (e_flag)
{
puts("ON");
}
else
{
puts("OFF");
}
return;
}
if (!strcmp(Argv[1], "v"))
{
if (v_flag)
{
v_flag = 0;
}
else
{
v_flag = 1;
}
printf("-%s: verbose output is now ", prog);
if (v_flag)
{
puts("ON");
}
else
{
puts("OFF");
}
return;
}
if (!strcmp(Argv[1], "d"))
{
if (d_flag)
{
d_flag = 0;
}
else
{
d_flag = 1;
}
printf("-%s: debug output is now ", prog);
if (d_flag)
{
puts("ON");
}
else
{
puts("OFF");
}
return;
}
fprintf(stderr, "-%s: error: don't know how to toggle %s\n" \
"-%s: try 'toggle [d|e|v]'\n",
prog, Argv[1], prog);
}
void Options(void)
{
printf("-%s: Running with:\n", prog);
Print_Options();
}
void Print_Options()
{
if (i_flag)
{
puts(" [-i] interactive prompt");
}
if (e_flag)
{
puts(" [-e] echo commands before execution");
}
if (v_flag)
{
puts(" [-v] verbose output");
}
if (d_flag)
{
puts(" [-d] debug output");
}
if (s_flag)
{
printf(" [-s] alternate startup file %s\n", s_filename);
}
}
/*----------------------------------------------------------------------------*/
void Set(char *Argv[])
{
if (Argv[1] == NULL)
{
fprintf(stderr, "-%s: set format:\n", prog);
fprintf(stderr, "-%s: set [echo|verbose|debug] [on|off]\n", prog);
fprintf(stderr, "-%s: set [exec] [vp|ve|lp]\n", prog);
}
else if (!strcmp(Argv[1], "exec")) {
Sexec(Argv);
}
else if (!strcmp(Argv[1], "echo")) {
Secho(Argv);
}
else if (!strcmp(Argv[1], "verbose")) {
Sverbose(Argv);
}
else if (!strcmp(Argv[1], "debug")) {
Sdebug(Argv);
}
else
{
fprintf(stderr, "-%s: error: don't know how to set %s\n", prog, Argv[1]);
}
}
/*----------------------------------------------------------------------------*/
void Sexec(char *Argv[])
{
if (!strcmp(Argv[2], "vp")) {
exec_flag = 0;
}
if (!strcmp(Argv[2], "ve")) {
exec_flag = 1;
}
if (!strcmp(Argv[2], "lp")) {
exec_flag = 2;
}
if (v_flag)
{
fprintf(stderr, "-%s: set: set exec mode %s\n",
prog, Argv[2]);
}
}
/*----------------------------------------------------------------------------*/
void Secho(char *Argv[])
{
if (!strcmp(Argv[2], "on")) {
e_flag = 1;
}
else if (!strcmp(Argv[2], "off")) {
e_flag = 0;
}
else {
fprintf(stderr, "-%s: error: don't know how to set echo \"%s\"\n",
prog, Argv[2]);
return;
}
if (v_flag)
{
fprintf(stderr, "-%s: set: successfully set echo %s\n",
prog, Argv[2]);
}
}
/*----------------------------------------------------------------------------*/
void Sverbose(char *Argv[])
{
if (!strcmp(Argv[2], "on")) {
v_flag = 1;
}
else if (!strcmp(Argv[2], "off")) {
v_flag = 0;
}
else {
fprintf(stderr, "-%s: error: don't know how to set verbose \"%s\"\n",
prog, Argv[2]);
return;
}
if (v_flag)
{
fprintf(stderr, "-%s: set: successfully set verbose %s\n",
prog, Argv[2]);
}
}
/*----------------------------------------------------------------------------*/
void Sdebug(char *Argv[])
{
if (!strcmp(Argv[2], "on")) {
d_flag = 1;
}
else if (!strcmp(Argv[2], "off")) {
d_flag = 0;
}
else {
fprintf(stderr, "-%s: error: don't know how to set debug \"%s\"\n",
prog, Argv[2]);
return;
}
if (v_flag)
{
fprintf(stderr, "-%s: set: successfully set debug %s\n",
prog, Argv[2]);
}
}
/*----------------------------------------------------------------------------*/
void Limits(void)
{
puts("Shell Limitations:");
printf(" %4d Maximum input length in characters\n", MAX_INPUT);
printf(" %4d Maximum path length in characters\n", MAX_PATH);
printf(" %4d Maximum number of children forkable from shell process\n", CHILD_MAX);
}
/*----------------------------------------------------------------------------*/
void pr7_Kill(char *Argv[])
{
if (Argv[1])
{
unsigned int pid_to_kill = (unsigned int) strtoul(Argv[1], NULL, 10);
if (Kill(pid_to_kill, SIGKILL, 1, __func__, __LINE__) == -1)
{
fprintf(stderr, "-%s: error: can't kill PID %d: %s\n",
Argv[0], pid_to_kill, strerror(errno));
}
}
else
{
fprintf(stderr, "-%s: to forcibly end a process try 'kill [PID]'\n", prog);
fprintf(stderr, "-%s: to see list of currently running background processes, try 'pjobs'\n", prog);
}
}