blob: 4ee3ecbbe2f9bce417463f6b9d3f03ab0fa65540 [file] [log] [blame]
H. Peter Anvin22538e22016-05-25 05:42:47 -07001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * nasmlib.c library routines for the Netwide Assembler
36 */
37
38#include "compiler.h"
H. Peter Anvin22538e22016-05-25 05:42:47 -070039#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080040#include "nctype.h"
H. Peter Anvin22538e22016-05-25 05:42:47 -070041
42#ifndef nasm_stricmp
43int nasm_stricmp(const char *s1, const char *s2)
44{
45 unsigned char c1, c2;
46 int d;
47
48 while (1) {
49 c1 = nasm_tolower(*s1++);
50 c2 = nasm_tolower(*s2++);
51 d = c1-c2;
52
53 if (d)
54 return d;
55 if (!c1)
56 break;
57 }
58 return 0;
59}
60#endif
61
62#ifndef nasm_strnicmp
63int nasm_strnicmp(const char *s1, const char *s2, size_t n)
64{
65 unsigned char c1, c2;
66 int d;
67
68 while (n--) {
69 c1 = nasm_tolower(*s1++);
70 c2 = nasm_tolower(*s2++);
71 d = c1-c2;
72
73 if (d)
74 return d;
75 if (!c1)
76 break;
77 }
78 return 0;
79}
80#endif
81
82int nasm_memicmp(const char *s1, const char *s2, size_t n)
83{
84 unsigned char c1, c2;
85 int d;
86
87 while (n--) {
88 c1 = nasm_tolower(*s1++);
89 c2 = nasm_tolower(*s2++);
90 d = c1-c2;
91 if (d)
92 return d;
93 }
94 return 0;
95}
96
97#ifndef nasm_strsep
98char *nasm_strsep(char **stringp, const char *delim)
99{
100 char *s = *stringp;
101 char *e;
102
103 if (!s)
104 return NULL;
105
106 e = strpbrk(s, delim);
107 if (e)
108 *e++ = '\0';
109
110 *stringp = e;
111 return s;
112}
113#endif
114
115/* skip leading spaces */
116char *nasm_skip_spaces(const char *p)
117{
118 if (p)
119 while (*p && nasm_isspace(*p))
120 p++;
121 return (char *)p;
122}
123
124/* skip leading non-spaces */
125char *nasm_skip_word(const char *p)
126{
127 if (p)
128 while (*p && !nasm_isspace(*p))
129 p++;
130 return (char *)p;
131}
132
133/* zap leading spaces with zero */
134char *nasm_zap_spaces_fwd(char *p)
135{
136 if (p)
137 while (*p && nasm_isspace(*p))
138 *p++ = 0x0;
139 return p;
140}
141
142/* zap spaces with zero in reverse order */
143char *nasm_zap_spaces_rev(char *p)
144{
145 if (p)
146 while (*p && nasm_isspace(*p))
147 *p-- = 0x0;
148 return p;
149}
150
151/* zap leading and trailing spaces */
152char *nasm_trim_spaces(char *p)
153{
154 p = nasm_zap_spaces_fwd(p);
155 nasm_zap_spaces_fwd(nasm_skip_word(p));
156
157 return p;
158}
159
160/*
161 * return the word extracted from a stream
162 * or NULL if nothing left
163 */
164char *nasm_get_word(char *p, char **tail)
165{
166 char *word = nasm_skip_spaces(p);
167 char *next = nasm_skip_word(word);
168
169 if (word && *word) {
170 if (*next)
171 *next++ = '\0';
172 } else
173 word = next = NULL;
174
175 /* NOTE: the tail may start with spaces */
176 *tail = next;
177
178 return word;
179}
180
181/*
182 * Extract "opt=val" values from the stream and
183 * returns "opt"
184 *
185 * Exceptions:
186 * 1) If "=val" passed the NULL returned though
187 * you may continue handling the tail via "next"
188 * 2) If "=" passed the NULL is returned and "val"
189 * is set to NULL as well
190 */
191char *nasm_opt_val(char *p, char **val, char **next)
192{
193 char *q, *nxt;
194
195 *val = *next = NULL;
196
197 p = nasm_get_word(p, &nxt);
198 if (!p)
199 return NULL;
200
201 q = strchr(p, '=');
202 if (q) {
203 if (q == p)
204 p = NULL;
205 *q++='\0';
206 if (*q) {
207 *val = q;
208 } else {
209 q = nasm_get_word(q + 1, &nxt);
210 if (q)
211 *val = q;
212 }
213 } else {
214 q = nasm_skip_spaces(nxt);
215 if (q && *q == '=') {
216 q = nasm_get_word(q + 1, &nxt);
217 if (q)
218 *val = q;
219 }
220 }
221
222 *next = nxt;
223 return p;
224}