1 /*
2 * Help index test program for CUPS.
3 *
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2017 by Apple Inc.
6 * Copyright © 1997-2007 by Easy Software Products.
7 *
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
9 * information.
10 */
11
12 /*
13 * Include necessary headers...
14 */
15
16 #include "cgi.h"
17
18
19 /*
20 * Local functions...
21 */
22
23 static void list_nodes(const char *title, cups_array_t *nodes);
24 static int usage(void);
25
26
27 /*
28 * 'main()' - Test the help index code.
29 */
30
31 int /* O - Exit status */
main(int argc,char * argv[])32 main(int argc, /* I - Number of command-line arguments */
33 char *argv[]) /* I - Command-line arguments */
34 {
35 int i; /* Looping var */
36 help_index_t *hi, /* Help index */
37 *search; /* Search index */
38 const char *opt, /* Current option character */
39 *dir = ".", /* Directory to index */
40 *q = NULL, /* Query string */
41 *section = NULL, /* Section string */
42 *filename = NULL; /* Filename string */
43
44
45 /*
46 * Parse the command-line...
47 */
48
49 for (i = 1; i < argc; i ++)
50 {
51 if (argv[i][0] == '-')
52 {
53 if (!strcmp(argv[i], "--help"))
54 {
55 usage();
56 return (0);
57 }
58
59 for (opt = argv[i] + 1; *opt; opt ++)
60 {
61 switch (*opt)
62 {
63 case 'd' : /* -d directory */
64 i ++;
65 if (i < argc)
66 {
67 dir = argv[i];
68 }
69 else
70 {
71 fputs("testhi: Missing directory for \"-d\" option.\n", stderr);
72 return (usage());
73 }
74 break;
75
76 case 's' : /* -s section */
77 i ++;
78 if (i < argc)
79 {
80 section = argv[i];
81 }
82 else
83 {
84 fputs("testhi: Missing section name for \"-s\" option.\n", stderr);
85 return (usage());
86 }
87 break;
88
89 default :
90 fprintf(stderr, "testhi: Unknown option \"-%c\".\n", *opt);
91 return (usage());
92 }
93 }
94 }
95 else if (!q)
96 q = argv[i];
97 else if (!filename)
98 filename = argv[i];
99 else
100 {
101 fprintf(stderr, "testhi: Unknown argument \"%s\".\n", argv[i]);
102 return (usage());
103 }
104 }
105
106 /*
107 * Load the help index...
108 */
109
110 hi = helpLoadIndex("testhi.index", dir);
111
112 list_nodes("nodes", hi->nodes);
113 list_nodes("sorted", hi->sorted);
114
115 /*
116 * Do any searches...
117 */
118
119 if (q)
120 {
121 search = helpSearchIndex(hi, q, section, filename);
122
123 if (search)
124 {
125 list_nodes(argv[1], search->sorted);
126 helpDeleteIndex(search);
127 }
128 else
129 printf("%s (0 nodes)\n", q);
130 }
131
132 helpDeleteIndex(hi);
133
134 /*
135 * Return with no errors...
136 */
137
138 return (0);
139 }
140
141
142 /*
143 * 'list_nodes()' - List nodes in an array...
144 */
145
146 static void
list_nodes(const char * title,cups_array_t * nodes)147 list_nodes(const char *title, /* I - Title string */
148 cups_array_t *nodes) /* I - Nodes */
149 {
150 int i; /* Looping var */
151 help_node_t *node; /* Current node */
152
153
154 printf("%s (%d nodes):\n", title, cupsArrayCount(nodes));
155 for (i = 1, node = (help_node_t *)cupsArrayFirst(nodes);
156 node;
157 i ++, node = (help_node_t *)cupsArrayNext(nodes))
158 {
159 if (node->anchor)
160 printf(" %d: %s#%s \"%s\"", i, node->filename, node->anchor,
161 node->text);
162 else
163 printf(" %d: %s \"%s\"", i, node->filename, node->text);
164
165 printf(" (%d words)\n", cupsArrayCount(node->words));
166 }
167 }
168
169
170 /*
171 * 'usage()' - Show program usage.
172 */
173
174 static int /* O - Exit status */
usage(void)175 usage(void)
176 {
177 puts("Usage: ./testhi [options] [\"query\"] [filename]");
178 puts("Options:");
179 puts("-d directory Specify index directory.");
180 puts("-s section Specify search section.");
181
182 return (1);
183 }
184