mrsh/highlight.c -rw-r--r-- 10.0 KiB View raw
                                                                                
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <assert.h>
#include <errno.h>
#include <mrsh/buffer.h>
#include <mrsh/parser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define READ_SIZE 4096
#define FORMAT_STACK_SIZE 64

enum format {
	FORMAT_RESET = 0,
	FORMAT_GREEN = 32,
	FORMAT_YELLOW = 33,
	FORMAT_BLUE = 34,
	FORMAT_CYAN = 36,
	FORMAT_DEFAULT = 39,
	FORMAT_LIGHT_BLUE = 94,
};

struct highlight_state {
	const char *buf;
	size_t offset;

	enum format fmt_stack[FORMAT_STACK_SIZE];
	size_t fmt_stack_len;
};

static void highlight(struct highlight_state *state, struct mrsh_position *pos,
		enum format fmt) {
	assert(pos->offset >= state->offset);

	fwrite(&state->buf[state->offset], sizeof(char),
		pos->offset - state->offset, stdout);
	state->offset = pos->offset;

	if (fmt == FORMAT_RESET) {
		assert(state->fmt_stack_len > 0);
		--state->fmt_stack_len;
		if (state->fmt_stack_len > 0) {
			fmt = state->fmt_stack[state->fmt_stack_len - 1];
		}
	} else {
		if (state->fmt_stack_len >= FORMAT_STACK_SIZE) {
			fprintf(stderr, "format stack overflow\n");
			exit(EXIT_FAILURE);
		}
		state->fmt_stack[state->fmt_stack_len] = fmt;
		++state->fmt_stack_len;
	}

	fprintf(stdout, "%c[%dm", 0x1B, fmt);
}

static void highlight_str(struct highlight_state *state,
		struct mrsh_range *range, enum format fmt) {
	highlight(state, &range->begin, fmt);
	highlight(state, &range->end, FORMAT_RESET);
}

static void highlight_char(struct highlight_state *state,
		struct mrsh_position *pos, enum format fmt) {
	struct mrsh_position next = { .offset = pos->offset + 1 };
	highlight(state, pos, fmt);
	highlight(state, &next, FORMAT_RESET);
}

static void highlight_word(struct highlight_state *state,
		struct mrsh_word *word, bool cmd_name, bool quoted) {
	switch (word->type) {
	case MRSH_WORD_STRING:;
		struct mrsh_word_string *ws = mrsh_word_get_string(word);
		if (!quoted) {
			enum format fmt = FORMAT_CYAN;
			if (ws->single_quoted) {
				fmt = FORMAT_YELLOW;
			} else if (cmd_name) {
				fmt = FORMAT_BLUE;
			}
			highlight_str(state, &ws->range, fmt);
		}
		break;
	case MRSH_WORD_PARAMETER:;
		struct mrsh_word_parameter *wp = mrsh_word_get_parameter(word);
		highlight(state, &wp->dollar_pos, FORMAT_CYAN);
		highlight_char(state, &wp->dollar_pos, FORMAT_GREEN);
		if (mrsh_position_valid(&wp->lbrace_pos)) {
			highlight_char(state, &wp->lbrace_pos, FORMAT_GREEN);
		}
		if (mrsh_range_valid(&wp->op_range)) {
			highlight_str(state, &wp->op_range, FORMAT_GREEN);
		}
		if (wp->arg != NULL) {
			highlight_word(state, wp->arg, false, false);
		}
		struct mrsh_position end = {0};
		if (mrsh_position_valid(&wp->rbrace_pos)) {
			highlight_char(state, &wp->rbrace_pos, FORMAT_GREEN);
			end.offset = wp->rbrace_pos.offset + 1;
		} else {
			end = wp->name_range.end;
		}
		highlight(state, &end, FORMAT_RESET);
		break;
	case MRSH_WORD_COMMAND:;
		struct mrsh_word_command *wc = mrsh_word_get_command(word);
		if (wc->back_quoted) {
			highlight_char(state, &wc->range.begin, FORMAT_GREEN);
		}
		// TODO: highlight inside
		if (wc->back_quoted) {
			struct mrsh_position rquote = {
				.offset = wc->range.end.offset - 1,
			};
			highlight_char(state, &rquote, FORMAT_GREEN);
		}
		break;
	case MRSH_WORD_ARITHMETIC:
		assert(false); // TODO
	case MRSH_WORD_LIST:;
		struct mrsh_word_list *wl = mrsh_word_get_list(word);
		if (wl->children.len == 0) {
			break;
		}

		if (wl->double_quoted) {
			highlight(state, &wl->lquote_pos, FORMAT_YELLOW);
		}

		for (size_t i = 0; i < wl->children.len; ++i) {
			struct mrsh_word *child = wl->children.data[i];
			highlight_word(state, child, cmd_name, wl->double_quoted);
		}

		if (wl->double_quoted) {
			struct mrsh_position end = { .offset = wl->rquote_pos.offset + 1 };
			highlight(state, &end, FORMAT_RESET);
		}
		break;
	}
}

static void highlight_simple_command(struct highlight_state *state,
		struct mrsh_simple_command *cmd) {
	if (cmd->name != NULL) {
		highlight_word(state, cmd->name, true, false);
	}

	for (size_t i = 0; i < cmd->arguments.len; ++i) {
		struct mrsh_word *arg = cmd->arguments.data[i];
		highlight_word(state, arg, false, false);
	}

	// TODO: cmd->io_redirects, cmd->assignments
}

static void highlight_command_list_array(struct highlight_state *state,
	struct mrsh_array *array);

static void highlight_command(struct highlight_state *state,
		struct mrsh_command *cmd) {
	switch (cmd->type) {
	case MRSH_SIMPLE_COMMAND:;
		struct mrsh_simple_command *sc = mrsh_command_get_simple_command(cmd);
		highlight_simple_command(state, sc);
		break;
	case MRSH_BRACE_GROUP:;
		struct mrsh_brace_group *bg = mrsh_command_get_brace_group(cmd);
		highlight_char(state, &bg->lbrace_pos, FORMAT_GREEN);
		highlight_command_list_array(state, &bg->body);
		highlight_char(state, &bg->rbrace_pos, FORMAT_GREEN);
		break;
	case MRSH_SUBSHELL:;
		struct mrsh_subshell *s = mrsh_command_get_subshell(cmd);
		highlight_char(state, &s->lparen_pos, FORMAT_GREEN);
		highlight_command_list_array(state, &s->body);
		highlight_char(state, &s->rparen_pos, FORMAT_GREEN);
		break;
	case MRSH_IF_CLAUSE:;
		struct mrsh_if_clause *ic = mrsh_command_get_if_clause(cmd);
		highlight_str(state, &ic->if_range, FORMAT_BLUE);
		highlight_command_list_array(state, &ic->condition);
		highlight_str(state, &ic->then_range, FORMAT_BLUE);
		highlight_command_list_array(state, &ic->body);
		if (ic->else_part != NULL) {
			if (mrsh_range_valid(&ic->else_range)) {
				highlight_str(state, &ic->else_range, FORMAT_BLUE);
			}
			highlight_command(state, ic->else_part);
		}
		if (mrsh_range_valid(&ic->fi_range)) {
			highlight_str(state, &ic->fi_range, FORMAT_BLUE);
		}
		break;
	case MRSH_FOR_CLAUSE:;
		struct mrsh_for_clause *fc = mrsh_command_get_for_clause(cmd);
		highlight_str(state, &fc->for_range, FORMAT_BLUE);
		highlight_str(state, &fc->name_range, FORMAT_CYAN);
		if (mrsh_range_valid(&fc->in_range)) {
			highlight_str(state, &fc->in_range, FORMAT_BLUE);
		}
		for (size_t i = 0; i < fc->word_list.len; ++i) {
			struct mrsh_word *word = fc->word_list.data[i];
			highlight_word(state, word, false, false);
		}
		highlight_str(state, &fc->do_range, FORMAT_BLUE);
		highlight_command_list_array(state, &fc->body);
		highlight_str(state, &fc->done_range, FORMAT_BLUE);
		break;
	case MRSH_LOOP_CLAUSE:;
		struct mrsh_loop_clause *lc = mrsh_command_get_loop_clause(cmd);
		highlight_str(state, &lc->while_until_range, FORMAT_BLUE);
		highlight_command_list_array(state, &lc->condition);
		highlight_str(state, &lc->do_range, FORMAT_BLUE);
		highlight_command_list_array(state, &lc->body);
		highlight_str(state, &lc->done_range, FORMAT_BLUE);
		break;
	case MRSH_CASE_CLAUSE:;
		struct mrsh_case_clause *cc = mrsh_command_get_case_clause(cmd);
		highlight_str(state, &cc->case_range, FORMAT_BLUE);
		highlight_word(state, cc->word, false, false);
		highlight_str(state, &cc->in_range, FORMAT_BLUE);
		for (size_t i = 0; i < cc->items.len; ++i) {
			struct mrsh_case_item *item = cc->items.data[i];
			if (mrsh_position_valid(&item->lparen_pos)) {
				highlight_char(state, &item->lparen_pos, FORMAT_GREEN);
			}
			for (size_t j = 0; j < item->patterns.len; ++j) {
				struct mrsh_word *pattern = item->patterns.data[j];
				highlight_word(state, pattern, false, false);
			}
			highlight_char(state, &item->rparen_pos, FORMAT_GREEN);
			highlight_command_list_array(state, &item->body);
			if (mrsh_range_valid(&item->dsemi_range)) {
				highlight_str(state, &item->dsemi_range, FORMAT_GREEN);
			}
		}
		highlight_str(state, &cc->esac_range, FORMAT_BLUE);
		break;
	case MRSH_FUNCTION_DEFINITION:;
		struct mrsh_function_definition *fd =
			mrsh_command_get_function_definition(cmd);
		highlight_str(state, &fd->name_range, FORMAT_BLUE);
		highlight_char(state, &fd->lparen_pos, FORMAT_GREEN);
		highlight_char(state, &fd->rparen_pos, FORMAT_GREEN);
		highlight_command(state, fd->body);
		break;
	}
}

static void highlight_node(struct highlight_state *state,
		struct mrsh_node *node) {
	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_node_get_pipeline(node);
		if (mrsh_position_valid(&pl->bang_pos)) {
			highlight_char(state, &pl->bang_pos, FORMAT_GREEN);
		}
		for (size_t i = 0; i < pl->commands.len; ++i) {
			struct mrsh_command *cmd = pl->commands.data[i];
			highlight_command(state, cmd);
		}
		break;
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
		highlight_node(state, binop->left);
		highlight_str(state, &binop->op_range, FORMAT_GREEN);
		highlight_node(state, binop->right);
		break;
	}
}

static void highlight_command_list(struct highlight_state *state,
		struct mrsh_command_list *list) {
	highlight_node(state, list->node);
	if (mrsh_position_valid(&list->separator_pos)) {
		highlight_char(state, &list->separator_pos, FORMAT_GREEN);
	}
}

static void highlight_command_list_array(struct highlight_state *state,
		struct mrsh_array *array) {
	for (size_t i = 0; i < array->len; ++i) {
		struct mrsh_command_list *l = array->data[i];
		highlight_command_list(state, l);
	}
}

static void highlight_program(struct highlight_state *state,
		struct mrsh_program *prog) {
	highlight_command_list_array(state, &prog->body);
}

int main(int argc, char *argv[]) {
	struct mrsh_buffer buf = {0};
	while (true) {
		char *dst = mrsh_buffer_reserve(&buf, READ_SIZE);
		ssize_t n_read = read(STDIN_FILENO, dst, READ_SIZE);
		if (n_read < 0) {
			fprintf(stderr, "read() failed: %s\n", strerror(errno));
			return EXIT_FAILURE;
		} else if (n_read == 0) {
			break;
		}
		buf.len += n_read;
	}

	struct mrsh_parser *parser = mrsh_parser_with_data(buf.data, buf.len);
	struct mrsh_program *prog = mrsh_parse_program(parser);
	if (prog == NULL) {
		const char *err_msg = mrsh_parser_error(parser, NULL);
		if (err_msg != NULL) {
			fprintf(stderr, "failed to parse script: %s\n", err_msg);
			return EXIT_FAILURE;
		}
		return EXIT_SUCCESS;
	}
	mrsh_parser_destroy(parser);

	struct highlight_state state = {
		.buf = buf.data,
	};

	struct mrsh_position begin = { .offset = 0 };
	highlight(&state, &begin, FORMAT_DEFAULT);

	highlight_program(&state, prog);

	struct mrsh_position end = { .offset = buf.len };
	highlight(&state, &end, FORMAT_RESET);
	assert(state.fmt_stack_len == 0);

	mrsh_buffer_finish(&buf);
	return EXIT_SUCCESS;
}