1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
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.
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.
32 * ----------------------------------------------------------------------- */
35 * nasmlib.c library routines for the Netwide Assembler
46 * Prepare a table of tolower() results. This avoids function calls
50 unsigned char nasm_tolower_tab
[256];
52 void tolower_init(void)
56 for (i
= 0; i
< 256; i
++)
57 nasm_tolower_tab
[i
] = tolower(i
);
61 int nasm_stricmp(const char *s1
, const char *s2
)
67 c1
= nasm_tolower(*s1
++);
68 c2
= nasm_tolower(*s2
++);
81 int nasm_strnicmp(const char *s1
, const char *s2
, size_t n
)
87 c1
= nasm_tolower(*s1
++);
88 c2
= nasm_tolower(*s2
++);
100 int nasm_memicmp(const char *s1
, const char *s2
, size_t n
)
102 unsigned char c1
, c2
;
106 c1
= nasm_tolower(*s1
++);
107 c2
= nasm_tolower(*s2
++);
116 char *nasm_strsep(char **stringp
, const char *delim
)
124 e
= strpbrk(s
, delim
);
133 /* skip leading spaces */
134 char *nasm_skip_spaces(const char *p
)
137 while (*p
&& nasm_isspace(*p
))
142 /* skip leading non-spaces */
143 char *nasm_skip_word(const char *p
)
146 while (*p
&& !nasm_isspace(*p
))
151 /* zap leading spaces with zero */
152 char *nasm_zap_spaces_fwd(char *p
)
155 while (*p
&& nasm_isspace(*p
))
160 /* zap spaces with zero in reverse order */
161 char *nasm_zap_spaces_rev(char *p
)
164 while (*p
&& nasm_isspace(*p
))
169 /* zap leading and trailing spaces */
170 char *nasm_trim_spaces(char *p
)
172 p
= nasm_zap_spaces_fwd(p
);
173 nasm_zap_spaces_fwd(nasm_skip_word(p
));
179 * return the word extracted from a stream
180 * or NULL if nothing left
182 char *nasm_get_word(char *p
, char **tail
)
184 char *word
= nasm_skip_spaces(p
);
185 char *next
= nasm_skip_word(word
);
193 /* NOTE: the tail may start with spaces */
200 * Extract "opt=val" values from the stream and
204 * 1) If "=val" passed the NULL returned though
205 * you may continue handling the tail via "next"
206 * 2) If "=" passed the NULL is returned and "val"
207 * is set to NULL as well
209 char *nasm_opt_val(char *p
, char **val
, char **next
)
215 p
= nasm_get_word(p
, &nxt
);
227 q
= nasm_get_word(q
+ 1, &nxt
);
232 q
= nasm_skip_spaces(nxt
);
233 if (q
&& *q
== '=') {
234 q
= nasm_get_word(q
+ 1, &nxt
);