aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-from-text.c
blob: 328fd23d1cbdced74172e30a982df1304c57b381 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#ifndef TYPE
#define INCLUDE_UINT16
#include "common.h"

USAGE("")

#define FILE "blind-from-text.c"
#include "define-functions.h"

int
main(int argc, char *argv[])
{
	struct stream stream;
	size_t size = 0;
	char *line = NULL;
	ssize_t len;
	void (*process)(void);

	UNOFLAGS(argc);

	len = getline(&line, &size, stdin);
	if (len < 0) {
		if (ferror(stdin))
	  		eprintf("getline <stdin>:");
		else
			eprintf("<stdin>: no input\n");
	}
	if (len && line[len - 1] == '\n')
		line[--len] = '\0';
	if ((size_t)len + 6 > sizeof(stream.buf))
		eprintf("<stdin>: head is too long\n");
	stream.fd = -1;
	stream.file = "<stdin>";
	memcpy(stream.buf, line, (size_t)len);
	memcpy(stream.buf + len, "\n\0uivf", 6);
	stream.ptr = (size_t)len + 6;
	free(line);
	ewriteall(STDOUT_FILENO, stream.buf, stream.ptr, "<stdout>");
	einit_stream(&stream);

	SELECT_PROCESS_FUNCTION(&stream);
	process();

	efshut(stdin, "<stdin>");
	return 0;
}

#else

static void
PROCESS(void)
{
	TYPE buf[BUFSIZ / sizeof(TYPE)];
	size_t i;
	int r, done = 0;
	while (!done) {
		for (i = 0; i < ELEMENTSOF(buf); i += (size_t)r) {
			r = scanf("%"SCAN_TYPE, buf + i);
			if (r == EOF) {
				done = 1;
				break;
			}
		}
		ewriteall(STDOUT_FILENO, buf, i * sizeof(*buf), "<stdout>");
	}
}

#endif