2 014c66b6 2023-06-25 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 014c66b6 2023-06-25 op * Permission to use, copy, modify, and distribute this software for any
5 014c66b6 2023-06-25 op * purpose with or without fee is hereby granted, provided that the above
6 014c66b6 2023-06-25 op * copyright notice and this permission notice appear in all copies.
8 014c66b6 2023-06-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 014c66b6 2023-06-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 014c66b6 2023-06-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 014c66b6 2023-06-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 014c66b6 2023-06-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 014c66b6 2023-06-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 014c66b6 2023-06-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 014c66b6 2023-06-25 op #include <sys/tree.h>
19 014c66b6 2023-06-25 op #include <ctype.h>
20 014c66b6 2023-06-25 op #include <event.h>
21 014c66b6 2023-06-25 op #include <fnmatch.h>
22 014c66b6 2023-06-25 op #include <limits.h>
23 014c66b6 2023-06-25 op #include <signal.h>
24 014c66b6 2023-06-25 op #include <stdlib.h>
25 014c66b6 2023-06-25 op #include <string.h>
26 014c66b6 2023-06-25 op #include <unistd.h>
28 014c66b6 2023-06-25 op #include <sqlite3.h>
30 014c66b6 2023-06-25 op #include "log.h"
31 014c66b6 2023-06-25 op #include "pkg.h"
32 014c66b6 2023-06-25 op #include "tmpl.h"
34 014c66b6 2023-06-25 op #ifndef nitems
35 014c66b6 2023-06-25 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 014c66b6 2023-06-25 op char dbpath[PATH_MAX];
40 014c66b6 2023-06-25 op void server_sig_handler(int, short, void *);
41 014c66b6 2023-06-25 op void server_open_db(struct env *);
42 014c66b6 2023-06-25 op void server_close_db(struct env *);
43 014c66b6 2023-06-25 op __dead void server_shutdown(struct env *);
44 014c66b6 2023-06-25 op int server_reply(struct client *, int, const char *);
46 014c66b6 2023-06-25 op int route_dispatch(struct env *, struct client *);
47 014c66b6 2023-06-25 op int route_home(struct env *, struct client *);
48 014c66b6 2023-06-25 op int route_search(struct env *, struct client *);
49 014c66b6 2023-06-25 op int route_categories(struct env *, struct client *);
50 014c66b6 2023-06-25 op int route_listing(struct env *, struct client *);
51 014c66b6 2023-06-25 op int route_port(struct env *, struct client *);
53 014c66b6 2023-06-25 op typedef int (*route_t)(struct env *, struct client *);
55 014c66b6 2023-06-25 op static const struct route {
56 014c66b6 2023-06-25 op const char *r_path;
58 014c66b6 2023-06-25 op } routes[] = {
59 014c66b6 2023-06-25 op { "/", route_home },
60 014c66b6 2023-06-25 op { "/search", route_search },
61 014c66b6 2023-06-25 op { "/all", route_categories },
62 014c66b6 2023-06-25 op { "/*", route_port },
66 014c66b6 2023-06-25 op server_sig_handler(int sig, short ev, void *arg)
68 014c66b6 2023-06-25 op struct env *env = arg;
71 014c66b6 2023-06-25 op * Normal signal handler rules don't apply because libevent
72 014c66b6 2023-06-25 op * decouples for us.
75 014c66b6 2023-06-25 op switch (sig) {
77 014c66b6 2023-06-25 op log_info("re-opening the db");
78 014c66b6 2023-06-25 op server_close_db(env);
79 014c66b6 2023-06-25 op server_open_db(env);
83 014c66b6 2023-06-25 op server_shutdown(env);
86 014c66b6 2023-06-25 op fatalx("unexpected signal %d", sig);
90 014c66b6 2023-06-25 op static inline void
91 014c66b6 2023-06-25 op loadstmt(sqlite3 *db, sqlite3_stmt **stmt, const char *sql)
95 014c66b6 2023-06-25 op err = sqlite3_prepare_v2(db, sql, -1, stmt, NULL);
96 014c66b6 2023-06-25 op if (err != SQLITE_OK)
97 014c66b6 2023-06-25 op fatalx("failed prepare statement \"%s\": %s",
98 014c66b6 2023-06-25 op sql, sqlite3_errstr(err));
102 014c66b6 2023-06-25 op server_open_db(struct env *env)
106 014c66b6 2023-06-25 op err = sqlite3_open_v2(dbpath, &env->env_db,
107 014c66b6 2023-06-25 op SQLITE_OPEN_READONLY, NULL);
108 014c66b6 2023-06-25 op if (err != SQLITE_OK)
109 014c66b6 2023-06-25 op fatalx("can't open database %s: %s", dbpath,
110 014c66b6 2023-06-25 op sqlite3_errmsg(env->env_db));
112 014c66b6 2023-06-25 op /* load prepared statements */
113 014c66b6 2023-06-25 op loadstmt(env->env_db, &env->env_qsearch,
114 014c66b6 2023-06-25 op "select webpkg_fts.pkgstem, webpkg_fts.comment, paths.fullpkgpath"
115 014c66b6 2023-06-25 op " from webpkg_fts"
116 014c66b6 2023-06-25 op " join _ports p on p.fullpkgpath = webpkg_fts.id"
117 014c66b6 2023-06-25 op " join _paths paths on paths.id = webpkg_fts.id"
118 014c66b6 2023-06-25 op " where webpkg_fts match ?"
119 014c66b6 2023-06-25 op " order by bm25(webpkg_fts)");
121 014c66b6 2023-06-25 op loadstmt(env->env_db, &env->env_qfullpkgpath,
122 014c66b6 2023-06-25 op "select p.fullpkgpath, pp.pkgstem, pp.comment, pp.pkgname,"
123 014c66b6 2023-06-25 op " d.value, e.value, r.value, pp.homepage"
124 014c66b6 2023-06-25 op " from _paths p"
125 014c66b6 2023-06-25 op " join _descr d on d.fullpkgpath = p.id"
126 014c66b6 2023-06-25 op " join _ports pp on pp.fullpkgpath = p.id"
127 014c66b6 2023-06-25 op " join _email e on e.keyref = pp.maintainer"
128 014c66b6 2023-06-25 op " left join _readme r on r.fullpkgpath = p.id"
129 014c66b6 2023-06-25 op " where p.fullpkgpath = ?");
131 014c66b6 2023-06-25 op loadstmt(env->env_db, &env->env_qcats,
132 014c66b6 2023-06-25 op "select distinct value from categories order by value");
134 014c66b6 2023-06-25 op loadstmt(env->env_db, &env->env_qbycat,
135 014c66b6 2023-06-25 op "select fullpkgpath from categories where value = ?"
136 014c66b6 2023-06-25 op " order by fullpkgpath");
140 014c66b6 2023-06-25 op server_close_db(struct env *env)
144 014c66b6 2023-06-25 op sqlite3_finalize(env->env_qsearch);
145 014c66b6 2023-06-25 op sqlite3_finalize(env->env_qfullpkgpath);
146 014c66b6 2023-06-25 op sqlite3_finalize(env->env_qcats);
147 014c66b6 2023-06-25 op sqlite3_finalize(env->env_qbycat);
149 014c66b6 2023-06-25 op if ((err = sqlite3_close(env->env_db)) != SQLITE_OK)
150 014c66b6 2023-06-25 op log_warnx("sqlite3_close %s", sqlite3_errstr(err));
154 014c66b6 2023-06-25 op server_main(const char *db)
156 014c66b6 2023-06-25 op struct env env;
157 014c66b6 2023-06-25 op struct event sighup;
158 014c66b6 2023-06-25 op struct event sigint;
159 014c66b6 2023-06-25 op struct event sigterm;
161 014c66b6 2023-06-25 op signal(SIGPIPE, SIG_IGN);
163 014c66b6 2023-06-25 op memset(&env, 0, sizeof(env));
165 014c66b6 2023-06-25 op if (pledge("stdio rpath flock unix", NULL) == -1)
166 014c66b6 2023-06-25 op fatal("pledge");
168 014c66b6 2023-06-25 op if (realpath(db, dbpath) == NULL)
169 014c66b6 2023-06-25 op fatal("realpath %s", db);
171 014c66b6 2023-06-25 op server_open_db(&env);
173 014c66b6 2023-06-25 op event_init();
175 014c66b6 2023-06-25 op env.env_sockfd = 3;
177 014c66b6 2023-06-25 op event_set(&env.env_sockev, env.env_sockfd, EV_READ | EV_PERSIST,
178 014c66b6 2023-06-25 op fcgi_accept, &env);
179 014c66b6 2023-06-25 op event_add(&env.env_sockev, NULL);
181 014c66b6 2023-06-25 op evtimer_set(&env.env_pausev, fcgi_accept, &env);
183 014c66b6 2023-06-25 op signal_set(&sighup, SIGHUP, server_sig_handler, &env);
184 014c66b6 2023-06-25 op signal_set(&sigint, SIGINT, server_sig_handler, &env);
185 014c66b6 2023-06-25 op signal_set(&sigterm, SIGTERM, server_sig_handler, &env);
187 014c66b6 2023-06-25 op signal_add(&sighup, NULL);
188 014c66b6 2023-06-25 op signal_add(&sigint, NULL);
189 014c66b6 2023-06-25 op signal_add(&sigterm, NULL);
191 014c66b6 2023-06-25 op log_info("ready");
192 014c66b6 2023-06-25 op event_dispatch();
194 014c66b6 2023-06-25 op server_shutdown(&env);
198 014c66b6 2023-06-25 op server_shutdown(struct env *env)
200 014c66b6 2023-06-25 op log_info("shutting down");
201 014c66b6 2023-06-25 op server_close_db(env);
206 014c66b6 2023-06-25 op server_reply(struct client *clt, int status, const char *ctype)
208 014c66b6 2023-06-25 op if (clt_printf(clt, "%02d %s\r\n", status, ctype) == -1)
214 014c66b6 2023-06-25 op server_handle(struct env *env, struct client *clt)
216 883611a9 2023-08-08 op log_debug("SCRIPT_NAME %s", clt->clt_script_name);
217 883611a9 2023-08-08 op log_debug("PATH_INFO %s", clt->clt_path_info);
218 014c66b6 2023-06-25 op return (route_dispatch(env, clt));
222 014c66b6 2023-06-25 op server_client_free(struct client *clt)
225 014c66b6 2023-06-25 op template_free(clt->clt_tp);
227 014c66b6 2023-06-25 op free(clt->clt_server_name);
228 014c66b6 2023-06-25 op free(clt->clt_script_name);
229 014c66b6 2023-06-25 op free(clt->clt_path_info);
230 014c66b6 2023-06-25 op free(clt->clt_query);
234 014c66b6 2023-06-25 op static inline int
235 014c66b6 2023-06-25 op unquote(char *str)
239 014c66b6 2023-06-25 op unsigned long x;
241 014c66b6 2023-06-25 op hex[2] = '\0';
244 014c66b6 2023-06-25 op switch (*p) {
246 014c66b6 2023-06-25 op if (!isxdigit((unsigned char)p[1]) ||
247 014c66b6 2023-06-25 op !isxdigit((unsigned char)p[2]) ||
248 014c66b6 2023-06-25 op (p[1] == '0' && p[2] == '0'))
251 014c66b6 2023-06-25 op hex[0] = p[1];
252 014c66b6 2023-06-25 op hex[1] = p[2];
254 014c66b6 2023-06-25 op x = strtoul(hex, NULL, 16);
255 014c66b6 2023-06-25 op *q++ = (char)x;
267 014c66b6 2023-06-25 op static inline int
268 014c66b6 2023-06-25 op fts_escape(const char *p, char *buf, size_t bufsize)
273 014c66b6 2023-06-25 op * split p into words and quote them into buf.
274 014c66b6 2023-06-25 op * quoting means wrapping each word into "..." and
275 014c66b6 2023-06-25 op * replace every " with "".
276 014c66b6 2023-06-25 op * i.e. 'C++ "framework"' -> '"C++" """framework"""'
277 014c66b6 2023-06-25 op * flatting all the whitespaces seems fine too.
281 014c66b6 2023-06-25 op while (bufsize != 0) {
282 014c66b6 2023-06-25 op p += strspn(p, " \f\n\r\t\v");
283 014c66b6 2023-06-25 op if (*p == '\0')
288 014c66b6 2023-06-25 op while (*p && !isspace((unsigned char)*p) && bufsize != 0) {
289 014c66b6 2023-06-25 op if (*p == '"') { /* double the quote character */
292 014c66b6 2023-06-25 op if (bufsize == 0)
299 014c66b6 2023-06-25 op if (bufsize < 2)
303 014c66b6 2023-06-25 op bufsize -= 2;
305 014c66b6 2023-06-25 op if ((*p == '\0') && bufsize != 0) {
314 014c66b6 2023-06-25 op route_dispatch(struct env *env, struct client *clt)
316 014c66b6 2023-06-25 op const struct route *r;
319 014c66b6 2023-06-25 op for (i = 0; i < nitems(routes); ++i) {
320 014c66b6 2023-06-25 op r = &routes[i];
322 014c66b6 2023-06-25 op if (fnmatch(r->r_path, clt->clt_path_info, 0) != 0)
324 014c66b6 2023-06-25 op return (r->r_fn(env, clt));
327 014c66b6 2023-06-25 op if (server_reply(clt, 51, "not found") == -1)
329 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
333 014c66b6 2023-06-25 op route_home(struct env *env, struct client *clt)
335 014c66b6 2023-06-25 op if (server_reply(clt, 20, "text/gemini") == -1)
339 014c66b6 2023-06-25 op if (clt_printf(clt, "# pkg_fcgi\n\n") == -1)
341 014c66b6 2023-06-25 op if (clt_printf(clt, "Welcome to pkg_fcgi, the Gemini interface "
342 014c66b6 2023-06-25 op "for the OpenBSD ports collection.\n\n") == -1)
344 dd58c961 2023-08-08 op if (clt_printf(clt, "=> %s/search Search for a package\n",
345 dd58c961 2023-08-08 op clt->clt_script_name) == -1)
347 106d2b7a 2023-08-09 op if (clt_printf(clt, "=> %s/all All categories\n",
348 dd58c961 2023-08-08 op clt->clt_script_name) == -1)
350 014c66b6 2023-06-25 op if (clt_printf(clt, "\n") == -1)
352 014c66b6 2023-06-25 op if (clt_printf(clt, "What you search will be matched against the "
353 014c66b6 2023-06-25 op "package name (pkgstem), comment, DESCR and maintainer.\n") == -1)
356 014c66b6 2023-06-25 op if (tp_home(clt->clt_tp) == -1)
360 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
364 014c66b6 2023-06-25 op route_search(struct env *env, struct client *clt)
366 014c66b6 2023-06-25 op const char *stem, *comment, *fullpkgpath;
367 014c66b6 2023-06-25 op char *query = clt->clt_query;
368 014c66b6 2023-06-25 op char equery[1024];
370 014c66b6 2023-06-25 op int found = 0;
372 014c66b6 2023-06-25 op if (query == NULL || *query == '\0') {
373 014c66b6 2023-06-25 op if (server_reply(clt, 10, "search for a package") == -1)
375 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
378 014c66b6 2023-06-25 op if (unquote(query) == -1 ||
379 014c66b6 2023-06-25 op fts_escape(query, equery, sizeof(equery)) == -1) {
380 014c66b6 2023-06-25 op if (server_reply(clt, 59, "bad request") == -1)
382 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 1));
385 014c66b6 2023-06-25 op log_debug("searching for %s", equery);
387 014c66b6 2023-06-25 op err = sqlite3_bind_text(env->env_qsearch, 1, equery, -1, NULL);
388 014c66b6 2023-06-25 op if (err != SQLITE_OK) {
389 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_bind_text \"%s\": %s", __func__,
390 014c66b6 2023-06-25 op query, sqlite3_errstr(err));
391 014c66b6 2023-06-25 op sqlite3_reset(env->env_qsearch);
393 014c66b6 2023-06-25 op if (server_reply(clt, 42, "internal error") == -1)
395 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 1));
398 014c66b6 2023-06-25 op if (server_reply(clt, 20, "text/gemini") == -1)
401 014c66b6 2023-06-25 op if (clt_printf(clt, "# search results for %s\n\n", query) == -1)
405 014c66b6 2023-06-25 op err = sqlite3_step(env->env_qsearch);
406 014c66b6 2023-06-25 op if (err == SQLITE_DONE)
408 014c66b6 2023-06-25 op if (err != SQLITE_ROW) {
409 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_step %s", __func__,
410 014c66b6 2023-06-25 op sqlite3_errstr(err));
415 014c66b6 2023-06-25 op stem = sqlite3_column_text(env->env_qsearch, 0);
416 014c66b6 2023-06-25 op comment = sqlite3_column_text(env->env_qsearch, 1);
417 014c66b6 2023-06-25 op fullpkgpath = sqlite3_column_text(env->env_qsearch, 2);
419 dd58c961 2023-08-08 op if (clt_printf(clt, "=> %s/%s %s: %s\n", clt->clt_script_name,
420 dd58c961 2023-08-08 op fullpkgpath, stem, comment) == -1)
424 014c66b6 2023-06-25 op sqlite3_reset(env->env_qsearch);
426 014c66b6 2023-06-25 op if (!found && clt_printf(clt, "No ports found\n") == -1)
429 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
432 014c66b6 2023-06-25 op sqlite3_reset(env->env_qsearch);
437 014c66b6 2023-06-25 op route_categories(struct env *env, struct client *clt)
439 014c66b6 2023-06-25 op const char *fullpkgpath;
442 014c66b6 2023-06-25 op if (server_reply(clt, 20, "text/gemini") == -1)
444 014c66b6 2023-06-25 op if (clt_printf(clt, "# list of all categories\n") == -1)
447 014c66b6 2023-06-25 op if (clt_puts(clt, "\n") == -1)
451 014c66b6 2023-06-25 op err = sqlite3_step(env->env_qcats);
452 014c66b6 2023-06-25 op if (err == SQLITE_DONE)
454 014c66b6 2023-06-25 op if (err != SQLITE_ROW) {
455 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_step %s", __func__,
456 014c66b6 2023-06-25 op sqlite3_errstr(err));
460 014c66b6 2023-06-25 op fullpkgpath = sqlite3_column_text(env->env_qcats, 0);
462 dd58c961 2023-08-08 op if (clt_printf(clt, "=> %s/%s %s\n", clt->clt_script_name,
463 dd58c961 2023-08-08 op fullpkgpath, fullpkgpath) == -1) {
464 014c66b6 2023-06-25 op sqlite3_reset(env->env_qcats);
469 014c66b6 2023-06-25 op sqlite3_reset(env->env_qcats);
470 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
474 014c66b6 2023-06-25 op route_listing(struct env *env, struct client *clt)
476 014c66b6 2023-06-25 op char buf[128], *s;
477 014c66b6 2023-06-25 op const char *path = clt->clt_path_info + 1;
478 014c66b6 2023-06-25 op const char *fullpkgpath;
481 014c66b6 2023-06-25 op strlcpy(buf, path, sizeof(buf));
482 014c66b6 2023-06-25 op while ((s = strrchr(buf, '/')) != NULL)
485 014c66b6 2023-06-25 op err = sqlite3_bind_text(env->env_qbycat, 1, buf, -1, NULL);
486 014c66b6 2023-06-25 op if (err != SQLITE_OK) {
487 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_bind_text \"%s\": %s", __func__,
488 014c66b6 2023-06-25 op path, sqlite3_errstr(err));
489 014c66b6 2023-06-25 op sqlite3_reset(env->env_qbycat);
491 014c66b6 2023-06-25 op if (server_reply(clt, 42, "internal error") == -1)
493 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 1));
496 014c66b6 2023-06-25 op if (server_reply(clt, 20, "text/gemini") == -1)
499 014c66b6 2023-06-25 op if (clt_printf(clt, "# port(s) under %s\n\n", path) == -1)
503 014c66b6 2023-06-25 op err = sqlite3_step(env->env_qbycat);
504 014c66b6 2023-06-25 op if (err == SQLITE_DONE)
506 014c66b6 2023-06-25 op if (err != SQLITE_ROW) {
507 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_step %s", __func__,
508 014c66b6 2023-06-25 op sqlite3_errstr(err));
512 014c66b6 2023-06-25 op fullpkgpath = sqlite3_column_text(env->env_qbycat, 0);
514 dd58c961 2023-08-08 op if (clt_printf(clt, "=> %s/%s %s\n", clt->clt_script_name,
515 dd58c961 2023-08-08 op fullpkgpath, fullpkgpath) == -1) {
516 014c66b6 2023-06-25 op sqlite3_reset(env->env_qbycat);
521 014c66b6 2023-06-25 op sqlite3_reset(env->env_qbycat);
522 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
525 014c66b6 2023-06-25 op sqlite3_reset(env->env_qbycat);
530 5219f09d 2023-06-25 op print_maintainer(struct client *clt, const char *mail)
532 5219f09d 2023-06-25 op int r, in_addr;
534 5219f09d 2023-06-25 op for (in_addr = 0; *mail != '\0'; ++mail) {
535 5219f09d 2023-06-25 op if (!in_addr) {
536 5219f09d 2023-06-25 op if (clt_putc(clt, *mail) == -1)
538 5219f09d 2023-06-25 op if (*mail == '<')
543 5219f09d 2023-06-25 op switch (*mail) {
545 5219f09d 2023-06-25 op r = clt_puts(clt, " at ");
548 5219f09d 2023-06-25 op r = clt_puts(clt, " dot ");
552 5219f09d 2023-06-25 op /* fallthrough */
554 5219f09d 2023-06-25 op r = clt_putc(clt, *mail);
565 014c66b6 2023-06-25 op route_port(struct env *env, struct client *clt)
567 014c66b6 2023-06-25 op const char *path = clt->clt_path_info + 1;
568 014c66b6 2023-06-25 op const char *fullpkgpath, *stem, *pkgname, *descr;
569 014c66b6 2023-06-25 op const char *comment, *maintainer, *readme, *www;
570 014c66b6 2023-06-25 op const char *version;
573 014c66b6 2023-06-25 op err = sqlite3_bind_text(env->env_qfullpkgpath, 1, path, -1, NULL);
574 014c66b6 2023-06-25 op if (err != SQLITE_OK) {
575 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_bind_text \"%s\": %s", __func__,
576 014c66b6 2023-06-25 op path, sqlite3_errstr(err));
577 014c66b6 2023-06-25 op sqlite3_reset(env->env_qfullpkgpath);
579 014c66b6 2023-06-25 op if (server_reply(clt, 42, "internal error") == -1)
581 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 1));
584 014c66b6 2023-06-25 op err = sqlite3_step(env->env_qfullpkgpath);
585 014c66b6 2023-06-25 op if (err == SQLITE_DONE) {
586 014c66b6 2023-06-25 op /* No rows, retry as a category */
587 014c66b6 2023-06-25 op sqlite3_reset(env->env_qfullpkgpath);
588 014c66b6 2023-06-25 op return (route_listing(env, clt));
591 014c66b6 2023-06-25 op if (err != SQLITE_ROW) {
592 014c66b6 2023-06-25 op log_warnx("%s: sqlite3_step %s", __func__,
593 014c66b6 2023-06-25 op sqlite3_errstr(err));
594 014c66b6 2023-06-25 op if (server_reply(clt, 42, "internal error") == -1)
599 014c66b6 2023-06-25 op fullpkgpath = sqlite3_column_text(env->env_qfullpkgpath, 0);
600 014c66b6 2023-06-25 op stem = sqlite3_column_text(env->env_qfullpkgpath, 1);
601 014c66b6 2023-06-25 op comment = sqlite3_column_text(env->env_qfullpkgpath, 2);
602 014c66b6 2023-06-25 op pkgname = sqlite3_column_text(env->env_qfullpkgpath, 3);
603 014c66b6 2023-06-25 op descr = sqlite3_column_text(env->env_qfullpkgpath, 4);
604 014c66b6 2023-06-25 op maintainer = sqlite3_column_text(env->env_qfullpkgpath, 5);
605 014c66b6 2023-06-25 op readme = sqlite3_column_text(env->env_qfullpkgpath, 6);
606 014c66b6 2023-06-25 op www = sqlite3_column_text(env->env_qfullpkgpath, 7);
608 014c66b6 2023-06-25 op if ((version = strrchr(pkgname, '-')) != NULL)
611 014c66b6 2023-06-25 op version = " unknown";
613 014c66b6 2023-06-25 op if (server_reply(clt, 20, "text/gemini") == -1)
616 014c66b6 2023-06-25 op if (clt_printf(clt, "# %s v%s\n", path, version) == -1 ||
617 014c66b6 2023-06-25 op clt_puts(clt, "\n") == -1 ||
618 014c66b6 2023-06-25 op clt_printf(clt, "``` Command to install the package %s\n",
619 014c66b6 2023-06-25 op stem) == -1 ||
620 014c66b6 2023-06-25 op clt_printf(clt, "# pkg_add %s\n", stem) == -1 ||
621 014c66b6 2023-06-25 op clt_printf(clt, "```\n") == -1 ||
622 014c66b6 2023-06-25 op clt_printf(clt, "\n") == -1 ||
623 014c66b6 2023-06-25 op clt_printf(clt, "> %s\n", comment) == -1 ||
624 014c66b6 2023-06-25 op clt_printf(clt, "\n") == -1 ||
625 014c66b6 2023-06-25 op clt_printf(clt, "=> https://cvsweb.openbsd.org/ports/%s "
626 014c66b6 2023-06-25 op "CVS Web\n", fullpkgpath) == -1)
629 014c66b6 2023-06-25 op if (www && *www != '\0' &&
630 014c66b6 2023-06-25 op clt_printf(clt, "=> %s Port Homepage (WWW)\n", www) == -1)
633 014c66b6 2023-06-25 op if (clt_printf(clt, "\n") == -1 ||
634 5219f09d 2023-06-25 op clt_printf(clt, "Maintainer: ") == -1 ||
635 5219f09d 2023-06-25 op print_maintainer(clt, maintainer) == -1 ||
636 5219f09d 2023-06-25 op clt_puts(clt, "\n\n") == -1 ||
637 c436ee2d 2023-06-25 op clt_printf(clt, "## Description\n\n") == -1 ||
638 014c66b6 2023-06-25 op clt_printf(clt, "``` %s description\n", stem) == -1 ||
639 014c66b6 2023-06-25 op clt_puts(clt, descr) == -1 ||
640 014c66b6 2023-06-25 op clt_puts(clt, "```\n") == -1 ||
641 014c66b6 2023-06-25 op clt_puts(clt, "\n") == -1)
644 014c66b6 2023-06-25 op if (readme && *readme != '\0') {
645 c436ee2d 2023-06-25 op if (clt_puts(clt, "## Readme\n\n") == -1 ||
646 014c66b6 2023-06-25 op clt_puts(clt, "\n") == -1 ||
647 014c66b6 2023-06-25 op clt_printf(clt, "``` README for %s\n", stem) == -1 ||
648 014c66b6 2023-06-25 op clt_puts(clt, readme) == -1 ||
649 014c66b6 2023-06-25 op clt_puts(clt, "\n") == -1)
654 014c66b6 2023-06-25 op sqlite3_reset(env->env_qfullpkgpath);
655 014c66b6 2023-06-25 op return (fcgi_end_request(clt, 0));
658 014c66b6 2023-06-25 op sqlite3_reset(env->env_qfullpkgpath);