]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
4 * Online help CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Main entry for CGI.
21 * Include necessary headers...
24 #include "cgi-private.h"
28 * 'main()' - Main entry for CGI.
31 int /* O - Exit status */
32 main(int argc
, /* I - Number of command-line arguments */
33 char *argv
[]) /* I - Command-line arguments */
35 help_index_t
*hi
, /* Help index */
36 *si
; /* Search index */
37 help_node_t
*n
; /* Current help node */
38 int i
; /* Looping var */
39 const char *query
; /* Search query */
40 const char *cache_dir
; /* CUPS_CACHEDIR environment variable */
41 const char *docroot
; /* CUPS_DOCROOT environment variable */
42 const char *helpfile
; /* Current help file */
43 const char *topic
; /* Current topic */
44 char topic_data
[1024]; /* Topic form data */
45 const char *section
; /* Current section */
46 char filename
[1024], /* Filename */
47 directory
[1024]; /* Directory */
48 cups_file_t
*fp
; /* Help file */
49 char line
[1024]; /* Line from file */
50 int printable
; /* Show printable version? */
54 * Get any form variables...
59 printable
= cgiGetVariable("PRINTABLE") != NULL
;
62 * Set the web interface section...
65 cgiSetVariable("SECTION", "help");
66 cgiSetVariable("REFRESH_PAGE", "");
69 * Load the help index...
72 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
73 cache_dir
= CUPS_CACHEDIR
;
75 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
77 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
78 docroot
= CUPS_DOCROOT
;
80 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
82 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
85 hi
= helpLoadIndex(filename
, directory
);
90 cgiStartHTML(cgiText(_("Online Help")));
91 cgiSetVariable("ERROR", "Unable to load help index!");
92 cgiCopyTemplateLang("error.tmpl");
98 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
99 cupsArrayCount(hi
->nodes
));
102 * See if we are viewing a file...
105 for (i
= 0; i
< argc
; i
++)
106 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
108 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
119 * Verify that the help file exists and is part of the index...
122 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
124 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
127 if (access(filename
, R_OK
))
131 cgiStartHTML(cgiText(_("Online Help")));
132 cgiSetVariable("ERROR", "Unable to access help file!");
133 cgiCopyTemplateLang("error.tmpl");
139 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
141 cgiStartHTML(cgiText(_("Online Help")));
142 cgiSetVariable("ERROR", "Help file not in index!");
143 cgiCopyTemplateLang("error.tmpl");
150 * Set the page title and save the help file...
153 cgiSetVariable("HELPFILE", helpfile
);
154 cgiSetVariable("HELPTITLE", n
->text
);
155 cgiSetVariable("TOPIC", n
->section
);
158 * Send a standard page header...
162 puts("Content-Type: text/html;charset=utf-8\n");
164 cgiStartHTML(n
->text
);
169 * Send a standard page header...
172 cgiStartHTML(cgiText(_("Online Help")));
176 * Do a search as needed...
179 if (cgiGetVariable("CLEAR"))
180 cgiSetVariable("QUERY", "");
182 query
= cgiGetVariable("QUERY");
183 topic
= cgiGetVariable("TOPIC");
184 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
188 cgiSetVariable("QUERY", query
);
190 cgiSetVariable("TOPIC", topic
);
192 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
193 query
? query
: "(null)", topic
? topic
: "(null)");
197 help_node_t
*nn
; /* Parent node */
201 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
202 si
->sorted
, cupsArrayCount(si
->sorted
));
204 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
206 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
208 if (helpfile
&& n
->anchor
)
209 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
211 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
212 query
? query
: "", n
->anchor
);
214 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
217 cgiSetArray("QTEXT", i
, n
->text
);
218 cgiSetArray("QLINK", i
, line
);
220 if (!helpfile
&& n
->anchor
)
222 nn
= helpFindNode(hi
, n
->filename
, NULL
);
224 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
227 cgiSetArray("QPTEXT", i
, nn
->text
);
228 cgiSetArray("QPLINK", i
, line
);
232 cgiSetArray("QPTEXT", i
, "");
233 cgiSetArray("QPLINK", i
, "");
236 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
243 * OK, now list the bookmarks within the index...
246 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
248 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
254 * Add a section link as needed...
258 (!section
|| strcmp(n
->section
, section
)))
261 * Add a link for this node...
264 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
265 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
267 cgiSetArray("BMLINK", i
, line
);
268 cgiSetArray("BMTEXT", i
, n
->section
);
269 cgiSetArray("BMINDENT", i
, "0");
272 section
= n
->section
;
275 if (!topic
|| strcmp(n
->section
, topic
))
279 * Add a link for this node...
282 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
283 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
285 cgiSetArray("BMLINK", i
, line
);
286 cgiSetArray("BMTEXT", i
, n
->text
);
287 cgiSetArray("BMINDENT", i
, "1");
291 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
293 help_node_t
*nn
; /* Pointer to sub-node */
296 cupsArraySave(hi
->sorted
);
298 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
300 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
301 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
304 * Add a link for this node...
307 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
308 cgiSetArray("BMLINK", i
, line
);
309 cgiSetArray("BMTEXT", i
, nn
->text
);
310 cgiSetArray("BMINDENT", i
, "2");
315 cupsArrayRestore(hi
->sorted
);
320 * Show the search and bookmark content...
323 if (!helpfile
|| !printable
)
324 cgiCopyTemplateLang("help-header.tmpl");
326 cgiCopyTemplateLang("help-printable.tmpl");
329 * If we are viewing a file, copy it in now...
334 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
336 int inbody
; /* Are we inside the body? */
341 while (cupsFileGets(fp
, line
, sizeof(line
)))
345 if (!strncasecmp(line
, "</BODY>", 7))
348 printf("%s\n", line
);
350 else if (!strncasecmp(line
, "<BODY", 5))
359 cgiSetVariable("ERROR", "Unable to open help file.");
360 cgiCopyTemplateLang("error.tmpl");
365 * Send a standard trailer...
370 cgiCopyTemplateLang("help-trailer.tmpl");
374 puts("</BODY>\n</HTML>");
377 * Delete the index...
383 * Return with no errors...