]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / help.c
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 */
46 help_node_t *n; /* Current help node */
47 int i; /* Looping var */
48 const char *query; /* Search query */
49 const char *cache_dir; /* CUPS_CACHEDIR environment variable */
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
77 if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
78 cache_dir = CUPS_CACHEDIR;
79
80 snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
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
103 fprintf(stderr, "DEBUG: %d nodes in help index...\n",
104 cupsArrayCount(hi->nodes));
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)
114 {
115 helpfile ++;
116
117 if (!*helpfile)
118 helpfile = NULL;
119 }
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);
159 cgiSetVariable("HELPTITLE", n->text);
160
161 /*
162 * Send a standard page header...
163 */
164
165 cgiStartHTML(n->text);
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
184 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n", query, topic);
185
186 if (si)
187 {
188 help_node_t *nn; /* Parent node */
189
190
191 fprintf(stderr,
192 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
193 si->sorted, cupsArrayCount(si->sorted));
194
195 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
196 n;
197 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
198 {
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);
204 else
205 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
206 query ? query : "");
207
208 cgiSetArray("QTEXT", i, n->text);
209 cgiSetArray("QLINK", i, line);
210
211 if (!helpfile && n->anchor)
212 {
213 nn = helpFindNode(hi, n->filename, NULL);
214
215 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
216 query ? query : "");
217
218 cgiSetArray("QPTEXT", i, nn->text);
219 cgiSetArray("QPLINK", i, line);
220 }
221 else
222 {
223 cgiSetArray("QPTEXT", i, "");
224 cgiSetArray("QPLINK", i, "");
225 }
226
227 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
228 }
229
230 helpDeleteIndex(si);
231 }
232
233 /*
234 * OK, now list the bookmarks within the index...
235 */
236
237 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
238 n;
239 n = (help_node_t *)cupsArrayNext(hi->sorted))
240 {
241 if (n->anchor)
242 continue;
243
244 /*
245 * Add a section link as needed...
246 */
247
248 if (n->section &&
249 (!section || strcmp(n->section, section)))
250 {
251 /*
252 * Add a link for this node...
253 */
254
255 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
256 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
257 query ? query : "");
258 cgiSetArray("BMLINK", i, line);
259 cgiSetArray("BMTEXT", i, n->section);
260 cgiSetArray("BMINDENT", i, "0");
261
262 i ++;
263 section = n->section;
264 }
265
266 if (!topic || strcmp(n->section, topic))
267 continue;
268
269 /*
270 * Add a link for this node...
271 */
272
273 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
274 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
275 query ? query : "");
276 cgiSetArray("BMLINK", i, line);
277 cgiSetArray("BMTEXT", i, n->text);
278 cgiSetArray("BMINDENT", i, "1");
279
280 i ++;
281
282 if (helpfile && !strcmp(helpfile, n->filename))
283 {
284 help_node_t *nn; /* Pointer to sub-node */
285
286
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))
293 {
294 /*
295 * Add a link for this node...
296 */
297
298 snprintf(line, sizeof(line), "#%s", nn->anchor);
299 cgiSetArray("BMLINK", i, line);
300 cgiSetArray("BMTEXT", i, nn->text);
301 cgiSetArray("BMINDENT", i, "2");
302
303 i ++;
304 }
305
306 cupsArrayRestore(hi->sorted);
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 */