]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help-index.c
2 * Online help index routines for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cgi-private.h"
20 * List of common English words that should not be indexed...
23 static char help_common_words
[][6] =
128 static help_word_t
*help_add_word(help_node_t
*n
, const char *text
);
129 static void help_delete_node(help_node_t
*n
);
130 static void help_delete_word(help_word_t
*w
);
131 static int help_load_directory(help_index_t
*hi
,
132 const char *directory
,
133 const char *relative
);
134 static int help_load_file(help_index_t
*hi
,
135 const char *filename
,
136 const char *relative
,
138 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);
139 static int help_sort_by_name(help_node_t
*p1
, help_node_t
*p2
);
140 static int help_sort_by_score(help_node_t
*p1
, help_node_t
*p2
);
141 static int help_sort_words(help_word_t
*w1
, help_word_t
*w2
);
145 * 'helpDeleteIndex()' - Delete an index, freeing all memory used.
149 helpDeleteIndex(help_index_t
*hi
) /* I - Help index */
151 help_node_t
*node
; /* Current node */
157 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
159 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
162 help_delete_node(node
);
165 cupsArrayDelete(hi
->nodes
);
166 cupsArrayDelete(hi
->sorted
);
173 * 'helpFindNode()' - Find a node in an index.
176 help_node_t
* /* O - Node pointer or NULL */
177 helpFindNode(help_index_t
*hi
, /* I - Index */
178 const char *filename
, /* I - Filename */
179 const char *anchor
) /* I - Anchor */
181 help_node_t key
; /* Search key */
185 * Range check input...
188 if (!hi
|| !filename
)
192 * Initialize the search key...
195 key
.filename
= (char *)filename
;
196 key
.anchor
= (char *)anchor
;
199 * Return any match...
202 return ((help_node_t
*)cupsArrayFind(hi
->nodes
, &key
));
207 * 'helpLoadIndex()' - Load a help index from disk.
210 help_index_t
* /* O - Index pointer or NULL */
211 helpLoadIndex(const char *hifile
, /* I - Index filename */
212 const char *directory
) /* I - Directory that is indexed */
214 help_index_t
*hi
; /* Help index */
215 cups_file_t
*fp
; /* Current file */
216 char line
[2048], /* Line from file */
217 *ptr
, /* Pointer into line */
218 *filename
, /* Filename in line */
219 *anchor
, /* Anchor in line */
220 *sectptr
, /* Section pointer in line */
221 section
[1024], /* Section name */
222 *text
; /* Text in line */
223 time_t mtime
; /* Modification time */
224 off_t offset
; /* Offset into file */
225 size_t length
; /* Length in bytes */
226 int update
; /* Update? */
227 help_node_t
*node
; /* Current node */
228 help_word_t
*word
; /* Current word */
232 * Create a new, empty index.
235 if ((hi
= (help_index_t
*)calloc(1, sizeof(help_index_t
))) == NULL
)
238 hi
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
239 hi
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
241 if (!hi
->nodes
|| !hi
->sorted
)
243 cupsArrayDelete(hi
->nodes
);
244 cupsArrayDelete(hi
->sorted
);
250 * Try loading the existing index file...
253 if ((fp
= cupsFileOpen(hifile
, "r")) != NULL
)
256 * Lock the file and then read the first line...
261 if (cupsFileGets(fp
, line
, sizeof(line
)) && !strcmp(line
, "HELPV2"))
264 * Got a valid header line, now read the data lines...
269 while (cupsFileGets(fp
, line
, sizeof(line
)))
272 * Each line looks like one of the following:
274 * filename mtime offset length "section" "text"
275 * filename#anchor offset length "text"
282 * Read a word in the current node...
285 if (!node
|| (ptr
= strrchr(line
, ' ')) == NULL
)
288 if ((word
= help_add_word(node
, ptr
+ 1)) != NULL
)
289 word
->count
= atoi(line
+ 1);
299 if ((ptr
= strchr(line
, ' ')) == NULL
)
302 while (isspace(*ptr
& 255))
305 if ((anchor
= strrchr(filename
, '#')) != NULL
)
311 mtime
= strtol(ptr
, &ptr
, 10);
313 offset
= strtoll(ptr
, &ptr
, 10);
314 length
= (size_t)strtoll(ptr
, &ptr
, 10);
316 while (isspace(*ptr
& 255))
331 while (*ptr
&& *ptr
!= '\"')
339 strlcpy(section
, sectptr
, sizeof(section
));
341 while (isspace(*ptr
& 255))
353 while (*ptr
&& *ptr
!= '\"')
361 if ((node
= help_new_node(filename
, anchor
, section
, text
,
362 mtime
, offset
, length
)) == NULL
)
367 cupsArrayAdd(hi
->nodes
, node
);
376 * Scan for new/updated files...
379 update
= help_load_directory(hi
, directory
, NULL
);
382 * Remove any files that are no longer installed...
385 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
387 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
391 * Delete this node...
394 cupsArrayRemove(hi
->nodes
, node
);
395 help_delete_node(node
);
399 * Add nodes to the sorted array...
402 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
404 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
405 cupsArrayAdd(hi
->sorted
, node
);
408 * Save the index if we updated it...
412 helpSaveIndex(hi
, hifile
);
415 * Return the index...
423 * 'helpSaveIndex()' - Save a help index to disk.
426 int /* O - 0 on success, -1 on error */
427 helpSaveIndex(help_index_t
*hi
, /* I - Index */
428 const char *hifile
) /* I - Index filename */
430 cups_file_t
*fp
; /* Index file */
431 help_node_t
*node
; /* Current node */
432 help_word_t
*word
; /* Current word */
436 * Try creating a new index file...
439 if ((fp
= cupsFileOpen(hifile
, "w9")) == NULL
)
443 * Lock the file while we write it...
448 cupsFilePuts(fp
, "HELPV2\n");
450 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
452 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
455 * Write the current node with/without the anchor...
460 if (cupsFilePrintf(fp
, "%s#%s " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\"\n",
461 node
->filename
, node
->anchor
,
462 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
468 if (cupsFilePrintf(fp
, "%s %d " CUPS_LLFMT
" " CUPS_LLFMT
" \"%s\" \"%s\"\n",
469 node
->filename
, (int)node
->mtime
,
470 CUPS_LLCAST node
->offset
, CUPS_LLCAST node
->length
,
471 node
->section
? node
->section
: "", node
->text
) < 0)
476 * Then write the words associated with the node...
479 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
481 word
= (help_word_t
*)cupsArrayNext(node
->words
))
482 if (cupsFilePrintf(fp
, " %d %s\n", word
->count
, word
->text
) < 0)
488 if (cupsFileClose(fp
) < 0)
498 * 'helpSearchIndex()' - Search an index.
501 help_index_t
* /* O - Search index */
502 helpSearchIndex(help_index_t
*hi
, /* I - Index */
503 const char *query
, /* I - Query string */
504 const char *section
, /* I - Limit search to this section */
505 const char *filename
) /* I - Limit search to this file */
507 help_index_t
*search
; /* Search index */
508 help_node_t
*node
; /* Current node */
509 help_word_t
*word
; /* Current word */
510 void *sc
; /* Search context */
511 int matches
; /* Number of matches */
522 * Reset the scores of all nodes to 0...
525 for (node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
527 node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
531 * Find the first node to search in...
536 node
= helpFindNode(hi
, filename
, NULL
);
541 node
= (help_node_t
*)cupsArrayFirst(hi
->nodes
);
544 * Convert the query into a regular expression...
547 sc
= cgiCompileSearch(query
);
552 * Allocate a search index...
555 search
= calloc(1, sizeof(help_index_t
));
562 search
->nodes
= cupsArrayNew((cups_array_func_t
)help_sort_by_name
, NULL
);
563 search
->sorted
= cupsArrayNew((cups_array_func_t
)help_sort_by_score
, NULL
);
565 if (!search
->nodes
|| !search
->sorted
)
567 cupsArrayDelete(search
->nodes
);
568 cupsArrayDelete(search
->sorted
);
577 * Check each node in the index, adding matching nodes to the
581 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
582 if (section
&& strcmp(node
->section
, section
))
584 else if (filename
&& strcmp(node
->filename
, filename
))
588 matches
= cgiDoSearch(sc
, node
->text
);
590 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
592 word
= (help_word_t
*)cupsArrayNext(node
->words
))
593 if (cgiDoSearch(sc
, word
->text
) > 0)
594 matches
+= word
->count
;
599 * Found a match, add the node to the search index...
602 node
->score
= matches
;
604 cupsArrayAdd(search
->nodes
, node
);
605 cupsArrayAdd(search
->sorted
, node
);
610 * Free the search context...
616 * Return the results...
624 * 'help_add_word()' - Add a word to a node.
627 static help_word_t
* /* O - New word */
628 help_add_word(help_node_t
*n
, /* I - Node */
629 const char *text
) /* I - Word text */
631 help_word_t
*w
, /* New word */
632 key
; /* Search key */
636 * Create the words array as needed...
640 n
->words
= cupsArrayNew((cups_array_func_t
)help_sort_words
, NULL
);
643 * See if the word is already added...
646 key
.text
= (char *)text
;
648 if ((w
= (help_word_t
*)cupsArrayFind(n
->words
, &key
)) == NULL
)
651 * Create a new word...
654 if ((w
= calloc(1, sizeof(help_word_t
))) == NULL
)
657 if ((w
->text
= strdup(text
)) == NULL
)
663 cupsArrayAdd(n
->words
, w
);
667 * Bump the counter for this word and return it...
677 * 'help_delete_node()' - Free all memory used by a node.
681 help_delete_node(help_node_t
*n
) /* I - Node */
683 help_word_t
*w
; /* Current word */
701 for (w
= (help_word_t
*)cupsArrayFirst(n
->words
);
703 w
= (help_word_t
*)cupsArrayNext(n
->words
))
706 cupsArrayDelete(n
->words
);
713 * 'help_delete_word()' - Free all memory used by a word.
717 help_delete_word(help_word_t
*w
) /* I - Word */
730 * 'help_load_directory()' - Load a directory of files into an index.
733 static int /* O - 0 = success, -1 = error, 1 = updated */
735 help_index_t
*hi
, /* I - Index */
736 const char *directory
, /* I - Directory */
737 const char *relative
) /* I - Relative path */
739 cups_dir_t
*dir
; /* Directory file */
740 cups_dentry_t
*dent
; /* Directory entry */
741 char *ext
, /* Pointer to extension */
742 filename
[1024], /* Full filename */
743 relname
[1024]; /* Relative filename */
744 int update
; /* Updated? */
745 help_node_t
*node
; /* Current node */
749 * Open the directory and scan it...
752 if ((dir
= cupsDirOpen(directory
)) == NULL
)
757 while ((dent
= cupsDirRead(dir
)) != NULL
)
763 if (dent
->filename
[0] == '.')
767 * Get absolute and relative filenames...
770 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, dent
->filename
);
772 snprintf(relname
, sizeof(relname
), "%s/%s", relative
, dent
->filename
);
774 strlcpy(relname
, dent
->filename
, sizeof(relname
));
777 * Check if we have a HTML file...
780 if ((ext
= strstr(dent
->filename
, ".html")) != NULL
&&
781 (!ext
[5] || !strcmp(ext
+ 5, ".gz")))
784 * HTML file, see if we have already indexed the file...
787 if ((node
= helpFindNode(hi
, relname
, NULL
)) != NULL
)
790 * File already indexed - check dates to confirm that the
791 * index is up-to-date...
794 if (node
->mtime
== dent
->fileinfo
.st_mtime
)
797 * Same modification time, so mark all of the nodes
798 * for this file as up-to-date...
801 for (; node
; node
= (help_node_t
*)cupsArrayNext(hi
->nodes
))
802 if (!strcmp(node
->filename
, relname
))
813 help_load_file(hi
, filename
, relname
, dent
->fileinfo
.st_mtime
);
815 else if (S_ISDIR(dent
->fileinfo
.st_mode
))
818 * Process sub-directory...
821 if (help_load_directory(hi
, filename
, relname
) == 1)
833 * 'help_load_file()' - Load a HTML files into an index.
836 static int /* O - 0 = success, -1 = error */
838 help_index_t
*hi
, /* I - Index */
839 const char *filename
, /* I - Filename */
840 const char *relative
, /* I - Relative path */
841 time_t mtime
) /* I - Modification time */
843 cups_file_t
*fp
; /* HTML file */
844 help_node_t
*node
; /* Current node */
845 char line
[1024], /* Line from file */
846 temp
[1024], /* Temporary word */
847 section
[1024], /* Section */
848 *ptr
, /* Pointer into line */
849 *anchor
, /* Anchor name */
850 *text
; /* Text for anchor */
851 off_t offset
; /* File offset */
852 char quote
; /* Quote character */
853 help_word_t
*word
; /* Current word */
854 int wordlen
; /* Length of word */
857 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
863 strlcpy(section
, "Other", sizeof(section
));
865 while (cupsFileGets(fp
, line
, sizeof(line
)))
868 * Look for "<TITLE>", "<A NAME", or "<!-- SECTION:" prefix...
871 if ((ptr
= strstr(line
, "<!-- SECTION:")) != NULL
)
874 * Got section line, copy it!
877 for (ptr
+= 13; isspace(*ptr
& 255); ptr
++);
879 strlcpy(section
, ptr
, sizeof(section
));
880 if ((ptr
= strstr(section
, "-->")) != NULL
)
883 * Strip comment stuff from end of line...
886 for (*ptr
-- = '\0'; ptr
> line
&& isspace(*ptr
& 255); *ptr
-- = '\0');
888 if (isspace(*ptr
& 255))
894 for (ptr
= line
; (ptr
= strchr(ptr
, '<')) != NULL
;)
898 if (!_cups_strncasecmp(ptr
, "TITLE>", 6))
909 char *idptr
; /* Pointer to ID */
911 if (!_cups_strncasecmp(ptr
, "A NAME=", 7))
913 else if ((idptr
= strstr(ptr
, " ID=")) != NULL
)
915 else if ((idptr
= strstr(ptr
, " id=")) != NULL
)
924 if (*ptr
== '\"' || *ptr
== '\'')
927 * Get quoted anchor...
932 if ((ptr
= strchr(anchor
, quote
)) != NULL
)
940 * Get unquoted anchor...
945 for (ptr
= anchor
; *ptr
&& *ptr
!= '>' && !isspace(*ptr
& 255); ptr
++);
954 * Got the anchor, now lets find the end...
957 while (*ptr
&& *ptr
!= '>')
967 * Now collect text for the link...
971 while ((ptr
= strchr(text
, '<')) == NULL
)
973 ptr
= text
+ strlen(text
);
974 if (ptr
>= (line
+ sizeof(line
) - 2))
979 if (!cupsFileGets(fp
, ptr
, sizeof(line
) - (size_t)(ptr
- line
) - 1))
986 node
->length
= (size_t)(offset
- node
->offset
);
994 if ((node
= helpFindNode(hi
, relative
, anchor
)) != NULL
)
997 * Node already in the index, so replace the text and other
1001 cupsArrayRemove(hi
->nodes
, node
);
1004 free(node
->section
);
1011 for (word
= (help_word_t
*)cupsArrayFirst(node
->words
);
1013 word
= (help_word_t
*)cupsArrayNext(node
->words
))
1014 help_delete_word(word
);
1016 cupsArrayDelete(node
->words
);
1020 node
->section
= section
[0] ? strdup(section
) : NULL
;
1021 node
->text
= strdup(text
);
1022 node
->mtime
= mtime
;
1023 node
->offset
= offset
;
1032 node
= help_new_node(relative
, anchor
, section
, text
, mtime
, offset
, 0);
1036 * Go through the text value and replace tabs and newlines with
1037 * whitespace and eliminate extra whitespace...
1040 for (ptr
= node
->text
, text
= node
->text
; *ptr
;)
1041 if (isspace(*ptr
& 255))
1043 while (isspace(*ptr
& 255))
1048 else if (text
!= ptr
)
1059 * (Re)add the node to the array...
1062 cupsArrayAdd(hi
->nodes
, node
);
1072 * Scan this line for words...
1075 for (ptr
= line
; *ptr
; ptr
++)
1078 * Skip HTML stuff...
1083 if (!strncmp(ptr
, "<!--", 4))
1086 * Skip HTML comment...
1089 if ((text
= strstr(ptr
+ 4, "-->")) == NULL
)
1090 ptr
+= strlen(ptr
) - 1;
1097 * Skip HTML element...
1100 for (ptr
++; *ptr
&& *ptr
!= '>'; ptr
++)
1102 if (*ptr
== '\"' || *ptr
== '\'')
1104 for (quote
= *ptr
++; *ptr
&& *ptr
!= quote
; ptr
++);
1117 else if (*ptr
== '&')
1120 * Skip HTML entity...
1123 for (ptr
++; *ptr
&& *ptr
!= ';'; ptr
++);
1130 else if (!isalnum(*ptr
& 255))
1134 * Found the start of a word, search until we find the end...
1137 for (text
= ptr
, ptr
++; *ptr
&& isalnum(*ptr
& 255); ptr
++);
1139 wordlen
= (int)(ptr
- text
);
1141 memcpy(temp
, text
, (size_t)wordlen
);
1142 temp
[wordlen
] = '\0';
1146 if (wordlen
> 1 && !bsearch(temp
, help_common_words
,
1147 (sizeof(help_common_words
) /
1148 sizeof(help_common_words
[0])),
1149 sizeof(help_common_words
[0]),
1150 (int (*)(const void *, const void *))
1152 help_add_word(node
, temp
);
1157 * Get the offset of the next line...
1160 offset
= cupsFileTell(fp
);
1166 node
->length
= (size_t)(offset
- node
->offset
);
1173 * 'help_new_node()' - Create a new node and add it to an index.
1176 static help_node_t
* /* O - Node pointer or NULL on error */
1177 help_new_node(const char *filename
, /* I - Filename */
1178 const char *anchor
, /* I - Anchor */
1179 const char *section
, /* I - Section */
1180 const char *text
, /* I - Text */
1181 time_t mtime
, /* I - Modification time */
1182 off_t offset
, /* I - Offset in file */
1183 size_t length
) /* I - Length in bytes */
1185 help_node_t
*n
; /* Node */
1188 n
= (help_node_t
*)calloc(1, sizeof(help_node_t
));
1192 n
->filename
= strdup(filename
);
1193 n
->anchor
= anchor
? strdup(anchor
) : NULL
;
1194 n
->section
= (section
&& *section
) ? strdup(section
) : NULL
;
1195 n
->text
= strdup(text
);
1205 * 'help_sort_nodes_by_name()' - Sort nodes by section, filename, and anchor.
1208 static int /* O - Difference */
1209 help_sort_by_name(help_node_t
*n1
, /* I - First node */
1210 help_node_t
*n2
) /* I - Second node */
1212 int diff
; /* Difference */
1215 if ((diff
= strcmp(n1
->filename
, n2
->filename
)) != 0)
1218 if (!n1
->anchor
&& !n2
->anchor
)
1220 else if (!n1
->anchor
)
1222 else if (!n2
->anchor
)
1225 return (strcmp(n1
->anchor
, n2
->anchor
));
1230 * 'help_sort_nodes_by_score()' - Sort nodes by score and text.
1233 static int /* O - Difference */
1234 help_sort_by_score(help_node_t
*n1
, /* I - First node */
1235 help_node_t
*n2
) /* I - Second node */
1237 int diff
; /* Difference */
1240 if (n1
->score
!= n2
->score
)
1241 return (n2
->score
- n1
->score
);
1243 if (n1
->section
&& !n2
->section
)
1245 else if (!n1
->section
&& n2
->section
)
1247 else if (n1
->section
&& n2
->section
&&
1248 (diff
= strcmp(n1
->section
, n2
->section
)) != 0)
1251 return (_cups_strcasecmp(n1
->text
, n2
->text
));
1256 * 'help_sort_words()' - Sort words alphabetically.
1259 static int /* O - Difference */
1260 help_sort_words(help_word_t
*w1
, /* I - Second word */
1261 help_word_t
*w2
) /* I - Second word */
1263 return (_cups_strcasecmp(w1
->text
, w2
->text
));