Artifact 9c875852ae7fd115ba16ed3dd47d76280d6140feb5dd9774133bb97ea7b071cc:
- File
src/input.c
— part of check-in
[bb88ac58ac]
at
2026-11-19 20:35:01
on branch templates
— Prompt templates, as Pi's
Markdown files in ~/.pic/prompts/ and, once the workspace is trusted, WORKSPACE/.pic/prompts/ become /name commands: Pi's frontmatter (description, argument-hint), shell-like arguments and substitutions ($1, $@, $ARGUMENTS, ${1:-default}, ${@:N}, ${@:N:L}). They expand where text becomes a request, so ask, chat and the terminal agree; the slash menu lists them, built-in commands win a name clash, and /reload re-reads them. pic prompts [--json] lists them. Files are read through the platform's input reader, which gains a directory listing. Fixes a pollfd array one entry too small when a catalog refresh runs during a reply. (user: dmestas, size: 2654) [annotate] [blame] [check-ins using] [more...]
/* Reading inputs that are files by nature (see input.h). */ #include <dirent.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "input.h" #include "platform.h" static int trusted(const struct stat *st) { return (st->st_uid == getuid() || st->st_uid == 0) && (st->st_mode & 022) == 0; } char *pic_input_read(const char *path, size_t max, int flags, size_t *len, int *truncated) { return pic_platform_get()->input_read(path, max, flags, len, truncated); } int pic_input_list_host(const char *dir, int (*fn)(void *ctx, const char *name), void *ctx) { DIR *d = opendir(dir); struct dirent *e; if (!d) { return -1; } while ((e = readdir(d)) != NULL) { if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0 && fn(ctx, e->d_name) != 0) { break; } } closedir(d); return 0; } char *pic_input_read_host(const char *path, size_t max, int flags, size_t *len, int *truncated) { struct stat st, dst; char *buf; size_t got = 0; int fd; *len = 0; if (truncated) { *truncated = 0; } fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK); if (fd < 0) { return NULL; } if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { close(fd); errno = ENOENT; return NULL; } if (flags & PIC_INPUT_TRUSTED) { char dir[4096]; const char *slash = strrchr(path, '/'); (void)snprintf(dir, sizeof dir, "%.*s", slash ? (int)(slash - path) + 1 : 1, slash ? path : "."); if (!trusted(&st) || stat(dir, &dst) != 0 || !trusted(&dst)) { close(fd); errno = EPERM; return NULL; } } if (!(buf = malloc(max + 2))) { close(fd); errno = ENOMEM; return NULL; } for (;;) { /* one byte past max tells "over" from "exactly max" */ ssize_t n = read(fd, buf + got, max + 1 - got); if (n < 0 && errno == EINTR) { continue; } if (n < 0) { int e = errno; close(fd); free(buf); errno = e; return NULL; } got += (size_t)n; if (n == 0 || got > max) { break; } } close(fd); if (got > max) { if (!(flags & PIC_INPUT_TRUNCATE)) { free(buf); errno = EFBIG; return NULL; } got = max; if (truncated) { *truncated = 1; } } buf[got] = '\0'; *len = got; return buf; }