diff options
Diffstat (limited to 'include/lexer.h')
| -rw-r--r-- | include/lexer.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/include/lexer.h b/include/lexer.h new file mode 100644 index 0000000..bec44a9 --- /dev/null +++ b/include/lexer.h @@ -0,0 +1,75 @@ +/* + * Header file for the HUSH lexical analysis + * module + */ +#ifndef H_HUSH_LEXER +#define H_HUSH_LEXER + +#include <ctype.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <stdio.h> + +#include "config.h" + +enum token_type { + TKN_COMMAND, + TKN_OUT_REDIR, + TKN_IN_REDIR, + TKN_PIPE, + TKN_FILENAME, + TKN_ARG, + TKN_INVALID, + TKN_VARKEY, + TKN_VARVAL +}; + +typedef struct token { + char *text; + enum token_type type; + struct token *next; +} token; + +/* + * Accepts a null-terminate string representing + * a full command and returns the pointer to the + * head of a list of parsed tokens. The provided + * string must have been trimmed to remove all + * leading and trailing whitespace characters. + * + * The tokens contain pointers into the provided + * command string, and so the parsed command is + * only valid so long as the provided string has + * not been altered. + * + * This function's behavior is undefined if the + * input string is *not* null-terminated, or if + * it has leading or trailing whitespace. + */ + +token *parse_command(char *cmdstr); + +/* + * Accepts the pointer to the head of a list + * of parsed tokens and validates that the + * tokens are syntactically valid. If the + * sequence is valid, NULL will be returned. + * If the sequence is invalid, a pointer to + * the first invalid token will be returned. + */ +token *validate_command(token *cmd); + +/* + * Removes all leading and trailing whitespace from + * the provided string, and returns a pointer to the + * beginning of the trimmed string. Necessary as the + * first stage of parsing. + */ +char *trim(char *str); + +void print_parsed_command(FILE *file, token *cmd); + +void destroy_tokens(token *tokens); + +#endif |