summaryrefslogtreecommitdiffhomepage
path: root/src/parser.h
blob: e2853d86973ea88042f37f83da6b680051da32e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * @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