Blame gnulib-tests/test-exclude.c

Packit 709fb3
/* Test suite for exclude.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
   This file is part of the GNUlib Library.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <errno.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
#include <stdbool.h>
Packit 709fb3
#include <fnmatch.h>
Packit 709fb3
Packit 709fb3
#include "exclude.h"
Packit 709fb3
#include "error.h"
Packit 709fb3
#include "argmatch.h"
Packit 709fb3
Packit 709fb3
#ifndef FNM_CASEFOLD
Packit 709fb3
# define FNM_CASEFOLD 0
Packit 709fb3
#endif
Packit 709fb3
#ifndef FNM_LEADING_DIR
Packit 709fb3
# define FNM_LEADING_DIR 0
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
char const * const exclude_keywords[] = {
Packit 709fb3
  "noescape",
Packit 709fb3
  "pathname",
Packit 709fb3
  "period",
Packit 709fb3
  "leading_dir",
Packit 709fb3
  "casefold",
Packit 709fb3
  "anchored",
Packit 709fb3
  "include",
Packit 709fb3
  "wildcards",
Packit 709fb3
  NULL
Packit 709fb3
};
Packit 709fb3
Packit 709fb3
int exclude_flags[] = {
Packit 709fb3
  FNM_NOESCAPE,
Packit 709fb3
  FNM_PATHNAME,
Packit 709fb3
  FNM_PERIOD,
Packit 709fb3
  FNM_LEADING_DIR,
Packit 709fb3
  FNM_CASEFOLD,
Packit 709fb3
  EXCLUDE_ANCHORED,
Packit 709fb3
  EXCLUDE_INCLUDE,
Packit 709fb3
  EXCLUDE_WILDCARDS
Packit 709fb3
};
Packit 709fb3
Packit 709fb3
ARGMATCH_VERIFY (exclude_keywords, exclude_flags);
Packit 709fb3
Packit 709fb3
/* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
Packit 709fb3
   thus must link with a definition of that function.  Provide it here.  */
Packit 709fb3
#ifdef ARGMATCH_DIE_DECL
Packit 709fb3
Packit 709fb3
_Noreturn ARGMATCH_DIE_DECL;
Packit 709fb3
ARGMATCH_DIE_DECL { exit (1); }
Packit 709fb3
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (int argc, char **argv)
Packit 709fb3
{
Packit 709fb3
  int exclude_options = 0;
Packit 709fb3
  struct exclude *exclude = new_exclude ();
Packit 709fb3
Packit 709fb3
  if (argc == 1)
Packit 709fb3
    error (1, 0, "usage: %s file -- words...", argv[0]);
Packit 709fb3
Packit 709fb3
  while (--argc)
Packit 709fb3
    {
Packit 709fb3
      char *opt = *++argv;
Packit 709fb3
      if (opt[0] == '-')
Packit 709fb3
        {
Packit 709fb3
          int neg = 0;
Packit 709fb3
          int flag;
Packit 709fb3
          char *s = opt + 1;
Packit 709fb3
Packit 709fb3
          if (opt[1] == '-' && opt[2] == 0)
Packit 709fb3
            {
Packit 709fb3
              argc--;
Packit 709fb3
              break;
Packit 709fb3
            }
Packit 709fb3
          if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0)
Packit 709fb3
            {
Packit 709fb3
              neg = 1;
Packit 709fb3
              s += 3;
Packit 709fb3
            }
Packit 709fb3
          flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags);
Packit 709fb3
          if (neg)
Packit 709fb3
            exclude_options &= ~flag;
Packit 709fb3
          else
Packit 709fb3
            exclude_options |= flag;
Packit 709fb3
Packit 709fb3
          /* Skip this test if invoked with -leading-dir on a system that
Packit 709fb3
             lacks support for FNM_LEADING_DIR. */
Packit 709fb3
          if (strcmp (s, "leading_dir") == 0 && FNM_LEADING_DIR == 0)
Packit 709fb3
            exit (77);
Packit 709fb3
Packit 709fb3
          /* Likewise for -casefold and FNM_CASEFOLD.  */
Packit 709fb3
          if (strcmp (s, "casefold") == 0 && FNM_CASEFOLD == 0)
Packit 709fb3
            exit (77);
Packit 709fb3
        }
Packit 709fb3
      else if (add_exclude_file (add_exclude, exclude, opt,
Packit 709fb3
                                 exclude_options, '\n') != 0)
Packit 709fb3
        error (1, errno, "error loading %s", opt);
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  for (; argc; --argc)
Packit 709fb3
    {
Packit 709fb3
      char *word = *++argv;
Packit 709fb3
Packit 709fb3
      printf ("%s: %d\n", word, excluded_file_name (exclude, word));
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  free_exclude (exclude);
Packit 709fb3
  return 0;
Packit 709fb3
}