Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
shell
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
coms352
shell
Commits
f7775a38
Commit
f7775a38
authored
Feb 27, 2018
by
Matthew Burket
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added background, cd, and moved off of readline
parent
bbe80d14
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
9 deletions
+32
-9
main.c
main.c
+29
-6
tokenize.c
tokenize.c
+2
-2
tokenize.h
tokenize.h
+1
-1
No files found.
main.c
View file @
f7775a38
...
...
@@ -14,28 +14,51 @@
int
main
()
{
// Read line examples: https://eli.thegreenplace.net/2016/basics-of-using-the-readline-library/
int
should_run
=
1
;
char
*
readBuff
;
char
*
readBuff
=
malloc
(
sizeof
(
char
)
*
5000
)
;
fprintf
(
stdout
,
"Welcome to Burket Shell (bsh)
\n
"
);
fprintf
(
stdout
,
"Copyright 2018 Matthew Burket
\n
"
);
fprintf
(
stdout
,
"Released under the GPL v3 License
\n
"
);
while
(
should_run
)
{
while
((
readBuff
=
readline
(
"bsh >> "
))
!=
NULL
)
{
fprintf
(
stdout
,
"bsh # "
);
fgets
(
readBuff
,
sizeof
(
char
)
*
5000
,
stdin
);
// Remove the newline from fgets
strtok
(
readBuff
,
"
\n
"
);
//while ((readBuff = readline("bsh >> ")) != 0) {
if
(
strlen
(
readBuff
)
>
0
)
{
add_history
(
readBuff
);
}
if
(
strcmp
(
"exit"
,
readBuff
)
==
TRUE
)
{
should_run
=
0
;
free
(
readBuff
);
exit
(
EXIT_SUCCESS
);
}
char
**
tokens
=
malloc
(
sizeof
(
char
)
*
5000
);
tokenize
(
readBuff
,
tokens
);
int
numTokens
=
tokenize
(
readBuff
,
tokens
);
char
*
lastToken
=
tokens
[
numTokens
-
2
];
if
(
strcmp
(
tokens
[
0
],
"cd"
)
==
TRUE
)
{
if
(
tokens
[
1
]
==
NULL
)
{
fprintf
(
stderr
,
"bsh: expected one argument for cd"
);
continue
;
}
else
{
chdir
(
tokens
[
1
]);
}
}
int
runInBackground
=
0
;
if
(
strcmp
(
lastToken
,
"&"
)
==
TRUE
)
{
runInBackground
=
1
;
tokens
[
numTokens
-
2
]
=
NULL
;
}
if
(
fork
()
==
0
)
{
int
returnCode
=
execvp
(
tokens
[
0
],
tokens
);
printf
(
"Return code %d
\n
"
,
returnCode
);
}
else
{
wait
(
NULL
);
if
(
runInBackground
==
0
)
{
wait
(
NULL
);
}
}
free
(
readBuff
);
free
(
tokens
);
}
}
//
}
}
\ No newline at end of file
tokenize.c
View file @
f7775a38
...
...
@@ -4,7 +4,7 @@
#include "tokenize.h"
char
**
tokenize
(
char
*
string
,
char
**
output
)
{
int
tokenize
(
char
*
string
,
char
**
output
)
{
char
*
strtok_save
;
char
*
del
=
" "
;
output
[
0
]
=
strtok_r
(
string
,
del
,
&
strtok_save
);
...
...
@@ -15,5 +15,5 @@ char **tokenize(char *string, char **output) {
output
[
i
]
=
token
;
i
++
;
}
return
output
;
return
i
;
}
tokenize.h
View file @
f7775a38
...
...
@@ -5,5 +5,5 @@
#ifndef COMS352_SHELL_TOKENIZE_H
#define COMS352_SHELL_TOKENIZE_H
#include <string.h>
char
**
tokenize
(
char
*
string
,
char
**
output
);
int
tokenize
(
char
*
string
,
char
**
output
);
#endif //COMS352_SHELL_TOKENIZE_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment