Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs503zchen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ziheng Chen
cs503zchen
Commits
06044eb6
Commit
06044eb6
authored
4 months ago
by
Ziheng Chen
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
75ee0b9f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
w5/codes/dsh_cli.c
+108
-0
108 additions, 0 deletions
w5/codes/dsh_cli.c
with
108 additions
and
0 deletions
w5/codes/dsh_cli.c
0 → 100644
+
108
−
0
View file @
06044eb6
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
"dshlib.h"
/*
* Implement your main function by building a loop that prompts the
* user for input. Use the SH_PROMPT constant from dshlib.h and then
* use fgets to accept user input. Since we want fgets to also handle
* end of file so we can run this headless for testing we need to check
* the return code of fgets. I have provided an example below of how
* to do this assuming you are storing user input inside of the cmd_buff
* variable.
*
* while(1){
* printf("%s", SH_PROMPT);
* if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){
* printf("\n");
* break;
* }
* //remove the trailing \n from cmd_buff
* cmd_buff[strcspn(cmd_buff,"\n")] = '\0';
*
* //IMPLEMENT THE REST OF THE REQUIREMENTS
* }
*
* Also, use the constants in the dshlib.h in this code.
* SH_CMD_MAX maximum buffer size for user input
* EXIT_CMD constant that terminates the dsh program
* SH_PROMPT the shell prompt
* OK the command was parsed properly
* WARN_NO_CMDS the user command was empty
* ERR_TOO_MANY_COMMANDS too many pipes used
*
* Expected output:
*
* CMD_OK_HEADER if the command parses properly. You will
* follow this by the command details
*
* CMD_WARN_NO_CMD if the user entered a blank command
* CMD_ERR_PIPE_LIMIT if the user entered too many commands using
* the pipe feature, e.g., cmd1 | cmd2 | ... |
*
* See the provided test cases for output expectations.
*/
int
main
()
{
char
cmd_buff
[
SH_CMD_MAX
];
// Buffer for user input
int
rc
=
0
;
command_list_t
clist
;
while
(
1
)
{
// Display the shell prompt
printf
(
"%s"
,
SH_PROMPT
);
fflush
(
stdout
);
// Ensure prompt is displayed before user input
// Read input from user
if
(
fgets
(
cmd_buff
,
SH_CMD_MAX
,
stdin
)
==
NULL
)
{
printf
(
"
\n
"
);
// Handle EOF
//rc = 0;
break
;
}
// Remove trailing newline character
cmd_buff
[
strcspn
(
cmd_buff
,
"
\n
"
)]
=
'\0'
;
// Check for empty input
if
(
strlen
(
cmd_buff
)
==
0
)
{
printf
(
CMD_WARN_NO_CMD
);
continue
;
}
// Exit command handling
if
(
strcmp
(
cmd_buff
,
EXIT_CMD
)
==
0
)
{
break
;
}
// Parse the input into command list
rc
=
build_cmd_list
(
cmd_buff
,
&
clist
);
// Handle possible errors
if
(
rc
==
WARN_NO_CMDS
)
{
printf
(
CMD_WARN_NO_CMD
);
rc
=
0
;
// Reset error code
}
else
if
(
rc
==
ERR_TOO_MANY_COMMANDS
)
{
printf
(
CMD_ERR_PIPE_LIMIT
,
CMD_MAX
);
rc
=
0
;
// Reset error code
}
else
{
// Print parsed output
printf
(
CMD_OK_HEADER
,
clist
.
num
);
for
(
int
i
=
0
;
i
<
clist
.
num
;
i
++
)
{
if
(
strlen
(
clist
.
commands
[
i
].
args
)
>
0
)
{
printf
(
"<%d>%s[%s]
\n
"
,
i
+
1
,
clist
.
commands
[
i
].
exe
,
clist
.
commands
[
i
].
args
);
}
else
{
printf
(
"<%d>%s
\n
"
,
i
+
1
,
clist
.
commands
[
i
].
exe
);
}
}
}
}
return
0
;
//printf(M_NOT_IMPL);
//exit(EXIT_NOT_IMPL);
}
\ No newline at end of file
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