+static int
+cmd_search(const char *str) {
+ regoff_t first = 0;
+ regoff_t last = strlen(str);
+ regmatch_t m = {first, last};
+ int flags;
+
+ if (last == 0) return REG_NOMATCH; /* empty string */
+
+ flags = REG_STARTEND;
+ while(regexec(&re_quoted_string, str, 1, &m, flags) == 0) {
+ /*
+ * We have matched a single quoted string, from m.rm_so to m.rm_eo.
+ * So the (non-quote string) from first to m.rm_so needs to be
+ * checked for command substitution. Then we use REG_STARTEND to
+ * look for any other single quote strings after this one.
+ */
+ regmatch_t head = {first, m.rm_so};
+ if (regexec(&re_cmd, str, 1, &head, flags) == 0) {
+ return 0; /* found a command substitution */
+ }
+ flags = REG_NOTBOL | REG_STARTEND;
+ m.rm_so = first = m.rm_eo;
+ m.rm_eo = last;
+ }
+ /* Check the remaining string */
+ flags = REG_STARTEND;
+ if (m.rm_so > 0) flags |= REG_NOTBOL;
+ return regexec(&re_cmd, str, 1, &m, flags);
+}
+