/* * include/lexer.h * * HUSH lexical analysis module * CISC 301 -- Operating Systems, Project 2 * * Copyright (C) 2025 Douglas B. Rumbaugh * * Distributed under the Modified BSD License * */ #ifndef H_HUSH_LEXER #define H_HUSH_LEXER #include #include #include #include #include #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