2020-04-24 08:49:14 +00:00
|
|
|
/* help.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Timo Teräs <timo.teras@iki.fi>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2020-04-24 08:49:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
#include "apk_applet.h"
|
|
|
|
#include "apk_print.h"
|
2021-12-14 18:43:31 +00:00
|
|
|
#include "help.h"
|
2020-04-24 08:49:14 +00:00
|
|
|
|
2021-12-10 20:33:52 +00:00
|
|
|
static LIST_HEAD(apk_applet_list);
|
2020-10-05 15:52:51 +00:00
|
|
|
|
|
|
|
#define apk_applet_foreach(iter) list_for_each_entry(iter, &apk_applet_list, node)
|
|
|
|
|
|
|
|
void apk_applet_register(struct apk_applet *applet)
|
|
|
|
{
|
|
|
|
list_init(&applet->node);
|
|
|
|
list_add_tail(&applet->node, &apk_applet_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct apk_applet *apk_applet_find(const char *name)
|
|
|
|
{
|
|
|
|
struct apk_applet *a;
|
|
|
|
|
|
|
|
apk_applet_foreach(a) {
|
|
|
|
if (strcmp(name, a->name) == 0)
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:43:31 +00:00
|
|
|
#ifndef NO_HELP
|
2020-05-19 07:50:57 +00:00
|
|
|
static inline int is_group(struct apk_applet *applet, const char *topic)
|
2020-04-24 08:49:14 +00:00
|
|
|
{
|
|
|
|
if (!applet) return strcasecmp(topic, "apk") == 0;
|
|
|
|
if (strcasecmp(topic, applet->name) == 0) return 1;
|
|
|
|
for (int i = 0; applet->optgroups[i] && i < ARRAY_SIZE(applet->optgroups); i++)
|
2020-05-04 18:45:11 +00:00
|
|
|
if (strcasecmp(applet->optgroups[i]->desc, topic) == 0) return 1;
|
2020-04-24 08:49:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2021-12-14 18:43:31 +00:00
|
|
|
#endif
|
2020-04-24 08:49:14 +00:00
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
void apk_applet_help(struct apk_applet *applet, struct apk_out *out)
|
2020-04-24 08:49:14 +00:00
|
|
|
{
|
2020-05-19 07:50:57 +00:00
|
|
|
#ifndef NO_HELP
|
2020-04-24 08:49:14 +00:00
|
|
|
char buf[uncompressed_help_size], *ptr, *msg;
|
|
|
|
unsigned long len = sizeof buf;
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
uncompress((unsigned char*) buf, &len, compressed_help, sizeof compressed_help);
|
|
|
|
for (ptr = buf; *ptr && ptr < &buf[len]; ptr = msg + strlen(msg) + 1) {
|
|
|
|
msg = ptr + strlen(ptr) + 1;
|
|
|
|
if (is_group(applet, ptr)) {
|
|
|
|
fputc('\n', stdout);
|
|
|
|
fwrite(msg, strlen(msg), 1, stdout);
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 15:52:51 +00:00
|
|
|
if (num == 0) apk_err(out, "Help not found");
|
2020-05-19 07:50:57 +00:00
|
|
|
#else
|
|
|
|
fputc('\n', stdout);
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "This apk-tools has been built without help");
|
2020-05-19 07:50:57 +00:00
|
|
|
#endif
|
2020-04-24 08:49:14 +00:00
|
|
|
}
|