+/* Build an exclude structure given an exclude pattern. The value in "pat"
+ * is not null-terminated. */
+static void make_exclude(struct exclude_list_struct *listp, const char *pat,
+ unsigned int pat_len, unsigned int mflags)
+{
+ struct exclude_struct *ret;
+ const char *cp;
+ unsigned int ex_len;
+
+ if (verbose > 2) {
+ rprintf(FINFO, "[%s] make_exclude(%.*s, %s%s)\n",
+ who_am_i(), (int)pat_len, pat,
+ mflags & MATCHFLG_PERDIR_MERGE ? "per-dir-merge"
+ : mflags & MATCHFLG_INCLUDE ? "include" : "exclude",
+ listp->debug_type);
+ }
+
+ ret = new(struct exclude_struct);
+ if (!ret)
+ out_of_memory("make_exclude");
+
+ memset(ret, 0, sizeof ret[0]);
+
+ if (mflags & MATCHFLG_ABS_PATH) {
+ if (*pat != '/') {
+ mflags &= ~MATCHFLG_ABS_PATH;
+ ex_len = 0;
+ } else
+ ex_len = dirbuf_len - module_dirlen - 1;
+ } else
+ ex_len = 0;
+ ret->pattern = new_array(char, ex_len + pat_len + 1);
+ if (!ret->pattern)
+ out_of_memory("make_exclude");
+ if (ex_len)
+ memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
+ strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
+ pat_len += ex_len;
+
+ if (strpbrk(ret->pattern, "*[?")) {
+ mflags |= MATCHFLG_WILD;
+ if ((cp = strstr(ret->pattern, "**")) != NULL) {
+ mflags |= MATCHFLG_WILD2;
+ /* If the pattern starts with **, note that. */
+ if (cp == ret->pattern)
+ mflags |= MATCHFLG_WILD2_PREFIX;
+ }
+ }
+
+ if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
+ ret->pattern[pat_len-1] = 0;
+ mflags |= MATCHFLG_DIRECTORY;
+ }
+
+ if (mflags & MATCHFLG_PERDIR_MERGE) {
+ struct exclude_list_struct *lp;
+ unsigned int len;
+ int i;
+
+ if ((cp = strrchr(ret->pattern, '/')) != NULL)
+ cp++;
+ else
+ cp = ret->pattern;
+
+ /* If the local merge file was already mentioned, don't
+ * add it again. */
+ for (i = 0; i < mergelist_cnt; i++) {
+ struct exclude_struct *ex = mergelist_parents[i];
+ const char *s = strrchr(ex->pattern, '/');
+ if (s)
+ s++;
+ else
+ s = ex->pattern;
+ len = strlen(s);
+ if (len == pat_len - (cp - ret->pattern)
+ && memcmp(s, cp, len) == 0) {
+ free_exclude(ret);
+ return;
+ }
+ }
+
+ if (!(lp = new_array(struct exclude_list_struct, 1)))
+ out_of_memory("make_exclude");
+ lp->head = lp->tail = NULL;
+ if (asprintf(&lp->debug_type, " (per-dir %s)", cp) < 0)
+ out_of_memory("make_exclude");
+ ret->u.mergelist = lp;
+
+ if (mergelist_cnt == mergelist_size) {
+ mergelist_size += 5;
+ mergelist_parents = realloc_array(mergelist_parents,
+ struct exclude_struct *,
+ mergelist_size);
+ if (!mergelist_parents)
+ out_of_memory("make_exclude");
+ }
+ mergelist_parents[mergelist_cnt++] = ret;
+ } else {
+ for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
+ ret->u.slash_cnt++;
+ }
+
+ ret->match_flags = mflags;
+
+ if (!listp->tail) {
+ ret->next = listp->head;
+ listp->head = listp->tail = ret;
+ } else {
+ ret->next = listp->tail->next;
+ listp->tail->next = ret;
+ listp->tail = ret;
+ }
+}