~emersion/mrsh

ref: bda92abbcf206e6374593ee7e1962ba6341be98e mrsh/frontend/basic.c -rw-r--r-- 687 bytes 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
/* Basic, strictly POSIX interactive line interface */
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mrsh/shell.h>
#include <mrsh/parser.h>
#include "frontend.h"

void interactive_init(struct mrsh_state *state) {
	// no-op
}

size_t interactive_next(struct mrsh_state *state,
		char **line, const char *prompt) {
	fprintf(stderr, "%s", prompt);
	size_t len = 0;
	char *_line = NULL;
	errno = 0;
	ssize_t n_read = getline(&_line, &len, stdin);
	if (n_read < 0) {
		free(_line);
		if (errno != 0) {
			fprintf(stderr, "getline() failed: %s\n", strerror(errno));
		}
		return 0;
	}
	*line = _line;
	return n_read;
}