]> git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
Lint fixes.
[mandoc.git] / mandoc.c
1 /* $Id: mandoc.c,v 1.5 2009/10/30 05:58:38 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "libmandoc.h"
26
27 int
28 mandoc_special(const char *p)
29 {
30 int c;
31
32 if ('\\' != *p++)
33 return(0);
34
35 switch (*p) {
36 case ('\\'):
37 /* FALLTHROUGH */
38 case ('\''):
39 /* FALLTHROUGH */
40 case ('`'):
41 /* FALLTHROUGH */
42 case ('q'):
43 /* FALLTHROUGH */
44 case ('-'):
45 /* FALLTHROUGH */
46 case ('~'):
47 /* FALLTHROUGH */
48 case ('^'):
49 /* FALLTHROUGH */
50 case ('%'):
51 /* FALLTHROUGH */
52 case ('0'):
53 /* FALLTHROUGH */
54 case (' '):
55 /* FALLTHROUGH */
56 case ('|'):
57 /* FALLTHROUGH */
58 case ('&'):
59 /* FALLTHROUGH */
60 case ('.'):
61 /* FALLTHROUGH */
62 case (':'):
63 /* FALLTHROUGH */
64 case ('c'):
65 return(2);
66 case ('e'):
67 return(2);
68 case ('f'):
69 if (0 == *++p || ! isgraph((u_char)*p))
70 return(0);
71 return(3);
72 case ('*'):
73 if (0 == *++p || ! isgraph((u_char)*p))
74 return(0);
75 switch (*p) {
76 case ('('):
77 if (0 == *++p || ! isgraph((u_char)*p))
78 return(0);
79 return(4);
80 case ('['):
81 for (c = 3, p++; *p && ']' != *p; p++, c++)
82 if ( ! isgraph((u_char)*p))
83 break;
84 return(*p == ']' ? c : 0);
85 default:
86 break;
87 }
88 return(3);
89 case ('('):
90 if (0 == *++p || ! isgraph((u_char)*p))
91 return(0);
92 if (0 == *++p || ! isgraph((u_char)*p))
93 return(0);
94 return(4);
95 case ('['):
96 break;
97 default:
98 return(0);
99 }
100
101 for (c = 3, p++; *p && ']' != *p; p++, c++)
102 if ( ! isgraph((u_char)*p))
103 break;
104
105 return(*p == ']' ? c : 0);
106 }
107
108
109 void *
110 mandoc_calloc(size_t num, size_t size)
111 {
112 void *ptr;
113
114 ptr = calloc(num, size);
115 if (NULL == ptr) {
116 fprintf(stderr, "memory exhausted\n");
117 exit(EXIT_FAILURE);
118 }
119
120 return(ptr);
121 }
122
123
124 void *
125 mandoc_malloc(size_t size)
126 {
127 void *ptr;
128
129 ptr = malloc(size);
130 if (NULL == ptr) {
131 fprintf(stderr, "memory exhausted\n");
132 exit(EXIT_FAILURE);
133 }
134
135 return(ptr);
136 }
137
138
139 void *
140 mandoc_realloc(void *ptr, size_t size)
141 {
142
143 ptr = realloc(ptr, size);
144 if (NULL == ptr) {
145 fprintf(stderr, "memory exhausted\n");
146 exit(EXIT_FAILURE);
147 }
148
149 return(ptr);
150 }
151
152
153 char *
154 mandoc_strdup(const char *ptr)
155 {
156 char *p;
157
158 p = strdup(ptr);
159 if (NULL == p) {
160 fprintf(stderr, "memory exhausted\n");
161 exit(EXIT_FAILURE);
162 }
163
164 return(p);
165 }