Blame gnulib-tests/dfa-match-aux.c

Packit 709fb3
/* Auxiliary program to test a DFA code path that cannot be triggered
Packit 709fb3
   by grep or gawk.
Packit 709fb3
   Copyright 2014-2017 Free Software Foundation, Inc.
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, or (at your option)
Packit 709fb3
   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, write to the Free Software
Packit 709fb3
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
Packit 709fb3
   02110-1301, USA.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
#include <locale.h>
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
#include <regex.h>
Packit 709fb3
#include <dfa.h>
Packit 709fb3
#include <localeinfo.h>
Packit 709fb3
Packit 709fb3
#include "getprogname.h"
Packit 709fb3
Packit 709fb3
_Noreturn void
Packit 709fb3
dfaerror (char const *mesg)
Packit 709fb3
{
Packit 709fb3
  printf ("dfaerror: %s\n", mesg);
Packit 709fb3
  exit (EXIT_FAILURE);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
_Noreturn void
Packit 709fb3
dfawarn (char const *mesg)
Packit 709fb3
{
Packit 709fb3
  printf ("dfawarn: %s\n", mesg);
Packit 709fb3
  exit (EXIT_FAILURE);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (int argc, char **argv)
Packit 709fb3
{
Packit 709fb3
  struct dfa *dfa;
Packit 709fb3
  char *beg, *end, *p;
Packit 709fb3
  int allow_nl;
Packit 709fb3
  struct localeinfo localeinfo;
Packit 709fb3
Packit 709fb3
  if (argc < 3)
Packit 709fb3
    exit (EXIT_FAILURE);
Packit 709fb3
Packit 709fb3
  setlocale (LC_ALL, "");
Packit 709fb3
  init_localeinfo (&localeinfo);
Packit 709fb3
Packit 709fb3
  dfa = dfaalloc ();
Packit 709fb3
  dfasyntax (dfa, &localeinfo, RE_SYNTAX_GREP | RE_NO_EMPTY_RANGES, 0);
Packit 709fb3
  dfacomp (argv[1], strlen (argv[1]), dfa, 0);
Packit 709fb3
Packit 709fb3
  beg = argv[2];
Packit 709fb3
  end = argv[2] + strlen (argv[2]);
Packit 709fb3
  allow_nl = argc > 3 && atoi (argv[3]);
Packit 709fb3
Packit 709fb3
  p = dfaexec (dfa, beg, end, allow_nl, NULL, NULL);
Packit 709fb3
Packit 709fb3
  if (p != NULL)
Packit 709fb3
    printf ("%zd\n", p - beg);
Packit 709fb3
Packit 709fb3
  exit (EXIT_SUCCESS);
Packit 709fb3
}