]>
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 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for CGI.
30 * Include necessary headers...
33 #include "cgi-private.h"
37 * 'main()' - Main entry for CGI.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
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 int printable
; /* Show printable version? */
63 * Get any form variables...
68 printable
= cgiGetVariable("PRINTABLE") != NULL
;
71 * Set the web interface section...
74 cgiSetVariable("SECTION", "help");
77 * Load the help index...
80 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
81 cache_dir
= CUPS_CACHEDIR
;
83 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
85 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
86 docroot
= CUPS_DOCROOT
;
88 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
90 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
93 hi
= helpLoadIndex(filename
, directory
);
98 cgiStartHTML(cgiText(_("Help")));
99 cgiSetVariable("ERROR", "Unable to load help index!");
100 cgiCopyTemplateLang("error.tmpl");
106 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
107 cupsArrayCount(hi
->nodes
));
110 * See if we are viewing a file...
113 for (i
= 0; i
< argc
; i
++)
114 fprintf(stderr
, "argv[%d]=\"%s\"\n", i
, argv
[i
]);
116 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
127 * Verify that the help file exists and is part of the index...
130 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
132 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
135 if (access(filename
, R_OK
))
139 cgiStartHTML(cgiText(_("Help")));
140 cgiSetVariable("ERROR", "Unable to access help file!");
141 cgiCopyTemplateLang("error.tmpl");
147 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
149 cgiStartHTML(cgiText(_("Help")));
150 cgiSetVariable("ERROR", "Help file not in index!");
151 cgiCopyTemplateLang("error.tmpl");
158 * Set the page title and save the help file...
161 cgiSetVariable("HELPFILE", helpfile
);
162 cgiSetVariable("HELPTITLE", n
->text
);
165 * Send a standard page header...
169 puts("Content-Type: text/html;charset=utf-8\n");
171 cgiStartHTML(n
->text
);
176 * Send a standard page header...
179 cgiStartHTML(cgiText(_("Help")));
183 * Do a search as needed...
186 query
= cgiGetVariable("QUERY");
187 topic
= cgiGetVariable("TOPIC");
188 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
190 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n", query
, topic
);
194 help_node_t
*nn
; /* Parent node */
198 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
199 si
->sorted
, cupsArrayCount(si
->sorted
));
201 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
203 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
205 if (helpfile
&& n
->anchor
)
206 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
208 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
209 query
? query
: "", n
->anchor
);
211 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
214 cgiSetArray("QTEXT", i
, n
->text
);
215 cgiSetArray("QLINK", i
, line
);
217 if (!helpfile
&& n
->anchor
)
219 nn
= helpFindNode(hi
, n
->filename
, NULL
);
221 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
224 cgiSetArray("QPTEXT", i
, nn
->text
);
225 cgiSetArray("QPLINK", i
, line
);
229 cgiSetArray("QPTEXT", i
, "");
230 cgiSetArray("QPLINK", i
, "");
233 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
240 * OK, now list the bookmarks within the index...
243 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
245 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
251 * Add a section link as needed...
255 (!section
|| strcmp(n
->section
, section
)))
258 * Add a link for this node...
261 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
262 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
264 cgiSetArray("BMLINK", i
, line
);
265 cgiSetArray("BMTEXT", i
, n
->section
);
266 cgiSetArray("BMINDENT", i
, "0");
269 section
= n
->section
;
272 if (!topic
|| strcmp(n
->section
, topic
))
276 * Add a link for this node...
279 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
280 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
282 cgiSetArray("BMLINK", i
, line
);
283 cgiSetArray("BMTEXT", i
, n
->text
);
284 cgiSetArray("BMINDENT", i
, "1");
288 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
290 help_node_t
*nn
; /* Pointer to sub-node */
293 cupsArraySave(hi
->sorted
);
295 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
297 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
298 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
301 * Add a link for this node...
304 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
305 cgiSetArray("BMLINK", i
, line
);
306 cgiSetArray("BMTEXT", i
, nn
->text
);
307 cgiSetArray("BMINDENT", i
, "2");
312 cupsArrayRestore(hi
->sorted
);
317 * Show the search and bookmark content...
320 if (!helpfile
|| !printable
)
321 cgiCopyTemplateLang("help-header.tmpl");
323 cgiCopyTemplateLang("help-printable.tmpl");
326 * If we are viewing a file, copy it in now...
331 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
333 int inbody
; /* Are we inside the body? */
338 while (cupsFileGets(fp
, line
, sizeof(line
)))
342 if (!strncasecmp(line
, "</BODY>", 7))
345 printf("%s\n", line
);
347 else if (!strncasecmp(line
, "<BODY", 5))
356 cgiSetVariable("ERROR", "Unable to open help file.");
357 cgiCopyTemplateLang("error.tmpl");
362 * Send a standard trailer...
368 puts("</BODY>\n</HTML>");
371 * Delete the index...
377 * Return with no errors...