1 24c02865 2005-01-04 devnull #include <u.h>
2 24c02865 2005-01-04 devnull #include <libc.h>
3 24c02865 2005-01-04 devnull #include <draw.h>
4 05a4d855 2007-03-26 devnull #include <thread.h>
5 24c02865 2005-01-04 devnull #include <bio.h>
6 05a4d855 2007-03-26 devnull #include <cursor.h>
7 24c02865 2005-01-04 devnull #include "page.h"
10 24c02865 2005-01-04 devnull emalloc(int sz)
13 24c02865 2005-01-04 devnull v = malloc(sz);
14 24c02865 2005-01-04 devnull if(v == nil) {
15 24c02865 2005-01-04 devnull fprint(2, "out of memory allocating %d\n", sz);
16 24c02865 2005-01-04 devnull wexits("mem");
18 24c02865 2005-01-04 devnull memset(v, 0, sz);
19 24c02865 2005-01-04 devnull return v;
23 24c02865 2005-01-04 devnull erealloc(void *v, int sz)
25 24c02865 2005-01-04 devnull v = realloc(v, sz);
26 24c02865 2005-01-04 devnull if(v == nil) {
27 24c02865 2005-01-04 devnull fprint(2, "out of memory allocating %d\n", sz);
28 24c02865 2005-01-04 devnull wexits("mem");
30 24c02865 2005-01-04 devnull return v;
34 24c02865 2005-01-04 devnull estrdup(char *s)
37 24c02865 2005-01-04 devnull if((t = strdup(s)) == nil) {
38 24c02865 2005-01-04 devnull fprint(2, "out of memory in strdup(%.10s)\n", s);
39 24c02865 2005-01-04 devnull wexits("mem");
41 24c02865 2005-01-04 devnull return t;
45 24c02865 2005-01-04 devnull * spool standard input to /tmp.
46 24c02865 2005-01-04 devnull * we've already read the initial in bytes into ibuf.
49 24c02865 2005-01-04 devnull spooltodisk(uchar *ibuf, int in, char **name)
51 24c02865 2005-01-04 devnull uchar buf[8192];
52 24c02865 2005-01-04 devnull int fd, n;
53 24c02865 2005-01-04 devnull char temp[40];
55 24c02865 2005-01-04 devnull strcpy(temp, "/tmp/pagespoolXXXXXXXXX");
56 24c02865 2005-01-04 devnull fd = opentemp(temp);
58 24c02865 2005-01-04 devnull *name = estrdup(temp);
60 24c02865 2005-01-04 devnull if(write(fd, ibuf, in) != in){
61 24c02865 2005-01-04 devnull fprint(2, "error writing temporary file\n");
62 24c02865 2005-01-04 devnull wexits("write temp");
65 24c02865 2005-01-04 devnull while((n = read(stdinfd, buf, sizeof buf)) > 0){
66 24c02865 2005-01-04 devnull if(write(fd, buf, n) != n){
67 24c02865 2005-01-04 devnull fprint(2, "error writing temporary file\n");
68 24c02865 2005-01-04 devnull wexits("write temp0");
71 24c02865 2005-01-04 devnull seek(fd, 0, 0);
72 24c02865 2005-01-04 devnull return fd;
75 05a4d855 2007-03-26 devnull typedef struct StdinArg StdinArg;
77 05a4d855 2007-03-26 devnull struct StdinArg {
78 05a4d855 2007-03-26 devnull Channel *cp;
79 05a4d855 2007-03-26 devnull uchar *ibuf;
84 24c02865 2005-01-04 devnull * spool standard input into a pipe.
85 24c02865 2005-01-04 devnull * we've already ready the first in bytes into ibuf
87 05a4d855 2007-03-26 devnull static void
88 05a4d855 2007-03-26 devnull _stdinpipe(void *a)
90 24c02865 2005-01-04 devnull uchar buf[8192];
91 05a4d855 2007-03-26 devnull StdinArg *arg;
92 24c02865 2005-01-04 devnull int p[2];
97 24c02865 2005-01-04 devnull if(pipe(p) < 0){
98 24c02865 2005-01-04 devnull fprint(2, "pipe fails: %r\n");
99 24c02865 2005-01-04 devnull wexits("pipe");
102 05a4d855 2007-03-26 devnull send(arg->cp, &p[0]);
104 05a4d855 2007-03-26 devnull write(p[1], arg->ibuf, arg->in);
105 24c02865 2005-01-04 devnull while((n = read(stdinfd, buf, sizeof buf)) > 0)
106 24c02865 2005-01-04 devnull write(p[1], buf, n);
108 05a4d855 2007-03-26 devnull close(p[1]);
109 05a4d855 2007-03-26 devnull threadexits(0);
113 05a4d855 2007-03-26 devnull stdinpipe(uchar *ibuf, int in) {
114 05a4d855 2007-03-26 devnull StdinArg arg;
117 05a4d855 2007-03-26 devnull arg.ibuf = ibuf;
118 05a4d855 2007-03-26 devnull arg.in = in;
119 05a4d855 2007-03-26 devnull arg.cp = chancreate(sizeof(int), 0);
120 05a4d855 2007-03-26 devnull proccreate(_stdinpipe, &arg, mainstacksize);
121 05a4d855 2007-03-26 devnull recv(arg.cp, &fd);
122 05a4d855 2007-03-26 devnull chanfree(arg.cp);
124 05a4d855 2007-03-26 devnull return fd;