#include "cgi.h"
static void list_nodes(const char *title, cups_array_t *nodes);
static int usage(void);
int
main(int argc,
char *argv[])
{
int i;
help_index_t *hi,
*search;
const char *opt,
*dir = ".",
*q = NULL,
*section = NULL,
*filename = NULL;
for (i = 1; i < argc; i ++)
{
if (argv[i][0] == '-')
{
if (!strcmp(argv[i], "--help"))
{
usage();
return (0);
}
for (opt = argv[i] + 1; *opt; opt ++)
{
switch (*opt)
{
case 'd' :
i ++;
if (i < argc)
{
dir = argv[i];
}
else
{
fputs("testhi: Missing directory for \"-d\" option.\n", stderr);
return (usage());
}
break;
case 's' :
i ++;
if (i < argc)
{
section = argv[i];
}
else
{
fputs("testhi: Missing section name for \"-s\" option.\n", stderr);
return (usage());
}
break;
default :
fprintf(stderr, "testhi: Unknown option \"-%c\".\n", *opt);
return (usage());
}
}
}
else if (!q)
q = argv[i];
else if (!filename)
filename = argv[i];
else
{
fprintf(stderr, "testhi: Unknown argument \"%s\".\n", argv[i]);
return (usage());
}
}
hi = helpLoadIndex("testhi.index", dir);
list_nodes("nodes", hi->nodes);
list_nodes("sorted", hi->sorted);
if (q)
{
search = helpSearchIndex(hi, q, section, filename);
if (search)
{
list_nodes(argv[1], search->sorted);
helpDeleteIndex(search);
}
else
printf("%s (0 nodes)\n", q);
}
helpDeleteIndex(hi);
return (0);
}
static void
list_nodes(const char *title,
cups_array_t *nodes)
{
int i;
help_node_t *node;
printf("%s (%d nodes):\n", title, cupsArrayCount(nodes));
for (i = 1, node = (help_node_t *)cupsArrayFirst(nodes);
node;
i ++, node = (help_node_t *)cupsArrayNext(nodes))
{
if (node->anchor)
printf(" %d: %s#%s \"%s\"", i, node->filename, node->anchor,
node->text);
else
printf(" %d: %s \"%s\"", i, node->filename, node->text);
printf(" (%d words)\n", cupsArrayCount(node->words));
}
}
static int
usage(void)
{
puts("Usage: ./testhi [options] [\"query\"] [filename]");
puts("Options:");
puts("-d directory Specify index directory.");
puts("-s section Specify search section.");
return (1);
}