]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/help.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / help.c
CommitLineData
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
40int /* O - Exit status */
41main(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 */
4744bd90 59 int printable; /* Show printable version? */
ef416fc2 60
61
62 /*
63 * Get any form variables...
64 */
65
66 cgiInitialize();
67
4744bd90 68 printable = cgiGetVariable("PRINTABLE") != NULL;
69
ef416fc2 70 /*
71 * Set the web interface section...
72 */
73
74 cgiSetVariable("SECTION", "help");
75
76 /*
77 * Load the help index...
78 */
79
ecdc0628 80 if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
81 cache_dir = CUPS_CACHEDIR;
ef416fc2 82
ecdc0628 83 snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
ef416fc2 84
85 if ((docroot = getenv("CUPS_DOCROOT")) == NULL)
86 docroot = CUPS_DOCROOT;
87
88 snprintf(directory, sizeof(directory), "%s/help", docroot);
89
90 fprintf(stderr, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
91 filename, directory);
92
93 hi = helpLoadIndex(filename, directory);
94 if (!hi)
95 {
96 perror(filename);
97
4744bd90 98 cgiStartHTML(cgiText(_("Help")));
ef416fc2 99 cgiSetVariable("ERROR", "Unable to load help index!");
100 cgiCopyTemplateLang("error.tmpl");
101 cgiEndHTML();
102
103 return (1);
104 }
105
ecdc0628 106 fprintf(stderr, "DEBUG: %d nodes in help index...\n",
107 cupsArrayCount(hi->nodes));
ef416fc2 108
109 /*
110 * See if we are viewing a file...
111 */
112
113 for (i = 0; i < argc; i ++)
114 fprintf(stderr, "argv[%d]=\"%s\"\n", i, argv[i]);
115
116 if ((helpfile = getenv("PATH_INFO")) != NULL)
b423cd4c 117 {
ef416fc2 118 helpfile ++;
b423cd4c 119
120 if (!*helpfile)
121 helpfile = NULL;
122 }
ef416fc2 123
124 if (helpfile)
125 {
126 /*
127 * Verify that the help file exists and is part of the index...
128 */
129
130 snprintf(filename, sizeof(filename), "%s/help/%s", docroot, helpfile);
131
132 fprintf(stderr, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
133 helpfile, filename);
134
135 if (access(filename, R_OK))
136 {
137 perror(filename);
138
4744bd90 139 cgiStartHTML(cgiText(_("Help")));
ef416fc2 140 cgiSetVariable("ERROR", "Unable to access help file!");
141 cgiCopyTemplateLang("error.tmpl");
142 cgiEndHTML();
143
144 return (1);
145 }
146
147 if ((n = helpFindNode(hi, helpfile, NULL)) == NULL)
148 {
4744bd90 149 cgiStartHTML(cgiText(_("Help")));
ef416fc2 150 cgiSetVariable("ERROR", "Help file not in index!");
151 cgiCopyTemplateLang("error.tmpl");
152 cgiEndHTML();
153
154 return (1);
155 }
156
157 /*
158 * Set the page title and save the help file...
159 */
160
161 cgiSetVariable("HELPFILE", helpfile);
ecdc0628 162 cgiSetVariable("HELPTITLE", n->text);
ef416fc2 163
164 /*
165 * Send a standard page header...
166 */
167
4744bd90 168 if (printable)
169 puts("Content-Type: text/html;charset=utf-8\n");
170 else
171 cgiStartHTML(n->text);
ef416fc2 172 }
173 else
174 {
175 /*
176 * Send a standard page header...
177 */
178
4744bd90 179 cgiStartHTML(cgiText(_("Help")));
ef416fc2 180 }
181
182 /*
183 * Do a search as needed...
184 */
185
186 query = cgiGetVariable("QUERY");
187 topic = cgiGetVariable("TOPIC");
188 si = helpSearchIndex(hi, query, topic, helpfile);
189
b423cd4c 190 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n", query, topic);
191
ef416fc2 192 if (si)
193 {
ecdc0628 194 help_node_t *nn; /* Parent node */
ef416fc2 195
196
ecdc0628 197 fprintf(stderr,
198 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
199 si->sorted, cupsArrayCount(si->sorted));
ef416fc2 200
b423cd4c 201 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
ecdc0628 202 n;
b423cd4c 203 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
ef416fc2 204 {
ecdc0628 205 if (helpfile && n->anchor)
206 snprintf(line, sizeof(line), "#%s", n->anchor);
207 else if (n->anchor)
208 snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename,
209 query ? query : "", n->anchor);
ef416fc2 210 else
ecdc0628 211 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
ef416fc2 212 query ? query : "");
213
ecdc0628 214 cgiSetArray("QTEXT", i, n->text);
ef416fc2 215 cgiSetArray("QLINK", i, line);
216
ecdc0628 217 if (!helpfile && n->anchor)
ef416fc2 218 {
ecdc0628 219 nn = helpFindNode(hi, n->filename, NULL);
ef416fc2 220
ecdc0628 221 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
ef416fc2 222 query ? query : "");
223
ecdc0628 224 cgiSetArray("QPTEXT", i, nn->text);
ef416fc2 225 cgiSetArray("QPLINK", i, line);
226 }
227 else
228 {
229 cgiSetArray("QPTEXT", i, "");
230 cgiSetArray("QPLINK", i, "");
231 }
232
ecdc0628 233 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
ef416fc2 234 }
235
236 helpDeleteIndex(si);
237 }
238
239 /*
240 * OK, now list the bookmarks within the index...
241 */
242
ecdc0628 243 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
244 n;
245 n = (help_node_t *)cupsArrayNext(hi->sorted))
ef416fc2 246 {
ecdc0628 247 if (n->anchor)
ef416fc2 248 continue;
249
250 /*
251 * Add a section link as needed...
252 */
253
ecdc0628 254 if (n->section &&
255 (!section || strcmp(n->section, section)))
ef416fc2 256 {
257 /*
258 * Add a link for this node...
259 */
260
261 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
ecdc0628 262 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
ef416fc2 263 query ? query : "");
ecdc0628 264 cgiSetArray("BMLINK", i, line);
265 cgiSetArray("BMTEXT", i, n->section);
266 cgiSetArray("BMINDENT", i, "0");
ef416fc2 267
ecdc0628 268 i ++;
269 section = n->section;
ef416fc2 270 }
271
ecdc0628 272 if (!topic || strcmp(n->section, topic))
ef416fc2 273 continue;
274
275 /*
276 * Add a link for this node...
277 */
278
ecdc0628 279 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
280 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
ef416fc2 281 query ? query : "");
ecdc0628 282 cgiSetArray("BMLINK", i, line);
283 cgiSetArray("BMTEXT", i, n->text);
284 cgiSetArray("BMINDENT", i, "1");
ef416fc2 285
ecdc0628 286 i ++;
ef416fc2 287
ecdc0628 288 if (helpfile && !strcmp(helpfile, n->filename))
ef416fc2 289 {
ecdc0628 290 help_node_t *nn; /* Pointer to sub-node */
ef416fc2 291
292
ecdc0628 293 cupsArraySave(hi->sorted);
294
295 for (nn = (help_node_t *)cupsArrayFirst(hi->sorted);
296 nn;
297 nn = (help_node_t *)cupsArrayNext(hi->sorted))
298 if (nn->anchor && !strcmp(helpfile, nn->filename))
ef416fc2 299 {
300 /*
301 * Add a link for this node...
302 */
303
ecdc0628 304 snprintf(line, sizeof(line), "#%s", nn->anchor);
305 cgiSetArray("BMLINK", i, line);
306 cgiSetArray("BMTEXT", i, nn->text);
307 cgiSetArray("BMINDENT", i, "2");
ef416fc2 308
ecdc0628 309 i ++;
ef416fc2 310 }
ecdc0628 311
312 cupsArrayRestore(hi->sorted);
ef416fc2 313 }
314 }
315
316 /*
317 * Show the search and bookmark content...
318 */
319
4744bd90 320 if (!helpfile || !printable)
321 cgiCopyTemplateLang("help-header.tmpl");
322 else
323 cgiCopyTemplateLang("help-printable.tmpl");
ef416fc2 324
325 /*
326 * If we are viewing a file, copy it in now...
327 */
328
329 if (helpfile)
330 {
331 if ((fp = cupsFileOpen(filename, "r")) != NULL)
332 {
333 int inbody; /* Are we inside the body? */
334
335
336 inbody = 0;
337
338 while (cupsFileGets(fp, line, sizeof(line)))
339 {
340 if (inbody)
341 {
342 if (!strncasecmp(line, "</BODY>", 7))
343 break;
344
345 printf("%s\n", line);
346 }
347 else if (!strncasecmp(line, "<BODY", 5))
348 inbody = 1;
349 }
350
351 cupsFileClose(fp);
352 }
353 else
354 {
355 perror(filename);
356 cgiSetVariable("ERROR", "Unable to open help file.");
357 cgiCopyTemplateLang("error.tmpl");
358 }
359 }
360
361 /*
362 * Send a standard trailer...
363 */
364
4744bd90 365 if (!printable)
366 cgiEndHTML();
367 else
368 puts("</BODY>\n</HTML>");
ef416fc2 369
370 /*
371 * Delete the index...
372 */
373
374 helpDeleteIndex(hi);
375
376 /*
377 * Return with no errors...
378 */
379
380 return (0);
381}
382
383
384/*
385 * End of "$Id$".
386 */