aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/htmlroff/t19.c
blob: bd633834fc0d012e07866e98be3751ecc31a96ae (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
#include "a.h"

/*
 * 19. Input/output file switching.
 */

/* .so - push new source file */
void
r_so(int argc, Rune **argv)
{
	USED(argc);
	pushinputfile(erunesmprint("%s", unsharp(esmprint("%S", argv[1]))));
}

/* .nx - end this file, switch to arg */
void
r_nx(int argc, Rune **argv)
{
	int n;

	if(argc == 1){
		while(popinput())
			;
	}else{
		if(argc > 2)
			warn("too many arguments for .nx");
		while((n=popinput()) && n != 2)
			;
		pushinputfile(argv[1]);
	}
}

/* .sy - system: run string */
void
r_sy(Rune *name)
{
	USED(name);
	warn(".sy not implemented");
}

/* .pi - pipe output to string */
void
r_pi(Rune *name)
{
	USED(name);
	warn(".pi not implemented");
}

/* .cf - copy contents of filename to output */
void
r_cf(int argc, Rune **argv)
{
	int c;
	char *p;
	Biobuf *b;

	USED(argc);
	p = esmprint("%S", argv[1]);
	if((b = Bopen(p, OREAD)) == nil){
		fprint(2, "%L: open %s: %r\n", p);
		free(p);
		return;
	}
	free(p);

	while((c = Bgetrune(b)) >= 0)
		outrune(c);
	Bterm(b);
}

void
r_inputpipe(Rune *name)
{
	Rune *cmd, *stop, *line;
	int n, pid, p[2], len;
	Waitmsg *w;

	USED(name);
	if(pipe(p) < 0){
		warn("pipe: %r");
		return;
	}
	stop = copyarg();
	cmd = readline(CopyMode);
	pid = fork();
	switch(pid){
	case 0:
		if(p[0] != 0){
			dup(p[0], 0);
			close(p[0]);
		}
		close(p[1]);
		execl(unsharp("#9/bin/rc"), "rc", "-c", esmprint("%S", cmd), nil);
		warn("%Cdp %S: %r", dot, cmd);
		_exits(nil);
	case -1:
		warn("fork: %r");
	default:
		close(p[0]);
		len = runestrlen(stop);
		fprint(p[1], ".ps %d\n", getnr(L(".s")));
		fprint(p[1], ".vs %du\n", getnr(L(".v")));
		fprint(p[1], ".ft %d\n", getnr(L(".f")));
		fprint(p[1], ".ll 8i\n");
		fprint(p[1], ".pl 30i\n");
		while((line = readline(~0)) != nil){
			if(runestrncmp(line, stop, len) == 0
			&& (line[len]==' ' || line[len]==0 || line[len]=='\t'
				|| (line[len]=='\\' && line[len+1]=='}')))
				break;
			n = runestrlen(line);
			line[n] = '\n';
			fprint(p[1], "%.*S", n+1, line);
			free(line);
		}
		free(stop);
		close(p[1]);
		w = wait();
		if(w == nil){
			warn("wait: %r");
			return;
		}
		if(w->msg[0])
			sysfatal("%C%S %S: %s", dot, name, cmd, w->msg);
		free(cmd);
		free(w);
	}
}

void
t19init(void)
{
	addreq(L("so"), r_so, 1);
	addreq(L("nx"), r_nx, -1);
	addraw(L("sy"), r_sy);
	addraw(L("inputpipe"), r_inputpipe);
	addraw(L("pi"), r_pi);
	addreq(L("cf"), r_cf, 1);

	nr(L("$$"), getpid());
}