summaryrefslogtreecommitdiff
path: root/src/user/lib/fwik/io/Term.cpp
blob: f8f686e8a973a3df814b479e7842a7a887c0ab56 (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
#include <IO/Term.h>

Term::Term(const Node &n) : Node(n) {
	_init();
}

Term::Term(FILE f) : Node(f) {
	_init();
	if (error E_INVALID_TYPE) libc::close(fd);
}

Term::Term(const char* filename, int mode) : Node(filename, mode) {
	_init();
	if (error == E_INVALID_TYPE) libc::close(fd);
}

void Term::_init() {
	if (error < 0) return;
	if (info.type & FT_TERMINAL) {
		w = info.size >> 16;
		h = info.size & 0xFFFF;
	} else {
		error = E_INVALID_TYPE;
	}
	hist.str = 0;
	hist.max = 12;
}

Term::~Term() {
	if (hist.str != 0) {
		for (int i = 0; i < hist.max; i++) {
			if (hist.str[i] != 0) free(hist.str[i]);
		}
		free(hist.str);
	}
}

void Term::print(const char *s) {
	libc::fprint(fd, s);
}

void Term::printf(const char* fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	libc::vfprintf(fd, fmt, ap);
	va_end(ap);
}

void Term::vprintf(const char* fmt, va_list ap) {
	libc::vfprintf(fd, fmt, ap);
}

String Term::readln() {
	char *s = libc::freadln(fd);
	String ret(s);
	free(s);
	return ret;
}

String Term::readline() {
	return String(libc::freadline(fd, &hist));
}