207
178
data = databuf(STDIN_FILENO);
210
if (data[len-1] == '\n')
211
data[--len] = '\0'; // clobber newline
213
printf("data: <%s>\n", data);
215
180
/* run the regex matcher */
216
ret = re_search(& pat, data, len, start, len, NULL);
217
printf("re_search with NULL returned position %d (%s)\n", ret, (ret >= 0) ? "true" : "false");
219
printf("pat.allocated = %ld\n", pat.allocated);
220
printf("pat.used = %ld\n", pat.used);
221
printf("pat.syntax = %ld\n", pat.syntax);
222
printf("pat.re_nsub = %ld\n", pat.re_nsub);
223
printf("pat.can_be_null = %d\n", pat.can_be_null);
224
printf("pat.regs_allocated = %d\n", pat.regs_allocated);
225
printf("pat.fastmap_accurate = %d\n", pat.fastmap_accurate);
226
printf("pat.no_sub = %d\n", pat.no_sub);
227
printf("pat.not_bol = %d\n", pat.not_bol);
228
printf("pat.not_eol = %d\n", pat.not_eol);
229
printf("pat.newline_anchor = %d\n", pat.newline_anchor);
232
ret = re_search(& pat, data, len, start, len, ®s);
181
ret = re_search(& pat, data, len, 0, len, NULL);
233
182
printf("re_search returned position %d (%s)\n", ret, (ret >= 0) ? "true" : "false");
235
printf("pat.allocated = %ld\n", pat.allocated);
236
printf("pat.used = %ld\n", pat.used);
237
printf("pat.syntax = %ld\n", pat.syntax);
238
printf("pat.re_nsub = %ld\n", pat.re_nsub);
239
printf("pat.can_be_null = %d\n", pat.can_be_null);
240
printf("pat.regs_allocated = %d\n", pat.regs_allocated);
241
printf("pat.fastmap_accurate = %d\n", pat.fastmap_accurate);
242
printf("pat.no_sub = %d\n", pat.no_sub);
243
printf("pat.not_bol = %d\n", pat.not_bol);
244
printf("pat.not_eol = %d\n", pat.not_eol);
245
printf("pat.newline_anchor = %d\n", pat.newline_anchor);
247
printf("pat.re_nsub = %d\n", pat.re_nsub);
248
for (int i = 0; i <= pat.re_nsub; i++)
249
printf("\treg[%d].so = %d, reg[%d].eo = %d\n", i, regs.start[i], i, regs.end[i]);
251
184
/* run the dfa matcher */
739
#ifdef GREP_DFA /* not needed for gawk */
740
/* xalloc.h -- malloc with out-of-memory checking
742
Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
743
2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation,
746
This program is free software: you can redistribute it and/or modify
747
it under the terms of the GNU General Public License as published by
748
the Free Software Foundation; either version 3 of the License, or
749
(at your option) any later version.
751
This program is distributed in the hope that it will be useful,
752
but WITHOUT ANY WARRANTY; without even the implied warranty of
753
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
754
GNU General Public License for more details.
756
You should have received a copy of the GNU General Public License
757
along with this program. If not, see <http://www.gnu.org/licenses/>. */
770
# ifndef __attribute__
771
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
772
# define __attribute__(x)
776
# ifndef ATTRIBUTE_NORETURN
777
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
780
# ifndef ATTRIBUTE_MALLOC
782
# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
784
# define ATTRIBUTE_MALLOC
788
/* This function is always triggered when memory is exhausted.
789
It must be defined by the application, either explicitly
790
or by using gnulib's xalloc-die module. This is the
791
function to call when one wants the program to die because of a
792
memory allocation failure. */
793
extern void xalloc_die (void);
795
void *xmalloc (size_t s) ATTRIBUTE_MALLOC;
796
void *xzalloc (size_t s) ATTRIBUTE_MALLOC;
797
void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
798
void *xrealloc (void *p, size_t s);
799
void *x2realloc (void *p, size_t *pn);
800
void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC;
801
char *xstrdup (char const *str) ATTRIBUTE_MALLOC;
803
/* Return 1 if an array of N objects, each of size S, cannot exist due
804
to size arithmetic overflow. S must be positive and N must be
805
nonnegative. This is a macro, not an inline function, so that it
806
works correctly even when SIZE_MAX < N.
808
By gnulib convention, SIZE_MAX represents overflow in size
809
calculations, so the conservative dividend to use here is
810
SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
811
However, malloc (SIZE_MAX) fails on all known hosts where
812
sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
813
exactly-SIZE_MAX allocations on such hosts; this avoids a test and
814
branch when S is known to be 1. */
815
# define xalloc_oversized(n, s) \
816
((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
819
/* In the following macros, T must be an elementary or structure/union or
820
typedef'ed type, or a pointer to such a type. To apply one of the
821
following macros to a function pointer or array type, you need to typedef
822
it first and use the typedef name. */
824
/* Allocate an object of type T dynamically, with error checking. */
825
/* extern t *XMALLOC (typename t); */
826
# define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
828
/* Allocate memory for N elements of type T, with error checking. */
829
/* extern t *XNMALLOC (size_t n, typename t); */
830
# define XNMALLOC(n, t) \
831
((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
833
/* Allocate an object of type T dynamically, with error checking,
835
/* extern t *XZALLOC (typename t); */
836
# define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
838
/* Allocate memory for N elements of type T, with error checking,
840
/* extern t *XCALLOC (size_t n, typename t); */
841
# define XCALLOC(n, t) \
842
((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
845
* Gawk uses this file only to keep dfa.c happy.
846
* We're therefore safe in manually defining HAVE_INLINE to
847
* make the !@#$%^&*() thing just work.
850
#define HAVE_INLINE 1 /* so there. nyah, nyah, nyah. */
854
# define static_inline static inline
856
void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
857
void *xnrealloc (void *p, size_t n, size_t s);
858
void *x2nrealloc (void *p, size_t *pn, size_t s);
859
char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;
863
/* Allocate an array of N objects, each with S bytes of memory,
864
dynamically, with error checking. S must be nonzero. */
867
xnmalloc (size_t n, size_t s)
869
if (xalloc_oversized (n, s))
871
return xmalloc (n * s);
874
/* Allocate an array of N objects, each with S bytes of memory,
875
dynamically, with error checking. S must be nonzero.
876
Clear the contents afterwards. */
879
xcalloc(size_t nmemb, size_t size)
881
void *p = xmalloc (nmemb * size);
882
memset(p, '\0', nmemb * size);
886
/* Reallocate a pointer to a new size, with error checking. */
889
xrealloc(void *p, size_t size)
891
void *new_p = realloc(p, size);
898
/* xalloc_die --- fatal error message when malloc fails, needed by dfa.c */
903
r_fatal("xalloc: malloc failed: %s"), strerror(errno);
906
/* Clone an object P of size S, with error checking. There's no need
907
for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
908
need for an arithmetic overflow check. */
911
xmemdup (void const *p, size_t s)
913
return memcpy (xmalloc (s), p, s);
916
/* Change the size of an allocated block of memory P to an array of N
917
objects each of S bytes, with error checking. S must be nonzero. */
920
xnrealloc (void *p, size_t n, size_t s)
922
if (xalloc_oversized (n, s))
924
return xrealloc (p, n * s);
927
/* If P is null, allocate a block of at least *PN such objects;
928
otherwise, reallocate P so that it contains more than *PN objects
929
each of S bytes. *PN must be nonzero unless P is null, and S must
930
be nonzero. Set *PN to the new number of objects, and return the
931
pointer to the new block. *PN is never set to zero, and the
932
returned pointer is never null.
934
Repeated reallocations are guaranteed to make progress, either by
935
allocating an initial block with a nonzero size, or by allocating a
938
In the following implementation, nonzero sizes are increased by a
939
factor of approximately 1.5 so that repeated reallocations have
940
O(N) overall cost rather than O(N**2) cost, but the
941
specification for this function does not guarantee that rate.
943
Here is an example of use:
947
size_t allocated = 0;
950
append_int (int value)
952
if (used == allocated)
953
p = x2nrealloc (p, &allocated, sizeof *p);
957
This causes x2nrealloc to allocate a block of some nonzero size the
958
first time it is called.
960
To have finer-grained control over the initial size, set *PN to a
961
nonzero value before calling this function with P == NULL. For
966
size_t allocated = 0;
967
size_t allocated1 = 1000;
970
append_int (int value)
972
if (used == allocated)
974
p = x2nrealloc (p, &allocated1, sizeof *p);
975
allocated = allocated1;
983
x2nrealloc (void *p, size_t *pn, size_t s)
991
/* The approximate size to use for initial small allocation
992
requests, when the invoking code specifies an old size of
993
zero. 64 bytes is the largest "small" request for the
994
GNU C library malloc. */
995
enum { DEFAULT_MXFAST = 64 };
997
n = DEFAULT_MXFAST / s;
1003
/* Set N = ceil (1.5 * N) so that progress is made if N == 1.
1004
Check for overflow, so that N * S stays in size_t range.
1005
The check is slightly conservative, but an exact check isn't
1006
worth the trouble. */
1007
if ((size_t) -1 / 3 * 2 / s <= n)
1013
return xrealloc (p, n * s);
1016
/* Return a pointer to a new buffer of N bytes. This is like xmalloc,
1017
except it returns char *. */
1020
xcharalloc (size_t n)
1022
return XNMALLOC (n, char);
1025
/* Allocate S bytes of zeroed memory dynamically, with error checking.
1026
There's no need for xnzalloc (N, S), since it would be equivalent
1027
to xcalloc (N, S). */
1032
return memset (xmalloc (s), 0, s);
1040
/* C++ does not allow conversions from void * to other pointer types
1041
without a cast. Use templates to work around the problem when
1044
template <typename T> inline T *
1045
xrealloc (T *p, size_t s)
1047
return (T *) xrealloc ((void *) p, s);
1050
template <typename T> inline T *
1051
xnrealloc (T *p, size_t n, size_t s)
1053
return (T *) xnrealloc ((void *) p, n, s);
1056
template <typename T> inline T *
1057
x2realloc (T *p, size_t *pn)
1059
return (T *) x2realloc ((void *) p, pn);
1062
template <typename T> inline T *
1063
x2nrealloc (T *p, size_t *pn, size_t s)
1065
return (T *) x2nrealloc ((void *) p, pn, s);
1068
template <typename T> inline T *
1069
xmemdup (T const *p, size_t s)
1071
return (T *) xmemdup ((void const *) p, s);
1076
#endif /* !XALLOC_H_ */
1077
#endif /* GREP_DFA */