(root)/
grep-3.11/
gnulib-tests/
test-dfa-match-aux.c
       1  /* Auxiliary program to test a DFA code path that cannot be triggered
       2     by grep or gawk.
       3     Copyright 2014-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software; you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation, either version 3, or (at your option)
       8     any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program; if not, write to the Free Software
      17     Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
      18     02110-1301, USA.  */
      19  
      20  #include <config.h>
      21  #include <locale.h>
      22  #include <stdio.h>
      23  #include <stdlib.h>
      24  #include <string.h>
      25  #include <unistd.h>
      26  #include <regex.h>
      27  #include <dfa.h>
      28  #include <localeinfo.h>
      29  
      30  #include "binary-io.h"
      31  
      32  _Noreturn void
      33  dfaerror (char const *mesg)
      34  {
      35    printf ("dfaerror: %s\n", mesg);
      36    exit (EXIT_FAILURE);
      37  }
      38  
      39  static int exit_status = EXIT_SUCCESS;
      40  
      41  void
      42  dfawarn (char const *mesg)
      43  {
      44    printf ("dfawarn: %s\n", mesg);
      45    exit_status = EXIT_FAILURE;
      46  }
      47  
      48  int
      49  main (int argc, char **argv)
      50  {
      51    struct dfa *dfa;
      52    char *beg, *end, *p;
      53    int allow_nl;
      54    struct localeinfo localeinfo;
      55  
      56    if (argc < 3)
      57      exit (EXIT_FAILURE);
      58  
      59    /* This test's fixture needs to compare this program's output with an expected
      60       output.  On native Windows, the CR-LF newlines would cause this comparison
      61       to fail.  But we don't want to postprocess this program's output.  */
      62    set_binary_mode (STDOUT_FILENO, O_BINARY);
      63  
      64    setlocale (LC_ALL, "");
      65    init_localeinfo (&localeinfo);
      66  
      67    dfa = dfaalloc ();
      68    dfasyntax (dfa, &localeinfo, RE_SYNTAX_EGREP | RE_NO_EMPTY_RANGES, 0);
      69    dfacomp (argv[1], strlen (argv[1]), dfa, 0);
      70  
      71    beg = argv[2];
      72    end = argv[2] + strlen (argv[2]);
      73    allow_nl = argc > 3 && atoi (argv[3]);
      74  
      75    p = dfaexec (dfa, beg, end, allow_nl, NULL, NULL);
      76  
      77    if (p != NULL)
      78      printf ("%zd\n", p - beg);
      79  
      80    exit (exit_status);
      81  }