]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id$" | |
3 | * | |
4 | * On-line help CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Main entry for CGI. | |
27 | */ | |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include "cgi-private.h" | |
34 | ||
35 | ||
36 | /* | |
37 | * 'main()' - Main entry for CGI. | |
38 | */ | |
39 | ||
40 | int /* O - Exit status */ | |
41 | main(int argc, /* I - Number of command-line arguments */ | |
42 | char *argv[]) /* I - Command-line arguments */ | |
43 | { | |
44 | help_index_t *hi, /* Help index */ | |
45 | *si; /* Search index */ | |
ecdc0628 | 46 | help_node_t *n; /* Current help node */ |
47 | int i; /* Looping var */ | |
ef416fc2 | 48 | const char *query; /* Search query */ |
ecdc0628 | 49 | const char *cache_dir; /* CUPS_CACHEDIR environment variable */ |
ef416fc2 | 50 | const char *docroot; /* CUPS_DOCROOT environment variable */ |
51 | const char *helpfile; /* Current help file */ | |
52 | const char *topic; /* Current topic */ | |
53 | char topic_data[1024]; /* Topic form data */ | |
54 | const char *section; /* Current section */ | |
55 | char filename[1024], /* Filename */ | |
56 | directory[1024]; /* Directory */ | |
57 | cups_file_t *fp; /* Help file */ | |
58 | char line[1024]; /* Line from file */ | |
59 | ||
60 | ||
61 | /* | |
62 | * Get any form variables... | |
63 | */ | |
64 | ||
65 | cgiInitialize(); | |
66 | ||
67 | /* | |
68 | * Set the web interface section... | |
69 | */ | |
70 | ||
71 | cgiSetVariable("SECTION", "help"); | |
72 | ||
73 | /* | |
74 | * Load the help index... | |
75 | */ | |
76 | ||
ecdc0628 | 77 | if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL) |
78 | cache_dir = CUPS_CACHEDIR; | |
ef416fc2 | 79 | |
ecdc0628 | 80 | snprintf(filename, sizeof(filename), "%s/help.index", cache_dir); |
ef416fc2 | 81 | |
82 | if ((docroot = getenv("CUPS_DOCROOT")) == NULL) | |
83 | docroot = CUPS_DOCROOT; | |
84 | ||
85 | snprintf(directory, sizeof(directory), "%s/help", docroot); | |
86 | ||
87 | fprintf(stderr, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n", | |
88 | filename, directory); | |
89 | ||
90 | hi = helpLoadIndex(filename, directory); | |
91 | if (!hi) | |
92 | { | |
93 | perror(filename); | |
94 | ||
95 | cgiStartHTML("Help"); | |
96 | cgiSetVariable("ERROR", "Unable to load help index!"); | |
97 | cgiCopyTemplateLang("error.tmpl"); | |
98 | cgiEndHTML(); | |
99 | ||
100 | return (1); | |
101 | } | |
102 | ||
ecdc0628 | 103 | fprintf(stderr, "DEBUG: %d nodes in help index...\n", |
104 | cupsArrayCount(hi->nodes)); | |
ef416fc2 | 105 | |
106 | /* | |
107 | * See if we are viewing a file... | |
108 | */ | |
109 | ||
110 | for (i = 0; i < argc; i ++) | |
111 | fprintf(stderr, "argv[%d]=\"%s\"\n", i, argv[i]); | |
112 | ||
113 | if ((helpfile = getenv("PATH_INFO")) != NULL) | |
b423cd4c | 114 | { |
ef416fc2 | 115 | helpfile ++; |
b423cd4c | 116 | |
117 | if (!*helpfile) | |
118 | helpfile = NULL; | |
119 | } | |
ef416fc2 | 120 | |
121 | if (helpfile) | |
122 | { | |
123 | /* | |
124 | * Verify that the help file exists and is part of the index... | |
125 | */ | |
126 | ||
127 | snprintf(filename, sizeof(filename), "%s/help/%s", docroot, helpfile); | |
128 | ||
129 | fprintf(stderr, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n", | |
130 | helpfile, filename); | |
131 | ||
132 | if (access(filename, R_OK)) | |
133 | { | |
134 | perror(filename); | |
135 | ||
136 | cgiStartHTML("Help"); | |
137 | cgiSetVariable("ERROR", "Unable to access help file!"); | |
138 | cgiCopyTemplateLang("error.tmpl"); | |
139 | cgiEndHTML(); | |
140 | ||
141 | return (1); | |
142 | } | |
143 | ||
144 | if ((n = helpFindNode(hi, helpfile, NULL)) == NULL) | |
145 | { | |
146 | cgiStartHTML("Help"); | |
147 | cgiSetVariable("ERROR", "Help file not in index!"); | |
148 | cgiCopyTemplateLang("error.tmpl"); | |
149 | cgiEndHTML(); | |
150 | ||
151 | return (1); | |
152 | } | |
153 | ||
154 | /* | |
155 | * Set the page title and save the help file... | |
156 | */ | |
157 | ||
158 | cgiSetVariable("HELPFILE", helpfile); | |
ecdc0628 | 159 | cgiSetVariable("HELPTITLE", n->text); |
ef416fc2 | 160 | |
161 | /* | |
162 | * Send a standard page header... | |
163 | */ | |
164 | ||
ecdc0628 | 165 | cgiStartHTML(n->text); |
ef416fc2 | 166 | } |
167 | else | |
168 | { | |
169 | /* | |
170 | * Send a standard page header... | |
171 | */ | |
172 | ||
173 | cgiStartHTML("Help"); | |
174 | } | |
175 | ||
176 | /* | |
177 | * Do a search as needed... | |
178 | */ | |
179 | ||
180 | query = cgiGetVariable("QUERY"); | |
181 | topic = cgiGetVariable("TOPIC"); | |
182 | si = helpSearchIndex(hi, query, topic, helpfile); | |
183 | ||
b423cd4c | 184 | fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n", query, topic); |
185 | ||
ef416fc2 | 186 | if (si) |
187 | { | |
ecdc0628 | 188 | help_node_t *nn; /* Parent node */ |
ef416fc2 | 189 | |
190 | ||
ecdc0628 | 191 | fprintf(stderr, |
192 | "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si, | |
193 | si->sorted, cupsArrayCount(si->sorted)); | |
ef416fc2 | 194 | |
b423cd4c | 195 | for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted); |
ecdc0628 | 196 | n; |
b423cd4c | 197 | i ++, n = (help_node_t *)cupsArrayNext(si->sorted)) |
ef416fc2 | 198 | { |
ecdc0628 | 199 | if (helpfile && n->anchor) |
200 | snprintf(line, sizeof(line), "#%s", n->anchor); | |
201 | else if (n->anchor) | |
202 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename, | |
203 | query ? query : "", n->anchor); | |
ef416fc2 | 204 | else |
ecdc0628 | 205 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename, |
ef416fc2 | 206 | query ? query : ""); |
207 | ||
ecdc0628 | 208 | cgiSetArray("QTEXT", i, n->text); |
ef416fc2 | 209 | cgiSetArray("QLINK", i, line); |
210 | ||
ecdc0628 | 211 | if (!helpfile && n->anchor) |
ef416fc2 | 212 | { |
ecdc0628 | 213 | nn = helpFindNode(hi, n->filename, NULL); |
ef416fc2 | 214 | |
ecdc0628 | 215 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename, |
ef416fc2 | 216 | query ? query : ""); |
217 | ||
ecdc0628 | 218 | cgiSetArray("QPTEXT", i, nn->text); |
ef416fc2 | 219 | cgiSetArray("QPLINK", i, line); |
220 | } | |
221 | else | |
222 | { | |
223 | cgiSetArray("QPTEXT", i, ""); | |
224 | cgiSetArray("QPLINK", i, ""); | |
225 | } | |
226 | ||
ecdc0628 | 227 | fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line); |
ef416fc2 | 228 | } |
229 | ||
230 | helpDeleteIndex(si); | |
231 | } | |
232 | ||
233 | /* | |
234 | * OK, now list the bookmarks within the index... | |
235 | */ | |
236 | ||
ecdc0628 | 237 | for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted); |
238 | n; | |
239 | n = (help_node_t *)cupsArrayNext(hi->sorted)) | |
ef416fc2 | 240 | { |
ecdc0628 | 241 | if (n->anchor) |
ef416fc2 | 242 | continue; |
243 | ||
244 | /* | |
245 | * Add a section link as needed... | |
246 | */ | |
247 | ||
ecdc0628 | 248 | if (n->section && |
249 | (!section || strcmp(n->section, section))) | |
ef416fc2 | 250 | { |
251 | /* | |
252 | * Add a link for this node... | |
253 | */ | |
254 | ||
255 | snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s", | |
ecdc0628 | 256 | cgiFormEncode(topic_data, n->section, sizeof(topic_data)), |
ef416fc2 | 257 | query ? query : ""); |
ecdc0628 | 258 | cgiSetArray("BMLINK", i, line); |
259 | cgiSetArray("BMTEXT", i, n->section); | |
260 | cgiSetArray("BMINDENT", i, "0"); | |
ef416fc2 | 261 | |
ecdc0628 | 262 | i ++; |
263 | section = n->section; | |
ef416fc2 | 264 | } |
265 | ||
ecdc0628 | 266 | if (!topic || strcmp(n->section, topic)) |
ef416fc2 | 267 | continue; |
268 | ||
269 | /* | |
270 | * Add a link for this node... | |
271 | */ | |
272 | ||
ecdc0628 | 273 | snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename, |
274 | cgiFormEncode(topic_data, n->section, sizeof(topic_data)), | |
ef416fc2 | 275 | query ? query : ""); |
ecdc0628 | 276 | cgiSetArray("BMLINK", i, line); |
277 | cgiSetArray("BMTEXT", i, n->text); | |
278 | cgiSetArray("BMINDENT", i, "1"); | |
ef416fc2 | 279 | |
ecdc0628 | 280 | i ++; |
ef416fc2 | 281 | |
ecdc0628 | 282 | if (helpfile && !strcmp(helpfile, n->filename)) |
ef416fc2 | 283 | { |
ecdc0628 | 284 | help_node_t *nn; /* Pointer to sub-node */ |
ef416fc2 | 285 | |
286 | ||
ecdc0628 | 287 | cupsArraySave(hi->sorted); |
288 | ||
289 | for (nn = (help_node_t *)cupsArrayFirst(hi->sorted); | |
290 | nn; | |
291 | nn = (help_node_t *)cupsArrayNext(hi->sorted)) | |
292 | if (nn->anchor && !strcmp(helpfile, nn->filename)) | |
ef416fc2 | 293 | { |
294 | /* | |
295 | * Add a link for this node... | |
296 | */ | |
297 | ||
ecdc0628 | 298 | snprintf(line, sizeof(line), "#%s", nn->anchor); |
299 | cgiSetArray("BMLINK", i, line); | |
300 | cgiSetArray("BMTEXT", i, nn->text); | |
301 | cgiSetArray("BMINDENT", i, "2"); | |
ef416fc2 | 302 | |
ecdc0628 | 303 | i ++; |
ef416fc2 | 304 | } |
ecdc0628 | 305 | |
306 | cupsArrayRestore(hi->sorted); | |
ef416fc2 | 307 | } |
308 | } | |
309 | ||
310 | /* | |
311 | * Show the search and bookmark content... | |
312 | */ | |
313 | ||
314 | cgiCopyTemplateLang("help-header.tmpl"); | |
315 | ||
316 | /* | |
317 | * If we are viewing a file, copy it in now... | |
318 | */ | |
319 | ||
320 | if (helpfile) | |
321 | { | |
322 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
323 | { | |
324 | int inbody; /* Are we inside the body? */ | |
325 | ||
326 | ||
327 | inbody = 0; | |
328 | ||
329 | while (cupsFileGets(fp, line, sizeof(line))) | |
330 | { | |
331 | if (inbody) | |
332 | { | |
333 | if (!strncasecmp(line, "</BODY>", 7)) | |
334 | break; | |
335 | ||
336 | printf("%s\n", line); | |
337 | } | |
338 | else if (!strncasecmp(line, "<BODY", 5)) | |
339 | inbody = 1; | |
340 | } | |
341 | ||
342 | cupsFileClose(fp); | |
343 | } | |
344 | else | |
345 | { | |
346 | perror(filename); | |
347 | cgiSetVariable("ERROR", "Unable to open help file."); | |
348 | cgiCopyTemplateLang("error.tmpl"); | |
349 | } | |
350 | } | |
351 | ||
352 | /* | |
353 | * Send a standard trailer... | |
354 | */ | |
355 | ||
356 | cgiEndHTML(); | |
357 | ||
358 | /* | |
359 | * Delete the index... | |
360 | */ | |
361 | ||
362 | helpDeleteIndex(hi); | |
363 | ||
364 | /* | |
365 | * Return with no errors... | |
366 | */ | |
367 | ||
368 | return (0); | |
369 | } | |
370 | ||
371 | ||
372 | /* | |
373 | * End of "$Id$". | |
374 | */ |