/* pic help; see help.h. */ #include "help.h" #include #include #include typedef struct { const char *name, *title; int reference; const char *const *chunks; /* the file's text, NULL-terminated */ } pic_help_topic; #include "manual.inc" #define NTOPICS (sizeof help_topics / sizeof help_topics[0]) const char *const pic_changelog[] = { #include "changelog.inc" }; void pic_version(char *out, size_t cap) { (void)snprintf(out, cap, "unknown"); for (size_t i = 0; pic_changelog[i]; i++) { const char *at = strstr(pic_changelog[i], "\n## "); if (at) { (void)snprintf(out, cap, "%.10s", at + 4); return; } } } size_t pic_help_count(void) { return NTOPICS; } const char *pic_help_name(size_t i) { return i < NTOPICS ? help_topics[i].name : NULL; } const char *pic_help_title(size_t i) { return i < NTOPICS ? help_topics[i].title : NULL; } int pic_help_is_reference(size_t i) { return i < NTOPICS && help_topics[i].reference; } static const pic_help_topic *find(const char *name) { for (size_t i = 0; name && i < NTOPICS; i++) { if (strcmp(help_topics[i].name, name) == 0) { return &help_topics[i]; } } return NULL; } char *pic_help_text(const char *name) { const pic_help_topic *t = find(name); size_t len = 0; char *out; if (!t) { return NULL; } for (const char *const *c = t->chunks; *c; c++) { len += strlen(*c); } if (!(out = malloc(len + 1))) { return NULL; } len = 0; for (const char *const *c = t->chunks; *c; c++) { size_t n = strlen(*c); memcpy(out + len, *c, n); len += n; } out[len] = '\0'; return out; } void pic_help_index(FILE *out) { fputs("pic help TOPIC shows a topic; /help TOPIC does in the terminal interface.\n", out); for (int ref = 0; ref <= 1; ref++) { fputs(ref ? "\nReference:\n" : "\nManual:\n", out); for (size_t i = 0; i < NTOPICS; i++) { if (help_topics[i].reference == ref) { fprintf(out, " %-20s %s\n", help_topics[i].name, help_topics[i].title); } } } } int pic_help_command(int argc, char **argv) { const pic_help_topic *t; if (argc == 0) { pic_help_index(stdout); return 0; } if (argc > 1 || !(t = find(argv[0]))) { if (argc > 1) { fputs("pic help: one topic at a time\n", stderr); } else { fprintf(stderr, "pic help: no topic named %s\n", argv[0]); } pic_help_index(stderr); return 1; } for (const char *const *c = t->chunks; *c; c++) { fputs(*c, stdout); } return 0; }