Skip to content
pi-c Artifact [cc37e7fbbe]
Login

Artifact [cc37e7fbbe]

Artifact cc37e7fbbe236be9937f88ae3d7633e556f4ac00170132652bceb6ecbbfdd1d7:


/* pic help; see help.h. */
#include "help.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rpc.h"

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;
}

/* get_help: the topics' names, and TOPIC's text when the command names one
 * ("text" is null for an unknown topic). */
static int help_answer(pic_rpc_call *c) {
    char *topic = pic_rpc_text(c, "$.topic"), *text = topic ? pic_help_text(topic) : NULL, *res;
    sqlite3_str *t = sqlite3_str_new(NULL);
    sqlite3_str_appendall(t, "{\"topics\":[");
    for (size_t i = 0; i < NTOPICS; i++) {
        sqlite3_str_appendall(t, i ? "," : "");
        pic_rpc_quote(t, help_topics[i].name);
    }
    sqlite3_str_appendall(t, "],\"text\":");
    if (text) {
        pic_rpc_quote(t, text);
    } else {
        sqlite3_str_appendall(t, "null");
    }
    sqlite3_str_appendall(t, "}");
    res = sqlite3_str_finish(t);
    c->data = res ? strdup(res) : NULL;
    sqlite3_free(res);
    free(topic);
    free(text);
    return c->data ? 0 : -1;
}

/* get_changelog: CHANGELOG.md's text, as compiled in. */
static int changelog_answer(pic_rpc_call *c) {
    sqlite3_str *all = sqlite3_str_new(NULL), *t = sqlite3_str_new(NULL);
    char *text, *res;
    for (size_t i = 0; pic_changelog[i]; i++) {
        sqlite3_str_appendall(all, pic_changelog[i]);
    }
    text = sqlite3_str_finish(all);
    sqlite3_str_appendall(t, "{\"text\":");
    pic_rpc_quote(t, text ? text : "");
    sqlite3_str_appendall(t, "}");
    sqlite3_free(text);
    res = sqlite3_str_finish(t);
    c->data = res ? strdup(res) : NULL;
    sqlite3_free(res);
    return c->data ? 0 : -1;
}

const pic_rpc_command pic_help_rpc = {"get_help", "the manual's topics, and one topic's text",
                                      PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, help_answer};
const pic_rpc_command pic_changelog_rpc = {"get_changelog", "the changelog's text",
                                           PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, changelog_answer};