9 typedef struct Page Page;
48 ulong imemlimit = 8*MiB;
51 Image *frame, *paper, *ground;
53 char pagespool[] = "/tmp/pagespool.";
86 [Corigsize] "orig size", 'o', Kesc, 0,
87 [Czoomin] "zoom in", '+', 0, 0,
88 [Czoomout] "zoom out", '-', 0, 0,
89 [Cfitwidth] "fit width", 'f', 0, 0,
90 [Cfitheight] "fit height", 'h', 0, 0,
91 [Crotate90] "rotate 90", 'r', 0, 0,
92 [Cupsidedown] "upside down", 'u', 0, 0,
93 [Cdummy1] "", 0, 0, 0,
94 [Cnext] "next", Kright, ' ', '\n',
95 [Cprev] "prev", Kleft, Kbs, 0,
96 [Csnarf] "snarf", 's', 0, 0,
97 [Czerox] "zerox", 'z', 0, 0,
98 [Cwrite] "write", 'w', 0, 0,
99 [Cext] "ext", 'x', 0, 0,
100 [Cdummy2] "", 0, 0, 0,
101 [Cquit] "quit", 'q', Kdel, Keof,
104 char *pagemenugen(int i);
105 char *cmdmenugen(int i);
121 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
122 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
123 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
124 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
125 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
126 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
127 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
128 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }
131 int pagewalk1(Page *p);
132 void showpage1(Page *);
133 void showpage(Page *);
134 void drawpage(Page *);
135 Point pagesize(Page *);
138 addpage(Page *up, char *name, int (*popen)(Page *), void *pdata, int fd)
142 p = mallocz(sizeof(*p), 1);
143 p->name = strdup(name);
153 up->down = up->tail = p;
161 if(up && current == up){
170 resizewin(Point size)
174 if((wctl = open("/dev/wctl", OWRITE)) < 0)
177 size = addpt(size, Pt(Borderwidth*2, Borderwidth*2));
179 Point dsize = subpt(display->image->r.max, display->image->r.min);
184 /* can't just conver whole display */
185 if(eqpt(size, dsize))
188 fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
197 snprint(nam, sizeof nam, "%s%s%.12d%.8lux", pagespool, pfx, getpid(), id++);
198 return create(nam, OEXCL|ORCLOSE|ORDWR, 0600);
202 catchnote(void *, char *msg)
204 if(strstr(msg, "sys: write on closed pipe"))
206 if(strstr(msg, "hangup"))
208 if(strstr(msg, "alarm"))
210 if(strstr(msg, "interrupt"))
212 if(strstr(msg, "kill"))
225 for(mfd = 0; fd >= 0; fd = va_arg(arg, int), mfd++)
230 if((fd = open("/fd", OREAD)) < 0)
231 sysfatal("open: %r");
232 n = dirreadall(fd, &dir);
234 if(strstr(dir[i].name, "ctl"))
236 fd = atoi(dir[i].name);
244 pipeline(int fd, char *fmt, ...)
246 char buf[NPATH], *argv[4];
256 vsnprint(buf, sizeof buf, fmt, arg);
258 switch(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT)){
264 dupfds(fd, pfd[1], 2, -1);
269 exec("/bin/rc", argv);
270 sysfatal("exec: %r");
281 static char buf[NR*UTFmax];
290 k += chartorune(&r, s+k);
294 strcpy(buf+k, "...");
295 while((l-i) >= NR/2-4){
296 k += chartorune(&r, s+k);
304 pageaddr1(Page *p, char *s, char *e)
306 if(p == nil || p == root)
308 return seprint(pageaddr1(p->up, s, e), e, "%s%s", p->up->delim, p->name);
312 * returns address string of a page in the form:
313 * /dir/filename!page!subpage!...
316 pageaddr(Page *p, char *buf, int nbuf)
319 pageaddr1(p, buf, buf+nbuf);
332 if((fd = dup(p->fd, -1)) < 0){
341 if(strcmp(p->ext, "ico") == 0)
342 pipeline(fd, "exec %s -c", p->ext);
344 pipeline(fd, "exec %s -t9", p->ext);
348 * dont keep the file descriptor arround if it can simply
351 fd2path(p->fd, nam, sizeof(nam));
352 if(strncmp(nam, pagespool, strlen(pagespool))){
355 p->data = strdup(nam);
367 pipeline(p->fd, "exec %s", (char*)p->data);
377 char mnt[32], cmd[64], *argv[4];
380 snprint(mnt, sizeof(mnt), "/n/tapefs.%.12d%.8lux", getpid(), (ulong)(uintptr)p);
381 snprint(cmd, sizeof(cmd), "exec %s -m %s /fd/0", (char*)p->data, mnt);
382 switch(rfork(RFPROC|RFMEM|RFFDG|RFREND)){
388 dupfds(p->fd, 1, 2, -1);
393 exec("/bin/rc", argv);
394 sysfatal("exec: %r");
399 p->data = strdup(mnt);
407 char buf[NPATH], *s, *e;
413 e = buf+sizeof(buf)-1;
414 s += snprint(s, e-s, "%s/", (char*)p->data);
417 pipeline(fd, "awk '/\\<rootfile/{"
418 "if(match($0, /full\\-path\\=\\\"([^\\\"]+)\\\"/)){"
419 "print substr($0, RSTART+11,RLENGTH-12);exit}}'");
420 n = read(fd, s, e - s);
424 while(n > 0 && s[n-1] == '\n')
428 if((fd = open(buf, OREAD)) < 0)
430 pipeline(fd, "awk '/\\<item/{"
431 "if(match($0, /id\\=\\\"([^\\\"]+)\\\"/)){"
432 "id=substr($0, RSTART+4, RLENGTH-5);"
433 "if(match($0, /href\\=\\\"([^\\\"]+)\\\"/)){"
434 "item[id]=substr($0, RSTART+6, RLENGTH-7)}}};"
436 "if(match($0, /idref\\=\\\"([^\\\"]+)\\\"/)){"
437 "ref=substr($0, RSTART+7, RLENGTH-8);"
438 "print item[ref]; fflush}}'");
439 s = strrchr(buf, '/')+1;
440 while((n = read(fd, s, e-s)) > 0){
441 while(n > 0 && s[n-1] == '\n')
444 addpage(p, s, popenfile, strdup(buf), -1);
450 typedef struct Ghost Ghost;
469 switch(rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)){
477 dupfds(gs->pdat, gs->pin, pfd[1], -1);
478 fprint(1, "%s DoPDFPage\n"
481 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
482 "flushfile\n", p->name);
483 while((n = read(0, buf, sizeof buf)) > 0){
484 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
496 infernobithdr(char *buf, int n)
499 if(memcmp(buf, "compressed\n", 11) == 0)
501 if(strtochan((char*)buf))
503 if(memcmp(buf, " ", 10) == 0 &&
504 '0' <= buf[10] && buf[10] <= '9' &&
514 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
515 char buf[NBUF], nam[32], *argv[16];
522 if(read(ifd, buf, 5) != 5)
525 if(memcmp(buf, "%PDF-", 5) == 0)
545 argv[0] = (char*)p->data;
546 switch(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT)){
551 dupfds(pin[1], pout[1], 2, pdat[1], ifd, -1);
553 dupfds(nullfd, nullfd, 2, pdat[1], ifd, -1);
555 pipeline(4, "%s", argv[0]);
558 argv[2] = "-sDEVICE=plan9";
559 argv[3] = "-sOutputFile=/fd/3";
561 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
563 argv[7] = "-dTextAlphaBits=4";
564 argv[8] = "-dGraphicsAlphaBits=4";
565 snprint(buf, sizeof buf, "-r%d", ppi);
567 argv[10] = "-dDOINTERPOLATE";
568 argv[11] = pdf ? "-" : "/fd/4";
570 exec("/bin/gs", argv);
571 sysfatal("exec: %r");
582 "/PAGEOUT (/fd/1) (w) file def\n"
583 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
587 "/PDFSave null def\n"
588 "/DSCPageCount 0 def\n"
589 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
591 "GS_PDF_ProcSet begin\n"
593 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
595 "pdfpagecount PAGE==\n";
598 if(write(pin[0], prolog, n) != n)
600 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
608 gs = mallocz(sizeof(*gs), 1);
613 snprint(nam, sizeof nam, "%d", i);
614 addpage(p, nam, popenpdf, gs, -1);
617 /* keep ghostscript arround */
622 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
623 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
624 snprint(nam, sizeof nam, "%d", i);
625 addpage(p, nam, popenimg, nil, ofd);
631 snprint(nam, sizeof nam, "%.4d", ++i);
632 if((ofd = createtmp(nam)) < 0)
633 ofd = dup(nullfd, -1);
635 if(write(ofd, buf, n) != n)
649 filetype(char *buf, int nbuf, char *typ, int ntyp)
651 int n, ifd[2], ofd[2];
654 if(infernobithdr(buf, nbuf)){
655 strncpy(typ, "image/p9bit", ntyp);
667 if(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT) == 0){
668 dupfds(ifd[1], ofd[1], 2, -1);
672 exec("/bin/file", argv);
676 if(rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT) == 0){
682 if((n = readn(ofd[0], typ, ntyp-1)) < 0)
685 while(n > 0 && typ[n-1] == '\n')
692 dircmp(void *p1, void *p2)
699 return strcmp(d1->name, d2->name);
710 "application/pdf", popengs, nil,
711 "application/postscript", popengs, nil,
712 "application/troff", popengs, "lp -dstdout",
713 "text/plain", popengs, "lp -dstdout",
714 "text/html", popengs, "uhtml | html2ms | tbl | troff -ms | lp -dstdout",
715 "application/dvi", popengs, "dvips -Pps -r0 -q1 -f1",
716 "application/doc", popengs, "doc2ps",
717 "application/zip", popentape, "fs/zipfs",
718 "application/x-tar", popentape, "fs/tarfs",
719 "application/x-ustar", popentape, "fs/tarfs",
720 "application/x-compress", popenfilter, "uncompress",
721 "application/x-gzip", popenfilter, "gunzip",
722 "application/x-bzip2", popenfilter, "bunzip2",
723 "image/gif", popenimg, "gif",
724 "image/jpeg", popenimg, "jpg",
725 "image/png", popenimg, "png",
726 "image/tiff", popenimg, "tif",
727 "image/ppm", popenimg, "ppm",
728 "image/bmp", popenimg, "bmp",
729 "image/tga", popenimg, "tga",
730 "image/x-icon", popenimg, "ico",
731 "image/p9bit", popenimg, nil,
734 char buf[NBUF], typ[128], *file;
745 if((fd = open(file, OREAD)) < 0){
752 if((d = dirfstat(fd)) == nil){
761 snprint(buf, sizeof(buf), "%s/META-INF/container.xml", file);
762 if((tfd = open(buf, OREAD)) >= 0){
769 if(strcmp(pageaddr(p, buf, sizeof(buf)), file) == 0)
771 if((n = dirreadall(fd, &d)) < 0)
773 qsort(d, n, sizeof d[0], dircmp);
775 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
781 memset(buf, 0, NBUF/2);
782 if((n = readn(fd, buf, NBUF/2)) <= 0)
784 filetype(buf, n, typ, sizeof(typ));
785 for(i=0; i<nelem(tab); i++)
786 if(strncmp(typ, tab[i].typ, strlen(tab[i].typ)) == 0)
789 werrstr("unknown image format: %s", typ);
793 p->data = tab[i].data;
794 p->open = tab[i].open;
795 if(seek(fd, 0, 0) < 0)
797 if((i = readn(fd, buf+n, n)) < 0)
799 if(i != n || memcmp(buf, buf+n, i)){
802 if((tfd = createtmp("file")) < 0)
805 if(write(tfd, buf, n) != n)
807 if((n = read(fd, buf, sizeof(buf))) < 0)
810 if(dup(tfd, fd) < 0){
840 for(p = root->down; p; p = t)
841 if((t = nextpage(p)) == x)
853 if(p->open == nil || (fd = p->open(p)) < 0)
857 pipeline(fd, "exec rotate -r %d", rotate);
859 pipeline(fd, "exec resize -x %d", resize.x);
861 pipeline(fd, "exec resize -y %d", resize.y);
871 return Dy(i->r)*bytesperline(i->r, i->depth);
877 if(p->lnext == nil || p->lnext == p)
879 p->lnext->lprev = p->lprev;
880 p->lprev->lnext = p->lnext;
892 p->lnext = lru.lnext;
898 if(p->open && p->image == nil){
901 if((p->image = readimage(display, fd, 1)) == nil)
902 fprint(2, "readimage: %r\n");
908 lockdisplay(display);
909 imemsize += imagesize(p->image);
910 unlockdisplay(display);
922 if(p->open == nil || p->image == nil)
924 lockdisplay(display);
925 imemsize -= imagesize(p->image);
927 unlockdisplay(display);
932 unloadpages(ulong limit)
936 while(imemsize >= limit && (p = lru.lprev) != &lru){
944 loadpages(Page *p, int oviewgen)
946 while(p && viewgen == oviewgen){
949 if(viewgen != oviewgen){
959 if(size.x && size.y && newwin){
963 lockdisplay(display);
965 unlockdisplay(display);
968 if(p != current || imemsize >= imemlimit)
975 * A draw operation that touches only the area contained in bot but not in top.
976 * mp and sp get aligned with bot.min.
979 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
980 Image *src, Point sp, Image *mask, Point mp, int op)
986 if(Dx(bot)*Dy(bot) == 0)
989 /* no points in bot - top */
990 if(rectinrect(bot, top))
993 /* bot - top ≡ bot */
994 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
995 gendrawop(dst, bot, src, sp, mask, mp, op);
1000 /* split bot into rectangles that don't intersect top */
1002 if(bot.min.x < top.min.x){
1003 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
1004 delta = subpt(r.min, origin);
1005 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
1006 bot.min.x = top.min.x;
1010 if(bot.max.x > top.max.x){
1011 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
1012 delta = subpt(r.min, origin);
1013 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
1014 bot.max.x = top.max.x;
1018 if(bot.min.y < top.min.y){
1019 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
1020 delta = subpt(r.min, origin);
1021 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
1022 bot.min.y = top.min.y;
1026 if(bot.max.y > top.max.y){
1027 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
1028 delta = subpt(r.min, origin);
1029 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
1030 bot.max.y = top.max.y;
1035 alphachan(ulong chan)
1037 for(; chan; chan >>= 8)
1038 if(TYPE(chan) == CAlpha)
1044 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *b, Image *s, Point sp, int f)
1052 if(r.min.x < d->r.min.x){
1053 sp.x += (d->r.min.x - r.min.x)/f;
1054 a.x = (d->r.min.x - r.min.x)%f;
1055 r.min.x = d->r.min.x;
1057 if(r.min.y < d->r.min.y){
1058 sp.y += (d->r.min.y - r.min.y)/f;
1059 a.y = (d->r.min.y - r.min.y)%f;
1060 r.min.y = d->r.min.y;
1063 w = s->r.max.x - sp.x;
1067 dr.max.x = dr.min.x+w;
1068 if(!alphachan(s->chan))
1071 if(b) gendrawdiff(d, dr, top, b, sp, nil, ZP, SoverD);
1072 gendrawdiff(d, dr, top, s, sp, nil, ZP, SoverD);
1075 if((t = allocimage(display, dr, s->chan, 0, 0)) == nil)
1077 for(; dr.min.y < r.max.y; dr.min.y++){
1078 dr.max.y = dr.min.y+1;
1079 draw(t, dr, s, nil, sp);
1086 for(sp=dr.min; dr.min.x < r.max.x; sp.x++){
1087 dr.max.x = dr.min.x+1;
1088 if(b) gendrawdiff(d, dr, top, b, sp, nil, ZP, SoverD);
1089 gendrawdiff(d, dr, top, t, sp, nil, ZP, SoverD);
1090 for(dr.min.x++; ++a.x < f && dr.min.x < r.max.x; dr.min.x++){
1091 dr.max.x = dr.min.x+1;
1092 gendrawdiff(d, dr, top, d, Pt(dr.min.x-1, dr.min.y), nil, ZP, SoverD);
1102 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
1106 drawframe(Rectangle r)
1108 border(screen, r, -Borderwidth, frame, ZP);
1109 gendrawdiff(screen, screen->r, insetrect(r, -Borderwidth), ground, ZP, nil, ZP, SoverD);
1110 flushimage(display, 1);
1120 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
1121 zoomdraw(screen, r, ZR, paper, i, i->r.min, zoom);
1123 r = Rpt(ZP, stringsize(font, p->name));
1124 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2),
1125 divpt(r.max, 2)), screen->r.min));
1126 draw(screen, r, paper, nil, ZP);
1127 string(screen, r.min, display->black, ZP, font, p->name);
1133 translate(Page *p, Point d)
1139 if(i==0 || d.x==0 && d.y==0)
1141 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
1142 pos = addpt(pos, d);
1143 nr = rectaddpt(r, d);
1144 if(rectclip(&r, screen->r))
1145 draw(screen, rectaddpt(r, d), screen, nil, r.min);
1148 zoomdraw(screen, nr, rectaddpt(r, d), paper, i, i->r.min, zoom);
1158 if((s = pagewalk) == nil || *s == 0)
1160 n = strlen(p->name);
1161 if(n == 0 || strncmp(s, p->name, n) != 0)
1167 if(s[n] == '/' || s[n] == '!'){
1175 trywalk(char *name, char *addr)
1177 static char buf[NPATH];
1181 memset(buf, 0, sizeof(buf));
1182 snprint(buf, sizeof(buf), "%s%s%s",
1184 (name && addr) ? "!" : "",
1192 for(; p; p = p->next)
1203 findpage(char *name)
1205 static char buf[NPATH], *f[32];
1213 /* look in current document */
1214 if(current && current->up){
1215 for(p = current->up->down; p; p = p->next)
1216 if(cistrncmp(p->name, name, n) == 0)
1219 /* look everywhere */
1221 for(p = root->down; p; p = nextpage(p))
1222 if(cistrncmp(p->name, name, n) == 0)
1226 return trywalk(name, nil);
1230 writeaddr(Page *p, char *file)
1232 char buf[NPATH], *s;
1235 s = pageaddr(p, buf, sizeof(buf));
1236 if((fd = open(file, OWRITE)) >= 0){
1237 write(fd, s, strlen(s));
1247 for(p = root->down; i > 0 && p; p = nextpage(p))
1258 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
1260 return (p == x) ? i : -1;
1269 return shortlabel(p->name);
1276 if(i < 0 || i >= nelem(cmds))
1282 * spawn new proc to load a run of pages starting with p
1283 * the display should *not* be locked as it gets called
1284 * from recursive page load.
1294 esetcursor(&reading);
1295 writeaddr(p, "/dev/label");
1298 switch(rfork(RFPROC|RFMEM)){
1300 sysfatal("rfork: %r");
1302 loadpages(p, oviewgen);
1305 if(++nproc >= NPROC)
1310 /* recursive display lock, called from main proc only */
1312 drawlock(int dolock){
1316 lockdisplay(display);
1319 unlockdisplay(display);
1331 unloadpages(imemlimit/2);
1341 for(p = nextpage(current); p; p = nextpage(p))
1342 if(p->image || p->open)
1352 for(p = prevpage(current); p; p = prevpage(p))
1353 if(p->image || p->open)
1361 char nam[64], *argv[4];
1368 if((fd = openpage(p)) < 0)
1370 if(rfork(RFPROC|RFMEM|RFFDG|RFENVG|RFNOTEG|RFNOWAIT) == 0){
1371 dupfds(fd, 1, 2, -1);
1372 snprint(nam, sizeof nam, "/bin/%s", argv0);
1377 sysfatal("exec: %r");
1388 char label[64], *argv[4];
1394 snprint(label, sizeof(label), "%s %s", p->ext, p->name);
1397 ps = addpt(subpt(p->image->r.max, p->image->r.min), Pt(24, 24));
1399 if((fd = p->fd) < 0){
1400 if(p->open != popenfile)
1402 fd = open((char*)p->data, OREAD);
1407 if(rfork(RFPROC|RFMEM|RFFDG|RFNOTEG|RFREND|RFNOWAIT) == 0){
1408 if(newwindow(nil) != -1){
1409 dupfds(fd, open("/dev/cons", OWRITE), open("/dev/cons", OWRITE), -1);
1410 if((fd = open("/dev/label", OWRITE)) >= 0){
1411 write(fd, label, strlen(label));
1420 exec("/bin/rc", argv);
1434 if(new && getwindow(display, Refnone) == -1)
1435 sysfatal("getwindow: %r");
1446 void killcohort(void)
1449 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
1450 postnote(PNGROUP, cohort, "kill");
1455 void drawerr(Display *, char *msg)
1457 sysfatal("draw: %s", msg);
1463 fprint(2, "usage: %s [ -iRw ] [ -m mb ] [ -p ppi ] [ -j addr ] [ file ... ]\n", argv0);
1468 docmd(int i, Mouse *m)
1470 char buf[NPATH], *s;
1496 resize = subpt(screen->r.max, screen->r.min);
1502 resize = subpt(screen->r.max, screen->r.min);
1507 if(current == nil || !canqlock(current))
1509 o = subpt(m->xy, screen->r.min);
1513 pos = addpt(mulpt(subpt(pos, o), 2), o);
1518 pos = addpt(divpt(subpt(pos, o), 2), o);
1525 if(current == nil || !canqlock(current))
1529 if(current->up && current->up != root)
1530 s = current->up->name;
1531 snprint(buf, sizeof(buf), "%s%s%s.bit",
1535 if(eenter("Write", buf, sizeof(buf), m) > 0){
1536 if((fd = create(buf, OWRITE, 0666)) < 0){
1537 errstr(buf, sizeof(buf));
1538 eenter(buf, 0, 0, m);
1540 esetcursor(&reading);
1541 writeimage(fd, current->image, 0);
1550 if(current == nil || !canqlock(current))
1556 writeaddr(current, "/dev/snarf");
1578 if(current == nil || !canqlock(current))
1582 p = prevpage(current);
1591 z.y = Dy(screen->r);
1592 if(pos.y+z.y > Dy(screen->r))
1593 pos.y = Dy(screen->r) - z.y;
1600 z = pagesize(current);
1601 if(pos.y+z.y <= Dy(screen->r)){
1602 p = nextpage(current);
1613 translate(current, Pt(0, -y));
1618 main(int argc, char *argv[])
1620 enum { Eplumb = 4 };
1648 trywalk(EARGF(usage()), nil);
1651 imemlimit = atol(EARGF(usage()))*MiB;
1654 ppi = atoi(EARGF(usage()));
1661 if(newwindow(nil) < 0)
1662 sysfatal("newwindow: %r");
1666 * so that we can stop all subprocesses with a note,
1667 * and to isolate rendezvous from other processes
1669 atnotify(catchnote, 1);
1670 if(cohort = rfork(RFPROC|RFNOTEG|RFNAMEG|RFREND)){
1677 if(initdraw(drawerr, nil, argv0) < 0)
1678 sysfatal("initdraw: %r");
1679 paper = display->white;
1680 frame = display->black;
1681 ground = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x777777FF);
1682 display->locking = 1;
1683 unlockdisplay(display);
1685 einit(Ekeyboard|Emouse);
1686 eplumb(Eplumb, "image");
1687 memset(&m, 0, sizeof(m));
1688 if((nullfd = open("/dev/null", ORDWR)) < 0)
1689 sysfatal("open: %r");
1693 current = root = addpage(nil, "", nil, nil, -1);
1695 if(*argv == nil && !imode)
1696 addpage(root, "stdin", popenfile, strdup("/fd/0"), -1);
1697 for(; *argv; argv++)
1698 addpage(root, *argv, popenfile, strdup(*argv), -1);
1710 if(current && canqlock(current)){
1714 if((m.buttons & 1) == 0)
1716 translate(current, subpt(m.xy, o));
1720 } else if(m.buttons & 2){
1722 i = emenuhit(2, &m, &cmdmenu);
1725 } else if(m.buttons & 4){
1730 pagemenu.lasthit = pageindex(current);
1731 x = pageat(emenuhit(3, &m, &pagemenu));
1735 } else if(m.buttons & 8){
1736 scroll(screen->r.min.y - m.xy.y);
1737 } else if(m.buttons & 16){
1738 scroll(m.xy.y - screen->r.min.y);
1744 scroll(-Dy(screen->r)/3);
1747 scroll(-Dy(screen->r)/2);
1750 scroll(Dy(screen->r)/3);
1753 scroll(Dy(screen->r)/2);
1756 for(i = 0; i<nelem(cmds); i++)
1757 if((cmds[i].k1 == e.kbdc) ||
1758 (cmds[i].k2 == e.kbdc) ||
1759 (cmds[i].k3 == e.kbdc))
1761 if(i < nelem(cmds)){
1765 if((e.kbdc < 0x20) ||
1766 (e.kbdc & 0xFF00) == KF ||
1767 (e.kbdc & 0xFF00) == Spec)
1769 snprint(buf, sizeof(buf), "%C", (Rune)e.kbdc);
1770 if(eenter("Go to", buf, sizeof(buf), &m) > 0)
1771 showpage(findpage(buf));
1776 if(pm && pm->ndata > 0){
1781 s = plumblookup(pm->attr, "action");
1782 if(s && strcmp(s, "quit")==0)
1784 if(s && strcmp(s, "showdata")==0){
1785 if((fd = createtmp("plumb")) < 0){
1786 fprint(2, "plumb: createtmp: %r\n");
1790 if(fd2path(fd, s, NPATH) < 0){
1794 write(fd, pm->data, pm->ndata);
1795 }else if(pm->data[0] == '/'){
1796 s = strdup(pm->data);
1798 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1799 sprint(s, "%s/%s", pm->wdir, pm->data);
1802 j = trywalk(s, plumblookup(pm->attr, "addr"));
1806 j = addpage(root, s, popenfile, s, fd);