~arivigo/sline

sline/sline_test.c -rw-r--r-- 1.3 KiB
312912bfAriadna Vigo sline 1.1.2 a month ago
                                                                                
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
/* See LICENSE for copyright and license details. */

#include <stdio.h>
#include <string.h>

#include "sline.h"

#define BUF_SIZE 64
#define INIT_STR NULL /* Set to a literal string to test init strings */

static void print_err(void);
static void print_hist(void);

static void
print_err(void)
{
	fprintf(stderr, "sline: %s\n", sline_errmsg());
}

static void
print_hist(void)
{
	int i;
	const char *entry;

	printf("History contains:\n");
	
	for (i = 0; (entry = sline_history_get(i)) != NULL; ++i) {
		if (strlen(entry) > 0) /* Avoid printing "current blank" */
			printf("%s\n", entry);
	}
}

int
main(void)
{
	char buf[BUF_SIZE];
	int cnt, sline_stat;

	printf("sline_test: compiled with sline %s.\n", sline_version());
	printf("Type 'exit', 'quit', or Ctrl-D to exit.\n");

	if (sline_setup(BUF_SIZE) < 0) {
		print_err();
		return -1;
	}

	cnt = 0;
	while (feof(stdin) == 0) {
		/* Comment line below to use the default prompt. */
		sline_set_prompt("%d> ", cnt++);

		if ((sline_stat = sline(buf, BUF_SIZE, INIT_STR)) < 0)
			goto exit;

		if (strncmp(buf, "hist", BUF_SIZE) == 0) {
			print_hist();
			continue;
		} else if (strncmp(buf, "exit", BUF_SIZE) == 0
		         || strncmp(buf, "quit", BUF_SIZE) == 0) {
			break;
		}

		printf("Input was: %s\n", buf);
	}

exit:
	if (sline_stat < 0 && sline_err != SLINE_ERR_EOF)
		print_err();
	sline_end();

	return 0;
}