]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/help.c
Changelog.
[thirdparty/cups.git] / cgi-bin / help.c
index fd9df5b563271eb71b3b51a12211f3813c5d4e2d..215c98bc43e369eb3ee590caec6518b5f7f87f72 100644 (file)
@@ -1,25 +1,16 @@
 /*
  * "$Id$"
  *
- *   On-line help CGI for the Common UNIX Printing System (CUPS).
+ *   Online help CGI for CUPS.
  *
- *   Copyright 1997-2005 by Easy Software Products.
+ *   Copyright 2007-2011 by Apple Inc.
+ *   Copyright 1997-2006 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
@@ -30,8 +21,7 @@
  * Include necessary headers...
  */
 
-#include "ipp-var.h"
-#include "help-index.h"
+#include "cgi-private.h"
 
 
 /*
@@ -44,16 +34,21 @@ main(int  argc,                             /* I - Number of command-line arguments */
 {
   help_index_t *hi,                    /* Help index */
                *si;                    /* Search index */
-  help_node_t  **n;                    /* Current help node */
-  int          i, j;                   /* Looping vars */
+  help_node_t  *n;                     /* Current help node */
+  int          i;                      /* Looping var */
   const char   *query;                 /* Search query */
-  const char   *server_root;           /* CUPS_SERVERROOT environment variable */
+  const char   *cache_dir;             /* CUPS_CACHEDIR environment variable */
   const char   *docroot;               /* CUPS_DOCROOT environment variable */
-  const char   *helpfile;              /* Current help file */
+  const char   *helpfile,              /* Current help file */
+               *helptitle = NULL;      /* Current help title */
+  const char   *topic;                 /* Current topic */
+  char         topic_data[1024];       /* Topic form data */
+  const char   *section;               /* Current section */
   char         filename[1024],         /* Filename */
                directory[1024];        /* Directory */
   cups_file_t  *fp;                    /* Help file */
   char         line[1024];             /* Line from file */
+  int          printable;              /* Show printable version? */
 
 
  /*
@@ -62,20 +57,23 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
   cgiInitialize();
 
+  printable = cgiGetVariable("PRINTABLE") != NULL;
+
  /*
   * Set the web interface section...
   */
 
   cgiSetVariable("SECTION", "help");
+  cgiSetVariable("REFRESH_PAGE", "");
 
  /*
   * Load the help index...
   */
 
-  if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
-    server_root = CUPS_SERVERROOT;
+  if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
+    cache_dir = CUPS_CACHEDIR;
 
-  snprintf(filename, sizeof(filename), "%s/help.index", server_root);
+  snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
 
   if ((docroot = getenv("CUPS_DOCROOT")) == NULL)
     docroot = CUPS_DOCROOT;
@@ -90,29 +88,31 @@ main(int  argc,                             /* I - Number of command-line arguments */
   {
     perror(filename);
 
-    cgiStartHTML("Help");
-    cgiSetVariable("ERROR", "Unable to load help index!");
-    cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
+    cgiStartHTML(cgiText(_("Online Help")));
+    cgiSetVariable("ERROR", cgiText(_("Unable to load help index.")));
+    cgiCopyTemplateLang("error.tmpl");
     cgiEndHTML();
 
     return (1);
   }
 
-  fprintf(stderr, "hi->num_nodes=%d\n", hi->num_nodes);
+  fprintf(stderr, "DEBUG: %d nodes in help index...\n",
+          cupsArrayCount(hi->nodes));
 
  /*
   * See if we are viewing a file...
   */
 
   for (i = 0; i < argc; i ++)
-    fprintf(stderr, "argv[%d]=\"%s\"\n", i, argv[i]);
+    fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
 
   if ((helpfile = getenv("PATH_INFO")) != NULL)
+  {
     helpfile ++;
-  else if (strstr(argv[0], "help.cgi"))
-    helpfile = NULL;
-  else
-    helpfile = argv[0];
+
+    if (!*helpfile)
+      helpfile = NULL;
+  }
 
   if (helpfile)
   {
@@ -129,9 +129,9 @@ main(int  argc,                             /* I - Number of command-line arguments */
     {
       perror(filename);
 
-      cgiStartHTML("Help");
-      cgiSetVariable("ERROR", "Unable to access help file!");
-      cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
+      cgiStartHTML(cgiText(_("Online Help")));
+      cgiSetVariable("ERROR", cgiText(_("Unable to access help file.")));
+      cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
 
       return (1);
@@ -139,26 +139,29 @@ main(int  argc,                           /* I - Number of command-line arguments */
 
     if ((n = helpFindNode(hi, helpfile, NULL)) == NULL)
     {
-      cgiStartHTML("Help");
-      cgiSetVariable("ERROR", "Help file not in index!");
-      cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
+      cgiStartHTML(cgiText(_("Online Help")));
+      cgiSetVariable("ERROR", cgiText(_("Help file not in index.")));
+      cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
 
       return (1);
     }
 
    /*
-    * Set the page title and save the help file...
+    * Save the page title and help file...
     */
 
-    cgiSetVariable("HELPFILE", helpfile);
-    cgiSetVariable("HELPTITLE", n[0]->text);
+    helptitle = n->text;
+    topic     = n->section;
 
    /*
     * Send a standard page header...
     */
 
-    cgiStartHTML(n[0]->text);
+    if (printable)
+      puts("Content-Type: text/html;charset=utf-8\n");
+    else
+      cgiStartHTML(n->text);
   }
   else
   {
@@ -166,46 +169,67 @@ main(int  argc,                           /* I - Number of command-line arguments */
     * Send a standard page header...
     */
 
-    cgiStartHTML("Help");
+    cgiStartHTML(cgiText(_("Online Help")));
+
+    topic = cgiGetVariable("TOPIC");
   }
 
  /*
   * Do a search as needed...
   */
 
+  if (cgiGetVariable("CLEAR"))
+    cgiSetVariable("QUERY", "");
+
   query = cgiGetVariable("QUERY");
-  si    = helpSearchIndex(hi, query, helpfile);
+  si    = helpSearchIndex(hi, query, topic, helpfile);
+
+  cgiClearVariables();
+  if (query)
+    cgiSetVariable("QUERY", query);
+  if (topic)
+    cgiSetVariable("TOPIC", topic);
+  if (helpfile)
+    cgiSetVariable("HELPFILE", helpfile);
+  if (helptitle)
+    cgiSetVariable("HELPTITLE", helptitle);
+
+  fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
+          query ? query : "(null)", topic ? topic : "(null)");
 
   if (si)
   {
-    help_node_t        **nn;                   /* Parent node */
+    help_node_t        *nn;                    /* Parent node */
 
 
-    fprintf(stderr, "si=%p, si->num_nodes=%d, si->sorted=%p\n", si,
-            si->num_nodes, si->sorted);
+    fprintf(stderr,
+            "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
+            si->sorted, cupsArrayCount(si->sorted));
 
-    for (i = 0, n = si->sorted; i < si->num_nodes; i ++, n ++)
+    for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
+         n;
+        i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
     {
-      if (helpfile && n[0]->anchor)
-        snprintf(line, sizeof(line), "#%s", n[0]->anchor);
-      else if (n[0]->anchor)
-        snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n[0]->filename,
-                query ? query : "", n[0]->anchor);
+      if (helpfile && n->anchor)
+        snprintf(line, sizeof(line), "#%s", n->anchor);
+      else if (n->anchor)
+        snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename,
+                query ? query : "", n->anchor);
       else
-        snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n[0]->filename,
+        snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
                 query ? query : "");
 
-      cgiSetArray("QTEXT", i, n[0]->text);
+      cgiSetArray("QTEXT", i, n->text);
       cgiSetArray("QLINK", i, line);
 
-      if (!helpfile && n[0]->anchor)
+      if (!helpfile && n->anchor)
       {
-        nn = helpFindNode(hi, n[0]->filename, NULL);
+        nn = helpFindNode(hi, n->filename, NULL);
 
-        snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn[0]->filename,
+        snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
                 query ? query : "");
 
-        cgiSetArray("QPTEXT", i, nn[0]->text);
+        cgiSetArray("QPTEXT", i, nn->text);
        cgiSetArray("QPLINK", i, line);
       }
       else
@@ -214,7 +238,7 @@ main(int  argc,                             /* I - Number of command-line arguments */
        cgiSetArray("QPLINK", i, "");
       }
 
-      fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n[0]->text, line);
+      fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
     }
 
     helpDeleteIndex(si);
@@ -224,43 +248,76 @@ main(int  argc,                           /* I - Number of command-line arguments */
   * OK, now list the bookmarks within the index...
   */
 
-  for (i = hi->num_nodes, j = 0, n = hi->sorted; i > 0; i --, n ++)
+  for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
+       n;
+       n = (help_node_t *)cupsArrayNext(hi->sorted))
   {
-    if (n[0]->anchor)
+    if (n->anchor)
+      continue;
+
+   /*
+    * Add a section link as needed...
+    */
+
+    if (n->section &&
+        (!section || strcmp(n->section, section)))
+    {
+     /*
+      * Add a link for this node...
+      */
+
+      snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
+               cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
+              query ? query : "");
+      cgiSetArray("BMLINK", i, line);
+      cgiSetArray("BMTEXT", i, n->section);
+      cgiSetArray("BMINDENT", i, "0");
+
+      i ++;
+      section = n->section;
+    }
+
+    if (!topic || strcmp(n->section, topic))
       continue;
 
    /*
     * Add a link for this node...
     */
 
-    snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n[0]->filename,
-             query ? query : "");
-    cgiSetArray("BMLINK", j, line);
-    cgiSetArray("BMTEXT", j, n[0]->text);
-    cgiSetArray("BMINDENT", j, "0");
+    snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
+             cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
+            query ? query : "");
+    cgiSetArray("BMLINK", i, line);
+    cgiSetArray("BMTEXT", i, n->text);
+    cgiSetArray("BMINDENT", i, "1");
 
-    j ++;
+    i ++;
 
-    if (helpfile && !strcmp(helpfile, n[0]->filename))
+    if (helpfile && !strcmp(helpfile, n->filename))
     {
-      int              ii;             /* Looping var */
-      help_node_t      **nn;           /* Pointer to sub-node */
+      help_node_t      *nn;            /* Pointer to sub-node */
 
 
-      for (ii = hi->num_nodes, nn = hi->sorted; ii > 0; ii --, nn ++)
-        if (nn[0]->anchor && !strcmp(helpfile, nn[0]->filename))
+      cupsArraySave(hi->sorted);
+
+      for (nn = (help_node_t *)cupsArrayFirst(hi->sorted);
+           nn;
+          nn = (help_node_t *)cupsArrayNext(hi->sorted))
+        if (nn->anchor && !strcmp(helpfile, nn->filename))
        {
         /*
          * Add a link for this node...
          */
 
-         snprintf(line, sizeof(line), "#%s", nn[0]->anchor);
-         cgiSetArray("BMLINK", j, line);
-         cgiSetArray("BMTEXT", j, nn[0]->text);
-         cgiSetArray("BMINDENT", j, "1");
+         snprintf(line, sizeof(line), "#%s", nn->anchor);
+         cgiSetArray("BMLINK", i, line);
+         cgiSetArray("BMTEXT", i, nn->text);
+         cgiSetArray("BMINDENT", i, "2");
 
-         j ++;
+         i ++;
        }
+
+      cupsArrayRestore(hi->sorted);
     }
   }
 
@@ -268,7 +325,10 @@ main(int  argc,                            /* I - Number of command-line arguments */
   * Show the search and bookmark content...
   */
 
-  cgiCopyTemplateLang(stdout, TEMPLATES, "help-header.tmpl", getenv("LANG"));
+  if (!helpfile || !printable)
+    cgiCopyTemplateLang("help-header.tmpl");
+  else
+    cgiCopyTemplateLang("help-printable.tmpl");
 
  /*
   * If we are viewing a file, copy it in now...
@@ -287,12 +347,12 @@ main(int  argc,                           /* I - Number of command-line arguments */
       {
         if (inbody)
        {
-         if (!strncasecmp(line, "</BODY>", 7))
+         if (!_cups_strncasecmp(line, "</BODY>", 7))
            break;
 
          printf("%s\n", line);
         }
-       else if (!strncasecmp(line, "<BODY", 5))
+       else if (!_cups_strncasecmp(line, "<BODY", 5))
          inbody = 1;
       }
 
@@ -301,8 +361,8 @@ main(int  argc,                             /* I - Number of command-line arguments */
     else
     {
       perror(filename);
-      cgiSetVariable("ERROR", "Unable to open help file.");
-      cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
+      cgiSetVariable("ERROR", cgiText(_("Unable to open help file.")));
+      cgiCopyTemplateLang("error.tmpl");
     }
   }
 
@@ -310,7 +370,13 @@ main(int  argc,                            /* I - Number of command-line arguments */
   * Send a standard trailer...
   */
 
-  cgiEndHTML();
+  if (!printable)
+  {
+    cgiCopyTemplateLang("help-trailer.tmpl");
+    cgiEndHTML();
+  }
+  else
+    puts("</BODY>\n</HTML>");
 
  /*
   * Delete the index...