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
f9aa2b3d
Commit
f9aa2b3d
authored
Mar 01, 2018
by
Matthew Burket
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More work on history
parent
a4dface0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
6 deletions
+71
-6
main.c
main.c
+71
-6
No files found.
main.c
View file @
f9aa2b3d
...
...
@@ -3,6 +3,8 @@
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "tokenize.h"
#include "file_operations.h"
...
...
@@ -11,10 +13,14 @@
#define READ 0
void
storeHist
(
char
*
readBuff
,
int
*
historyPos
,
char
historyBuff
[
50
][
BUFFSIZE
]);
int
main
()
{
// Setup Some Vars
int
should_run
=
1
;
char
*
readBuff
=
malloc
(
BUFFSIZE
);
char
historyBuff
[
50
][
BUFFSIZE
];
int
historyPos
=
-
1
;
char
*
HOME
=
getenv
(
"HOME"
);
// Copyright statement
fprintf
(
stdout
,
"Welcome to Burket Shell (bsh)
\n
"
);
...
...
@@ -26,10 +32,6 @@ int main() {
fprintf
(
stdout
,
"bsh # "
);
// Getting User Input
fgets
(
readBuff
,
sizeof
(
char
)
*
5000
,
stdin
);
// History store stuff
if
(
strlen
(
readBuff
)
>
0
)
{
//
}
// Remove the newline from fgets
strtok
(
readBuff
,
"
\n
"
);
if
(
strcmp
(
"exit"
,
readBuff
)
==
TRUE
)
{
...
...
@@ -37,12 +39,16 @@ int main() {
free
(
readBuff
);
exit
(
EXIT_SUCCESS
);
}
char
**
tokens
=
malloc
(
sizeof
(
char
)
*
5000
);
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"
);
free
(
tokens
);
storeHist
(
readBuff
,
&
historyPos
,
historyBuff
);
continue
;
}
else
{
if
(
strcmp
(
tokens
[
1
],
"~"
)
==
0
)
{
...
...
@@ -51,13 +57,62 @@ int main() {
}
else
if
(
strcmp
(
tokens
[
1
],
"-"
)
==
0
)
{
chdir
(
getenv
(
"OLDPWD"
));
strcpy
(
tokens
[
1
],
getenv
(
"OLDPWD"
));
}
else
{
}
else
{
chdir
(
tokens
[
1
]);
}
}
}
if
(
strcmp
(
tokens
[
0
],
"history"
)
==
0
)
{
if
(
historyPos
==
-
1
)
{
fprintf
(
stderr
,
"
\n
No commands in history
\n
"
);
storeHist
(
readBuff
,
&
historyPos
,
historyBuff
);
free
(
tokens
);
continue
;
}
for
(
int
i
=
historyPos
;
i
>=
0
;
i
--
)
{
fprintf
(
stdout
,
"%2d %s
\n
"
,
i
+
1
,
historyBuff
[
i
]);
}
storeHist
(
readBuff
,
&
historyPos
,
historyBuff
);
free
(
tokens
);
continue
;
}
char
*
ptr
=
malloc
(
sizeof
(
char
)
*
10
);
if
(
*
tokens
[
0
]
==
'!'
)
{
errno
=
0
;
// Using strtol allows us to do error checking
long
cmdNum
=
strtol
(
tokens
[
0
]
+
1
,
&
ptr
,
10
);
if
(
errno
!=
0
)
{
printf
(
"Invalid history command lookup!"
);
free
(
tokens
);
free
(
ptr
);
continue
;
}
if
(
cmdNum
<=
0
||
cmdNum
>
historyPos
)
{
printf
(
"Invalid history command lookup!"
);
free
(
tokens
);
free
(
ptr
);
continue
;
}
free
(
ptr
);
}
if
(
strcmp
(
tokens
[
0
],
"!!"
)
==
0
)
{
if
(
historyPos
==
-
1
)
{
fprintf
(
stderr
,
"
\n
bsh: No such command in history.
\n
"
);
storeHist
(
readBuff
,
&
historyPos
,
historyBuff
);
free
(
tokens
);
continue
;
}
else
{
strcpy
(
readBuff
,
historyBuff
[
historyPos
]);
}
}
storeHist
(
readBuff
,
&
historyPos
,
historyBuff
);
int
runInBackground
=
0
;
if
(
strcmp
(
lastToken
,
"&"
)
==
TRUE
)
{
runInBackground
=
1
;
...
...
@@ -76,4 +131,14 @@ int main() {
}
free
(
tokens
);
}
}
void
storeHist
(
char
*
readBuff
,
int
*
historyPos
,
char
historyBuff
[
50
][
BUFFSIZE
])
{
// History store stuff
if
(
strlen
(
readBuff
)
>
0
)
{
if
(
*
historyPos
<
49
)
{
*
historyPos
=
*
historyPos
+
1
;
strcpy
(
historyBuff
[
*
historyPos
],
readBuff
);
}
}
}
\ No newline at end of file
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