]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help-index.c
5f76d7aa55ab9993a89027d39dd77f261e5a6abc
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
, const char *section
, const char *text
, time_t mtime
, off_t offset
, size_t length
) _CUPS_NONNULL(1,3,4);
138 static int help_sort_by_name(help_node_t
*p1
, help_node_t
*p2
);
139 static int help_sort_by_score(help_node_t
*p1
, help_node_t
*p2
);
140 static int help_sort_words(help_word_t
*w1
, help_word_t
*w2
);
144 * 'helpDeleteIndex()' - Delete an index, freeing all memory used.
148 helpDeleteIndex(help_index_t
*hi
) /* I - Help index */
150 help_node_t
*node
; /* Current node */
153 DEBUG_printf(("helpDeleteIndex(hi=%p)", hi
));
158 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
160 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
163 help_delete_node(node
);
166 cupsArrayDelete(hi
->nodes
);
167 cupsArrayDelete(hi
->sorted
);
174 * 'helpFindNode()' - Find a node in an index.
177 help_node_t
* /* O - Node pointer or NULL */
178 helpFindNode(help_index_t
*hi
, /* I - Index */
179 const char *filename
, /* I - Filename */
180 const char *anchor
) /* I - Anchor */
182 help_node_t key
; /* Search key */
185 DEBUG_printf(("helpFindNode(hi=%p, filename=\"%s\", anchor=\"%s\")",
186 hi
, filename
, anchor
));
189 * Range check input...
192 if (!hi
|| !filename
)
196 * Initialize the search key...
199 key
.filename
= (char *)filename
;
200 key
.anchor
= (char *)anchor
;
203 * Return any match...
206 return ((help_node_t
*)cupsArrayFind(hi
->nodes
, &key
));
211 * 'helpLoadIndex()' - Load a help index from disk.
214 help_index_t
* /* O - Index pointer or NULL */
215 helpLoadIndex(const char *hifile
, /* I - Index filename */
216 const char *directory
) /* I - Directory that is indexed */
218 help_index_t
*hi
; /* Help index */
219 cups_file_t
*fp
; /* Current file */
220 char line
[2048], /* Line from file */
221 *ptr
, /* Pointer into line */
222 *filename
, /* Filename in line */
223 *anchor
, /* Anchor in line */
224 *sectptr
, /* Section pointer in line */
225 section
[1024], /* Section name */
226 *text
; /* Text in line */
227 time_t mtime
; /* Modification time */
228 off_t offset
; /* Offset into file */
229 size_t length
; /* Length in bytes */
230 int update
; /* Update? */
231 help_node_t
*node
; /* Current node */
232 help_word_t
*word
; /* Current word */
235 DEBUG_printf(("helpLoadIndex(hifile=\"%s\", directory=\"%s\")",
239 * Create a new, empty index.
242 if ((hi
= (help_index_t
*)calloc(1, sizeof(help_index_t
))) == NULL
)
245 hi
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
246 hi
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
248 if (!hi
->nodes
|| !hi
->sorted
)
250 cupsArrayDelete(hi
->nodes
);
251 cupsArrayDelete(hi
->sorted
);
257 * Try loading the existing index file...
260 if ((fp
= cupsFileOpen(hifile
, "r")) != NULL
)
263 * Lock the file and then read the first line...
268 if (cupsFileGets(fp
, line
, sizeof(line
)) && !strcmp(line
, "HELPV2"))
271 * Got a valid header line, now read the data lines...
276 while (cupsFileGets(fp
, line
, sizeof(line
)))
279 * Each line looks like one of the following:
281 * filename mtime offset length "section" "text"
282 * filename#anchor offset length "text"
289 * Read a word in the current node...
292 if (!node
|| (ptr
= strrchr(line
, ' ')) == NULL
)
295 if ((word
= help_add_word(node
, ptr
+ 1)) != NULL
)
296 word
->count
= atoi(line
+ 1);
306 if ((ptr
= strchr(line
, ' ')) == NULL
)
309 while (isspace(*ptr
& 255))
312 if ((anchor
= strrchr(filename
, '#')) != NULL
)
318 mtime
= strtol(ptr
, &ptr
, 10);
320 offset
= strtoll(ptr
, &ptr
, 10);
321 length
= (size_t)strtoll(ptr
, &ptr
, 10);
323 while (isspace(*ptr
& 255))
338 while (*ptr
&& *ptr
!= '\"')
346 strlcpy(section
, sectptr
, sizeof(section
));
348 while (isspace(*ptr
& 255))
358 while (*ptr
&& *ptr
!= '\"')
366 if ((node
= help_new_node(filename
, anchor
, section
, text
,
367 mtime
, offset
, length
)) == NULL
)
372 cupsArrayAdd(hi
->nodes
, node
);
381 * Scan for new/updated files...
384 update
= help_load_directory(hi
, directory
, NULL
);
387 * Remove any files that are no longer installed...
390 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
392 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
396 * Delete this node...
399 cupsArrayRemove(hi
->nodes
, node
);
400 help_delete_node(node
);
404 * Add nodes to the sorted array...
407 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
409 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
410 cupsArrayAdd(hi
->sorted
, node
);
413 * Save the index if we updated it...
417 helpSaveIndex(hi
, hifile
);
420 * Return the index...
428 * 'helpSaveIndex()' - Save a help index to disk.
431 int /* O - 0 on success, -1 on error */
432 helpSaveIndex(help_index_t
*hi
, /* I - Index */
433 const char *hifile
) /* I - Index filename */
435 cups_file_t
*fp
; /* Index file */
436 help_node_t
*node
; /* Current node */
437 help_word_t
*word
; /* Current word */
440 DEBUG_printf(("helpSaveIndex(hi=%p, hifile=\"%s\")", hi
, hifile
));
443 * Try creating a new index file...
446 if ((fp
= cupsFileOpen(hifile
, "w9")) == NULL
)
450 * Lock the file while we write it...
455 cupsFilePuts(fp
, "HELPV2\n");
457 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
459 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
462 * Write the current node with/without the anchor...
467 if (cupsFilePrintf(fp
, "%s#%s " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\"\n",
468 node
->filename
, node
->anchor
,
469 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
475 if (cupsFilePrintf(fp
, "%s %d " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\" \"%s\"\n",
476 node
->filename
, (int)node
->mtime
,
477 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
478 node
->section
? node
->section
: "", node
->text
) < 0)
483 * Then write the words associated with the node...
486 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
488 word
= (help_word_t
*)cupsArrayNext(node
->words
))
489 if (cupsFilePrintf(fp
, " %d %s\n", word
->count
, word
->text
) < 0)
495 if (cupsFileClose(fp
) < 0)
505 * 'helpSearchIndex()' - Search an index.
508 help_index_t
* /* O - Search index */
509 helpSearchIndex(help_index_t
*hi
, /* I - Index */
510 const char *query
, /* I - Query string */
511 const char *section
, /* I - Limit search to this section */
512 const char *filename
) /* I - Limit search to this file */
514 help_index_t
*search
; /* Search index */
515 help_node_t
*node
; /* Current node */
516 help_word_t
*word
; /* Current word */
517 void *sc
; /* Search context */
518 int matches
; /* Number of matches */
521 DEBUG_printf(("helpSearchIndex(hi=%p, query=\"%s\", filename=\"%s\")",
522 hi
, query
, filename
));
532 * Reset the scores of all nodes to 0...
535 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
537 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
541 * Find the first node to search in...
546 node
= helpFindNode(hi
, filename
, NULL
);
551 node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
554 * Convert the query into a regular expression...
557 sc
= cgiCompileSearch(query
);
562 * Allocate a search index...
565 search
= calloc(1, sizeof(help_index_t
));
572 search
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
573 search
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
575 if (!search
->nodes
|| !search
->sorted
)
577 cupsArrayDelete(search
->nodes
);
578 cupsArrayDelete(search
->sorted
);
587 * Check each node in the index, adding matching nodes to the
591 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
592 if (section
&& strcmp(node
->section
, section
))
594 else if (filename
&& strcmp(node
->filename
, filename
))
598 matches
= cgiDoSearch(sc
, node
->text
);
600 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
602 word
= (help_word_t
*)cupsArrayNext(node
->words
))
603 if (cgiDoSearch(sc
, word
->text
) > 0)
604 matches
+= word
->count
;
609 * Found a match, add the node to the search index...
612 node
->score
= matches
;
614 cupsArrayAdd(search
->nodes
, node
);
615 cupsArrayAdd(search
->sorted
, node
);
620 * Free the search context...
626 * Return the results...
634 * 'help_add_word()' - Add a word to a node.
637 static help_word_t
* /* O - New word */
638 help_add_word(help_node_t
*n
, /* I - Node */
639 const char *text
) /* I - Word text */
641 help_word_t
*w
, /* New word */
642 key
; /* Search key */
645 DEBUG_printf(("2help_add_word(n=%p, text=\"%s\")", n
, text
));
648 * Create the words array as needed...
652 n
->words
= cupsArrayNew((cups_array_func_t
)help_sort_words
, NULL
);
655 * See if the word is already added...
658 key
.text
= (char *)text
;
660 if ((w
= (help_word_t
*)cupsArrayFind(n
->words
, &key
)) == NULL
)
663 * Create a new word...
666 if ((w
= calloc(1, sizeof(help_word_t
))) == NULL
)
669 if ((w
->text
= strdup(text
)) == NULL
)
675 cupsArrayAdd(n
->words
, w
);
679 * Bump the counter for this word and return it...
689 * 'help_delete_node()' - Free all memory used by a node.
693 help_delete_node(help_node_t
*n
) /* I - Node */
695 help_word_t
*w
; /* Current word */
698 DEBUG_printf(("2help_delete_node(n=%p)", n
));
715 for (w
= (help_word_t
*)cupsArrayFirst(n
->words
);
717 w
= (help_word_t
*)cupsArrayNext(n
->words
))
720 cupsArrayDelete(n
->words
);
727 * 'help_delete_word()' - Free all memory used by a word.
731 help_delete_word(help_word_t
*w
) /* I - Word */
733 DEBUG_printf(("2help_delete_word(w=%p)", w
));
746 * 'help_load_directory()' - Load a directory of files into an index.
749 static int /* O - 0 = success, -1 = error, 1 = updated */
751 help_index_t
*hi
, /* I - Index */
752 const char *directory
, /* I - Directory */
753 const char *relative
) /* I - Relative path */
755 cups_dir_t
*dir
; /* Directory file */
756 cups_dentry_t
*dent
; /* Directory entry */
757 char *ext
, /* Pointer to extension */
758 filename
[1024], /* Full filename */
759 relname
[1024]; /* Relative filename */
760 int update
; /* Updated? */
761 help_node_t
*node
; /* Current node */
764 DEBUG_printf(("2help_load_directory(hi=%p, directory=\"%s\", relative=\"%s\")",
765 hi
, directory
, relative
));
768 * Open the directory and scan it...
771 if ((dir
= cupsDirOpen(directory
)) == NULL
)
776 while ((dent
= cupsDirRead(dir
)) != NULL
)
782 if (dent
->filename
[0] == '.')
786 * Get absolute and relative filenames...
789 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, dent
->filename
);
791 snprintf(relname
, sizeof(relname
), "%s/%s", relative
, dent
->filename
);
793 strlcpy(relname
, dent
->filename
, sizeof(relname
));
796 * Check if we have a HTML file...
799 if ((ext
= strstr(dent
->filename
, ".html")) != NULL
&&
800 (!ext
[5] || !strcmp(ext
+ 5, ".gz")))
803 * HTML file, see if we have already indexed the file...
806 if ((node
= helpFindNode(hi
, relname
, NULL
)) != NULL
)
809 * File already indexed - check dates to confirm that the
810 * index is up-to-date...
813 if (node
->mtime
== dent
->fileinfo
.st_mtime
)
816 * Same modification time, so mark all of the nodes
817 * for this file as up-to-date...
820 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
821 if (!strcmp(node
->filename
, relname
))
832 help_load_file(hi
, filename
, relname
, dent
->fileinfo
.st_mtime
);
834 else if (S_ISDIR(dent
->fileinfo
.st_mode
))
837 * Process sub-directory...
840 if (help_load_directory(hi
, filename
, relname
) == 1)
852 * 'help_load_file()' - Load a HTML files into an index.
855 static int /* O - 0 = success, -1 = error */
857 help_index_t
*hi
, /* I - Index */
858 const char *filename
, /* I - Filename */
859 const char *relative
, /* I - Relative path */
860 time_t mtime
) /* I - Modification time */
862 cups_file_t
*fp
; /* HTML file */
863 help_node_t
*node
; /* Current node */
864 char line
[1024], /* Line from file */
865 temp
[1024], /* Temporary word */
866 section
[1024], /* Section */
867 *ptr
, /* Pointer into line */
868 *anchor
, /* Anchor name */
869 *text
; /* Text for anchor */
870 off_t offset
; /* File offset */
871 char quote
; /* Quote character */
872 help_word_t
*word
; /* Current word */
873 int wordlen
; /* Length of word */
876 DEBUG_printf(("2help_load_file(hi=%p, filename=\"%s\", relative=\"%s\", "
877 "mtime=%ld)", hi
, filename
, relative
, (long)mtime
));
879 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
885 strlcpy(section
, "Other", sizeof(section
));
887 while (cupsFileGets(fp
, line
, sizeof(line
)))
890 * Look for "<TITLE>", "<A NAME", or "<!-- SECTION:" prefix...
893 if ((ptr
= strstr(line
, "<!-- SECTION:")) != NULL
)
896 * Got section line, copy it!
899 for (ptr
+= 13; isspace(*ptr
& 255); ptr
++);
901 strlcpy(section
, ptr
, sizeof(section
));
902 if ((ptr
= strstr(section
, "-->")) != NULL
)
905 * Strip comment stuff from end of line...
908 for (*ptr
-- = '\0'; ptr
> line
&& isspace(*ptr
& 255); *ptr
-- = '\0');
910 if (isspace(*ptr
& 255))
916 for (ptr
= line
; (ptr
= strchr(ptr
, '<')) != NULL
;)
920 if (!_cups_strncasecmp(ptr
, "TITLE>", 6))
931 char *idptr
; /* Pointer to ID */
933 if (!_cups_strncasecmp(ptr
, "A NAME=", 7))
935 else if ((idptr
= strstr(ptr
, " ID=")) != NULL
)
937 else if ((idptr
= strstr(ptr
, " id=")) != NULL
)
946 if (*ptr
== '\"' || *ptr
== '\'')
949 * Get quoted anchor...
954 if ((ptr
= strchr(anchor
, quote
)) != NULL
)
962 * Get unquoted anchor...
967 for (ptr
= anchor
; *ptr
&& *ptr
!= '>' && !isspace(*ptr
& 255); ptr
++);
976 * Got the anchor, now lets find the end...
979 while (*ptr
&& *ptr
!= '>')
989 * Now collect text for the link...
993 while ((ptr
= strchr(text
, '<')) == NULL
)
995 ptr
= text
+ strlen(text
);
996 if (ptr
>= (line
+ sizeof(line
) - 2))
1001 if (!cupsFileGets(fp
, ptr
, sizeof(line
) - (size_t)(ptr
- line
) - 1))
1008 node
->length
= (size_t)(offset
- node
->offset
);
1016 if ((node
= helpFindNode(hi
, relative
, anchor
)) != NULL
)
1019 * Node already in the index, so replace the text and other
1023 cupsArrayRemove(hi
->nodes
, node
);
1026 free(node
->section
);
1033 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
1035 word
= (help_word_t
*)cupsArrayNext(node
->words
))
1036 help_delete_word(word
);
1038 cupsArrayDelete(node
->words
);
1042 node
->section
= section
[0] ? strdup(section
) : NULL
;
1043 node
->text
= strdup(text
);
1044 node
->mtime
= mtime
;
1045 node
->offset
= offset
;
1054 node
= help_new_node(relative
, anchor
, section
, text
, mtime
, offset
, 0);
1058 * Go through the text value and replace tabs and newlines with
1059 * whitespace and eliminate extra whitespace...
1062 for (ptr
= node
->text
, text
= node
->text
; *ptr
;)
1063 if (isspace(*ptr
& 255))
1065 while (isspace(*ptr
& 255))
1070 else if (text
!= ptr
)
1081 * (Re)add the node to the array...
1084 cupsArrayAdd(hi
->nodes
, node
);
1094 * Scan this line for words...
1097 for (ptr
= line
; *ptr
; ptr
++)
1100 * Skip HTML stuff...
1105 if (!strncmp(ptr
, "<!--", 4))
1108 * Skip HTML comment...
1111 if ((text
= strstr(ptr
+ 4, "-->")) == NULL
)
1112 ptr
+= strlen(ptr
) - 1;
1119 * Skip HTML element...
1122 for (ptr
++; *ptr
&& *ptr
!= '>'; ptr
++)
1124 if (*ptr
== '\"' || *ptr
== '\'')
1126 for (quote
= *ptr
++; *ptr
&& *ptr
!= quote
; ptr
++);
1139 else if (*ptr
== '&')
1142 * Skip HTML entity...
1145 for (ptr
++; *ptr
&& *ptr
!= ';'; ptr
++);
1152 else if (!isalnum(*ptr
& 255))
1156 * Found the start of a word, search until we find the end...
1159 for (text
= ptr
, ptr
++; *ptr
&& isalnum(*ptr
& 255); ptr
++);
1161 wordlen
= (int)(ptr
- text
);
1163 memcpy(temp
, text
, (size_t)wordlen
);
1164 temp
[wordlen
] = '\0';
1168 if (wordlen
> 1 && !bsearch(temp
, help_common_words
,
1169 (sizeof(help_common_words
) /
1170 sizeof(help_common_words
[0])),
1171 sizeof(help_common_words
[0]),
1172 (int (*)(const void *, const void *))
1174 help_add_word(node
, temp
);
1179 * Get the offset of the next line...
1182 offset
= cupsFileTell(fp
);
1188 node
->length
= (size_t)(offset
- node
->offset
);
1195 * 'help_new_node()' - Create a new node and add it to an index.
1198 static help_node_t
* /* O - Node pointer or NULL on error */
1199 help_new_node(const char *filename
, /* I - Filename */
1200 const char *anchor
, /* I - Anchor */
1201 const char *section
, /* I - Section */
1202 const char *text
, /* I - Text */
1203 time_t mtime
, /* I - Modification time */
1204 off_t offset
, /* I - Offset in file */
1205 size_t length
) /* I - Length in bytes */
1207 help_node_t
*n
; /* Node */
1210 DEBUG_printf(("2help_new_node(filename=\"%s\", anchor=\"%s\", text=\"%s\", "
1211 "mtime=%ld, offset=%ld, length=%ld)", filename
, anchor
, text
,
1212 (long)mtime
, (long)offset
, (long)length
));
1214 n
= (help_node_t
*)calloc(1, sizeof(help_node_t
));
1218 n
->filename
= strdup(filename
);
1219 n
->anchor
= anchor
? strdup(anchor
) : NULL
;
1220 n
->section
= *section
? strdup(section
) : NULL
;
1221 n
->text
= strdup(text
);
1231 * 'help_sort_nodes_by_name()' - Sort nodes by section, filename, and anchor.
1234 static int /* O - Difference */
1235 help_sort_by_name(help_node_t
*n1
, /* I - First node */
1236 help_node_t
*n2
) /* I - Second node */
1238 int diff
; /* Difference */
1241 DEBUG_printf(("2help_sort_by_name(n1=%p(%s#%s), n2=%p(%s#%s)",
1242 n1
, n1
->filename
, n1
->anchor
,
1243 n2
, n2
->filename
, n2
->anchor
));
1245 if ((diff
= strcmp(n1
->filename
, n2
->filename
)) != 0)
1248 if (!n1
->anchor
&& !n2
->anchor
)
1250 else if (!n1
->anchor
)
1252 else if (!n2
->anchor
)
1255 return (strcmp(n1
->anchor
, n2
->anchor
));
1260 * 'help_sort_nodes_by_score()' - Sort nodes by score and text.
1263 static int /* O - Difference */
1264 help_sort_by_score(help_node_t
*n1
, /* I - First node */
1265 help_node_t
*n2
) /* I - Second node */
1267 int diff
; /* Difference */
1270 DEBUG_printf(("2help_sort_by_score(n1=%p(%d \"%s\" \"%s\"), "
1271 "n2=%p(%d \"%s\" \"%s\")",
1272 n1
, n1
->score
, n1
->section
, n1
->text
,
1273 n2
, n2
->score
, n2
->section
, n2
->text
));
1275 if (n1
->score
!= n2
->score
)
1276 return (n2
->score
- n1
->score
);
1278 if (n1
->section
&& !n2
->section
)
1280 else if (!n1
->section
&& n2
->section
)
1282 else if (n1
->section
&& n2
->section
&&
1283 (diff
= strcmp(n1
->section
, n2
->section
)) != 0)
1286 return (_cups_strcasecmp(n1
->text
, n2
->text
));
1291 * 'help_sort_words()' - Sort words alphabetically.
1294 static int /* O - Difference */
1295 help_sort_words(help_word_t
*w1
, /* I - Second word */
1296 help_word_t
*w2
) /* I - Second word */
1298 DEBUG_printf(("2help_sort_words(w1=%p(\"%s\"), w2=%p(\"%s\"))",
1299 w1
, w1
->text
, w2
, w2
->text
));
1301 return (_cups_strcasecmp(w1
->text
, w2
->text
));