sanity checkin of work-in-progress on an nfs implementation.
[newos.git] / apps / shell / commands.c
blobd53674eb75349e48950c91903db4fb8f7623455b
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <sys/syscalls.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <ctype.h>
12 #include "commands.h"
13 #include "file_utils.h"
14 #include "shell_defs.h"
16 struct command cmds[] = {
17 {"exec", &cmd_exec},
18 {"stat", &cmd_stat},
19 {"mkdir", &cmd_mkdir},
20 {"cat", &cmd_cat},
21 {"cd", &cmd_cd},
22 {"pwd", &cmd_pwd},
23 {"help", &cmd_help},
24 {NULL, NULL}
27 int cmd_exec(int argc, char *argv[])
29 return cmd_create_proc(argc - 1,argv+1);
32 int cmd_create_proc(int argc,char *argv[])
34 bool must_wait=true;
35 proc_id pid;
37 int arg_len;
38 char *tmp;
39 char filename[SCAN_SIZE+1];
41 if(argc <1){
42 printf("not enough args to exec\n");
43 return 0;
46 tmp = argv[argc - 1];
48 if( !find_file_in_path(argv[0],filename,SCAN_SIZE)){
49 printf("can't find '%s' \n",argv[0]);
50 return 0;
54 // a hack to support the unix '&'
55 if(argc >= 1) {
56 arg_len = strlen(tmp);
57 if(arg_len > 0){
58 tmp += arg_len -1;
59 if(*tmp == '&'){
60 if(arg_len == 1){
61 argc --;
62 } else {
63 *tmp = 0;
65 must_wait = false;
70 pid = sys_proc_create_proc(filename,filename, argv, argc, 5);
71 if(pid >= 0) {
72 int retcode;
74 if(must_wait) {
75 sys_proc_wait_on_proc(pid, &retcode);
77 } else {
78 printf("Error: cannot execute '%s'\n", filename);
79 return 0; // should be -1, but the shell would exit
82 return 0;
85 int cmd_mkdir(int argc, char *argv[])
87 int rc;
89 if(argc < 2) {
90 printf("not enough arguments to mkdir\n");
91 return 0;
94 rc = sys_create(argv[1], STREAM_TYPE_DIR);
95 if (rc < 0) {
96 printf("sys_mkdir() returned error: %s\n", strerror(rc));
97 } else {
98 printf("%s successfully created.\n", argv[1]);
101 return 0;
104 int cmd_cat(int argc, char *argv[])
106 int rc;
107 int fd;
108 char buf[257];
109 int len;
111 if(argc < 2) {
112 printf("not enough arguments to cat\n");
113 return 0;
116 fd = sys_open(argv[1], STREAM_TYPE_FILE, 0);
117 if(fd < 0) {
118 printf("cat: sys_open() returned error: %s!\n", strerror(fd));
119 goto done_cat;
122 for(;;) {
123 rc = sys_read(fd, buf, -1, sizeof(buf) -1);
124 if(rc <= 0)
125 break;
127 write(1, buf, rc);
129 sys_close(fd);
131 done_cat:
132 return 0;
135 int cmd_cd(int argc, char *argv[])
137 int rc;
139 if(argc < 2) {
140 printf("not enough arguments to cd\n");
141 return 0;
144 rc = sys_setcwd(argv[1]);
145 if (rc < 0) {
146 printf("cd: sys_setcwd() returned error: %s!\n", strerror(rc));
149 return 0;
152 int cmd_pwd(int argc, char *argv[])
154 char *cwd;
156 cwd= getcwd(NULL, 1024);
157 if (!cwd) {
158 printf("cd: sys_getcwd() returned error: %s!\n", "xx"); //strerror(rc));
159 } else {
160 printf("pwd: cwd=\'%s\'\n", cwd);
163 free(cwd);
165 return 0;
168 int cmd_stat(int argc, char *argv[])
170 int rc;
171 struct file_stat stat;
173 if(argc < 2) {
174 printf("not enough arguments to stat\n");
175 return 0;
178 rc = sys_rstat(argv[1], &stat);
179 if(rc >= 0) {
180 printf("stat of file '%s': \n", argv[1]);
181 printf("vnid 0x%x\n", (unsigned int)stat.vnid);
182 printf("type %d\n", stat.type);
183 printf("size %d\n", (int)stat.size);
184 } else {
185 printf("stat failed for file '%s'\n", argv[1]);
187 return 0;
190 int cmd_help(int argc, char *argv[])
192 printf("command list:\n\n");
193 printf("exit : quits this copy of the shell\n");
194 printf("exec <file> : load this file as a binary and run it\n");
195 printf("mkdir <path> : makes a directory at <path>\n");
196 printf("cd <path> : sets the current working directory at <path>\n");
197 printf("ls <path> : directory list of <path>. If no path given it lists the current dir\n");
198 printf("stat <file> : gives detailed file statistics of <file>\n");
199 printf("help : this command\n");
200 printf("cat <file> : dumps the file to stdout\n");
201 printf("mount <path> <device> <fsname> : tries to mount <device> at <path>\n");
202 printf("unmount <path> : tries to unmount at <path>\n");
204 return 0;