Skip to content

Commit f0ad7ad

Browse files
author
hyqooo
committed
Repaired tabs
1 parent 168f9a2 commit f0ad7ad

File tree

1 file changed

+80
-80
lines changed

1 file changed

+80
-80
lines changed

shell.c

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ struct termios shell_tmodes;
2828
pid_t shell_pgid;
2929

3030
/*
31-
Definition of struct tokens. We need repeat it because using 'struct tokens *tokens'
32-
referencing to header's 'struct tokens' so that causes error of incomplete type
33-
*/
31+
Definition of struct tokens. We need repeat it because using 'struct tokens *tokens'
32+
referencing to header's 'struct tokens' so that causes error of incomplete type
33+
*/
3434
struct tokens{
3535
size_t tokens_length;
3636
char **tokens;
@@ -48,28 +48,28 @@ typedef int cmd_fun_t(struct tokens *tokens);
4848

4949
/* Built-in command struct and lookup table */
5050
typedef struct fun_desc {
51-
cmd_fun_t *fun;
52-
char *cmd;
53-
char *doc;
51+
cmd_fun_t *fun;
52+
char *cmd;
53+
char *doc;
5454
} fun_desc_t;
5555

5656
fun_desc_t cmd_table[] = {
57-
{cmd_help, "?", "show this help menu"},
58-
{cmd_exit, "exit", "exit the command shell"},
59-
{cmd_pwd, "pwd", "prints the current working directory"},
60-
{cmd_cd, "cd", "changes current working directory on directory provided by arg"},
57+
{cmd_help, "?", "show this help menu"},
58+
{cmd_exit, "exit", "exit the command shell"},
59+
{cmd_pwd, "pwd", "prints the current working directory"},
60+
{cmd_cd, "cd", "changes current working directory on directory provided by arg"},
6161
};
6262

6363
/* Prints a helpful description for the given command */
6464
int cmd_help(unused struct tokens *tokens) {
65-
for (unsigned int i = 0; i < sizeof(cmd_table) / sizeof(fun_desc_t); i++)
66-
printf("%s - %s\n", cmd_table[i].cmd, cmd_table[i].doc);
67-
return 1;
65+
for (unsigned int i = 0; i < sizeof(cmd_table) / sizeof(fun_desc_t); i++)
66+
printf("%s - %s\n", cmd_table[i].cmd, cmd_table[i].doc);
67+
return 1;
6868
}
6969

7070
/* Exits this shell */
7171
int cmd_exit(unused struct tokens *tokens) {
72-
exit(0);
72+
exit(0);
7373
}
7474

7575
/* Prints current working directory */
@@ -78,7 +78,7 @@ int cmd_pwd(unused struct tokens *tokens){
7878
char *path_buff = NULL;
7979
path_buff = (char *)malloc(sizeof(char) * MAX_PATH_SIZE);
8080
if (!path_buff)
81-
return -1;
81+
return -1;
8282
/* Copy absolute path of working directory to path_buff */
8383
getcwd(path_buff, MAX_PATH_SIZE);
8484
fprintf(stdout, "Path to current directory: %s\n", path_buff);
@@ -89,99 +89,99 @@ int cmd_pwd(unused struct tokens *tokens){
8989
/* Changes current working directory */
9090
int cmd_cd(struct tokens *tokens){
9191
if(chdir(tokens->tokens[1]) == -1){
92-
printf("No such file or directory\n");
92+
printf("No such file or directory\n");
9393
}
9494
return 1;
9595
}
9696

9797
/* Execute program */
9898
int shell_exec(struct tokens *tokens){
99-
char *path = tokens_get_token(tokens, 0);
100-
char **args = (char **)malloc(tokens->tokens_length * sizeof(char *));
101-
for (int i = 0; i < tokens->tokens_length; i++){
102-
args[i] = tokens_get_token(tokens, i);
103-
}
104-
pid_t cpid;
105-
int status;
106-
cpid = fork();
107-
/* cpid > 0 - parent process, cpid == 0 - child process, cpid < 0 - error */
108-
if (cpid > 0) {
109-
wait(&status);
110-
} else if (cpid == 0){
111-
/* executes program according to path and given arguments */
112-
execv(path, args);
113-
exit(0);
114-
} else {
115-
/* cannot fork current process */
116-
exit(1);
117-
}
118-
return 1;
99+
char *path = tokens_get_token(tokens, 0);
100+
char **args = (char **)malloc(tokens->tokens_length * sizeof(char *));
101+
for (int i = 0; i < tokens->tokens_length; i++){
102+
args[i] = tokens_get_token(tokens, i);
103+
}
104+
pid_t cpid;
105+
int status;
106+
cpid = fork();
107+
/* cpid > 0 - parent process, cpid == 0 - child process, cpid < 0 - error */
108+
if (cpid > 0) {
109+
wait(&status);
110+
} else if (cpid == 0){
111+
/* executes program according to path and given arguments */
112+
execv(path, args);
113+
exit(0);
114+
} else {
115+
/* cannot fork current process */
116+
exit(1);
117+
}
118+
return 1;
119119
}
120120

121121
/* Looks up the built-in command, if it exists. */
122122
int lookup(char cmd[]) {
123-
for (unsigned int i = 0; i < sizeof(cmd_table) / sizeof(fun_desc_t); i++)
124-
if (cmd && (strcmp(cmd_table[i].cmd, cmd) == 0))
125-
return i;
126-
return -1;
123+
for (unsigned int i = 0; i < sizeof(cmd_table) / sizeof(fun_desc_t); i++)
124+
if (cmd && (strcmp(cmd_table[i].cmd, cmd) == 0))
125+
return i;
126+
return -1;
127127
}
128128

129129
/* Intialization procedures for this shell */
130130
void init_shell() {
131-
/* Our shell is connected to standard input. */
132-
shell_terminal = STDIN_FILENO;
131+
/* Our shell is connected to standard input. */
132+
shell_terminal = STDIN_FILENO;
133133

134-
/* Check if we are running interactively */
135-
shell_is_interactive = isatty(shell_terminal);
134+
/* Check if we are running interactively */
135+
shell_is_interactive = isatty(shell_terminal);
136136

137-
if (shell_is_interactive) {
138-
/* If the shell is not currently in the foreground, we must pause the shell until it becomes a
139-
* foreground process. We use SIGTTIN to pause the shell. When the shell gets moved to the
140-
* foreground, we'll receive a SIGCONT. */
141-
while (tcgetpgrp(shell_terminal) != (shell_pgid = getpgrp()))
142-
kill(-shell_pgid, SIGTTIN);
137+
if (shell_is_interactive) {
138+
/* If the shell is not currently in the foreground, we must pause the shell until it becomes a
139+
* foreground process. We use SIGTTIN to pause the shell. When the shell gets moved to the
140+
* foreground, we'll receive a SIGCONT. */
141+
while (tcgetpgrp(shell_terminal) != (shell_pgid = getpgrp()))
142+
kill(-shell_pgid, SIGTTIN);
143143

144-
/* Saves the shell's process id */
145-
shell_pgid = getpid();
144+
/* Saves the shell's process id */
145+
shell_pgid = getpid();
146146

147-
/* Take control of the terminal */
148-
tcsetpgrp(shell_terminal, shell_pgid);
147+
/* Take control of the terminal */
148+
tcsetpgrp(shell_terminal, shell_pgid);
149149

150-
/* Save the current termios to a variable, so it can be restored later. */
151-
tcgetattr(shell_terminal, &shell_tmodes);
152-
}
150+
/* Save the current termios to a variable, so it can be restored later. */
151+
tcgetattr(shell_terminal, &shell_tmodes);
152+
}
153153
}
154154

155155
int main(unused int argc, unused char *argv[]) {
156-
init_shell();
156+
init_shell();
157157

158-
static char line[4096];
159-
int line_num = 0;
158+
static char line[4096];
159+
int line_num = 0;
160160

161-
/* Please only print shell prompts when standard input is not a tty */
162-
if (shell_is_interactive)
163-
fprintf(stdout, "%d: ", line_num);
161+
/* Please only print shell prompts when standard input is not a tty */
162+
if (shell_is_interactive)
163+
fprintf(stdout, "%d: ", line_num);
164164

165-
while (fgets(line, 4096, stdin)) {
166-
/* Split our line into words. */
167-
struct tokens *tokens = tokenize(line);
165+
while (fgets(line, 4096, stdin)) {
166+
/* Split our line into words. */
167+
struct tokens *tokens = tokenize(line);
168168

169-
/* Find which built-in function to run. */
170-
int fundex = lookup(tokens_get_token(tokens, 0));
169+
/* Find which built-in function to run. */
170+
int fundex = lookup(tokens_get_token(tokens, 0));
171171

172-
if (fundex >= 0) {
173-
cmd_table[fundex].fun(tokens);
174-
} else {
175-
shell_exec(tokens);
176-
}
172+
if (fundex >= 0) {
173+
cmd_table[fundex].fun(tokens);
174+
} else {
175+
shell_exec(tokens);
176+
}
177177

178-
if (shell_is_interactive)
179-
/* Please only print shell prompts when standard input is not a tty */
180-
fprintf(stdout, "%d: ", ++line_num);
178+
if (shell_is_interactive)
179+
/* Please only print shell prompts when standard input is not a tty */
180+
fprintf(stdout, "%d: ", ++line_num);
181181

182-
/* Clean up memory */
183-
tokens_destroy(tokens);
184-
}
182+
/* Clean up memory */
183+
tokens_destroy(tokens);
184+
}
185185

186-
return 0;
186+
return 0;
187187
}

0 commit comments

Comments
 (0)