pcre.c, etc.: Support the PCRE2 library.
[anag] / anag.h
1 /* -*-c-*-
2 *
3 * External definitions for Anag
4 *
5 * (c) 2001 Mark Wooding
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Anag: a simple wordgame helper.
11 *
12 * Anag is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * Anag is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Anag; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #ifndef ANAG_H
28 #define ANAG_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 /*----- Preliminaries -----------------------------------------------------*/
48
49 #if defined(__GNUC__)
50 # define GCC_VERSION_P(maj, min) \
51 (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
52 #else
53 # define GCC_VERSION_P(maj, min) 0
54 #endif
55
56 #ifdef __clang__
57 # define CLANG_VERSION_P(maj, min) \
58 (__clang_major__ > (maj) || (__clang_major__ == (maj) && \
59 __clang_minor__ >= (min)))
60 #else
61 # define CLANG_VERSION_P(maj, min) 0
62 #endif
63
64 #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3)
65 # define NORETURN __attribute__((noreturn))
66 # define PRINTF_LIKE(fix, aix) __attribute__((format(printf, fix, aix)))
67 #endif
68
69 #ifndef NORETURN
70 # define NORETURN
71 #endif
72
73 #ifndef PRINTF_LIKE
74 # define PRINTF_LIKE
75 #endif
76
77 /*----- Data structures ---------------------------------------------------*/
78
79 typedef struct node {
80 int (*func)(struct node */*n*/, const char */*p*/, size_t /*sz*/);
81 } node;
82
83 typedef struct dstr {
84 char *buf;
85 size_t len;
86 size_t sz;
87 } dstr;
88
89 #define DSTR_INIT { 0, 0, 0 }
90
91 /*----- Node types --------------------------------------------------------*/
92
93 extern node *anagram(const char *const */*av*/);
94 extern node *subgram(const char *const */*av*/);
95 extern node *wildcard(const char *const */*av*/);
96 extern node *trackword(const char *const */*av*/);
97 extern node *mono(const char *const */*av*/);
98 #ifdef HAVE_REGCOMP
99 extern node *regexp(const char *const */*av*/);
100 #endif
101 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
102 extern node *pcrenode(const char *const */*av*/);
103 #endif
104 extern node *longest(const char *const */*av*/);
105 extern node *shortest(const char *const */*av*/);
106
107 /*----- Exit codes --------------------------------------------------------*/
108
109 #define EX_OK 0
110 #define EX_NONE 1
111 #define EX_FAIL 127
112
113 /*----- Error reporting ---------------------------------------------------*/
114
115 /* --- @ego@ --- *
116 *
117 * Arguments: @const char *p@ = pointer to program name
118 *
119 * Returns: ---
120 *
121 * Use: Stores what the program's name is.
122 */
123
124 extern void ego(const char */*p*/);
125
126 /* --- @pquis@ --- *
127 *
128 * Arguments: @FILE *fp@ = output stream to write on
129 * @const char *p@ = pointer to string to write
130 *
131 * Returns: Zero if everything worked, EOF if not.
132 *
133 * Use: Writes the string @p@ to the output stream @fp@. Occurrences
134 * of the character `$' in @p@ are replaced by the program name
135 * as reported by @quis@. A `$$' is replaced by a single `$'
136 * sign.
137 */
138
139 extern int pquis(FILE */*fp*/, const char */*p*/);
140
141 /* --- @die@ --- *
142 *
143 * Arguments: @const char *f@ = a @printf@-style format string
144 * @...@ = other arguments
145 *
146 * Returns: Never.
147 *
148 * Use: Reports an error and exits.
149 */
150
151 extern PRINTF_LIKE(1, 2) NORETURN void die(const char */*f*/, ...);
152
153 /*----- Memory allocation -------------------------------------------------*/
154
155 /* --- @xmalloc@ --- *
156 *
157 * Arguments: @size_t sz@ = size of block to allocate
158 *
159 * Returns: Pointer to allocated block.
160 *
161 * Use: Allocates memory. If there's not enough memory, the
162 * program exits.
163 */
164
165 extern void *xmalloc(size_t /*sz*/);
166
167 /* --- @xrealloc@ --- *
168 *
169 * Arguments: @void *p@ = a pointer to allocated memory
170 * @size_t sz@ = new size of block wanted
171 *
172 * Returns: Pointer to resized block.
173 *
174 * Use: Resizes an allocated block. If there's not enough memory,
175 * the program exits.
176 */
177
178 extern void *xrealloc(void */*p*/, size_t /*sz*/);
179
180 /*----- Dynamic string handling -------------------------------------------*/
181
182 /* --- @dstr_destroy@ --- *
183 *
184 * Arguments: @dstr *d@ = pointer to a dynamic string block
185 *
186 * Returns: ---
187 *
188 * Use: Reclaims the space used by a dynamic string.
189 */
190
191 extern void dstr_destroy(dstr */*d*/);
192
193 /* --- @dstr_reset@ --- *
194 *
195 * Arguments: @dstr *d@ = pointer to a dynamic string block
196 *
197 * Returns: ---
198 *
199 * Use: Resets a string so that new data gets put at the beginning.
200 */
201
202 extern void dstr_reset(dstr */*d*/);
203
204 /* --- @dstr_ensure@ --- *
205 *
206 * Arguments: @dstr *d@ = pointer to a dynamic string block
207 * @size_t sz@ = amount of free space to ensure
208 *
209 * Returns: ---
210 *
211 * Use: Ensures that at least @sz@ bytes are available in the
212 * given string.
213 */
214
215 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
216
217 /* --- @dstr_putline@ --- *
218 *
219 * Arguments: @dstr *d@ = pointer to a dynamic string block
220 * @FILE *fp@ = a stream to read from
221 *
222 * Returns: The number of characters read into the buffer, or @EOF@ if
223 * end-of-file was reached before any characters were read.
224 *
225 * Use: Appends the next line from the given input stream to the
226 * string. A trailing newline is not added; a trailing null
227 * byte is appended, as for @dstr_putz@.
228 */
229
230 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
231
232 /*----- Infrastructure ----------------------------------------------------*/
233
234 /* --- @atend_register@ --- *
235 *
236 * Arguments: @int (*func)(void *)@ = function to call
237 * @void *p@ = handle to pass to it
238 *
239 * Returns: ---
240 *
241 * Use: Adds a function to the list of things to do at the end of the
242 * program. The function should return nonzero if it produced
243 * any output.
244 */
245
246 extern void atend_register(int (*/*func*/)(void */*p*/), void */*p*/);
247
248 /*----- That's all, folks -------------------------------------------------*/
249
250 #ifdef __cplusplus
251 }
252 #endif
253
254 #endif