]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help-index.c
2 * Online help index routines for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cgi-private.h"
19 * List of common English words that should not be indexed...
22 static char help_common_words
[][6] =
127 static help_word_t
*help_add_word(help_node_t
*n
, const char *text
);
128 static void help_delete_node(help_node_t
*n
);
129 static void help_delete_word(help_word_t
*w
);
130 static int help_load_directory(help_index_t
*hi
,
131 const char *directory
,
132 const char *relative
);
133 static int help_load_file(help_index_t
*hi
,
134 const char *filename
,
135 const char *relative
,
137 static help_node_t
*help_new_node(const char *filename
, const char *anchor
,
138 const char *section
, const char *text
,
139 time_t mtime
, off_t offset
,
141 __attribute__((nonnull(1,3,4)));
142 static int help_sort_by_name(help_node_t
*p1
, help_node_t
*p2
);
143 static int help_sort_by_score(help_node_t
*p1
, help_node_t
*p2
);
144 static int help_sort_words(help_word_t
*w1
, help_word_t
*w2
);
148 * 'helpDeleteIndex()' - Delete an index, freeing all memory used.
152 helpDeleteIndex(help_index_t
*hi
) /* I - Help index */
154 help_node_t
*node
; /* Current node */
157 DEBUG_printf(("helpDeleteIndex(hi=%p)", hi
));
162 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
164 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
167 help_delete_node(node
);
170 cupsArrayDelete(hi
->nodes
);
171 cupsArrayDelete(hi
->sorted
);
178 * 'helpFindNode()' - Find a node in an index.
181 help_node_t
* /* O - Node pointer or NULL */
182 helpFindNode(help_index_t
*hi
, /* I - Index */
183 const char *filename
, /* I - Filename */
184 const char *anchor
) /* I - Anchor */
186 help_node_t key
; /* Search key */
189 DEBUG_printf(("helpFindNode(hi=%p, filename=\"%s\", anchor=\"%s\")",
190 hi
, filename
, anchor
));
193 * Range check input...
196 if (!hi
|| !filename
)
200 * Initialize the search key...
203 key
.filename
= (char *)filename
;
204 key
.anchor
= (char *)anchor
;
207 * Return any match...
210 return ((help_node_t
*)cupsArrayFind(hi
->nodes
, &key
));
215 * 'helpLoadIndex()' - Load a help index from disk.
218 help_index_t
* /* O - Index pointer or NULL */
219 helpLoadIndex(const char *hifile
, /* I - Index filename */
220 const char *directory
) /* I - Directory that is indexed */
222 help_index_t
*hi
; /* Help index */
223 cups_file_t
*fp
; /* Current file */
224 char line
[2048], /* Line from file */
225 *ptr
, /* Pointer into line */
226 *filename
, /* Filename in line */
227 *anchor
, /* Anchor in line */
228 *sectptr
, /* Section pointer in line */
229 section
[1024], /* Section name */
230 *text
; /* Text in line */
231 time_t mtime
; /* Modification time */
232 off_t offset
; /* Offset into file */
233 size_t length
; /* Length in bytes */
234 int update
; /* Update? */
235 help_node_t
*node
; /* Current node */
236 help_word_t
*word
; /* Current word */
239 DEBUG_printf(("helpLoadIndex(hifile=\"%s\", directory=\"%s\")",
243 * Create a new, empty index.
246 if ((hi
= (help_index_t
*)calloc(1, sizeof(help_index_t
))) == NULL
)
249 hi
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
250 hi
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
252 if (!hi
->nodes
|| !hi
->sorted
)
254 cupsArrayDelete(hi
->nodes
);
255 cupsArrayDelete(hi
->sorted
);
261 * Try loading the existing index file...
264 if ((fp
= cupsFileOpen(hifile
, "r")) != NULL
)
267 * Lock the file and then read the first line...
272 if (cupsFileGets(fp
, line
, sizeof(line
)) && !strcmp(line
, "HELPV2"))
275 * Got a valid header line, now read the data lines...
280 while (cupsFileGets(fp
, line
, sizeof(line
)))
283 * Each line looks like one of the following:
285 * filename mtime offset length "section" "text"
286 * filename#anchor offset length "text"
293 * Read a word in the current node...
296 if (!node
|| (ptr
= strrchr(line
, ' ')) == NULL
)
299 if ((word
= help_add_word(node
, ptr
+ 1)) != NULL
)
300 word
->count
= atoi(line
+ 1);
310 if ((ptr
= strchr(line
, ' ')) == NULL
)
313 while (isspace(*ptr
& 255))
316 if ((anchor
= strrchr(filename
, '#')) != NULL
)
322 mtime
= strtol(ptr
, &ptr
, 10);
324 offset
= strtoll(ptr
, &ptr
, 10);
325 length
= (size_t)strtoll(ptr
, &ptr
, 10);
327 while (isspace(*ptr
& 255))
342 while (*ptr
&& *ptr
!= '\"')
350 strlcpy(section
, sectptr
, sizeof(section
));
352 while (isspace(*ptr
& 255))
362 while (*ptr
&& *ptr
!= '\"')
370 if ((node
= help_new_node(filename
, anchor
, section
, text
,
371 mtime
, offset
, length
)) == NULL
)
376 cupsArrayAdd(hi
->nodes
, node
);
385 * Scan for new/updated files...
388 update
= help_load_directory(hi
, directory
, NULL
);
391 * Remove any files that are no longer installed...
394 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
396 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
400 * Delete this node...
403 cupsArrayRemove(hi
->nodes
, node
);
404 help_delete_node(node
);
408 * Add nodes to the sorted array...
411 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
413 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
414 cupsArrayAdd(hi
->sorted
, node
);
417 * Save the index if we updated it...
421 helpSaveIndex(hi
, hifile
);
424 * Return the index...
432 * 'helpSaveIndex()' - Save a help index to disk.
435 int /* O - 0 on success, -1 on error */
436 helpSaveIndex(help_index_t
*hi
, /* I - Index */
437 const char *hifile
) /* I - Index filename */
439 cups_file_t
*fp
; /* Index file */
440 help_node_t
*node
; /* Current node */
441 help_word_t
*word
; /* Current word */
444 DEBUG_printf(("helpSaveIndex(hi=%p, hifile=\"%s\")", hi
, hifile
));
447 * Try creating a new index file...
450 if ((fp
= cupsFileOpen(hifile
, "w9")) == NULL
)
454 * Lock the file while we write it...
459 cupsFilePuts(fp
, "HELPV2\n");
461 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
463 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
466 * Write the current node with/without the anchor...
471 if (cupsFilePrintf(fp
, "%s#%s " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\"\n",
472 node
->filename
, node
->anchor
,
473 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
479 if (cupsFilePrintf(fp
, "%s %d " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\" \"%s\"\n",
480 node
->filename
, (int)node
->mtime
,
481 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
482 node
->section
? node
->section
: "", node
->text
) < 0)
487 * Then write the words associated with the node...
490 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
492 word
= (help_word_t
*)cupsArrayNext(node
->words
))
493 if (cupsFilePrintf(fp
, " %d %s\n", word
->count
, word
->text
) < 0)
499 if (cupsFileClose(fp
) < 0)
509 * 'helpSearchIndex()' - Search an index.
512 help_index_t
* /* O - Search index */
513 helpSearchIndex(help_index_t
*hi
, /* I - Index */
514 const char *query
, /* I - Query string */
515 const char *section
, /* I - Limit search to this section */
516 const char *filename
) /* I - Limit search to this file */
518 help_index_t
*search
; /* Search index */
519 help_node_t
*node
; /* Current node */
520 help_word_t
*word
; /* Current word */
521 void *sc
; /* Search context */
522 int matches
; /* Number of matches */
525 DEBUG_printf(("helpSearchIndex(hi=%p, query=\"%s\", filename=\"%s\")",
526 hi
, query
, filename
));
536 * Reset the scores of all nodes to 0...
539 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
541 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
545 * Find the first node to search in...
550 node
= helpFindNode(hi
, filename
, NULL
);
555 node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
558 * Convert the query into a regular expression...
561 sc
= cgiCompileSearch(query
);
566 * Allocate a search index...
569 search
= calloc(1, sizeof(help_index_t
));
576 search
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
577 search
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
579 if (!search
->nodes
|| !search
->sorted
)
581 cupsArrayDelete(search
->nodes
);
582 cupsArrayDelete(search
->sorted
);
591 * Check each node in the index, adding matching nodes to the
595 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
596 if (section
&& strcmp(node
->section
, section
))
598 else if (filename
&& strcmp(node
->filename
, filename
))
602 matches
= cgiDoSearch(sc
, node
->text
);
604 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
606 word
= (help_word_t
*)cupsArrayNext(node
->words
))
607 if (cgiDoSearch(sc
, word
->text
) > 0)
608 matches
+= word
->count
;
613 * Found a match, add the node to the search index...
616 node
->score
= matches
;
618 cupsArrayAdd(search
->nodes
, node
);
619 cupsArrayAdd(search
->sorted
, node
);
624 * Free the search context...
630 * Return the results...
638 * 'help_add_word()' - Add a word to a node.
641 static help_word_t
* /* O - New word */
642 help_add_word(help_node_t
*n
, /* I - Node */
643 const char *text
) /* I - Word text */
645 help_word_t
*w
, /* New word */
646 key
; /* Search key */
649 DEBUG_printf(("2help_add_word(n=%p, text=\"%s\")", n
, text
));
652 * Create the words array as needed...
656 n
->words
= cupsArrayNew((cups_array_func_t
)help_sort_words
, NULL
);
659 * See if the word is already added...
662 key
.text
= (char *)text
;
664 if ((w
= (help_word_t
*)cupsArrayFind(n
->words
, &key
)) == NULL
)
667 * Create a new word...
670 if ((w
= calloc(1, sizeof(help_word_t
))) == NULL
)
673 if ((w
->text
= strdup(text
)) == NULL
)
679 cupsArrayAdd(n
->words
, w
);
683 * Bump the counter for this word and return it...
693 * 'help_delete_node()' - Free all memory used by a node.
697 help_delete_node(help_node_t
*n
) /* I - Node */
699 help_word_t
*w
; /* Current word */
702 DEBUG_printf(("2help_delete_node(n=%p)", n
));
719 for (w
= (help_word_t
*)cupsArrayFirst(n
->words
);
721 w
= (help_word_t
*)cupsArrayNext(n
->words
))
724 cupsArrayDelete(n
->words
);
731 * 'help_delete_word()' - Free all memory used by a word.
735 help_delete_word(help_word_t
*w
) /* I - Word */
737 DEBUG_printf(("2help_delete_word(w=%p)", w
));
750 * 'help_load_directory()' - Load a directory of files into an index.
753 static int /* O - 0 = success, -1 = error, 1 = updated */
755 help_index_t
*hi
, /* I - Index */
756 const char *directory
, /* I - Directory */
757 const char *relative
) /* I - Relative path */
759 cups_dir_t
*dir
; /* Directory file */
760 cups_dentry_t
*dent
; /* Directory entry */
761 char *ext
, /* Pointer to extension */
762 filename
[1024], /* Full filename */
763 relname
[1024]; /* Relative filename */
764 int update
; /* Updated? */
765 help_node_t
*node
; /* Current node */
768 DEBUG_printf(("2help_load_directory(hi=%p, directory=\"%s\", relative=\"%s\")",
769 hi
, directory
, relative
));
772 * Open the directory and scan it...
775 if ((dir
= cupsDirOpen(directory
)) == NULL
)
780 while ((dent
= cupsDirRead(dir
)) != NULL
)
786 if (dent
->filename
[0] == '.')
790 * Get absolute and relative filenames...
793 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, dent
->filename
);
795 snprintf(relname
, sizeof(relname
), "%s/%s", relative
, dent
->filename
);
797 strlcpy(relname
, dent
->filename
, sizeof(relname
));
800 * Check if we have a HTML file...
803 if ((ext
= strstr(dent
->filename
, ".html")) != NULL
&&
804 (!ext
[5] || !strcmp(ext
+ 5, ".gz")))
807 * HTML file, see if we have already indexed the file...
810 if ((node
= helpFindNode(hi
, relname
, NULL
)) != NULL
)
813 * File already indexed - check dates to confirm that the
814 * index is up-to-date...
817 if (node
->mtime
== dent
->fileinfo
.st_mtime
)
820 * Same modification time, so mark all of the nodes
821 * for this file as up-to-date...
824 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
825 if (!strcmp(node
->filename
, relname
))
836 help_load_file(hi
, filename
, relname
, dent
->fileinfo
.st_mtime
);
838 else if (S_ISDIR(dent
->fileinfo
.st_mode
))
841 * Process sub-directory...
844 if (help_load_directory(hi
, filename
, relname
) == 1)
856 * 'help_load_file()' - Load a HTML files into an index.
859 static int /* O - 0 = success, -1 = error */
861 help_index_t
*hi
, /* I - Index */
862 const char *filename
, /* I - Filename */
863 const char *relative
, /* I - Relative path */
864 time_t mtime
) /* I - Modification time */
866 cups_file_t
*fp
; /* HTML file */
867 help_node_t
*node
; /* Current node */
868 char line
[1024], /* Line from file */
869 temp
[1024], /* Temporary word */
870 section
[1024], /* Section */
871 *ptr
, /* Pointer into line */
872 *anchor
, /* Anchor name */
873 *text
; /* Text for anchor */
874 off_t offset
; /* File offset */
875 char quote
; /* Quote character */
876 help_word_t
*word
; /* Current word */
877 int wordlen
; /* Length of word */
880 DEBUG_printf(("2help_load_file(hi=%p, filename=\"%s\", relative=\"%s\", "
881 "mtime=%ld)", hi
, filename
, relative
, (long)mtime
));
883 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
889 strlcpy(section
, "Other", sizeof(section
));
891 while (cupsFileGets(fp
, line
, sizeof(line
)))
894 * Look for "<TITLE>", "<A NAME", or "<!-- SECTION:" prefix...
897 if ((ptr
= strstr(line
, "<!-- SECTION:")) != NULL
)
900 * Got section line, copy it!
903 for (ptr
+= 13; isspace(*ptr
& 255); ptr
++);
905 strlcpy(section
, ptr
, sizeof(section
));
906 if ((ptr
= strstr(section
, "-->")) != NULL
)
909 * Strip comment stuff from end of line...
912 for (*ptr
-- = '\0'; ptr
> line
&& isspace(*ptr
& 255); *ptr
-- = '\0');
914 if (isspace(*ptr
& 255))
920 for (ptr
= line
; (ptr
= strchr(ptr
, '<')) != NULL
;)
924 if (!_cups_strncasecmp(ptr
, "TITLE>", 6))
935 char *idptr
; /* Pointer to ID */
937 if (!_cups_strncasecmp(ptr
, "A NAME=", 7))
939 else if ((idptr
= strstr(ptr
, " ID=")) != NULL
)
941 else if ((idptr
= strstr(ptr
, " id=")) != NULL
)
950 if (*ptr
== '\"' || *ptr
== '\'')
953 * Get quoted anchor...
958 if ((ptr
= strchr(anchor
, quote
)) != NULL
)
966 * Get unquoted anchor...
971 for (ptr
= anchor
; *ptr
&& *ptr
!= '>' && !isspace(*ptr
& 255); ptr
++);
980 * Got the anchor, now lets find the end...
983 while (*ptr
&& *ptr
!= '>')
993 * Now collect text for the link...
997 while ((ptr
= strchr(text
, '<')) == NULL
)
999 ptr
= text
+ strlen(text
);
1000 if (ptr
>= (line
+ sizeof(line
) - 2))
1005 if (!cupsFileGets(fp
, ptr
, sizeof(line
) - (size_t)(ptr
- line
) - 1))
1012 node
->length
= (size_t)(offset
- node
->offset
);
1020 if ((node
= helpFindNode(hi
, relative
, anchor
)) != NULL
)
1023 * Node already in the index, so replace the text and other
1027 cupsArrayRemove(hi
->nodes
, node
);
1030 free(node
->section
);
1037 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
1039 word
= (help_word_t
*)cupsArrayNext(node
->words
))
1040 help_delete_word(word
);
1042 cupsArrayDelete(node
->words
);
1046 node
->section
= section
[0] ? strdup(section
) : NULL
;
1047 node
->text
= strdup(text
);
1048 node
->mtime
= mtime
;
1049 node
->offset
= offset
;
1058 node
= help_new_node(relative
, anchor
, section
, text
, mtime
, offset
, 0);
1062 * Go through the text value and replace tabs and newlines with
1063 * whitespace and eliminate extra whitespace...
1066 for (ptr
= node
->text
, text
= node
->text
; *ptr
;)
1067 if (isspace(*ptr
& 255))
1069 while (isspace(*ptr
& 255))
1074 else if (text
!= ptr
)
1085 * (Re)add the node to the array...
1088 cupsArrayAdd(hi
->nodes
, node
);
1098 * Scan this line for words...
1101 for (ptr
= line
; *ptr
; ptr
++)
1104 * Skip HTML stuff...
1109 if (!strncmp(ptr
, "<!--", 4))
1112 * Skip HTML comment...
1115 if ((text
= strstr(ptr
+ 4, "-->")) == NULL
)
1116 ptr
+= strlen(ptr
) - 1;
1123 * Skip HTML element...
1126 for (ptr
++; *ptr
&& *ptr
!= '>'; ptr
++)
1128 if (*ptr
== '\"' || *ptr
== '\'')
1130 for (quote
= *ptr
++; *ptr
&& *ptr
!= quote
; ptr
++);
1143 else if (*ptr
== '&')
1146 * Skip HTML entity...
1149 for (ptr
++; *ptr
&& *ptr
!= ';'; ptr
++);
1156 else if (!isalnum(*ptr
& 255))
1160 * Found the start of a word, search until we find the end...
1163 for (text
= ptr
, ptr
++; *ptr
&& isalnum(*ptr
& 255); ptr
++);
1165 wordlen
= (int)(ptr
- text
);
1167 memcpy(temp
, text
, (size_t)wordlen
);
1168 temp
[wordlen
] = '\0';
1172 if (wordlen
> 1 && !bsearch(temp
, help_common_words
,
1173 (sizeof(help_common_words
) /
1174 sizeof(help_common_words
[0])),
1175 sizeof(help_common_words
[0]),
1176 (int (*)(const void *, const void *))
1178 help_add_word(node
, temp
);
1183 * Get the offset of the next line...
1186 offset
= cupsFileTell(fp
);
1192 node
->length
= (size_t)(offset
- node
->offset
);
1199 * 'help_new_node()' - Create a new node and add it to an index.
1202 static help_node_t
* /* O - Node pointer or NULL on error */
1203 help_new_node(const char *filename
, /* I - Filename */
1204 const char *anchor
, /* I - Anchor */
1205 const char *section
, /* I - Section */
1206 const char *text
, /* I - Text */
1207 time_t mtime
, /* I - Modification time */
1208 off_t offset
, /* I - Offset in file */
1209 size_t length
) /* I - Length in bytes */
1211 help_node_t
*n
; /* Node */
1214 DEBUG_printf(("2help_new_node(filename=\"%s\", anchor=\"%s\", text=\"%s\", "
1215 "mtime=%ld, offset=%ld, length=%ld)", filename
, anchor
, text
,
1216 (long)mtime
, (long)offset
, (long)length
));
1218 n
= (help_node_t
*)calloc(1, sizeof(help_node_t
));
1222 n
->filename
= strdup(filename
);
1223 n
->anchor
= anchor
? strdup(anchor
) : NULL
;
1224 n
->section
= *section
? strdup(section
) : NULL
;
1225 n
->text
= strdup(text
);
1235 * 'help_sort_nodes_by_name()' - Sort nodes by section, filename, and anchor.
1238 static int /* O - Difference */
1239 help_sort_by_name(help_node_t
*n1
, /* I - First node */
1240 help_node_t
*n2
) /* I - Second node */
1242 int diff
; /* Difference */
1245 DEBUG_printf(("2help_sort_by_name(n1=%p(%s#%s), n2=%p(%s#%s)",
1246 n1
, n1
->filename
, n1
->anchor
,
1247 n2
, n2
->filename
, n2
->anchor
));
1249 if ((diff
= strcmp(n1
->filename
, n2
->filename
)) != 0)
1252 if (!n1
->anchor
&& !n2
->anchor
)
1254 else if (!n1
->anchor
)
1256 else if (!n2
->anchor
)
1259 return (strcmp(n1
->anchor
, n2
->anchor
));
1264 * 'help_sort_nodes_by_score()' - Sort nodes by score and text.
1267 static int /* O - Difference */
1268 help_sort_by_score(help_node_t
*n1
, /* I - First node */
1269 help_node_t
*n2
) /* I - Second node */
1271 int diff
; /* Difference */
1274 DEBUG_printf(("2help_sort_by_score(n1=%p(%d \"%s\" \"%s\"), "
1275 "n2=%p(%d \"%s\" \"%s\")",
1276 n1
, n1
->score
, n1
->section
, n1
->text
,
1277 n2
, n2
->score
, n2
->section
, n2
->text
));
1279 if (n1
->score
!= n2
->score
)
1280 return (n2
->score
- n1
->score
);
1282 if (n1
->section
&& !n2
->section
)
1284 else if (!n1
->section
&& n2
->section
)
1286 else if (n1
->section
&& n2
->section
&&
1287 (diff
= strcmp(n1
->section
, n2
->section
)) != 0)
1290 return (_cups_strcasecmp(n1
->text
, n2
->text
));
1295 * 'help_sort_words()' - Sort words alphabetically.
1298 static int /* O - Difference */
1299 help_sort_words(help_word_t
*w1
, /* I - Second word */
1300 help_word_t
*w2
) /* I - Second word */
1302 DEBUG_printf(("2help_sort_words(w1=%p(\"%s\"), w2=%p(\"%s\"))",
1303 w1
, w1
->text
, w2
, w2
->text
));
1305 return (_cups_strcasecmp(w1
->text
, w2
->text
));