0223c54ecc92b757131f8281b3ff4e40870d8524
[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 extern node *regexp(const char *const */*av*/);
99 extern node *pcrenode(const char *const */*av*/);
100 extern node *longest(const char *const */*av*/);
101 extern node *shortest(const char *const */*av*/);
102
103 /*----- Exit codes --------------------------------------------------------*/
104
105 #define EX_OK 0
106 #define EX_NONE 1
107 #define EX_FAIL 127
108
109 /*----- Error reporting ---------------------------------------------------*/
110
111 /* --- @ego@ --- *
112 *
113 * Arguments: @const char *p@ = pointer to program name
114 *
115 * Returns: ---
116 *
117 * Use: Stores what the program's name is.
118 */
119
120 extern void ego(const char */*p*/);
121
122 /* --- @pquis@ --- *
123 *
124 * Arguments: @FILE *fp@ = output stream to write on
125 * @const char *p@ = pointer to string to write
126 *
127 * Returns: Zero if everything worked, EOF if not.
128 *
129 * Use: Writes the string @p@ to the output stream @fp@. Occurrences
130 * of the character `$' in @p@ are replaced by the program name
131 * as reported by @quis@. A `$$' is replaced by a single `$'
132 * sign.
133 */
134
135 extern int pquis(FILE */*fp*/, const char */*p*/);
136
137 /* --- @die@ --- *
138 *
139 * Arguments: @const char *f@ = a @printf@-style format string
140 * @...@ = other arguments
141 *
142 * Returns: Never.
143 *
144 * Use: Reports an error and exits.
145 */
146
147 extern PRINTF_LIKE(1, 2) NORETURN void die(const char */*f*/, ...);
148
149 /*----- Memory allocation -------------------------------------------------*/
150
151 /* --- @xmalloc@ --- *
152 *
153 * Arguments: @size_t sz@ = size of block to allocate
154 *
155 * Returns: Pointer to allocated block.
156 *
157 * Use: Allocates memory. If there's not enough memory, the
158 * program exits.
159 */
160
161 extern void *xmalloc(size_t /*sz*/);
162
163 /* --- @xrealloc@ --- *
164 *
165 * Arguments: @void *p@ = a pointer to allocated memory
166 * @size_t sz@ = new size of block wanted
167 *
168 * Returns: Pointer to resized block.
169 *
170 * Use: Resizes an allocated block. If there's not enough memory,
171 * the program exits.
172 */
173
174 extern void *xrealloc(void */*p*/, size_t /*sz*/);
175
176 /*----- Dynamic string handling -------------------------------------------*/
177
178 /* --- @dstr_destroy@ --- *
179 *
180 * Arguments: @dstr *d@ = pointer to a dynamic string block
181 *
182 * Returns: ---
183 *
184 * Use: Reclaims the space used by a dynamic string.
185 */
186
187 extern void dstr_destroy(dstr */*d*/);
188
189 /* --- @dstr_reset@ --- *
190 *
191 * Arguments: @dstr *d@ = pointer to a dynamic string block
192 *
193 * Returns: ---
194 *
195 * Use: Resets a string so that new data gets put at the beginning.
196 */
197
198 extern void dstr_reset(dstr */*d*/);
199
200 /* --- @dstr_ensure@ --- *
201 *
202 * Arguments: @dstr *d@ = pointer to a dynamic string block
203 * @size_t sz@ = amount of free space to ensure
204 *
205 * Returns: ---
206 *
207 * Use: Ensures that at least @sz@ bytes are available in the
208 * given string.
209 */
210
211 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
212
213 /* --- @dstr_putline@ --- *
214 *
215 * Arguments: @dstr *d@ = pointer to a dynamic string block
216 * @FILE *fp@ = a stream to read from
217 *
218 * Returns: The number of characters read into the buffer, or @EOF@ if
219 * end-of-file was reached before any characters were read.
220 *
221 * Use: Appends the next line from the given input stream to the
222 * string. A trailing newline is not added; a trailing null
223 * byte is appended, as for @dstr_putz@.
224 */
225
226 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
227
228 /*----- Infrastructure ----------------------------------------------------*/
229
230 /* --- @atend_register@ --- *
231 *
232 * Arguments: @int (*func)(void *)@ = function to call
233 * @void *p@ = handle to pass to it
234 *
235 * Returns: ---
236 *
237 * Use: Adds a function to the list of things to do at the end of the
238 * program. The function should return nonzero if it produced
239 * any output.
240 */
241
242 extern void atend_register(int (*/*func*/)(void */*p*/), void */*p*/);
243
244 /*----- That's all, folks -------------------------------------------------*/
245
246 #ifdef __cplusplus
247 }
248 #endif
249
250 #endif