xref: /openbsd-src/libexec/tradcpp/utils.h (revision f9343feac8314427583210aa64cbdde958db9030)
1a9b3ff1aSjsg /*-
2a9b3ff1aSjsg  * Copyright (c) 2010 The NetBSD Foundation, Inc.
3a9b3ff1aSjsg  * All rights reserved.
4a9b3ff1aSjsg  *
5a9b3ff1aSjsg  * This code is derived from software contributed to The NetBSD Foundation
6a9b3ff1aSjsg  * by David A. Holland.
7a9b3ff1aSjsg  *
8a9b3ff1aSjsg  * Redistribution and use in source and binary forms, with or without
9a9b3ff1aSjsg  * modification, are permitted provided that the following conditions
10a9b3ff1aSjsg  * are met:
11a9b3ff1aSjsg  * 1. Redistributions of source code must retain the above copyright
12a9b3ff1aSjsg  *    notice, this list of conditions and the following disclaimer.
13a9b3ff1aSjsg  * 2. Redistributions in binary form must reproduce the above copyright
14a9b3ff1aSjsg  *    notice, this list of conditions and the following disclaimer in the
15a9b3ff1aSjsg  *    documentation and/or other materials provided with the distribution.
16a9b3ff1aSjsg  *
17a9b3ff1aSjsg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18a9b3ff1aSjsg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19a9b3ff1aSjsg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20a9b3ff1aSjsg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21a9b3ff1aSjsg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22a9b3ff1aSjsg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23a9b3ff1aSjsg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24a9b3ff1aSjsg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25a9b3ff1aSjsg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26a9b3ff1aSjsg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27a9b3ff1aSjsg  * POSSIBILITY OF SUCH DAMAGE.
28a9b3ff1aSjsg  */
29a9b3ff1aSjsg 
30a9b3ff1aSjsg #include <stddef.h>
31*f9343feaSjsg #include "bool.h"
32a9b3ff1aSjsg 
33a9b3ff1aSjsg struct place;
34a9b3ff1aSjsg 
35a9b3ff1aSjsg #if defined(__CLANG__) || defined(__GNUC__)
36a9b3ff1aSjsg #define PF(a, b) __attribute__((__format__(__printf__, a, b)))
37a9b3ff1aSjsg #define DEAD __attribute__((__noreturn__))
38a9b3ff1aSjsg #define UNUSED __attribute__((__unused__))
39a9b3ff1aSjsg #else
40a9b3ff1aSjsg #define PF(a, b)
41a9b3ff1aSjsg #define DEAD
42a9b3ff1aSjsg #define UNUSED
43a9b3ff1aSjsg #endif
44a9b3ff1aSjsg 
45a9b3ff1aSjsg #define HOWMANY(arr) (sizeof(arr)/sizeof((arr)[0]))
46a9b3ff1aSjsg 
47a9b3ff1aSjsg extern const char ws[];
48a9b3ff1aSjsg extern const char alnum[];
49a9b3ff1aSjsg 
50a9b3ff1aSjsg 
51a9b3ff1aSjsg void *domalloc(size_t len);
52a9b3ff1aSjsg void *dorealloc(void *ptr, size_t oldlen, size_t newlen);
53a9b3ff1aSjsg void dofree(void *ptr, size_t len);
54a9b3ff1aSjsg 
55a9b3ff1aSjsg char *dostrdup(const char *s);
56a9b3ff1aSjsg char *dostrdup2(const char *s, const char *t);
57a9b3ff1aSjsg char *dostrdup3(const char *s, const char *t, const char *u);
58a9b3ff1aSjsg char *dostrndup(const char *s, size_t len);
59a9b3ff1aSjsg void dostrfree(char *s);
60a9b3ff1aSjsg 
61a9b3ff1aSjsg size_t notrailingws(char *buf, size_t len);
62a9b3ff1aSjsg bool is_identifier(const char *str);
63a9b3ff1aSjsg 
64a9b3ff1aSjsg /* in place.c */
65a9b3ff1aSjsg void complain_init(const char *progname);
66*f9343feaSjsg PF(2, 3) void complain(const struct place *, const char *fmt, ...);
67a9b3ff1aSjsg void complain_fail(void);
68a9b3ff1aSjsg bool complain_failed(void);
69a9b3ff1aSjsg 
70*f9343feaSjsg void debuglog_open(const struct place *p, /*const*/ char *file);
71*f9343feaSjsg void debuglog_close(void);
72*f9343feaSjsg PF(2, 3) void debuglog(const struct place *p, const char *fmt, ...);
73*f9343feaSjsg PF(3, 4) void debuglog2(const struct place *p, const struct place *p2,
74*f9343feaSjsg 			const char *fmt, ...);
75*f9343feaSjsg 
76a9b3ff1aSjsg /* in main.c */
77a9b3ff1aSjsg void freestringlater(char *s);
78a9b3ff1aSjsg DEAD void die(void);
79