]>
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
, 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 */
156 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
158 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
161 help_delete_node(node
);
164 cupsArrayDelete(hi
->nodes
);
165 cupsArrayDelete(hi
->sorted
);
172 * 'helpFindNode()' - Find a node in an index.
175 help_node_t
* /* O - Node pointer or NULL */
176 helpFindNode(help_index_t
*hi
, /* I - Index */
177 const char *filename
, /* I - Filename */
178 const char *anchor
) /* I - Anchor */
180 help_node_t key
; /* Search key */
184 * Range check input...
187 if (!hi
|| !filename
)
191 * Initialize the search key...
194 key
.filename
= (char *)filename
;
195 key
.anchor
= (char *)anchor
;
198 * Return any match...
201 return ((help_node_t
*)cupsArrayFind(hi
->nodes
, &key
));
206 * 'helpLoadIndex()' - Load a help index from disk.
209 help_index_t
* /* O - Index pointer or NULL */
210 helpLoadIndex(const char *hifile
, /* I - Index filename */
211 const char *directory
) /* I - Directory that is indexed */
213 help_index_t
*hi
; /* Help index */
214 cups_file_t
*fp
; /* Current file */
215 char line
[2048], /* Line from file */
216 *ptr
, /* Pointer into line */
217 *filename
, /* Filename in line */
218 *anchor
, /* Anchor in line */
219 *sectptr
, /* Section pointer in line */
220 section
[1024], /* Section name */
221 *text
; /* Text in line */
222 time_t mtime
; /* Modification time */
223 off_t offset
; /* Offset into file */
224 size_t length
; /* Length in bytes */
225 int update
; /* Update? */
226 help_node_t
*node
; /* Current node */
227 help_word_t
*word
; /* Current word */
231 * Create a new, empty index.
234 if ((hi
= (help_index_t
*)calloc(1, sizeof(help_index_t
))) == NULL
)
237 hi
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
238 hi
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
240 if (!hi
->nodes
|| !hi
->sorted
)
242 cupsArrayDelete(hi
->nodes
);
243 cupsArrayDelete(hi
->sorted
);
249 * Try loading the existing index file...
252 if ((fp
= cupsFileOpen(hifile
, "r")) != NULL
)
255 * Lock the file and then read the first line...
260 if (cupsFileGets(fp
, line
, sizeof(line
)) && !strcmp(line
, "HELPV2"))
263 * Got a valid header line, now read the data lines...
268 while (cupsFileGets(fp
, line
, sizeof(line
)))
271 * Each line looks like one of the following:
273 * filename mtime offset length "section" "text"
274 * filename#anchor offset length "text"
281 * Read a word in the current node...
284 if (!node
|| (ptr
= strrchr(line
, ' ')) == NULL
)
287 if ((word
= help_add_word(node
, ptr
+ 1)) != NULL
)
288 word
->count
= atoi(line
+ 1);
298 if ((ptr
= strchr(line
, ' ')) == NULL
)
301 while (isspace(*ptr
& 255))
304 if ((anchor
= strrchr(filename
, '#')) != NULL
)
310 mtime
= strtol(ptr
, &ptr
, 10);
312 offset
= strtoll(ptr
, &ptr
, 10);
313 length
= (size_t)strtoll(ptr
, &ptr
, 10);
315 while (isspace(*ptr
& 255))
330 while (*ptr
&& *ptr
!= '\"')
338 strlcpy(section
, sectptr
, sizeof(section
));
340 while (isspace(*ptr
& 255))
350 while (*ptr
&& *ptr
!= '\"')
358 if ((node
= help_new_node(filename
, anchor
, section
, text
,
359 mtime
, offset
, length
)) == NULL
)
364 cupsArrayAdd(hi
->nodes
, node
);
373 * Scan for new/updated files...
376 update
= help_load_directory(hi
, directory
, NULL
);
379 * Remove any files that are no longer installed...
382 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
384 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
388 * Delete this node...
391 cupsArrayRemove(hi
->nodes
, node
);
392 help_delete_node(node
);
396 * Add nodes to the sorted array...
399 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
401 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
402 cupsArrayAdd(hi
->sorted
, node
);
405 * Save the index if we updated it...
409 helpSaveIndex(hi
, hifile
);
412 * Return the index...
420 * 'helpSaveIndex()' - Save a help index to disk.
423 int /* O - 0 on success, -1 on error */
424 helpSaveIndex(help_index_t
*hi
, /* I - Index */
425 const char *hifile
) /* I - Index filename */
427 cups_file_t
*fp
; /* Index file */
428 help_node_t
*node
; /* Current node */
429 help_word_t
*word
; /* Current word */
433 * Try creating a new index file...
436 if ((fp
= cupsFileOpen(hifile
, "w9")) == NULL
)
440 * Lock the file while we write it...
445 cupsFilePuts(fp
, "HELPV2\n");
447 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
449 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
452 * Write the current node with/without the anchor...
457 if (cupsFilePrintf(fp
, "%s#%s " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\"\n",
458 node
->filename
, node
->anchor
,
459 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
465 if (cupsFilePrintf(fp
, "%s %d " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\" \"%s\"\n",
466 node
->filename
, (int)node
->mtime
,
467 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
468 node
->section
? node
->section
: "", node
->text
) < 0)
473 * Then write the words associated with the node...
476 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
478 word
= (help_word_t
*)cupsArrayNext(node
->words
))
479 if (cupsFilePrintf(fp
, " %d %s\n", word
->count
, word
->text
) < 0)
485 if (cupsFileClose(fp
) < 0)
495 * 'helpSearchIndex()' - Search an index.
498 help_index_t
* /* O - Search index */
499 helpSearchIndex(help_index_t
*hi
, /* I - Index */
500 const char *query
, /* I - Query string */
501 const char *section
, /* I - Limit search to this section */
502 const char *filename
) /* I - Limit search to this file */
504 help_index_t
*search
; /* Search index */
505 help_node_t
*node
; /* Current node */
506 help_word_t
*word
; /* Current word */
507 void *sc
; /* Search context */
508 int matches
; /* Number of matches */
519 * Reset the scores of all nodes to 0...
522 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
524 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
528 * Find the first node to search in...
533 node
= helpFindNode(hi
, filename
, NULL
);
538 node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
541 * Convert the query into a regular expression...
544 sc
= cgiCompileSearch(query
);
549 * Allocate a search index...
552 search
= calloc(1, sizeof(help_index_t
));
559 search
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
560 search
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
562 if (!search
->nodes
|| !search
->sorted
)
564 cupsArrayDelete(search
->nodes
);
565 cupsArrayDelete(search
->sorted
);
574 * Check each node in the index, adding matching nodes to the
578 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
579 if (section
&& strcmp(node
->section
, section
))
581 else if (filename
&& strcmp(node
->filename
, filename
))
585 matches
= cgiDoSearch(sc
, node
->text
);
587 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
589 word
= (help_word_t
*)cupsArrayNext(node
->words
))
590 if (cgiDoSearch(sc
, word
->text
) > 0)
591 matches
+= word
->count
;
596 * Found a match, add the node to the search index...
599 node
->score
= matches
;
601 cupsArrayAdd(search
->nodes
, node
);
602 cupsArrayAdd(search
->sorted
, node
);
607 * Free the search context...
613 * Return the results...
621 * 'help_add_word()' - Add a word to a node.
624 static help_word_t
* /* O - New word */
625 help_add_word(help_node_t
*n
, /* I - Node */
626 const char *text
) /* I - Word text */
628 help_word_t
*w
, /* New word */
629 key
; /* Search key */
633 * Create the words array as needed...
637 n
->words
= cupsArrayNew((cups_array_func_t
)help_sort_words
, NULL
);
640 * See if the word is already added...
643 key
.text
= (char *)text
;
645 if ((w
= (help_word_t
*)cupsArrayFind(n
->words
, &key
)) == NULL
)
648 * Create a new word...
651 if ((w
= calloc(1, sizeof(help_word_t
))) == NULL
)
654 if ((w
->text
= strdup(text
)) == NULL
)
660 cupsArrayAdd(n
->words
, w
);
664 * Bump the counter for this word and return it...
674 * 'help_delete_node()' - Free all memory used by a node.
678 help_delete_node(help_node_t
*n
) /* I - Node */
680 help_word_t
*w
; /* Current word */
698 for (w
= (help_word_t
*)cupsArrayFirst(n
->words
);
700 w
= (help_word_t
*)cupsArrayNext(n
->words
))
703 cupsArrayDelete(n
->words
);
710 * 'help_delete_word()' - Free all memory used by a word.
714 help_delete_word(help_word_t
*w
) /* I - Word */
727 * 'help_load_directory()' - Load a directory of files into an index.
730 static int /* O - 0 = success, -1 = error, 1 = updated */
732 help_index_t
*hi
, /* I - Index */
733 const char *directory
, /* I - Directory */
734 const char *relative
) /* I - Relative path */
736 cups_dir_t
*dir
; /* Directory file */
737 cups_dentry_t
*dent
; /* Directory entry */
738 char *ext
, /* Pointer to extension */
739 filename
[1024], /* Full filename */
740 relname
[1024]; /* Relative filename */
741 int update
; /* Updated? */
742 help_node_t
*node
; /* Current node */
746 * Open the directory and scan it...
749 if ((dir
= cupsDirOpen(directory
)) == NULL
)
754 while ((dent
= cupsDirRead(dir
)) != NULL
)
760 if (dent
->filename
[0] == '.')
764 * Get absolute and relative filenames...
767 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, dent
->filename
);
769 snprintf(relname
, sizeof(relname
), "%s/%s", relative
, dent
->filename
);
771 strlcpy(relname
, dent
->filename
, sizeof(relname
));
774 * Check if we have a HTML file...
777 if ((ext
= strstr(dent
->filename
, ".html")) != NULL
&&
778 (!ext
[5] || !strcmp(ext
+ 5, ".gz")))
781 * HTML file, see if we have already indexed the file...
784 if ((node
= helpFindNode(hi
, relname
, NULL
)) != NULL
)
787 * File already indexed - check dates to confirm that the
788 * index is up-to-date...
791 if (node
->mtime
== dent
->fileinfo
.st_mtime
)
794 * Same modification time, so mark all of the nodes
795 * for this file as up-to-date...
798 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
799 if (!strcmp(node
->filename
, relname
))
810 help_load_file(hi
, filename
, relname
, dent
->fileinfo
.st_mtime
);
812 else if (S_ISDIR(dent
->fileinfo
.st_mode
))
815 * Process sub-directory...
818 if (help_load_directory(hi
, filename
, relname
) == 1)
830 * 'help_load_file()' - Load a HTML files into an index.
833 static int /* O - 0 = success, -1 = error */
835 help_index_t
*hi
, /* I - Index */
836 const char *filename
, /* I - Filename */
837 const char *relative
, /* I - Relative path */
838 time_t mtime
) /* I - Modification time */
840 cups_file_t
*fp
; /* HTML file */
841 help_node_t
*node
; /* Current node */
842 char line
[1024], /* Line from file */
843 temp
[1024], /* Temporary word */
844 section
[1024], /* Section */
845 *ptr
, /* Pointer into line */
846 *anchor
, /* Anchor name */
847 *text
; /* Text for anchor */
848 off_t offset
; /* File offset */
849 char quote
; /* Quote character */
850 help_word_t
*word
; /* Current word */
851 int wordlen
; /* Length of word */
854 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
860 strlcpy(section
, "Other", sizeof(section
));
862 while (cupsFileGets(fp
, line
, sizeof(line
)))
865 * Look for "<TITLE>", "<A NAME", or "<!-- SECTION:" prefix...
868 if ((ptr
= strstr(line
, "<!-- SECTION:")) != NULL
)
871 * Got section line, copy it!
874 for (ptr
+= 13; isspace(*ptr
& 255); ptr
++);
876 strlcpy(section
, ptr
, sizeof(section
));
877 if ((ptr
= strstr(section
, "-->")) != NULL
)
880 * Strip comment stuff from end of line...
883 for (*ptr
-- = '\0'; ptr
> line
&& isspace(*ptr
& 255); *ptr
-- = '\0');
885 if (isspace(*ptr
& 255))
891 for (ptr
= line
; (ptr
= strchr(ptr
, '<')) != NULL
;)
895 if (!_cups_strncasecmp(ptr
, "TITLE>", 6))
906 char *idptr
; /* Pointer to ID */
908 if (!_cups_strncasecmp(ptr
, "A NAME=", 7))
910 else if ((idptr
= strstr(ptr
, " ID=")) != NULL
)
912 else if ((idptr
= strstr(ptr
, " id=")) != NULL
)
921 if (*ptr
== '\"' || *ptr
== '\'')
924 * Get quoted anchor...
929 if ((ptr
= strchr(anchor
, quote
)) != NULL
)
937 * Get unquoted anchor...
942 for (ptr
= anchor
; *ptr
&& *ptr
!= '>' && !isspace(*ptr
& 255); ptr
++);
951 * Got the anchor, now lets find the end...
954 while (*ptr
&& *ptr
!= '>')
964 * Now collect text for the link...
968 while ((ptr
= strchr(text
, '<')) == NULL
)
970 ptr
= text
+ strlen(text
);
971 if (ptr
>= (line
+ sizeof(line
) - 2))
976 if (!cupsFileGets(fp
, ptr
, sizeof(line
) - (size_t)(ptr
- line
) - 1))
983 node
->length
= (size_t)(offset
- node
->offset
);
991 if ((node
= helpFindNode(hi
, relative
, anchor
)) != NULL
)
994 * Node already in the index, so replace the text and other
998 cupsArrayRemove(hi
->nodes
, node
);
1001 free(node
->section
);
1008 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
1010 word
= (help_word_t
*)cupsArrayNext(node
->words
))
1011 help_delete_word(word
);
1013 cupsArrayDelete(node
->words
);
1017 node
->section
= section
[0] ? strdup(section
) : NULL
;
1018 node
->text
= strdup(text
);
1019 node
->mtime
= mtime
;
1020 node
->offset
= offset
;
1029 node
= help_new_node(relative
, anchor
, section
, text
, mtime
, offset
, 0);
1033 * Go through the text value and replace tabs and newlines with
1034 * whitespace and eliminate extra whitespace...
1037 for (ptr
= node
->text
, text
= node
->text
; *ptr
;)
1038 if (isspace(*ptr
& 255))
1040 while (isspace(*ptr
& 255))
1045 else if (text
!= ptr
)
1056 * (Re)add the node to the array...
1059 cupsArrayAdd(hi
->nodes
, node
);
1069 * Scan this line for words...
1072 for (ptr
= line
; *ptr
; ptr
++)
1075 * Skip HTML stuff...
1080 if (!strncmp(ptr
, "<!--", 4))
1083 * Skip HTML comment...
1086 if ((text
= strstr(ptr
+ 4, "-->")) == NULL
)
1087 ptr
+= strlen(ptr
) - 1;
1094 * Skip HTML element...
1097 for (ptr
++; *ptr
&& *ptr
!= '>'; ptr
++)
1099 if (*ptr
== '\"' || *ptr
== '\'')
1101 for (quote
= *ptr
++; *ptr
&& *ptr
!= quote
; ptr
++);
1114 else if (*ptr
== '&')
1117 * Skip HTML entity...
1120 for (ptr
++; *ptr
&& *ptr
!= ';'; ptr
++);
1127 else if (!isalnum(*ptr
& 255))
1131 * Found the start of a word, search until we find the end...
1134 for (text
= ptr
, ptr
++; *ptr
&& isalnum(*ptr
& 255); ptr
++);
1136 wordlen
= (int)(ptr
- text
);
1138 memcpy(temp
, text
, (size_t)wordlen
);
1139 temp
[wordlen
] = '\0';
1143 if (wordlen
> 1 && !bsearch(temp
, help_common_words
,
1144 (sizeof(help_common_words
) /
1145 sizeof(help_common_words
[0])),
1146 sizeof(help_common_words
[0]),
1147 (int (*)(const void *, const void *))
1149 help_add_word(node
, temp
);
1154 * Get the offset of the next line...
1157 offset
= cupsFileTell(fp
);
1163 node
->length
= (size_t)(offset
- node
->offset
);
1170 * 'help_new_node()' - Create a new node and add it to an index.
1173 static help_node_t
* /* O - Node pointer or NULL on error */
1174 help_new_node(const char *filename
, /* I - Filename */
1175 const char *anchor
, /* I - Anchor */
1176 const char *section
, /* I - Section */
1177 const char *text
, /* I - Text */
1178 time_t mtime
, /* I - Modification time */
1179 off_t offset
, /* I - Offset in file */
1180 size_t length
) /* I - Length in bytes */
1182 help_node_t
*n
; /* Node */
1185 n
= (help_node_t
*)calloc(1, sizeof(help_node_t
));
1189 n
->filename
= strdup(filename
);
1190 n
->anchor
= anchor
? strdup(anchor
) : NULL
;
1191 n
->section
= *section
? strdup(section
) : NULL
;
1192 n
->text
= strdup(text
);
1202 * 'help_sort_nodes_by_name()' - Sort nodes by section, filename, and anchor.
1205 static int /* O - Difference */
1206 help_sort_by_name(help_node_t
*n1
, /* I - First node */
1207 help_node_t
*n2
) /* I - Second node */
1209 int diff
; /* Difference */
1212 if ((diff
= strcmp(n1
->filename
, n2
->filename
)) != 0)
1215 if (!n1
->anchor
&& !n2
->anchor
)
1217 else if (!n1
->anchor
)
1219 else if (!n2
->anchor
)
1222 return (strcmp(n1
->anchor
, n2
->anchor
));
1227 * 'help_sort_nodes_by_score()' - Sort nodes by score and text.
1230 static int /* O - Difference */
1231 help_sort_by_score(help_node_t
*n1
, /* I - First node */
1232 help_node_t
*n2
) /* I - Second node */
1234 int diff
; /* Difference */
1237 if (n1
->score
!= n2
->score
)
1238 return (n2
->score
- n1
->score
);
1240 if (n1
->section
&& !n2
->section
)
1242 else if (!n1
->section
&& n2
->section
)
1244 else if (n1
->section
&& n2
->section
&&
1245 (diff
= strcmp(n1
->section
, n2
->section
)) != 0)
1248 return (_cups_strcasecmp(n1
->text
, n2
->text
));
1253 * 'help_sort_words()' - Sort words alphabetically.
1256 static int /* O - Difference */
1257 help_sort_words(help_word_t
*w1
, /* I - Second word */
1258 help_word_t
*w2
) /* I - Second word */
1260 return (_cups_strcasecmp(w1
->text
, w2
->text
));