]> git.cameronkatri.com Git - mandoc.git/blob - xstd.c
Fixed mdoc_phrase escape handling.
[mandoc.git] / xstd.c
1 /* $Id: xstd.c,v 1.10 2009/03/16 22:19:19 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "private.h"
25
26 /*
27 * Contains wrappers for common functions to simplify their general
28 * usage throughout this codebase.
29 */
30
31 int
32 xstrcmp(const char *p1, const char *p2)
33 {
34
35 return(0 == strcmp(p1, p2));
36 }
37
38 int
39 xstrlcat(char *dst, const char *src, size_t sz)
40 {
41
42 return(strlcat(dst, src, sz) < sz);
43 }
44
45 int
46 xstrlcpy(char *dst, const char *src, size_t sz)
47 {
48
49 return(strlcpy(dst, src, sz) < sz);
50 }
51
52 void *
53 xrealloc(void *ptr, size_t sz)
54 {
55 void *p;
56
57 if (NULL == (p = realloc(ptr, sz)))
58 err(EXIT_FAILURE, "realloc");
59 return(p);
60 }
61
62 void *
63 xcalloc(size_t num, size_t sz)
64 {
65 void *p;
66
67 if (NULL == (p = calloc(num, sz)))
68 err(EXIT_FAILURE, "calloc");
69 return(p);
70 }
71
72 char *
73 xstrdup(const char *p)
74 {
75 char *pp;
76
77 if (NULL == (pp = strdup(p)))
78 err(EXIT_FAILURE, "strdup");
79 return(pp);
80 }
81
82 int
83 xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz)
84 {
85 char *p;
86
87 assert(sz > 0);
88 assert(buf);
89 *buf = 0;
90
91 for ( ; n; n = n->next) {
92 assert(MDOC_TEXT == n->type);
93 p = n->string;
94 if ( ! xstrlcat(buf, p, sz))
95 return(0);
96 if (n->next && ! xstrlcat(buf, " ", sz))
97 return(0);
98 }
99
100 return(1);
101 }