aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/db/output.c
blob: db4d0fa25eba5d9956b14267eae4644ed84d1249 (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
155
156
157
158
159
/*
 *
 *	debugger
 *
 */

#include "defs.h"
#include "fns.h"

int	printcol = 0;
int	infile = STDIN;
int	maxpos = MAXPOS;

Biobuf	bstdout;

void
printc(int c)
{
	dprint("%c", c);
}

/* was move to next f1-sized tab stop; now just print a tab */
int
tconv(Fmt *f)
{
	return fmtstrcpy(f, "\t");
}

void
flushbuf(void)
{
 	if (printcol != 0)
		printc(EOR);
}

void
prints(char *s)
{
	dprint("%s",s);
}

void
newline(void)
{
	printc(EOR);
}

#define	MAXIFD	5
struct {
	int	fd;
	int	r9;
} istack[MAXIFD];
int	ifiledepth;

void
iclose(int stack, int err)
{
	if (err) {
		if (infile) {
			close(infile);
			infile=STDIN;
		}
		while (--ifiledepth >= 0)
			if (istack[ifiledepth].fd)
				close(istack[ifiledepth].fd);
		ifiledepth = 0;
	} else if (stack == 0) {
		if (infile) {
			close(infile);
			infile=STDIN;
		}
	} else if (stack > 0) {
		if (ifiledepth >= MAXIFD)
			error("$<< nested too deeply");
		istack[ifiledepth].fd = infile;
		ifiledepth++;
		infile = STDIN;
	} else {
		if (infile) {
			close(infile);
			infile=STDIN;
		}
		if (ifiledepth > 0) {
			infile = istack[--ifiledepth].fd;
		}
	}
}

void
oclose(void)
{
	flushbuf();
	Bterm(&bstdout);
	Binit(&bstdout, 1, OWRITE);
}

void
redirout(char *file)
{
	int fd;

	if (file == 0){
		oclose();
		return;
	}
	flushbuf();
	if ((fd = open(file, 1)) >= 0)
		seek(fd, 0L, 2);
	else if ((fd = create(file, 1, 0666)) < 0)
		error("cannot create");
	Bterm(&bstdout);
	Binit(&bstdout, fd, OWRITE);
}

void
endline(void)
{

	if (maxpos <= printcol)
		newline();
}

void
flush(void)
{
	Bflush(&bstdout);
}

int
dprint(char *fmt, ...)
{
	int n, w;
	char *p;
 	char buf[4096];
	Rune r;
	va_list arg;

	if(mkfault)
		return -1;
	va_start(arg, fmt);
	n = vseprint(buf, buf+sizeof buf, fmt, arg) - buf;
	va_end(arg);
	Bwrite(&bstdout, buf, n);
	for(p=buf; *p; p+=w){
		w = chartorune(&r, p);
		if(r == '\n')
			printcol = 0;
		else
			printcol++;
	}
	return n;
}

void
outputinit(void)
{
	Binit(&bstdout, 1, OWRITE);
	fmtinstall('t', tconv);
}