/** * @file parser.h * @brief Input sources, the parser's interface, and aliases. */ #ifndef ZASH_PARSER_H #define ZASH_PARSER_H #include "ast.h" /** * @brief End of input, as returned by pgetc(). */ #define PEOF (-1) /* Defined below; an input can be the text of an alias. */ typedef struct alias alias; /** * @brief One source of shell text: a file descriptor or a string. * * Sources form a stack. The parser reads from the top one. */ typedef struct input_src { struct input_src *prev; const char *p; const char *end; char *buf; /**< read buffer for fd inputs */ int fd; /**< -1 for string inputs */ int lineno; bool eof; bool bytewise; /**< unseekable stdin: never read ahead */ bool seekable; /**< seekable stdin: give back read-ahead before commands */ bool tty; /**< print prompts */ bool autopop; /**< string input that vanishes when it runs out */ alias *alias; /**< the alias whose text this is, if any */ } input_src; /* ---- aliases (alias.c) ----------------------------------------------- */ /** * @brief An alias. */ typedef struct alias { struct alias *next; char *name; char *value; bool in_use; /**< its text is being read: do not expand it again */ } alias; extern int alias_count; /**< number of aliases defined */ extern bool alias_ended_blank; /**< see next_token() in parser.c */ alias *alias_find(const char *name); void alias_set(const char *name, const char *value); void alias_each(void (*fn)(const char *name, void *arg), void *arg); void input_push_alias(alias *a); void input_push_back(const char *s, size_t len); extern input_src *input; extern int prompt_state; /**< 0: none, 1: PS1 next, 2: PS2 next */ void input_push_fd(int fd, bool is_stdin); void input_push_string(const char *s, size_t len); void input_pop(void); void input_unwind(input_src *to); int input_fill(void); void input_sync(void); void input_discard_line(void); bool input_at_end(void); /** * @brief Read the next character of input. * * The common case is one compare and one load. input_fill() handles the * rest. */ static inline int pgetc(void) { return input->p < input->end ? (unsigned char)*input->p++ : input_fill(); } /* * Parse one complete command line into `a`. Returns NULL for a blank line; * sets *eof when the input is exhausted. */ node *parse_command(arena *a, bool *eof); void parser_reset(void); wpart *parse_text_parts(arena *a, const char *s, size_t len); /** * @brief Room for a snapshot of the parser's state. See parser_save(). */ #define PARSER_STATE_SIZE ((size_t)96) void parser_save(void *state); void parser_restore(const void *state); #endif