feat(builtin): add the founction to read exit status

This commit is contained in:
iridiumR 2022-09-18 23:35:26 +08:00
parent d9f5d5a708
commit 0a3efb2bd9
No known key found for this signature in database
GPG key ID: 5574BE4450D55618
3 changed files with 24 additions and 14 deletions

View file

@ -2,7 +2,7 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:16:19 * @Date: 2022-09-18 14:16:19
* @Last Modified by: 1ridic * @Last Modified by: 1ridic
* @Last Modified time: 2022-09-18 20:46:12 * @Last Modified time: 2022-09-18 23:32:50
*/ */
#include "builtin.h" #include "builtin.h"
#include <stdio.h> #include <stdio.h>
@ -18,12 +18,13 @@ int dish_clear(char **args);
int dish_setenv(char **args); int dish_setenv(char **args);
int dish_echo(char **args); int dish_echo(char **args);
int dish_unset(char **args); int dish_unset(char **args);
int dish_laststatus(char **args);
char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv", "echo", "unset"}; char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv", "echo", "unset","laststatus"};
int (*builtin_func[])(char **) = {&dish_cd, &dish_help, &dish_exit, int (*builtin_func[])(char **) = {&dish_cd, &dish_help, &dish_exit,
&dish_clear, &dish_setenv, &dish_echo, &dish_clear, &dish_setenv, &dish_echo,
&dish_unset}; &dish_unset,&dish_laststatus};
int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); } int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); }
@ -78,4 +79,11 @@ int dish_unset(char **args) {
i++; i++;
} }
return 0; return 0;
}
int dish_laststatus(char **args)
{
extern int status;
fprintf(stdout,"%d\n",status);
return 0;
} }

4
exec.c
View file

@ -2,7 +2,7 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:14:23 * @Date: 2022-09-18 14:14:23
* @Last Modified by: 1ridic * @Last Modified by: 1ridic
* @Last Modified time: 2022-09-18 23:21:56 * @Last Modified time: 2022-09-18 23:31:39
*/ */
#include "builtin.h" #include "builtin.h"
#include <stdio.h> #include <stdio.h>
@ -45,7 +45,7 @@ int forkExec(char **args) {
return WTERMSIG(stat_val); return WTERMSIG(stat_val);
} }
} }
return -1; return -100;
} }
int commandExec(char **args) { int commandExec(char **args) {

20
loop.c
View file

@ -1,24 +1,26 @@
/* /*
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:13:53 * @Date: 2022-09-18 14:13:53
* @Last Modified by: 1ridic * @Last Modified by: 1ridic
* @Last Modified time: 2022-09-18 14:13:53 * @Last Modified time: 2022-09-18 23:32:02
*/ */
#include "loop.h"
#include "exec.h"
#include "line.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "line.h"
#include "loop.h" int status;
#include "exec.h"
int loop() { int loop() {
char *line; char *line;
char **args; char **args;
printf("> "); printf("> ");
line = readLine(); line = readLine();
args = splitLine(line); args = splitLine(line);
commandExec(args); status = commandExec(args);
free(line); free(line);
free(args); free(args);
return 0; return 0;