back to scratko.xyz
summaryrefslogtreecommitdiff
path: root/lexical_analysis.c
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-06-14 15:15:41 +0300
committerscratko <m@scratko.xyz>2024-06-23 23:11:18 +0300
commitff38bddd4253b5adf08a84df34bfae32c8ae988d (patch)
tree69e39bf16cb93af3b9a592cf0fa3c489b13db499 /lexical_analysis.c
parent1ee0911f2def54f1b63d18b0924ac65c2db92f11 (diff)
downloadshell-V.tar.gz
shell-V.tar.bz2
shell-V.zip
Shell-V releaseshell-V
Added pipeline. Formatting shell.c (created lexical_analysys.c). Error codes. Some functions of queue.c are created by preprocessor.
Diffstat (limited to 'lexical_analysis.c')
-rw-r--r--lexical_analysis.c50
1 files changed, 30 insertions, 20 deletions
diff --git a/lexical_analysis.c b/lexical_analysis.c
index f931118..4349a69 100644
--- a/lexical_analysis.c
+++ b/lexical_analysis.c
@@ -1,5 +1,6 @@
#include "lexical_analysis.h"
-MAKE_QUEUE_PUSH(c_queue, cmdline, char**)
+
+#include <stdio.h>
int is_double_quotes_pair(struct param_type params)
{
@@ -48,18 +49,14 @@ int is_empty_word(int ch, struct param_type params)
int command_execution_condition(struct param_type *params)
{
return
- (!filename_waiting(params) && !params->pipeline) ||
+ (params->tokens != '<' && params->tokens != '>' &&
+ params->tokens != append && !params->pipeline) ||
(params->pipeline && params->tokens == '&');
}
-int is_special_token(int ch)
+int is_first_special_token_character(int ch)
{
- return ch == and || ch == or || ch == '&' || ch == ';' || ch == '|';
-}
-
-int is_redirect_token(int ch, int next_ch)
-{
- return ch == '<' || ch == '>' || (ch == '>' && next_ch == '>');
+ return ch == '<' || ch == '>' || ch == '&' || ch == '|' || ch == ';';
}
int excessive_words(int ch, struct param_type *params)
@@ -72,8 +69,10 @@ int excessive_words(int ch, struct param_type *params)
while((next_ch = getchar()) != new_line) {
if(next_ch == ' ')
continue;
- if(!is_special_token(next_ch) && next_ch != '<' && next_ch != '>')
+ if(!is_first_special_token_character(next_ch)) {
+ params->wrong_command = err_extra_chars_after_filename;
return 1;
+ }
else
break;
}
@@ -88,7 +87,7 @@ void add_word_or_filename(struct w_queue *word_chain,
{
/* filenames */
if(filename_waiting(params) && !params->wrong_command)
- add_filename(tmp_word, params);
+ add_filename(tmp_word, params);
/* execute command */
else if(params->is_word)
add_word(word_chain, tmp_word, params);
@@ -108,6 +107,17 @@ int is_double_token(struct param_type *params)
params->tokens == append;
}
+int is_special_token(int ch, int next_ch)
+{
+ return (ch == '&' && next_ch == '&') || (ch == '|' && ch == '|') ||
+ ch == '&' || ch == ';' || ch == '|';
+}
+
+int is_redirect_token(int ch, int next_ch)
+{
+ return ch == '<' || ch == '>' || (ch == '>' && next_ch == '>');
+}
+
/*
* redirection token verification
*/
@@ -130,14 +140,16 @@ int stream_redirect_tokens(struct w_queue *word_chain,
if(is_double_token(params))
getchar();
return 1;
- } else {
- fprintf(stderr, "syntax error\n");
- params->wrong_command = 1;
- }
+ } else
+ params->wrong_command = err_redirect_stream_again;
}
return 0;
}
+/*
+ * the first element of the pipeline outputs to a file or
+ * redirects to a file in the middle of the pipeline
+ */
int wrong_streams_redirection(struct param_type *params)
{
return
@@ -156,8 +168,7 @@ int pipeline_token_processing(struct w_queue *word_chain,
char **cmdline = NULL;
if(is_stream_redirection_set(params) &&
wrong_streams_redirection(params)) {
- /* TODO: add error codes */
- params->wrong_command = 1;
+ params->wrong_command = err_redirect_stream_in_pipeline;
return 0;
}
params->tokens = '|';
@@ -182,7 +193,7 @@ int special_tokens(struct w_queue *word_chain, struct c_queue *cmdlines,
next_ch = getchar();
ungetc(next_ch, stdin);
- if(is_special_token(ch)) {
+ if(is_special_token(ch, next_ch)) {
add_word_or_filename(word_chain, tmp_word, params);
if(params->wrong_command)
@@ -199,8 +210,7 @@ int special_tokens(struct w_queue *word_chain, struct c_queue *cmdlines,
else if(ch == '&') {
while((ch = getchar()) != new_line) {
if(ch != whitespace && ch != tab) {
- fprintf(stderr, "incorrect command\n");
- params->wrong_command = 1;
+ params->wrong_command = err_bg_process;
return 0;
}
}