/* perform convert command */ static int procconvert(const char *ibuf, int isiz, int fmt, const char *buri, const char *duri, bool page){ TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, ibuf); if(fmt == FMTWIKI){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); wikidump(rbuf, cols); fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); } else if(fmt == FMTTEXT){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); if(page) tcxstrprintf(rbuf, "------------------------ Tokyo Promenade ------------------------\n"); wikidumptext(rbuf, cols); if(page) tcxstrprintf(rbuf, "-----------------------------------------------------------------\n"); fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); } else if(fmt == FMTHTML){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); if(page){ const char *name = tcmapget2(cols, "name"); tcxstrprintf(rbuf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); tcxstrprintf(rbuf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"); tcxstrprintf(rbuf, "<html xmlns=\"http://www.w3.org/1999/xhtml\"" " xml:lang=\"en\" lang=\"en\">\n"); tcxstrprintf(rbuf, "<head>\n"); tcxstrprintf(rbuf, "<meta http-equiv=\"Content-Type\"" " content=\"text/html; charset=UTF-8\" />\n"); tcxstrprintf(rbuf, "<link rel=\"contents\" href=\"%@\" />\n", buri); tcxstrprintf(rbuf, "<title>%@</title>\n", name ? name : "Tokyo Promenade"); tcxstrprintf(rbuf, "</head>\n"); tcxstrprintf(rbuf, "<body>\n"); } wikidumphtml(rbuf, cols, buri, 0, duri); if(page){ tcxstrprintf(rbuf, "</body>\n"); tcxstrprintf(rbuf, "</html>\n"); } fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); } tcmapdel(cols); return 0; }
/* perform update command */ static int procupdate(const char *dbpath, int64_t id, const char *wiki){ TCTDB *tdb = tctdbnew(); if(!tctdbopen(tdb, dbpath, TDBOWRITER)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, wiki); if(!dbputart(tdb, id, cols)){ printdberr(tdb); err = true; } tcmapdel(cols); if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }
/* return 1 if handled, 0 otherwise */ int wikicmd(Wiki *w, char *s) { char *p; s = skip(s, ""); if(iscmd(s, "Del")){ if(windel(w->win, 0)) w->dead = 1; return 1; } if(iscmd(s, "New")){ wikinew(skip(s, "New")); return 1; } if(iscmd(s, "History")) return wikiload(w, "history.txt"); if(iscmd(s, "Diff")) return wikidiff(w); if(iscmd(s, "Get")){ if(winisdirty(w->win) && !w->win->warned){ w->win->warned = 1; fprint(2, "%s/%s modified\n", dir, w->arg); }else{ w->win->warned = 0; wikiget(w); } return 1; } if(iscmd(s, "Put")){ if((p=strchr(w->arg, '/')) && p[1]!='\0') fprint(2, "%s/%s is read-only\n", dir, w->arg); else wikiput(w); return 1; } return 0; }
void acmeevent(Wiki *wiki, Event *e) { Event *ea, *e2, *eq; Window *w; char *s, *t, *buf; int na; w = wiki->win; switch(e->c1){ /* origin of action */ default: Unknown: fprint(2, "unknown message %c%c\n", e->c1, e->c2); break; case 'F': /* generated by our actions; ignore */ break; case 'E': /* write to body or tag; can't affect us */ break; case 'K': /* type away; we don't care */ if(e->c2 == 'I' || e->c2 == 'D') w->warned = 0; break; case 'M': /* mouse event */ switch(e->c2){ /* type of action */ case 'x': /* mouse: button 2 in tag */ case 'X': /* mouse: button 2 in body */ ea = nil; //e2 = nil; s = e->b; if(e->flag & 2){ /* null string with non-null expansion */ e2 = recvp(w->cevent); if(e->nb==0) s = e2->b; } if(e->flag & 8){ /* chorded argument */ ea = recvp(w->cevent); /* argument */ na = ea->nb; recvp(w->cevent); /* ignore origin */ }else na = 0; /* append chorded arguments */ if(na){ t = emalloc(strlen(s)+1+na+1); sprint(t, "%s %s", s, ea->b); s = t; } /* if it's a known command, do it */ /* if it's a long message, it can't be for us anyway */ // DPRINT(2, "exec: %s\n", s); if(!wikicmd(wiki, s)) /* send it back */ winwriteevent(w, e); if(na) free(s); break; case 'l': /* mouse: button 3 in tag */ case 'L': /* mouse: button 3 in body */ //buf = nil; eq = e; if(e->flag & 2){ /* we do our own expansion for loads */ e2 = recvp(w->cevent); eq = expand(w, eq, e2); } s = eq->b; if(eq->q1>eq->q0 && eq->nb==0){ buf = emalloc((eq->q1-eq->q0)*UTFmax+1); winread(w, eq->q0, eq->q1, buf); s = buf; } if(!wikiload(wiki, s)) winwriteevent(w, e); break; case 'i': /* mouse: text inserted in tag */ case 'd': /* mouse: text deleted from tag */ break; case 'I': /* mouse: text inserted in body */ case 'D': /* mouse: text deleted from body */ w->warned = 0; break; default: goto Unknown; } } }
/* perform import command */ static int procimport(const char *dbpath, TCLIST *files, TCLIST *sufs){ TCTDB *tdb = tctdbnew(); if(!tctdbtune(tdb, TUNEBNUM, TUNEAPOW, TUNEFPOW, 0)){ printdberr(tdb); tctdbdel(tdb); return 1; } if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(!tctdbsetindex(tdb, "name", TDBITLEXICAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "cdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "mdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "xdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } tclistinvert(files); char *fpath; while((fpath = tclistpop2(files)) != NULL){ TCLIST *cfiles = tcreaddir(fpath); if(cfiles){ tclistsort(cfiles); for(int i = tclistnum(cfiles) - 1; i >= 0; i--){ const char *cfile = tclistval2(cfiles, i); bool hit = false; for(int j = 0; j < tclistnum(sufs); j++){ if(tcstribwm(cfile, tclistval2(sufs, j))){ hit = true; break; } } if(!hit) continue; char *lpath = tcsprintf("%s/%s", fpath, cfile); tclistpush2(files, lpath); tcfree(lpath); } tclistdel(cfiles); } else { int isiz; char *ibuf = tcreadfile(fpath, IOMAXSIZ, &isiz); if(ibuf){ TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, ibuf); const char *name = tcmapget2(cols, "name"); if(name && *name != '\0'){ int64_t id = tcatoi(tcmapget4(cols, "id", "")); if(dbputart(tdb, id, cols)){ id = tcatoi(tcmapget4(cols, "id", "")); printf("%s: imported: id=%lld name=%s\n", fpath, (long long)id, name); } else { printdberr(tdb); err = true; } } else { printf("%s: ignored because there is no name\n", fpath); } tcmapdel(cols); tcfree(ibuf); } } tcfree(fpath); } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }