CS350 COS
COS
Loading...
Searching...
No Matches
shell.c
Go to the documentation of this file.
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include <sys/dirent.h>
7#include <sys/wait.h>
8#include <sys/syscall.h>
9#include <unistd.h>
10#include <syscall.h>
11
12#define SHELL_MAX_ARGS 5
13#define SHELL_MAX_LINE 256
14
15void DispatchCommand(char *buf);
16
17int
18main(int argc, const char *argv[])
19{
20 char buf[256];
21
22 printf("System Shell\n");
23
24 while (1) {
25 fputs("Shell> ", stdout);
26 fgets(buf, sizeof(buf), stdin);
27
29 }
30}
31
32void
33Cmd_Help(int argc, const char *argv[])
34{
35 printf("bkpt Trigger a kernel breakpoint\n");
36 printf("exit Exit shell\n");
37 printf("help Display the list of commands\n");
38}
39
40void
41Cmd_Exit(int argc, const char *argv[])
42{
43 int val = 0;
44
45 if (argc != 1 && argc != 2) {
46 printf("Invalid number of arguments\n");
47 return;
48 }
49
50 if (argc == 2) {
51 val = atoi(argv[1]);
52 }
53
54 exit(val);
55}
56
57const char *searchpath[] = {
58 "",
59 "/sbin/",
60 "/bin/",
61 "/tests/",
62 NULL
63};
64
65void
66Cmd_Run(int argc, const char *argv[])
67{
68 int i, status;
69 char path[256];
70 struct stat sb;
71
72 if (argc == 0)
73 return;
74
75 i = 0;
76 while (searchpath[i] != NULL) {
77 strcpy(path, searchpath[i]);
78 strcat(path, argv[0]);
79
80 status = OSStat(path, &sb);
81 if (status != 0) {
82 i++;
83 continue;
84 }
85
86 argv[argc] = NULL;
87 status = spawn(path, &argv[0]);
88 if (status > 100000) {
89 printf("Spawn failed!\n");
90 }
91#if 0
92 status = OSWait(status);
93 printf("Process result: %d\n", status);
94#endif
95 return;
96 }
97
98 printf("Unknown command '%s'\n", argv[0]);
99}
100
101void
103{
104 int i;
105 int argc;
106 char *argv[SHELL_MAX_ARGS+1];
107 char *nextArg;
108
109 // Remove newline
110 for (i = 0; buf[i] != 0; i++) {
111 if (buf[i] == '\n') {
112 buf[i] = '\0';
113 break;
114 }
115 }
116
117 // parse input
118 nextArg = strtok(buf, " \t\r\n");
119 for (argc = 0; argc < SHELL_MAX_ARGS; argc++) {
120 if (nextArg == NULL)
121 break;
122
123 argv[argc] = nextArg;
124 nextArg = strtok(NULL, " \t\r\n");
125 }
126
127 // execute command
128 if (strcmp(argv[0], "help") == 0) {
129 Cmd_Help(argc, (const char **)argv);
130 } else if (strcmp(argv[0], "bkpt") == 0) {
131 asm volatile("int3");
132 } else if (strcmp(argv[0], "exit") == 0) {
133 Cmd_Exit(argc, (const char **)argv);
134 } else if (strcmp(argv[0], "#") == 0) {
135 // Ignore comments
136 } else if (buf[0] == '\0') {
137 // Ignore empty lines
138 } else {
139 Cmd_Run(argc, (const char **)argv);
140 }
141}
142
static char buf[4096]
Definition: ethdump.c:10
uint64_t OSWait(uint64_t pid)
Definition: syscall.c:35
int OSStat(const char *path, struct stat *sb)
Definition: syscall.c:89
void Cmd_Run(int argc, const char *argv[])
Definition: shell.c:66
#define SHELL_MAX_ARGS
Definition: shell.c:12
void Cmd_Exit(int argc, const char *argv[])
Definition: shell.c:41
const char * searchpath[]
Definition: shell.c:57
void Cmd_Help(int argc, const char *argv[])
Definition: shell.c:33
int main(int argc, const char *argv[])
Definition: shell.c:18
void DispatchCommand(char *buf)
Definition: shell.c:102
Definition: stat.h:5
#define NULL
Definition: stddef.h:6
char * fgets(char *str, int size, FILE *fh)
Definition: file.c:166
int printf(const char *fmt,...)
Definition: printf.c:212
int fputs(const char *str, FILE *fh)
Definition: file.c:132
FILE * stdout
Definition: file.c:16
FILE * stdin
Definition: file.c:15
void exit(int status)
Definition: exit.c:36
int atoi(const char *nptr)
Definition: stdlib.c:3
int strcmp(const char *s1, const char *s2)
Definition: string.c:81
char * strcpy(char *to, const char *from)
Definition: string.c:24
char * strtok(char *str, const char *delim)
Definition: string.c:156
char * strcat(char *s, const char *append)
Definition: string.c:46
pid_t spawn(const char *path, const char *argv[])
Definition: process.c:21