Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mysh-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
최지원
mysh-1
Commits
641757e9
Commit
641757e9
authored
7 years ago
by
Jaewon Choi
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring
parent
1241958d
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
include/commands.h
+4
-0
4 additions, 0 deletions
include/commands.h
src/built_in.c
+1
-1
1 addition, 1 deletion
src/built_in.c
src/commands.c
+67
-1
67 additions, 1 deletion
src/commands.c
src/main.c
+6
-32
6 additions, 32 deletions
src/main.c
src/utils.c
+2
-0
2 additions, 0 deletions
src/utils.c
with
80 additions
and
34 deletions
include/commands.h
+
4
−
0
View file @
641757e9
...
...
@@ -7,4 +7,8 @@ struct single_command
char
**
argv
;
};
int
evaluate_command
(
int
n_commands
,
struct
single_command
(
*
commands
)[]);
void
free_commands
(
int
n_commands
,
struct
single_command
(
*
commands
)[]);
#endif // MYSH_COMMANDS_H_
This diff is collapsed.
Click to expand it.
src/built_in.c
+
1
−
1
View file @
641757e9
...
...
@@ -61,7 +61,7 @@ int validate_pwd_argv(int argc, char** argv) {
}
int
validate_fg_argv
(
int
argc
,
char
**
argv
)
{
if
(
arg
v
!=
1
)
return
0
;
if
(
arg
c
!=
1
)
return
0
;
if
(
strcmp
(
argv
[
0
],
"fg"
)
!=
0
)
return
0
;
return
1
;
...
...
This diff is collapsed.
Click to expand it.
src/commands.c
+
67
−
1
View file @
641757e9
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<assert.h>
#include
"commands.h"
#include
"built_in.h"
struct
built_in_command
built_in_commands
[]
=
{
static
struct
built_in_command
built_in_commands
[]
=
{
{
"cd"
,
do_cd
,
validate_cd_argv
},
{
"pwd"
,
do_pwd
,
validate_pwd_argv
},
{
"fg"
,
do_fg
,
validate_fg_argv
}
};
static
int
is_built_in_command
(
const
char
*
command_name
)
{
static
const
int
n_built_in_commands
=
sizeof
(
built_in_commands
)
/
sizeof
(
built_in_commands
[
0
]);
for
(
int
i
=
0
;
i
<
n_built_in_commands
;
++
i
)
{
if
(
strcmp
(
command_name
,
built_in_commands
[
i
].
command_name
)
==
0
)
{
return
i
;
}
}
return
-
1
;
// Not found
}
/*
* Description: Currently this function only handles single built_in commands. You should modify this structure to launch process and offer pipeline functionality.
*/
int
evaluate_command
(
int
n_commands
,
struct
single_command
(
*
commands
)[])
{
if
(
n_commands
>
0
)
{
struct
single_command
*
com
=
(
*
commands
);
assert
(
com
->
argc
!=
0
);
int
built_in_pos
=
is_built_in_command
(
com
->
argv
[
0
]);
if
(
built_in_pos
!=
-
1
)
{
if
(
built_in_commands
[
built_in_pos
].
command_validate
(
com
->
argc
,
com
->
argv
))
{
if
(
built_in_commands
[
built_in_pos
].
command_do
(
com
->
argc
,
com
->
argv
)
!=
0
)
{
fprintf
(
stderr
,
"%s: Error occurs
\n
"
,
com
->
argv
[
0
]);
}
}
else
{
fprintf
(
stderr
,
"%s: Invalid arguments
\n
"
,
com
->
argv
[
0
]);
return
-
1
;
}
}
else
if
(
strcmp
(
com
->
argv
[
0
],
""
)
==
0
)
{
return
0
;
}
else
if
(
strcmp
(
com
->
argv
[
0
],
"exit"
)
==
0
)
{
return
1
;
}
else
{
fprintf
(
stderr
,
"%s: command not found
\n
"
,
com
->
argv
[
0
]);
return
-
1
;
}
}
return
0
;
}
void
free_commands
(
int
n_commands
,
struct
single_command
(
*
commands
)[])
{
for
(
int
i
=
0
;
i
<
n_commands
;
++
i
)
{
struct
single_command
*
com
=
(
*
commands
)
+
i
;
int
argc
=
com
->
argc
;
char
**
argv
=
com
->
argv
;
for
(
int
j
=
0
;
j
<
argc
;
++
j
)
{
free
(
argv
[
j
]);
}
free
(
argv
);
}
memset
((
*
commands
),
0
,
sizeof
(
struct
single_command
)
*
n_commands
);
}
This diff is collapsed.
Click to expand it.
src/main.c
+
6
−
32
View file @
641757e9
...
...
@@ -17,40 +17,14 @@ int main()
int
n_commands
=
0
;
mysh_parse_command
(
buf
,
&
n_commands
,
&
commands
);
for
(
int
i
=
0
;
i
<
n_commands
;
++
i
)
{
struct
single_command
*
com
=
commands
+
i
;
int
argc
=
com
->
argc
;
char
**
argv
=
com
->
argv
;
for
(
int
j
=
0
;
j
<
argc
;
++
j
)
{
printf
(
"%s "
,
argv
[
j
]);
}
printf
(
"
\n
"
);
}
int
ret
=
evaluate_command
(
n_commands
,
&
commands
);
/*
free_commands
(
n_commands
,
&
commands
);
n_commands
=
0
;
if (strcmp(argv[0], "") == 0) {
goto release_and_continue;
} else if (strcmp(argv[0], "cd") == 0) {
if (do_cd(argc, argv)) {
fprintf(stderr, "cd: Invalid arguments\n");
}
} else if (strcmp(argv[0], "pwd") == 0) {
if (do_pwd(argc, argv)) {
fprintf(stderr, "pwd: Invalid arguments\n");
}
} else if (strcmp(argv[0], "exit") == 0) {
goto release_and_exit;
} else {
fprintf(stderr, "%s: command not found\n", argv[0]);
}
release_and_continue:
release_argv(argc, &argv);
continue;
release_and_exit:
release_argv(argc, &argv);
if
(
ret
==
1
)
{
break
;
*/
}
}
return
0
;
...
...
This diff is collapsed.
Click to expand it.
src/utils.c
+
2
−
0
View file @
641757e9
...
...
@@ -33,6 +33,8 @@ void parse_single_command(const char* command,
{
const
int
kMaxArgc
=
512
;
*
argv
=
(
char
**
)
malloc
(
kMaxArgc
*
sizeof
(
char
*
));
for
(
int
i
=
0
;
i
<
kMaxArgc
;
++
i
)
(
*
argv
)[
i
]
=
NULL
;
char
buf
[
4096
];
strcpy
(
buf
,
command
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment