Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp 1 - shell
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
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sebastie.gendre
tp 1 - shell
Commits
657d2f4c
Commit
657d2f4c
authored
4 years ago
by
Sébastien Gendre
Browse files
Options
Downloads
Patches
Plain Diff
Rewrite command parsing to simplify it
parent
fa84dcbd
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/command.c
+31
-78
31 additions, 78 deletions
src/command.c
with
31 additions
and
78 deletions
src/command.c
+
31
−
78
View file @
657d2f4c
...
...
@@ -3,100 +3,53 @@
#include
"command.h"
int
parse_user_input_
for
_command
(
char
*
user_input
,
cmd_t
*
cmd
)
{
int
parse_user_input_
to
_command
(
char
*
user_input
,
cmd_t
*
cmd
)
{
// Stor next command argument, used in the do…while loop
char
*
next_arg
;
//Initialize a simple command (empty, simple, foreground)
cmd
->
command
=
NULL
;
cmd
->
argv
=
NULL
;
cmd
->
argc
=
-
1
;
cmd
->
argc
=
0
;
cmd
->
foreground
=
true
;
cmd
->
redirect_to
=
NULL
;
//Separate string in different token (i.e. command name + params + &)
do
{
//A new element will be added
cmd
->
argc
+=
1
;
//Allocate a new pointer on char for next argv element
if
((
cmd
->
argv
=
realloc
(
cmd
->
argv
,
(
cmd
->
argc
+
1
)
*
sizeof
(
char
*
)))
==
NULL
)
die_errno
(
"parse_command::realloc"
);
//Get the adress opf the next token (could be NULL to indicate end of argv)
cmd
->
argv
[
cmd
->
argc
]
=
strtok
(
user_input
,
DELIMIERS
);
user_input
=
NULL
;
//Useless to execute it each time but easier than having two different strtok calls
}
while
(
cmd
->
argv
[
cmd
->
argc
]
!=
NULL
);
// while there are still tokens
//Search for rediction symbols (>, >>, <) in the first cmd
for
(
int
i
=
0
;
i
<
cmd
->
argc
;
i
++
)
{
if
(
strcmp
(
">"
,
cmd
->
argv
[
i
])
==
0
)
cmd
->
type
=
CMD_FILEOUT
;
if
(
strcmp
(
">>"
,
cmd
->
argv
[
i
])
==
0
)
cmd
->
type
=
CMD_FILEAPPEND
;
if
(
strcmp
(
"<"
,
cmd
->
argv
[
i
])
==
0
)
cmd
->
type
=
CMD_FILEIN
;
// if symbol was found at i
if
((
cmd
->
type
==
CMD_FILEOUT
)
|
(
cmd
->
type
==
CMD_FILEAPPEND
)
|
(
cmd
->
type
==
CMD_FILEIN
))
{
if
(
cmd
->
argc
>
i
+
1
)
{
//Allocate a size two table of char* to store file + NULL
if
((
cmd
->
argv2
=
realloc
(
cmd
->
argv2
,
2
*
sizeof
(
char
*
)))
==
NULL
)
die_errno
(
"parse_command::realloc"
);
// Extract the commmand name
cmd
->
command
=
strtok
(
user_input
,
DELIMITERS
);
// copy argument following symbol to argv2
cmd
->
argv2
[
0
]
=
cmd
->
argv
[
i
+
1
];
cmd
->
argv2
[
1
]
=
NULL
;
cmd
->
argc2
=
1
;
// update argv to ignore symbol on the following args
cmd
->
argv
[
i
]
=
NULL
;
cmd
->
argc
=
i
;
// Extract the command arguments
do
{
// Get the next argument
next_arg
=
strtok
(
NULL
,
DELIMITERS
);
if
(
next_arg
!=
NULL
)
{
// A new element will be added to cmd->argv
cmd
->
argc
+=
1
;
// Extend the cmd->argv array witha new char pointer
cmd
->
argv
=
realloc
(
cmd
->
argv
,
(
cmd
->
argc
)
*
sizeof
(
char
*
));
if
(
cmd
->
argv
==
NULL
)
{
// In case the reallocation didn't work
die_errno
(
"parse_command::realloc"
);
}
else
return
-
1
;
//Nothing after symbol
}
}
//Search for pipe symbols in the first cmd
// TODO: this look very similar to indirection symbols search -> combine / creat common function ?
for
(
int
i
=
0
;
i
<
cmd
->
argc
;
i
++
)
{
if
(
strcmp
(
"|"
,
cmd
->
argv
[
i
])
==
0
)
{
if
(
cmd
->
argc
>
i
+
1
)
{
// If not args after & ignore it
cmd
->
type
=
CMD_PIPE
;
//Allocate a new pointer on char for next argv element
cmd
->
argc2
=
cmd
->
argc
-
i
-
1
;
if
((
cmd
->
argv2
=
realloc
(
cmd
->
argv2
,
(
cmd
->
argc
-
i
)
*
sizeof
(
char
*
)))
==
NULL
)
//one more than argc cause last is NULL
die_errno
(
"parse_command::realloc"
);
//copy arguments after the & to the argv2
for
(
int
j
=
0
;
j
<
cmd
->
argc2
;
j
++
)
// copy argument following symbol to argv2
cmd
->
argv2
[
j
]
=
cmd
->
argv
[
i
+
j
+
1
];
// update argv to ignore symbol on the following args
cmd
->
argv
[
i
]
=
NULL
;
cmd
->
argc
=
i
;
// Only first pipe symbol is considered no more should be found
break
;
}
// Store the argument inside cmd->argv
cmd
->
argv
[
cmd
->
argc
-
1
]
=
next_arg
;
}
}
//Check if the last command is a '&' to decide if we should run command
//as a foreground or background job
if
(
strcmp
(
cmd
->
argv
[
cmd
->
argc
-
1
],
"&"
)
==
0
)
{
cmd
->
foreground
=
false
;
//Delete the & from the command for execution
cmd
->
argv
[
cmd
->
argc
-
1
]
=
NULL
;
cmd
->
argc
--
;
}
}
while
(
next_arg
!=
NULL
);
return
0
;
}
void
dispose_command
(
cmd_t
*
cmd
)
{
// Free the command name str
free
(
cmd
->
command
);
// Free all the command arguments str inside argv
for
(
int
i
=
0
;
i
<
cmd
->
argc
;
++
i
)
{
free
(
cmd
->
argv
[
i
]);
}
// Then, finally, free argv
free
(
cmd
->
argv
);
}
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