]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/testhi.c
Merge pull request #5471 from Atalanttore/patch-2
[thirdparty/cups.git] / cgi-bin / testhi.c
index 6f09caeb8304d0c5d4e200d5df7b5eaea6a9f41b..dec6bdf765e4135a4a9323782e1b1ddec80ef3df 100644 (file)
@@ -1,30 +1,10 @@
 /*
- * "$Id: testhi.c 177 2006-06-21 00:20:03Z jlovell $"
+ * Help index test program for CUPS.
  *
- *   Help index test program for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2017 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   Copyright 1997-2005 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
- *
- * Contents:
- *
- *   main()       - Test the help index code.
- *   list_nodes() - List nodes in an array...
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -39,6 +19,7 @@
  */
 
 static void    list_nodes(const char *title, cups_array_t *nodes);
+static int     usage(void);
 
 
 /*
@@ -49,15 +30,82 @@ int                                 /* O - Exit status */
 main(int  argc,                                /* I - Number of command-line arguments */
      char *argv[])                     /* I - Command-line arguments */
 {
+  int          i;                      /* Looping var */
   help_index_t *hi,                    /* Help index */
                *search;                /* Search index */
+  const char   *opt,                   /* Current option character */
+               *dir = ".",             /* Directory to index */
+               *q = NULL,              /* Query string */
+               *section = NULL,        /* Section string */
+               *filename = NULL;       /* Filename string */
 
 
+ /*
+  * Parse the command-line...
+  */
+
+  for (i = 1; i < argc; i ++)
+  {
+    if (argv[i][0] == '-')
+    {
+      if (!strcmp(argv[i], "--help"))
+      {
+        usage();
+        return (0);
+      }
+
+      for (opt = argv[i] + 1; *opt; opt ++)
+      {
+        switch (*opt)
+        {
+          case 'd' : /* -d directory */
+              i ++;
+              if (i < argc)
+              {
+                dir = argv[i];
+              }
+              else
+              {
+                fputs("testhi: Missing directory for \"-d\" option.\n", stderr);
+                return (usage());
+              }
+              break;
+
+          case 's' : /* -s section */
+              i ++;
+              if (i < argc)
+              {
+                section = argv[i];
+              }
+              else
+              {
+                fputs("testhi: Missing section name for \"-s\" option.\n", stderr);
+                return (usage());
+              }
+              break;
+
+          default :
+             fprintf(stderr, "testhi: Unknown option \"-%c\".\n", *opt);
+             return (usage());
+        }
+      }
+    }
+    else if (!q)
+      q = argv[i];
+    else if (!filename)
+      filename = argv[i];
+    else
+    {
+      fprintf(stderr, "testhi: Unknown argument \"%s\".\n", argv[i]);
+      return (usage());
+    }
+  }
+
  /*
   * Load the help index...
   */
 
-  hi = helpLoadIndex("testhi.index", ".");
+  hi = helpLoadIndex("testhi.index", dir);
 
   list_nodes("nodes", hi->nodes);
   list_nodes("sorted", hi->sorted);
@@ -66,9 +114,9 @@ main(int  argc,                              /* I - Number of command-line arguments */
   * Do any searches...
   */
 
-  if (argc > 1)
+  if (q)
   {
-    search = helpSearchIndex(hi, argv[1], NULL, argv[2]);
+    search = helpSearchIndex(hi, q, section, filename);
 
     if (search)
     {
@@ -76,7 +124,7 @@ main(int  argc,                              /* I - Number of command-line arguments */
       helpDeleteIndex(search);
     }
     else
-      printf("%s (0 nodes)\n", argv[1]);
+      printf("%s (0 nodes)\n", q);
   }
 
   helpDeleteIndex(hi);
@@ -105,14 +153,29 @@ list_nodes(const char   *title,           /* I - Title string */
   for (i = 1, node = (help_node_t *)cupsArrayFirst(nodes);
        node;
        i ++, node = (help_node_t *)cupsArrayNext(nodes))
+  {
     if (node->anchor)
-      printf("    %d: %s#%s \"%s\"\n", i, node->filename, node->anchor,
+      printf("    %d: %s#%s \"%s\"", i, node->filename, node->anchor,
              node->text);
     else
-      printf("    %d: %s \"%s\"\n", i, node->filename, node->text);
+      printf("    %d: %s \"%s\"", i, node->filename, node->text);
+
+    printf(" (%d words)\n", cupsArrayCount(node->words));
+  }
 }
 
 
 /*
- * End of "$Id: testhi.c 177 2006-06-21 00:20:03Z jlovell $".
+ * 'usage()' - Show program usage.
  */
+
+static int                             /* O - Exit status */
+usage(void)
+{
+  puts("Usage: ./testhi [options] [\"query\"] [filename]");
+  puts("Options:");
+  puts("-d directory      Specify index directory.");
+  puts("-s section        Specify search section.");
+
+  return (1);
+}