file.c 18 KB

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