]> git.cameronkatri.com Git - mandoc.git/blob - mandoc_aux.c
Implement the obsolete macros .En .Es .Fr .Ot for backward compatibility,
[mandoc.git] / mandoc_aux.c
1 /* $Id: mandoc_aux.c,v 1.2 2014/04/23 21:06:41 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/types.h>
19
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "mandoc.h"
26 #include "mandoc_aux.h"
27
28 int
29 mandoc_asprintf(char **dest, const char *fmt, ...)
30 {
31 va_list ap;
32 int ret;
33
34 va_start(ap, fmt);
35 ret = vasprintf(dest, fmt, ap);
36 va_end(ap);
37
38 if (-1 == ret) {
39 perror(NULL);
40 exit((int)MANDOCLEVEL_SYSERR);
41 }
42 return(ret);
43 }
44
45 void *
46 mandoc_calloc(size_t num, size_t size)
47 {
48 void *ptr;
49
50 ptr = calloc(num, size);
51 if (NULL == ptr) {
52 perror(NULL);
53 exit((int)MANDOCLEVEL_SYSERR);
54 }
55 return(ptr);
56 }
57
58 void *
59 mandoc_malloc(size_t size)
60 {
61 void *ptr;
62
63 ptr = malloc(size);
64 if (NULL == ptr) {
65 perror(NULL);
66 exit((int)MANDOCLEVEL_SYSERR);
67 }
68 return(ptr);
69 }
70
71 void *
72 mandoc_realloc(void *ptr, size_t size)
73 {
74
75 ptr = realloc(ptr, size);
76 if (NULL == ptr) {
77 perror(NULL);
78 exit((int)MANDOCLEVEL_SYSERR);
79 }
80 return(ptr);
81 }
82
83 void *
84 mandoc_reallocarray(void *ptr, size_t num, size_t size)
85 {
86
87 ptr = reallocarray(ptr, num, size);
88 if (NULL == ptr) {
89 perror(NULL);
90 exit((int)MANDOCLEVEL_SYSERR);
91 }
92 return(ptr);
93 }
94
95 char *
96 mandoc_strdup(const char *ptr)
97 {
98 char *p;
99
100 p = strdup(ptr);
101 if (NULL == p) {
102 perror(NULL);
103 exit((int)MANDOCLEVEL_SYSERR);
104 }
105 return(p);
106 }
107
108 char *
109 mandoc_strndup(const char *ptr, size_t sz)
110 {
111 char *p;
112
113 p = mandoc_malloc(sz + 1);
114 memcpy(p, ptr, sz);
115 p[(int)sz] = '\0';
116 return(p);
117 }