aboutsummaryrefslogtreecommitdiff
path: root/client/rl.cpp
blob: 493f753a12154b9d641d6b790688f5caa59289ff (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
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>

#include <readline/readline.h>
#include <readline/history.h>

#include "rl.h"
#include "cmd.h"
#include "parse.h"

static char* saved_line;
static int saved_point, saved_end;

void _rl_printf_start() {
	// save line
	saved_line = rl_copy_text(0, rl_end);
	saved_point = rl_point;
	saved_end = rl_end;

	// clear line
	rl_save_prompt();
	rl_replace_line("", 0);
	rl_redisplay();
}

void _rl_printf_stop() {
	rl_restore_prompt();
	rl_replace_line(saved_line, 0);
	rl_point = saved_point;
	rl_end = saved_end;
	rl_redisplay();

	free(saved_line);
}

void rl_printf(const char *fmt, ...) {
	_rl_printf_start();

	va_list args;
	va_start(args, fmt);
	vprintf(fmt, args);
	va_end(args);

	_rl_printf_stop();
}

static void cli_cmd(char* cmd) {
	cmd += strspn(cmd, IFS); // skip leading whitespace
	char* line = consume_token(cmd, IFS);

	for (size_t i = 0; i < cmds_length; i++) {
		if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0)
			continue;

		cmds[i].handle(line);
		return;
	}

	printf("unknown command!\n");
}

static char* rl_completion_entries(const char *text, int state) {
	static size_t i = 0;
	if (state == 0) i = 0;

	while (i < cmds_length) {
		cmd_t cmd = cmds[i];
		i++;
		if (strncmp(text, cmd.name, strlen(text)) == 0) {
			return strdup(cmd.name);
		}
	}

	return NULL;
}

static char** rl_attempted_completion(const char * text, int start, int end) {
	// do not suggest filenames
	rl_attempted_completion_over = 1;

	// if first word in line buffer -> complete commands from cmds[]
	size_t cmd_start = strspn(rl_line_buffer, IFS);
	if (start == cmd_start)
		return rl_completion_matches(text, rl_completion_entries);

	// else, check specialized completion functions
	size_t cmd_len = strcspn(rl_line_buffer + cmd_start, IFS);
	for (size_t i = 0; i < cmds_length; i++) {
		cmd_t cmd = cmds[i];
		if (cmd.complete == NULL) continue;
		if (strncmp(cmd.name, rl_line_buffer + cmd_start, cmd_len) != 0) continue;
		return cmd.complete(text, start, end);
	}

	// else, no completion available
	return NULL;
}

int cli_main() {
	char* input = NULL;
	rl_attempted_completion_function = rl_attempted_completion;

	while (1) {
		if (input != NULL) free(input);
		input = readline(CLI_PROMPT);

		if (input == NULL) return EXIT_SUCCESS; // exit on ^D (EOF)
		if (*input == '\0') continue; // ignore empty lines
		add_history(input);

		cli_cmd(input);
	}	

	return EXIT_SUCCESS;
}

int rl_word(const char * line, int cursor) {
	int word = -1;
	for (int i = 0; line[i] != '\0';) {
		i += strspn(line + i, IFS);
		int len = strcspn(line + i, IFS);
		word++;
		i += len;
		if (i > cursor) break;
	}
	return word;
}

/// \internal
typedef struct {
	const char * word;
	const char ** options;
} __rl_complete_list_data_t;
char** rl_complete_list(const char * word, const char ** options) {
	__rl_complete_list_data_t data = {
		.word = word,
		.options = options,
	};
	return rl_completion_matches((char *) &data, [](const char * text, int state) -> char * {
		__rl_complete_list_data_t data = *(__rl_complete_list_data_t *) text;
		static size_t i = 0;
		if (state == 0) i = 0;

		while (data.options[i] != NULL) {
			const char * option = data.options[i++];
			if (strncmp(data.word, option, strlen(data.word)) == 0)
				return strdup(option);
		}
		return NULL;
	});
}