Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs283
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
Luis Hernandez
cs283
Commits
f275573f
Commit
f275573f
authored
3 months ago
by
luishernandez
Browse files
Options
Downloads
Patches
Plain Diff
3-ShellP1
parent
fdf36dd1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dsh_cli.c
+86
-0
86 additions, 0 deletions
dsh_cli.c
with
86 additions
and
0 deletions
dsh_cli.c
0 → 100644
+
86
−
0
View file @
f275573f
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<ctype.h>
#include
"dshlib.h"
/*
* main:
* - prompts the user for input using SH_PROMPT
* - reads a line (up to SH_CMD_MAX characters)
* - if the line is "exit" (after trimming leading spaces) then the shell quits
* - if the line is blank, prints CMD_WARN_NO_CMD and loops again
* - otherwise, calls build_cmd_list() to parse the command line
* - on OK, prints the header and then for each command prints:
* <i>exe if there are no arguments, or
* <i>exe[args] if there are arguments.
* - if build_cmd_list returns ERR_TOO_MANY_COMMANDS, prints CMD_ERR_PIPE_LIMIT.
*/
int
main
(
void
)
{
char
cmd_buff
[
SH_CMD_MAX
];
int
rc
;
command_list_t
clist
;
while
(
1
)
{
/* Print the shell prompt */
printf
(
"%s"
,
SH_PROMPT
);
/* Read a line from stdin. If EOF is encountered, print a newline and exit. */
if
(
fgets
(
cmd_buff
,
sizeof
(
cmd_buff
),
stdin
)
==
NULL
)
{
printf
(
"
\n
"
);
break
;
}
/* Remove the trailing newline character */
cmd_buff
[
strcspn
(
cmd_buff
,
"
\n
"
)]
=
'\0'
;
/* Trim leading whitespace manually */
char
*
start
=
cmd_buff
;
while
(
*
start
&&
isspace
((
unsigned
char
)
*
start
))
{
start
++
;
}
/* If the command is exactly EXIT_CMD, exit the shell */
if
(
strcmp
(
start
,
EXIT_CMD
)
==
0
)
{
break
;
}
/* If nothing remains after trimming, warn and loop again */
if
(
*
start
==
'\0'
)
{
printf
(
"%s"
,
CMD_WARN_NO_CMD
);
continue
;
}
/*
* Since build_cmd_list modifies the input string (by using strtok),
* make a copy of the (trimmed) command line.
*/
char
cmd_line_copy
[
SH_CMD_MAX
];
strncpy
(
cmd_line_copy
,
start
,
sizeof
(
cmd_line_copy
)
-
1
);
cmd_line_copy
[
sizeof
(
cmd_line_copy
)
-
1
]
=
'\0'
;
rc
=
build_cmd_list
(
cmd_line_copy
,
&
clist
);
if
(
rc
==
OK
)
{
/* Print the header showing the total number of commands parsed */
printf
(
CMD_OK_HEADER
,
clist
.
num
);
for
(
int
i
=
0
;
i
<
clist
.
num
;
i
++
)
{
printf
(
"<%d>%s"
,
i
+
1
,
clist
.
commands
[
i
].
exe
);
/* If any arguments were found, print them inside square brackets */
if
(
strlen
(
clist
.
commands
[
i
].
args
)
>
0
)
{
printf
(
"[%s]"
,
clist
.
commands
[
i
].
args
);
}
}
printf
(
"
\n
"
);
}
else
if
(
rc
==
ERR_TOO_MANY_COMMANDS
)
{
printf
(
CMD_ERR_PIPE_LIMIT
,
CMD_MAX
);
printf
(
"
\n
"
);
}
/* (Optionally, you could handle ERR_CMD_OR_ARGS_TOO_BIG here) */
}
return
0
;
}
\ 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