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;
54 2fb0c444 2009-08-20 jrm8005 strcpy(tempfile, "/tmp/pagespoolXXXXXXXXX");
55 2fb0c444 2009-08-20 jrm8005 fd = opentemp(tempfile, ORDWR);
57 2fb0c444 2009-08-20 jrm8005 *name = estrdup(tempfile);
59 24c02865 2005-01-04 devnull if(write(fd, ibuf, in) != in){
60 24c02865 2005-01-04 devnull fprint(2, "error writing temporary file\n");
61 24c02865 2005-01-04 devnull wexits("write temp");
64 24c02865 2005-01-04 devnull while((n = read(stdinfd, buf, sizeof buf)) > 0){
65 24c02865 2005-01-04 devnull if(write(fd, buf, n) != n){
66 24c02865 2005-01-04 devnull fprint(2, "error writing temporary file\n");
67 24c02865 2005-01-04 devnull wexits("write temp0");
70 24c02865 2005-01-04 devnull seek(fd, 0, 0);
71 24c02865 2005-01-04 devnull return fd;
74 05a4d855 2007-03-26 devnull typedef struct StdinArg StdinArg;
76 05a4d855 2007-03-26 devnull struct StdinArg {
77 05a4d855 2007-03-26 devnull Channel *cp;
78 05a4d855 2007-03-26 devnull uchar *ibuf;
83 24c02865 2005-01-04 devnull * spool standard input into a pipe.
84 24c02865 2005-01-04 devnull * we've already ready the first in bytes into ibuf
86 05a4d855 2007-03-26 devnull static void
87 05a4d855 2007-03-26 devnull _stdinpipe(void *a)
89 24c02865 2005-01-04 devnull uchar buf[8192];
90 05a4d855 2007-03-26 devnull StdinArg *arg;
91 24c02865 2005-01-04 devnull int p[2];
96 24c02865 2005-01-04 devnull if(pipe(p) < 0){
97 fa325e9b 2020-01-10 cross fprint(2, "pipe fails: %r\n");
98 24c02865 2005-01-04 devnull wexits("pipe");
101 05a4d855 2007-03-26 devnull send(arg->cp, &p[0]);
103 05a4d855 2007-03-26 devnull write(p[1], arg->ibuf, arg->in);
104 24c02865 2005-01-04 devnull while((n = read(stdinfd, buf, sizeof buf)) > 0)
105 24c02865 2005-01-04 devnull write(p[1], buf, n);
107 05a4d855 2007-03-26 devnull close(p[1]);
108 05a4d855 2007-03-26 devnull threadexits(0);
112 05a4d855 2007-03-26 devnull stdinpipe(uchar *ibuf, int in) {
113 05a4d855 2007-03-26 devnull StdinArg arg;
116 05a4d855 2007-03-26 devnull arg.ibuf = ibuf;
117 05a4d855 2007-03-26 devnull arg.in = in;
118 05a4d855 2007-03-26 devnull arg.cp = chancreate(sizeof(int), 0);
119 05a4d855 2007-03-26 devnull proccreate(_stdinpipe, &arg, mainstacksize);
120 05a4d855 2007-03-26 devnull recv(arg.cp, &fd);
121 05a4d855 2007-03-26 devnull chanfree(arg.cp);
123 05a4d855 2007-03-26 devnull return fd;