CS350 COS
COS
Loading...
Searching...
No Matches
shell.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dirent.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <syscall.h>
Include dependency graph for shell.c:

Go to the source code of this file.

Macros

#define SHELL_MAX_ARGS   5
 
#define SHELL_MAX_LINE   256
 

Functions

void DispatchCommand (char *buf)
 
int main (int argc, const char *argv[])
 
void Cmd_Help (int argc, const char *argv[])
 
void Cmd_Exit (int argc, const char *argv[])
 
void Cmd_Run (int argc, const char *argv[])
 

Variables

const char * searchpath []
 

Macro Definition Documentation

◆ SHELL_MAX_ARGS

#define SHELL_MAX_ARGS   5

Definition at line 12 of file shell.c.

◆ SHELL_MAX_LINE

#define SHELL_MAX_LINE   256

Definition at line 13 of file shell.c.

Function Documentation

◆ Cmd_Exit()

void Cmd_Exit ( int  argc,
const char *  argv[] 
)

Definition at line 41 of file shell.c.

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}
int printf(const char *fmt,...)
Definition: printf.c:212
void exit(int status)
Definition: exit.c:36
int atoi(const char *nptr)
Definition: stdlib.c:3
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Cmd_Help()

void Cmd_Help ( int  argc,
const char *  argv[] 
)

Definition at line 33 of file shell.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Cmd_Run()

void Cmd_Run ( int  argc,
const char *  argv[] 
)

Definition at line 66 of file shell.c.

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}
uint64_t OSWait(uint64_t pid)
Definition: syscall.c:35
int OSStat(const char *path, struct stat *sb)
Definition: syscall.c:89
const char * searchpath[]
Definition: shell.c:57
Definition: stat.h:5
#define NULL
Definition: stddef.h:6
char * strcpy(char *to, const char *from)
Definition: string.c:24
char * strcat(char *s, const char *append)
Definition: string.c:46
pid_t spawn(const char *path, const char *argv[])
Definition: process.c:21
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DispatchCommand()

void DispatchCommand ( char *  buf)

Definition at line 102 of file shell.c.

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}
static char buf[4096]
Definition: ethdump.c:10
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
void Cmd_Help(int argc, const char *argv[])
Definition: shell.c:33
int strcmp(const char *s1, const char *s2)
Definition: string.c:81
char * strtok(char *str, const char *delim)
Definition: string.c:156
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 18 of file shell.c.

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}
void DispatchCommand(char *buf)
Definition: shell.c:102
char * fgets(char *str, int size, FILE *fh)
Definition: file.c:166
int fputs(const char *str, FILE *fh)
Definition: file.c:132
FILE * stdout
Definition: file.c:16
FILE * stdin
Definition: file.c:15
Here is the call graph for this function:

Variable Documentation

◆ searchpath

const char* searchpath[]
Initial value:
= {
"",
"/sbin/",
"/bin/",
"/tests/",
}

Definition at line 57 of file shell.c.