]> git.cameronkatri.com Git - mandoc.git/blob - mandoc_aux.c
Add a new term_flushln() flag TERMP_BRIND (if break, then indent)
[mandoc.git] / mandoc_aux.c
1 /* $Id: mandoc_aux.c,v 1.1 2014/03/23 11:59:17 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 char *
84 mandoc_strdup(const char *ptr)
85 {
86 char *p;
87
88 p = strdup(ptr);
89 if (NULL == p) {
90 perror(NULL);
91 exit((int)MANDOCLEVEL_SYSERR);
92 }
93 return(p);
94 }
95
96 char *
97 mandoc_strndup(const char *ptr, size_t sz)
98 {
99 char *p;
100
101 p = mandoc_malloc(sz + 1);
102 memcpy(p, ptr, sz);
103 p[(int)sz] = '\0';
104 return(p);
105 }