]> git.proxmox.com Git - mirror_frr.git/blob - lib/printfrr.h
lib: record output positions in printfrr
[mirror_frr.git] / lib / printfrr.h
1 /*
2 * Copyright (c) 2019 David Lamparter, for NetDEF, Inc.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _FRR_PRINTFRR_H
18 #define _FRR_PRINTFRR_H
19
20 #include <stddef.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23
24 #include "compiler.h"
25 #include "memory.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 struct fmt_outpos {
32 unsigned int off_start, off_end;
33 };
34
35 struct fbuf {
36 char *buf;
37 char *pos;
38 size_t len;
39
40 struct fmt_outpos *outpos;
41 size_t outpos_n, outpos_i;
42 };
43
44 #define at(a, b) PRINTFRR(a, b)
45 #define atn(a, b) \
46 at(a, b) __attribute__((nonnull(1) _RET_NONNULL))
47 #define atm(a, b) \
48 atn(a, b) __attribute__((malloc))
49
50 /* return value is length needed for the full string (excluding \0) in all
51 * cases. The functions write as much as they can, but continue regardless,
52 * so the return value is independent of buffer length. Both bprintfrr and
53 * snprintf also accept NULL as output buffer.
54 */
55
56 /* bprintfrr does NOT null terminate! use sparingly (only provided since it's
57 * the most direct interface) - useful for incrementally building long text
58 * (call bprintfrr repeatedly with the same buffer)
59 */
60 ssize_t vbprintfrr(struct fbuf *out, const char *fmt, va_list) at(2, 0);
61 ssize_t bprintfrr(struct fbuf *out, const char *fmt, ...) at(2, 3);
62
63 /* these do null terminate like their snprintf cousins */
64 ssize_t vsnprintfrr(char *out, size_t sz, const char *fmt, va_list) at(3, 0);
65 ssize_t snprintfrr(char *out, size_t sz, const char *fmt, ...) at(3, 4);
66
67 /* c = continue / concatenate (append at the end of the string)
68 * return value is would-be string length (regardless of buffer length),
69 * i.e. includes already written chars */
70 ssize_t vcsnprintfrr(char *out, size_t sz, const char *fmt, va_list) at(3, 0);
71 ssize_t csnprintfrr(char *out, size_t sz, const char *fmt, ...) at(3, 4);
72
73 /* memory allocations don't fail in FRR, so you always get something here.
74 * (in case of error, returns a strdup of the format string) */
75 char *vasprintfrr(struct memtype *mt, const char *fmt, va_list) atm(2, 0);
76 char *asprintfrr(struct memtype *mt, const char *fmt, ...) atm(2, 3);
77
78 /* try to use provided buffer (presumably from stack), allocate if it's too
79 * short. Must call XFREE(mt, return value) if return value != out.
80 */
81 char *vasnprintfrr(struct memtype *mt, char *out, size_t sz,
82 const char *fmt, va_list) atn(4, 0);
83 char *asnprintfrr(struct memtype *mt, char *out, size_t sz,
84 const char *fmt, ...) atn(4, 5);
85
86 #define printfrr(fmt, ...) \
87 do { \
88 char buf[256], *out; \
89 out = asnprintfrr(MTYPE_TMP, buf, sizeof(buf), fmt, \
90 ##__VA_ARGS__); \
91 fputs(out, stdout); \
92 if (out != buf) \
93 XFREE(MTYPE_TMP, out); \
94 } while (0)
95
96 #undef at
97 #undef atm
98 #undef atn
99
100 /* extension specs must start with a capital letter (this is a restriction
101 * for both performance's and human understanding's sake.)
102 *
103 * Note that the entire thing mostly works because a letter directly following
104 * a %p print specifier is extremely unlikely to occur (why would you want to
105 * print "0x12345678HELLO"?) Normally, you'd expect spacing or punctuation
106 * after a placeholder. That also means that neither of those works well for
107 * extension purposes, e.g. "%p{foo}" is reasonable to see actually used.
108 *
109 * TODO: would be nice to support a "%pF%dF" specifier that consumes 2
110 * arguments, e.g. to pass an integer + a list of known values... can be
111 * done, but a bit tricky.
112 */
113 #define printfrr_ext_char(ch) ((ch) >= 'A' && (ch) <= 'Z')
114
115 struct printfrr_ext {
116 /* embedded string to minimize cache line pollution */
117 char match[8];
118
119 /* both can be given, if not the code continues searching
120 * (you can do %pX and %dX in 2 different entries)
121 *
122 * return value: number of bytes that would be printed if the buffer
123 * was large enough. be careful about not under-reporting this;
124 * otherwise asnprintf() & co. will get broken. Returning -1 means
125 * something went wrong & default %p/%d handling should be executed.
126 *
127 * to consume extra input flags after %pXY, increment *fmt. It points
128 * at the first character after %pXY at entry. Convention is to make
129 * those flags lowercase letters or numbers.
130 *
131 * prec is the precision specifier (the 999 in "%.999p") -1 means
132 * none given (value in the format string cannot be negative)
133 */
134 ssize_t (*print_ptr)(struct fbuf *buf, const char **fmt, int prec,
135 const void *);
136 ssize_t (*print_int)(struct fbuf *buf, const char **fmt, int prec,
137 uintmax_t);
138 };
139
140 /* no locking - must be called when single threaded (e.g. at startup.)
141 * this restriction hopefully won't be a huge bother considering normal usage
142 * scenarios...
143 */
144 void printfrr_ext_reg(const struct printfrr_ext *);
145
146 #define printfrr_ext_autoreg_p(matchs, print_fn) \
147 static ssize_t print_fn(struct fbuf *, const char **, int, \
148 const void *); \
149 static const struct printfrr_ext _printext_##print_fn = { \
150 .match = matchs, \
151 .print_ptr = print_fn, \
152 }; \
153 static void _printreg_##print_fn(void) __attribute__((constructor)); \
154 static void _printreg_##print_fn(void) { \
155 printfrr_ext_reg(&_printext_##print_fn); \
156 } \
157 /* end */
158
159 #define printfrr_ext_autoreg_i(matchs, print_fn) \
160 static ssize_t print_fn(struct fbuf *, const char **, int, uintmax_t); \
161 static const struct printfrr_ext _printext_##print_fn = { \
162 .match = matchs, \
163 .print_int = print_fn, \
164 }; \
165 static void _printreg_##print_fn(void) __attribute__((constructor)); \
166 static void _printreg_##print_fn(void) { \
167 printfrr_ext_reg(&_printext_##print_fn); \
168 } \
169 /* end */
170
171 /* fbuf helper functions */
172
173 static inline ssize_t bputs(struct fbuf *buf, const char *str)
174 {
175 size_t len = strlen(str);
176 size_t ncopy;
177
178 if (!buf)
179 return len;
180
181 ncopy = MIN(len, (size_t)(buf->buf + buf->len - buf->pos));
182 memcpy(buf->pos, str, ncopy);
183 buf->pos += ncopy;
184
185 return len;
186 }
187
188 static inline ssize_t bputch(struct fbuf *buf, char ch)
189 {
190 if (buf && buf->pos < buf->buf + buf->len)
191 *buf->pos++ = ch;
192 return 1;
193 }
194
195 #ifdef __cplusplus
196 }
197 #endif
198
199 #endif