use the -newos toolchain even if -elf is present.
[newos.git] / apps / shell / commands.c
blobfdc92ae6476a971edff5ea36448926d4e80a697d
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>
11 #include <newos/tty_priv.h>
13 #include "commands.h"
14 #include "file_utils.h"
15 #include "shell_defs.h"
17 struct command cmds[] = {
18 {"exec", &cmd_exec},
19 {"stat", &cmd_stat},
20 {"mkdir", &cmd_mkdir},
21 {"touch", &cmd_touch},
22 {"cat", &cmd_cat},
23 {"cd", &cmd_cd},
24 {"pwd", &cmd_pwd},
25 {"help", &cmd_help},
26 {NULL, NULL}
29 int cmd_exec(int argc, char *argv[])
31 return cmd_create_proc(argc - 1,argv+1);
34 int cmd_create_proc(int argc,char *argv[])
36 bool must_wait=true;
37 proc_id pid;
39 int arg_len;
40 char *tmp;
41 char filename[SCAN_SIZE+1];
43 if(argc <1){
44 printf("not enough args to exec\n");
45 return 0;
48 tmp = argv[argc - 1];
50 if( !find_file_in_path(argv[0],filename,SCAN_SIZE)){
51 printf("can't find '%s' \n",argv[0]);
52 return 0;
56 // a hack to support the unix '&'
57 if(argc >= 1) {
58 arg_len = strlen(tmp);
59 if(arg_len > 0){
60 tmp += arg_len -1;
61 if(*tmp == '&'){
62 if(arg_len == 1){
63 argc --;
64 } else {
65 *tmp = 0;
67 must_wait = false;
72 pid = _kern_proc_create_proc(filename,filename, argv, argc, 5, PROC_FLAG_SUSPENDED|PROC_FLAG_NEW_PGROUP);
73 if(pid >= 0) {
74 int retcode;
76 if(must_wait) {
77 ioctl(0, _TTY_IOCTL_SET_PGRP, &pid, sizeof(pgrp_id));
78 _kern_send_proc_signal(pid, SIGCONT);
79 _kern_proc_wait_on_proc(pid, &retcode);
80 pid = -1;
81 ioctl(0, _TTY_IOCTL_SET_PGRP, &pid, sizeof(pgrp_id));
82 } else {
83 _kern_send_proc_signal(pid, SIGCONT);
85 } else {
86 printf("Error: cannot execute '%s'\n", filename);
87 return 0; // should be -1, but the shell would exit
90 return 0;
93 int cmd_mkdir(int argc, char *argv[])
95 int rc;
97 if(argc < 2) {
98 printf("not enough arguments to mkdir\n");
99 return 0;
102 rc = _kern_mkdir(argv[1]);
103 if (rc < 0) {
104 printf("_kern_mkdir() returned error: %s\n", strerror(rc));
105 } else {
106 printf("%s successfully created.\n", argv[1]);
109 return 0;
112 int cmd_touch(int argc, char *argv[])
114 int rc;
116 if(argc < 2) {
117 printf("not enough arguments to touch\n");
118 return 0;
121 rc = _kern_create(argv[1]);
122 if (rc < 0) {
123 printf("_kern_create() returned error: %s\n", strerror(rc));
124 } else {
125 printf("%s successfully created.\n", argv[1]);
128 return 0;
131 int cmd_cat(int argc, char *argv[])
133 int rc;
134 int fd;
135 char buf[4096];
137 if(argc < 2) {
138 printf("not enough arguments to cat\n");
139 return 0;
142 fd = open(argv[1], 0);
143 if(fd < 0) {
144 printf("cat: open() returned error: %s!\n", strerror(fd));
145 goto done_cat;
148 for(;;) {
149 rc = read(fd, buf, sizeof(buf));
150 if(rc <= 0)
151 break;
153 write(1, buf, rc);
155 close(fd);
157 done_cat:
158 return 0;
161 int cmd_cd(int argc, char *argv[])
163 int rc;
165 if(argc < 2) {
166 printf("not enough arguments to cd\n");
167 return 0;
170 rc = chdir(argv[1]);
171 if (rc < 0) {
172 printf("cd: chdir() returned error: %s!\n", strerror(rc));
175 return 0;
178 int cmd_pwd(int argc, char *argv[])
180 char *cwd;
182 cwd= getwd(NULL);
183 if (!cwd) {
184 printf("cd: getcwd() returned error: %s!\n", "xx"); //strerror(rc));
185 } else {
186 printf("pwd: cwd=\'%s\'\n", cwd);
189 free(cwd);
191 return 0;
194 int cmd_stat(int argc, char *argv[])
196 int rc;
197 struct file_stat stat;
199 if(argc < 2) {
200 printf("not enough arguments to stat\n");
201 return 0;
204 rc = _kern_rstat(argv[1], &stat);
205 if(rc >= 0) {
206 printf("stat of file '%s': \n", argv[1]);
207 printf("vnid 0x%x\n", (unsigned int)stat.vnid);
208 printf("type %d\n", stat.type);
209 printf("size %d\n", (int)stat.size);
210 } else {
211 printf("stat failed for file '%s'\n", argv[1]);
213 return 0;
216 int cmd_help(int argc, char *argv[])
218 printf("command list:\n\n");
219 printf("exit : quits this copy of the shell\n");
220 printf("exec <file> : load this file as a binary and run it\n");
221 printf("mkdir <path> : makes a directory at <path>\n");
222 printf("touch <path> : creates a file at <path>\n");
223 printf("cd <path> : sets the current working directory at <path>\n");
224 printf("ls <path> : directory list of <path>. If no path given it lists the current dir\n");
225 printf("stat <file> : gives detailed file statistics of <file>\n");
226 printf("help : this command\n");
227 printf("cat <file> : dumps the file to stdout\n");
228 printf("mount <path> <device> <fsname> : tries to mount <device> at <path>\n");
229 printf("unmount <path> : tries to unmount at <path>\n");
231 return 0;