1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
/*
* File helper functions.
*
* Copyright (C) 2001-2007 Peter Johnson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <util.h>
/* Need either unistd.h or direct.h to prototype getcwd() and mkdir() */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
#ifdef _WIN32
#include <io.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <ctype.h>
#include <errno.h>
#include "errwarn.h"
#include "file.h"
#define BSIZE 8192 /* Fill block size */
void
yasm_scanner_initialize(yasm_scanner *s)
{
s->bot = NULL;
s->tok = NULL;
s->ptr = NULL;
s->cur = NULL;
s->lim = NULL;
s->top = NULL;
s->eof = NULL;
}
void
yasm_scanner_delete(yasm_scanner *s)
{
if (s->bot) {
yasm_xfree(s->bot);
s->bot = NULL;
}
}
int
yasm_fill_helper(yasm_scanner *s, unsigned char **cursor,
size_t (*input_func) (void *d, unsigned char *buf,
size_t max),
void *input_func_data)
{
size_t cnt;
int first = 0;
if (s->eof)
return 0;
cnt = s->tok - s->bot;
if (cnt > 0) {
memmove(s->bot, s->tok, (size_t)(s->lim - s->tok));
s->tok = s->bot;
s->ptr -= cnt;
*cursor -= cnt;
s->lim -= cnt;
}
if (!s->bot)
first = 1;
if ((s->top - s->lim) < BSIZE) {
unsigned char *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE);
memcpy(buf, s->tok, (size_t)(s->lim - s->tok));
s->tok = buf;
s->ptr = &buf[s->ptr - s->bot];
*cursor = &buf[*cursor - s->bot];
s->lim = &buf[s->lim - s->bot];
s->top = &s->lim[BSIZE];
if (s->bot)
yasm_xfree(s->bot);
s->bot = buf;
}
if ((cnt = input_func(input_func_data, s->lim, BSIZE)) == 0) {
s->eof = &s->lim[cnt];
*s->eof++ = '\n';
}
s->lim += cnt;
return first;
}
void
yasm_unescape_cstring(unsigned char *str, size_t *len)
{
unsigned char *s = str;
unsigned char *o = str;
unsigned char t[4];
while ((size_t)(s-str)<*len) {
if (*s == '\\' && (size_t)(&s[1]-str)<*len) {
s++;
switch (*s) {
case 'b': *o = '\b'; s++; break;
case 'f': *o = '\f'; s++; break;
case 'n': *o = '\n'; s++; break;
case 'r': *o = '\r'; s++; break;
case 't': *o = '\t'; s++; break;
case 'x':
/* hex escape; grab last two digits */
s++;
while ((size_t)(&s[2]-str)<*len && isxdigit(s[0])
&& isxdigit(s[1]) && isxdigit(s[2]))
s++;
if ((size_t)(s-str)<*len && isxdigit(*s)) {
t[0] = *s++;
t[1] = '\0';
t[2] = '\0';
if ((size_t)(s-str)<*len && isxdigit(*s))
t[1] = *s++;
*o = (unsigned char)strtoul((char *)t, NULL, 16);
} else
*o = '\0';
break;
default:
if (isdigit(*s)) {
int warn = 0;
/* octal escape */
if (*s > '7')
warn = 1;
*o = *s++ - '0';
if ((size_t)(s-str)<*len && isdigit(*s)) {
if (*s > '7')
warn = 1;
*o <<= 3;
*o += *s++ - '0';
if ((size_t)(s-str)<*len && isdigit(*s)) {
if (*s > '7')
warn = 1;
*o <<= 3;
*o += *s++ - '0';
}
}
if (warn)
yasm_warn_set(YASM_WARN_GENERAL,
N_("octal value out of range"));
} else
*o = *s++;
break;
}
o++;
} else
*o++ = *s++;
}
*len = o-str;
}
size_t
yasm__splitpath_unix(const char *path, /*@out@*/ const char **tail)
{
const char *s;
s = strrchr(path, '/');
if (!s) {
/* No head */
*tail = path;
return 0;
}
*tail = s+1;
/* Strip trailing ./ on path */
while ((s-1)>=path && *(s-1) == '.' && *s == '/'
&& !((s-2)>=path && *(s-2) == '.'))
s -= 2;
/* Strip trailing slashes on path (except leading) */
while (s>path && *s == '/')
s--;
/* Return length of head */
return s-path+1;
}
size_t
yasm__splitpath_win(const char *path, /*@out@*/ const char **tail)
{
const char *basepath = path;
const char *s;
/* split off drive letter first, if any */
if (isalpha(path[0]) && path[1] == ':')
basepath += 2;
s = basepath;
while (*s != '\0')
s++;
while (s >= basepath && *s != '\\' && *s != '/')
s--;
if (s < basepath) {
*tail = basepath;
if (path == basepath)
return 0; /* No head */
else
return 2; /* Drive letter is head */
}
*tail = s+1;
/* Strip trailing .\ or ./ on path */
while ((s-1)>=basepath && *(s-1) == '.' && (*s == '/' || *s == '\\')
&& !((s-2)>=basepath && *(s-2) == '.'))
s -= 2;
/* Strip trailing slashes on path (except leading) */
while (s>basepath && (*s == '/' || *s == '\\'))
s--;
/* Return length of head */
return s-path+1;
}
char *
yasm__getcwd(void)
{
char *buf;
size_t size;
size = 1024;
buf = yasm_xmalloc(size);
if (getenv("YASM_TEST_SUITE")) {
strcpy(buf, "./");
return buf;
}
while (getcwd(buf, size-1) == NULL) {
if (errno != ERANGE) {
yasm__fatal(N_("could not determine current working directory"));
yasm_xfree(buf);
return NULL;
}
size *= 2;
buf = yasm_xrealloc(buf, size);
}
/* append a '/' if not already present */
size = strlen(buf);
if (buf[size-1] != '\\' && buf[size-1] != '/') {
buf[size] = '/';
buf[size+1] = '\0';
}
return buf;
}
char *
yasm__abspath(const char *path)
{
char *curdir, *abspath;
curdir = yasm__getcwd();
abspath = yasm__combpath(curdir, path);
yasm_xfree(curdir);
return abspath;
}
char *
yasm__combpath_unix(const char *from, const char *to)
{
const char *tail;
size_t pathlen, i, j;
char *out;
if (to[0] == '/') {
/* absolute "to" */
out = yasm_xmalloc(strlen(to)+1);
/* Combine any double slashes when copying */
for (j=0; *to; to++) {
if (*to == '/' && *(to+1) == '/')
continue;
out[j++] = *to;
}
out[j++] = '\0';
return out;
}
/* Get path component; note this strips trailing slash */
pathlen = yasm__splitpath_unix(from, &tail);
out = yasm_xmalloc(pathlen+strlen(to)+2); /* worst case maximum len */
/* Combine any double slashes when copying */
for (i=0, j=0; i<pathlen; i++) {
if (i<pathlen-1 && from[i] == '/' && from[i+1] == '/')
continue;
out[j++] = from[i];
}
pathlen = j;
/* Add trailing slash back in */
if (pathlen > 0 && out[pathlen-1] != '/')
out[pathlen++] = '/';
/* Now scan from left to right through "to", stripping off "." and "..";
* if we see "..", back up one directory in out unless last directory in
* out is also "..".
*
* Note this does NOT back through ..'s in the "from" path; this is just
* as well as that could skip symlinks (e.g. "foo/bar/.." might not be
* the same as "foo").
*/
for (;;) {
if (to[0] == '.' && to[1] == '/') {
to += 2; /* current directory */
while (*to == '/')
to++; /* strip off any additional slashes */
} else if (pathlen == 0)
break; /* no more "from" path left, we're done */
else if (to[0] == '.' && to[1] == '.' && to[2] == '/') {
if (pathlen >= 3 && out[pathlen-1] == '/' && out[pathlen-2] == '.'
&& out[pathlen-3] == '.') {
/* can't ".." against a "..", so we're done. */
break;
}
to += 3; /* throw away "../" */
while (*to == '/')
to++; /* strip off any additional slashes */
/* and back out last directory in "out" if not already at root */
if (pathlen > 1) {
pathlen--; /* strip off trailing '/' */
while (pathlen > 0 && out[pathlen-1] != '/')
pathlen--;
}
} else
break;
}
/* Copy "to" to tail of output, and we're done */
/* Combine any double slashes when copying */
for (j=pathlen; *to; to++) {
if (*to == '/' && *(to+1) == '/')
continue;
out[j++] = *to;
}
out[j++] = '\0';
return out;
}
char *
yasm__combpath_win(const char *from, const char *to)
{
const char *tail;
size_t pathlen, i, j;
char *out;
if ((isalpha(to[0]) && to[1] == ':') || (to[0] == '/' || to[0] == '\\')) {
/* absolute or drive letter "to" */
out = yasm_xmalloc(strlen(to)+1);
/* Combine any double slashes when copying */
for (j=0; *to; to++) {
if ((*to == '/' || *to == '\\')
&& (*(to+1) == '/' || *(to+1) == '\\'))
continue;
if (*to == '/')
out[j++] = '\\';
else
out[j++] = *to;
}
out[j++] = '\0';
return out;
}
/* Get path component; note this strips trailing slash */
pathlen = yasm__splitpath_win(from, &tail);
out = yasm_xmalloc(pathlen+strlen(to)+2); /* worst case maximum len */
/* Combine any double slashes when copying */
for (i=0, j=0; i<pathlen; i++) {
if (i<pathlen-1 && (from[i] == '/' || from[i] == '\\')
&& (from[i+1] == '/' || from[i+1] == '\\'))
continue;
if (from[i] == '/')
out[j++] = '\\';
else
out[j++] = from[i];
}
pathlen = j;
/* Add trailing slash back in, unless it's only a raw drive letter */
if (pathlen > 0 && out[pathlen-1] != '\\'
&& !(pathlen == 2 && isalpha(out[0]) && out[1] == ':'))
out[pathlen++] = '\\';
/* Now scan from left to right through "to", stripping off "." and "..";
* if we see "..", back up one directory in out unless last directory in
* out is also "..".
*
* Note this does NOT back through ..'s in the "from" path; this is just
* as well as that could skip symlinks (e.g. "foo/bar/.." might not be
* the same as "foo").
*/
for (;;) {
if (to[0] == '.' && (to[1] == '/' || to[1] == '\\')) {
to += 2; /* current directory */
while (*to == '/' || *to == '\\')
to++; /* strip off any additional slashes */
} else if (pathlen == 0
|| (pathlen == 2 && isalpha(out[0]) && out[1] == ':'))
break; /* no more "from" path left, we're done */
else if (to[0] == '.' && to[1] == '.'
&& (to[2] == '/' || to[2] == '\\')) {
if (pathlen >= 3 && out[pathlen-1] == '\\'
&& out[pathlen-2] == '.' && out[pathlen-3] == '.') {
/* can't ".." against a "..", so we're done. */
break;
}
to += 3; /* throw away "../" (or "..\") */
while (*to == '/' || *to == '\\')
to++; /* strip off any additional slashes */
/* and back out last directory in "out" if not already at root */
if (pathlen > 1) {
pathlen--; /* strip off trailing '/' */
while (pathlen > 0 && out[pathlen-1] != '\\')
pathlen--;
}
} else
break;
}
/* Copy "to" to tail of output, and we're done */
/* Combine any double slashes when copying */
for (j=pathlen; *to; to++) {
if ((*to == '/' || *to == '\\') && (*(to+1) == '/' || *(to+1) == '\\'))
continue;
if (*to == '/')
out[j++] = '\\';
else
out[j++] = *to;
}
out[j++] = '\0';
return out;
}
size_t
yasm__createpath_common(const char *path, int win)
{
const char *pp = path, *pe;
char *ts, *tp;
size_t len, lth;
lth = len = strlen(path);
ts = tp = (char *) malloc(len + 1);
pe = pp + len;
while (pe > pp) {
if ((win && *pe == '\\') || *pe == '/')
break;
--pe;
--lth;
}
while (pp <= pe) {
if (pp == pe || (win && *pp == '\\') || *pp == '/') {
#ifdef _WIN32
struct _finddata_t fi;
intptr_t h;
#elif defined(HAVE_SYS_STAT_H)
struct stat fi;
#endif
*tp = '\0';
#ifdef _WIN32
h = _findfirst(ts, &fi);
if (h != -1) {
if (fi.attrib != _A_SUBDIR) {
_findclose(h);
break;
}
} else if (errno == ENOENT) {
if (_mkdir(ts) == -1) {
_findclose(h);
lth = -1;
break;
}
}
_findclose(h);
#elif defined(HAVE_SYS_STAT_H)
if (stat(ts, &fi) != -1) {
if (!S_ISDIR(fi.st_mode))
break;
} else if (errno == ENOENT) {
if (mkdir(ts, 0755) == -1) {
lth = 0;
break;
}
}
#else
break;
#endif
}
*tp++ = *pp++;
}
free(ts);
return lth;
}
typedef struct incpath {
STAILQ_ENTRY(incpath) link;
/*@owned@*/ char *path;
} incpath;
STAILQ_HEAD(incpath_head, incpath) incpaths = STAILQ_HEAD_INITIALIZER(incpaths);
FILE *
yasm_fopen_include(const char *iname, const char *from, const char *mode,
char **oname)
{
FILE *f;
char *combine;
incpath *np;
/* Try directly relative to from first, then each of the include paths */
if (from) {
combine = yasm__combpath(from, iname);
f = fopen(combine, mode);
if (f) {
if (oname)
*oname = combine;
else
yasm_xfree(combine);
return f;
}
yasm_xfree(combine);
}
STAILQ_FOREACH(np, &incpaths, link) {
combine = yasm__combpath(np->path, iname);
f = fopen(combine, mode);
if (f) {
if (oname)
*oname = combine;
else
yasm_xfree(combine);
return f;
}
yasm_xfree(combine);
}
if (oname)
*oname = NULL;
return NULL;
}
void
yasm_delete_include_paths(void)
{
incpath *n1, *n2;
n1 = STAILQ_FIRST(&incpaths);
while (n1) {
n2 = STAILQ_NEXT(n1, link);
yasm_xfree(n1->path);
yasm_xfree(n1);
n1 = n2;
}
STAILQ_INIT(&incpaths);
}
const char *
yasm_get_include_dir(void **iter)
{
incpath *p = (incpath *)*iter;
if (!p)
p = STAILQ_FIRST(&incpaths);
else
p = STAILQ_NEXT(p, link);
*iter = p;
if (p)
return p->path;
else
return NULL;
}
void
yasm_add_include_path(const char *path)
{
incpath *np = yasm_xmalloc(sizeof(incpath));
size_t len = strlen(path);
np->path = yasm_xmalloc(len+2);
memcpy(np->path, path, len+1);
/* Add trailing slash if it is missing */
if (path[len-1] != '\\' && path[len-1] != '/') {
np->path[len] = '/';
np->path[len+1] = '\0';
}
STAILQ_INSERT_TAIL(&incpaths, np, link);
}
size_t
yasm_fwrite_16_l(unsigned short val, FILE *f)
{
if (fputc(val & 0xFF, f) == EOF)
return 0;
if (fputc((val >> 8) & 0xFF, f) == EOF)
return 0;
return 1;
}
size_t
yasm_fwrite_32_l(unsigned long val, FILE *f)
{
if (fputc((int)(val & 0xFF), f) == EOF)
return 0;
if (fputc((int)((val >> 8) & 0xFF), f) == EOF)
return 0;
if (fputc((int)((val >> 16) & 0xFF), f) == EOF)
return 0;
if (fputc((int)((val >> 24) & 0xFF), f) == EOF)
return 0;
return 1;
}
size_t
yasm_fwrite_16_b(unsigned short val, FILE *f)
{
if (fputc((val >> 8) & 0xFF, f) == EOF)
return 0;
if (fputc(val & 0xFF, f) == EOF)
return 0;
return 1;
}
size_t
yasm_fwrite_32_b(unsigned long val, FILE *f)
{
if (fputc((int)((val >> 24) & 0xFF), f) == EOF)
return 0;
if (fputc((int)((val >> 16) & 0xFF), f) == EOF)
return 0;
if (fputc((int)((val >> 8) & 0xFF), f) == EOF)
return 0;
if (fputc((int)(val & 0xFF), f) == EOF)
return 0;
return 1;
}
|