]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
4 * On-line help CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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");
68 * Load the help index...
71 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
72 cache_dir
= CUPS_CACHEDIR
;
74 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
76 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
77 docroot
= CUPS_DOCROOT
;
79 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
81 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
84 hi
= helpLoadIndex(filename
, directory
);
89 cgiStartHTML(cgiText(_("Help")));
90 cgiSetVariable("ERROR", "Unable to load help index!");
91 cgiCopyTemplateLang("error.tmpl");
97 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
98 cupsArrayCount(hi
->nodes
));
101 * See if we are viewing a file...
104 for (i
= 0; i
< argc
; i
++)
105 fprintf(stderr
, "argv[%d]=\"%s\"\n", i
, argv
[i
]);
107 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
118 * Verify that the help file exists and is part of the index...
121 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
123 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
126 if (access(filename
, R_OK
))
130 cgiStartHTML(cgiText(_("Help")));
131 cgiSetVariable("ERROR", "Unable to access help file!");
132 cgiCopyTemplateLang("error.tmpl");
138 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
140 cgiStartHTML(cgiText(_("Help")));
141 cgiSetVariable("ERROR", "Help file not in index!");
142 cgiCopyTemplateLang("error.tmpl");
149 * Set the page title and save the help file...
152 cgiSetVariable("HELPFILE", helpfile
);
153 cgiSetVariable("HELPTITLE", n
->text
);
154 cgiSetVariable("TOPIC", n
->section
);
157 * Send a standard page header...
161 puts("Content-Type: text/html;charset=utf-8\n");
163 cgiStartHTML(n
->text
);
168 * Send a standard page header...
171 cgiStartHTML(cgiText(_("Help")));
175 * Do a search as needed...
178 query
= cgiGetVariable("QUERY");
179 topic
= cgiGetVariable("TOPIC");
180 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
182 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
183 query
? query
: "(null)", topic
? topic
: "(null)");
187 help_node_t
*nn
; /* Parent node */
191 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
192 si
->sorted
, cupsArrayCount(si
->sorted
));
194 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
196 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
198 if (helpfile
&& n
->anchor
)
199 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
201 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
202 query
? query
: "", n
->anchor
);
204 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
207 cgiSetArray("QTEXT", i
, n
->text
);
208 cgiSetArray("QLINK", i
, line
);
210 if (!helpfile
&& n
->anchor
)
212 nn
= helpFindNode(hi
, n
->filename
, NULL
);
214 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
217 cgiSetArray("QPTEXT", i
, nn
->text
);
218 cgiSetArray("QPLINK", i
, line
);
222 cgiSetArray("QPTEXT", i
, "");
223 cgiSetArray("QPLINK", i
, "");
226 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
233 * OK, now list the bookmarks within the index...
236 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
238 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
244 * Add a section link as needed...
248 (!section
|| strcmp(n
->section
, section
)))
251 * Add a link for this node...
254 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
255 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
257 cgiSetArray("BMLINK", i
, line
);
258 cgiSetArray("BMTEXT", i
, n
->section
);
259 cgiSetArray("BMINDENT", i
, "0");
262 section
= n
->section
;
265 if (!topic
|| strcmp(n
->section
, topic
))
269 * Add a link for this node...
272 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
273 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
275 cgiSetArray("BMLINK", i
, line
);
276 cgiSetArray("BMTEXT", i
, n
->text
);
277 cgiSetArray("BMINDENT", i
, "1");
281 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
283 help_node_t
*nn
; /* Pointer to sub-node */
286 cupsArraySave(hi
->sorted
);
288 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
290 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
291 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
294 * Add a link for this node...
297 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
298 cgiSetArray("BMLINK", i
, line
);
299 cgiSetArray("BMTEXT", i
, nn
->text
);
300 cgiSetArray("BMINDENT", i
, "2");
305 cupsArrayRestore(hi
->sorted
);
310 * Show the search and bookmark content...
313 if (!helpfile
|| !printable
)
314 cgiCopyTemplateLang("help-header.tmpl");
316 cgiCopyTemplateLang("help-printable.tmpl");
319 * If we are viewing a file, copy it in now...
324 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
326 int inbody
; /* Are we inside the body? */
331 while (cupsFileGets(fp
, line
, sizeof(line
)))
335 if (!strncasecmp(line
, "</BODY>", 7))
338 printf("%s\n", line
);
340 else if (!strncasecmp(line
, "<BODY", 5))
349 cgiSetVariable("ERROR", "Unable to open help file.");
350 cgiCopyTemplateLang("error.tmpl");
355 * Send a standard trailer...
361 puts("</BODY>\n</HTML>");
364 * Delete the index...
370 * Return with no errors...