static int tempget(Sfio_t* sp) { if (sfstrtell(sp) > sfstrsize(sp) / 2) sfstrseek(sp, 0, SEEK_SET); return sfstrtell(sp); }
static int post(Css_t* css, Cssdisc_t* disc, Connection_t* from, register Connection_t* to, int channel, const char* format, ...) { State_t* state = (State_t*)disc; char* s; ssize_t n; Sfulong_t m; va_list ap; sfprintf(state->tmp, "%d", channel); if (from) sfprintf(state->tmp, ".%d", from->fp->fd); sfputc(state->tmp, ' '); va_start(ap, format); sfvprintf(state->tmp, format, ap); va_end(ap); sfputc(state->tmp, '\n'); n = sfstrtell(state->tmp); if (!(s = sfstruse(state->tmp))) error(ERROR_SYSTEM|3, "out of space"); m = CHAN_MASK(channel); state->logged = 0; if (!to) { for (to = state->all; to; to = to->next) if ((to->mask & m) && to != from) note(css, to, state->log, s, n, 0, disc); } else if (to->mask & m) note(css, to, state->log, s, n, 0, disc); return 0; }
static int flush(Sfio_t* op) { register Col_t* col; register size_t n; if ((col = state.cols) && sfstrtell(col->sp)) { do { n = sfstrtell(col->sp); if (sfwrite(op, sfstrseek(col->sp, 0, SEEK_SET), n) != n || sfsync(op)) { error(ERROR_SYSTEM|2, "write error"); return -1; } } while (col = col->next); } state.cache = state.window; return 0; }
int pzheadprint(register Pz_t* pz, register Sfio_t* op, int parts) { register Pzpart_t* pp; char t; if (pz->flags & PZ_FORCE) sfprintf(op, "# fixed record input\n"); else { sfprintf(op, "# pzip %d.%d partition\n", pz->major, pz->minor); if (pz->disc->comment) sfprintf(op, "# %s\n", pz->disc->comment); sfprintf(op, "# window %I*u\n", sizeof(pz->win), pz->win); if (pz->prefix.count) { sfprintf(op, "\nprefix=%I*u", sizeof(pz->prefix.count), pz->prefix.count); if (pz->prefix.terminator >= 0) { t = pz->prefix.terminator; sfprintf(op, "*%s\n", fmtquote(&t, "'", "'", 1, FMT_ALWAYS)); } else sfputc(op, '\n'); } if (pz->headoptions || pz->det) { sfputc(op, '\n'); if (pz->headoptions) sfputr(op, pz->headoptions, '\n'); if (pz->det) { sfwrite(op, sfstrbase(pz->det), sfstrtell(pz->det)); sfputc(op, '\n'); } } } if (parts) { pp = pz->partdict ? (Pzpart_t*)dtfirst(pz->partdict) : pz->part; while (pp) { if (pzpartprint(pz, pp, op)) return -1; if (!pz->partdict) break; pp = (Pzpart_t*)dtnext(pz->partdict, pp); } } return sferror(op) ? -1 : 0; }
int pzheadwrite(Pz_t* pz, Sfio_t* op) { register size_t i; register size_t m; register size_t n; register char* s; if (pz->flags & PZ_HEAD) return 0; pz->oop = op; if (!(pz->flags & PZ_NOGZIP)) sfdcgzip(op, 0); if (pz->flags & PZ_NOPZIP) return 0; sfputc(op, PZ_MAGIC_1); sfputc(op, PZ_MAGIC_2); if (sfsync(op)) return -1; sfputc(op, pz->major = PZ_MAJOR); sfputc(op, pz->minor = PZ_MINOR); sfputu(op, pz->win); if (pz->disc->comment) { sfputc(op, PZ_HDR_comment); m = strlen(pz->disc->comment) + 1; sfputu(op, m); sfwrite(op, pz->disc->comment, m); } if (pz->det && (m = sfstrtell(pz->det))) { sfputc(op, PZ_HDR_options); if (!(s = sfstruse(pz->det))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "out of space"); return -1; } m++; sfputu(op, m); sfwrite(op, s, m); } if (i = pz->prefix.count) { sfputc(op, PZ_HDR_prefix); if (pz->prefix.terminator >= 0) { m = 0; while (i-- > 0) { if (!(s = sfgetr(pz->io, pz->prefix.terminator, 0))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix record%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s"); return -1; } m += n = sfvalue(pz->io); sfwrite(pz->tmp, s, n); } s = sfstrseek(pz->tmp, 0, SEEK_SET); } else { m = i; if (!(s = (char*)sfreserve(pz->io, m, 0))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix byte%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s"); return -1; } } sfputu(op, m); sfwrite(op, s, m); } pz->flags |= PZ_HEAD; return pzpartwrite(pz, op); }
int csclient(Cs_t* cs, int fd, const char* service, const char* prompt, char** argv, unsigned int flags) { register int i; char* s; Sfio_t* tmp; int done; int promptlen; int timeout; ssize_t n; int sdf[2]; Cspoll_t fds[2]; char buf[8 * 1024]; if (fd < 0 && (fd = csopen(cs, service, CS_OPEN_TEST)) < 0) { if (errno == ENOENT) error(3, "%s: server not running", service); else error(ERROR_SYSTEM|3, "%s: cannot open connect stream", service); } #if _hdr_termios if (flags & CS_CLIENT_RAW) { tcgetattr(0, &state.old_term); atexit(restore); state.new_term = state.old_term; state.new_term.c_iflag &= ~(BRKINT|IGNPAR|PARMRK|INLCR|IGNCR|ICRNL); state.new_term.c_lflag &= ~(ECHO|ECHOK|ICANON|ISIG); state.new_term.c_cc[VTIME] = 0; state.new_term.c_cc[VMIN] = 1; tcsetattr(0, TCSANOW, &state.new_term); } #endif sdf[0] = fd; sdf[1] = 1; if (argv && *argv) { fds[0].fd = 1; fds[0].events = CS_POLL_WRITE; } else { argv = 0; fds[0].fd = 0; fds[0].events = CS_POLL_READ; } fds[1].fd = fd; fds[1].events = CS_POLL_READ; done = 0; if (promptlen = (!argv && prompt && isatty(fds[0].fd) && isatty(1)) ? strlen(prompt) : 0) write(1, prompt, promptlen); timeout = CS_NEVER; tmp = 0; while (cspoll(cs, fds, elementsof(fds), timeout) > 0) for (i = 0; i < elementsof(fds); i++) if (fds[i].status & (CS_POLL_READ|CS_POLL_WRITE)) { if (!i && argv) { if (!*argv) { argv = 0; fds[0].fd = 0; if (flags & CS_CLIENT_ARGV) { if (done++) return 0; timeout = 500; } fds[0].events = CS_POLL_READ; continue; } if (!tmp && !(tmp = sfstropen())) error(ERROR_SYSTEM|3, "out of space"); for (;;) { s = *argv++; if ((flags & CS_CLIENT_SEP) && *s == ':' && !*(s + 1)) break; if (sfstrtell(tmp)) sfputc(tmp, ' '); sfprintf(tmp, "%s", s); if (!(flags & CS_CLIENT_SEP) || !*argv) break; } sfputc(tmp, '\n'); n = sfstrtell(tmp); s = sfstruse(tmp); } else if ((n = read(fds[i].fd, s = buf, sizeof(buf) - 1)) < 0) error(ERROR_SYSTEM|3, "/dev/fd/%d: read error", fds[i].fd); if (!n) { if (done++) return 0; if (!i) write(sdf[i], "quit\n", 5); continue; } if (!i) { #if _hdr_termios register char* u; register int m; s[n] = 0; if ((u = strchr(s, 035))) { if ((m = u - s) > 0 && write(sdf[i], s, m) != m) error(ERROR_SYSTEM|3, "/dev/fd/%d: write error", sdf[i]); tcsetattr(0, TCSANOW, &state.old_term); if (promptlen) write(1, prompt, promptlen); if ((n = read(fds[i].fd, s = buf, sizeof(buf) - 1)) <= 0) { write(1, "\n", 1); return 0; } buf[n - 1] = 0; if (*u == 'q' || *u == 'Q') return 0; tcsetattr(0, TCSANOW, &state.new_term); if (*u) error(1, "%s: unknown command", u); continue; } #endif } if (write(sdf[i], s, n) != n) error(ERROR_SYSTEM|3, "/dev/fd/%d: write error", sdf[i]); if (sdf[i] == 1 && promptlen) write(1, prompt, promptlen); } return error_info.errors != 0; }
static void act(register Ftw_t* ftw, int op) { char* s; Sfio_t* fp; int i; int j; int k; int n; int r; switch (op) { case ACT_CMDARG: if ((i = cmdarg(state.cmd, ftw->path, ftw->pathlen)) >= state.errexit) exit(i); break; case ACT_CODE: if (findwrite(state.find, ftw->path, ftw->pathlen, (ftw->info & FTW_D) ? "system/dir" : (char*)0)) state.finderror = 1; break; case ACT_CODETYPE: fp = sfopen(NiL, PATH(ftw), "r"); if (findwrite(state.find, ftw->path, ftw->pathlen, magictype(state.magic, fp, PATH(ftw), &ftw->statb))) state.finderror = 1; if (fp) sfclose(fp); break; case ACT_EVAL: eval(state.action, ftw); break; case ACT_INTERMEDIATE: intermediate(ftw, ftw->path); break; case ACT_LIST: sfputr(sfstdout, ftw->path, '\n'); break; case ACT_SNAPSHOT: print(state.snapshot.tmp, ftw, state.snapshot.format.path); sfputc(state.snapshot.tmp, state.snapshot.format.delim); i = sfstrtell(state.snapshot.tmp); print(state.snapshot.tmp, ftw, state.snapshot.format.easy); j = sfstrtell(state.snapshot.tmp); s = sfstrbase(state.snapshot.tmp); r = SNAPSHOT_new; if (!state.snapshot.prev) k = 1; else { do { if (!(k = urlcmp(state.snapshot.prev, s, state.snapshot.format.delim))) { r = SNAPSHOT_changed; if (!(k = memcmp(state.snapshot.prev + i, s + i, j - i) || state.snapshot.prev[j] != state.snapshot.format.delim)) { if ((n = (int)sfvalue(sfstdin)) > 4 && state.snapshot.prev[n-2] == state.snapshot.format.delim) { sfwrite(sfstdout, state.snapshot.prev, n - 4); sfputc(sfstdout, '\n'); } else sfwrite(sfstdout, state.snapshot.prev, n); } } else if (k > 0) break; else if (k < 0 && (n = (int)sfvalue(sfstdin)) > 4 && (state.snapshot.prev[n-2] != state.snapshot.format.delim || state.snapshot.prev[n-3] != SNAPSHOT_deleted)) { sfwrite(sfstdout, state.snapshot.prev, n - (state.snapshot.prev[n-2] == state.snapshot.format.delim ? 4 : 1)); sfputc(sfstdout, state.snapshot.format.delim); sfputc(sfstdout, SNAPSHOT_deleted); sfputc(sfstdout, state.snapshot.format.delim); sfputc(sfstdout, '\n'); if (state.cmdflags & CMD_TRACE) error(1, "%s deleted", ftw->path); } if (!(state.snapshot.prev = sfgetr(sfstdin, '\n', 0))) break; } while (k < 0); } if (k) { if (state.snapshot.format.hard && (ftw->info & FTW_F)) { sfputc(state.snapshot.tmp, state.snapshot.format.delim); print(state.snapshot.tmp, ftw, state.snapshot.format.hard); } sfputc(state.snapshot.tmp, state.snapshot.format.delim); sfputc(state.snapshot.tmp, r); sfputc(state.snapshot.tmp, state.snapshot.format.delim); sfputr(sfstdout, sfstruse(state.snapshot.tmp), '\n'); if (state.cmdflags & CMD_TRACE) error(1, "%s %s", ftw->path, r == SNAPSHOT_new ? "new" : "changed"); } else sfstrseek(state.snapshot.tmp, SEEK_SET, 0); break; } }
static void restore(register Joblist_t* job, Sfio_t* buf, Sfio_t* att) { register char* s; register char* b; char* u; char* down; char* back; char* sep; int downlen; int localview; void* pos; Var_t* v; Sfio_t* opt; Sfio_t* tmp; Sfio_t* context; push(job); localview = state.localview; state.localview = state.mam.statix && !state.expandview && state.user && !(job->flags & CO_ALWAYS); if ((job->flags & CO_LOCALSTACK) || (job->target->dynamic & D_hasscope)) { register Rule_t* r; register List_t* p; job->flags |= CO_LOCALSTACK; pos = pushlocal(); opt = sfstropen(); if (job->target->dynamic & D_hasscope) for (p = job->prereqs; p; p = p->next) if ((r = p->rule)->dynamic & D_scope) { if (*r->name == '-') set(r->name, 1, opt); else parse(NiL, r->name, r->name, opt); } else if ((r->property & (P_make|P_local|P_use)) == (P_make|P_local) && r->action) parse(NiL, r->action, r->name, opt); } context = state.context; if (state.targetcontext && *(u = unbound(job->target)) != '/' && (s = strrchr(u, '/'))) { size_t n; int c; tmp = sfstropen(); downlen = s - u; *s = 0; sfprintf(tmp, "%s%c", u, 0); n = sfstrtell(tmp); c = '/'; do { if (u = strchr(u, '/')) u++; else c = 0; sfputr(tmp, "..", c); } while (c); *s = '/'; back = (down = sfstrbase(tmp)) + n; state.context = buf; buf = sfstropen(); state.localview++; } else state.context = 0; if (job->action) expand(buf, job->action); if (state.context) { s = sfstruse(buf); sep = strchr(s, '\n') ? "\n" : "; "; sfprintf(state.context, "{ cd %s%s", down, sep); while (b = strchr(s, MARK_CONTEXT)) { sfwrite(state.context, s, b - s); if (!(s = strchr(++b, MARK_CONTEXT))) error(PANIC, "unbalanced MARK_CONTEXT"); *s++ = 0; if (*b == '/' || (u = getbound(b)) && *u == '/') sfputr(state.context, b, -1); else if (*b) { if (strneq(b, down, downlen)) switch (*(b + downlen)) { case 0: sfputc(state.context, '.'); continue; case '/': sfputr(state.context, b + downlen + 1, -1); continue; } if (streq(b, ".")) sfputr(state.context, back, -1); else if (isspace(*b)) sfputr(state.context, b, -1); else sfprintf(state.context, "%s/%s", back, b); } } sfprintf(state.context, "%s%s}", s, sep); sfstrclose(tmp); sfstrclose(buf); } state.context = context; sfprintf(att, "label=%s", job->target->name); if ((v = getvar(CO_ENV_ATTRIBUTES)) && !(v->property & V_import)) sfprintf(att, ",%s", v->value); if (job->flags & CO_LOCALSTACK) { poplocal(pos); if (*(s = sfstruse(opt))) set(s, 1, NiL); sfclose(opt); } state.localview = localview; pop(job); }