+int
+_hs_history_patsearch (const char *string, int direction, int flags)
+{
+ char *pat;
+ size_t len, start;
+ int ret, unescaped_backslash;
+
+#if defined (HAVE_FNMATCH)
+ /* Assume that the string passed does not have a leading `^' and any
+ anchored search request is captured in FLAGS */
+ len = strlen (string);
+ ret = len - 1;
+ /* fnmatch is required to reject a pattern that ends with an unescaped
+ backslash */
+ if (unescaped_backslash = (string[ret] == '\\'))
+ {
+ while (ret > 0 && string[--ret] == '\\')
+ unescaped_backslash = 1 - unescaped_backslash;
+ }
+ if (unescaped_backslash)
+ return -1;
+ pat = (char *)xmalloc (len + 3);
+ /* If the search string is not anchored, we'll be calling fnmatch (assuming
+ we have it). Prefix a `*' to the front of the search string so we search
+ anywhere in the line. */
+ if ((flags & ANCHORED_SEARCH) == 0 && string[0] != '*')
+ {
+ pat[0] = '*';
+ start = 1;
+ len++;
+ }
+ else
+ {
+ start = 0;
+ }
+
+ /* Attempt to reduce the number of searches by tacking a `*' onto the end
+ of a pattern that doesn't have one. Assume a pattern that ends in a
+ backslash contains an even number of trailing backslashes; we check
+ above */
+ strcpy (pat + start, string);
+ if (pat[len - 1] != '*')
+ {
+ pat[len] = '*'; /* XXX */
+ pat[len+1] = '\0';
+ }
+#else
+ pat = string;
+#endif
+
+ ret = history_search_internal (pat, direction, flags|PATTERN_SEARCH);
+
+ if (pat != string)
+ free (pat);
+ return ret;
+}
+