filename.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* filename.c Copyright (C) 1995-1997 Codemist Ltd */
  2. /* Signature: 63e0f8da 26-Jan-1999 */
  3. /*
  4. * Map file-names to expand references to shell variables etc.
  5. * and to provide portability of names across operating systems.
  6. */
  7. static char *look_in_lisp_variable(char *o, int prefix)
  8. {
  9. Lisp_Object nil, var;
  10. /*
  11. * I will start by tagging a '$' (or whatever) on in front of the
  12. * parameter name.
  13. */
  14. o[0] = prefix;
  15. var = make_undefined_symbol(o);
  16. nil = C_nil;
  17. /*
  18. * make_undefined_symbol() could fail either if we had utterly run out
  19. * of memory or if somebody generated an interrupt (eg ^C) around now. Ugh.
  20. */
  21. if (exception_pending())
  22. { flip_exception();
  23. return NULL;
  24. }
  25. /*
  26. * If the variable $name was undefined then I use an empty replacement
  27. * text for it. Otherwise I need to look harder at its value.
  28. */
  29. if (qvalue(var) == unset_var) return o;
  30. else
  31. { Header h;
  32. int32 len;
  33. var = qvalue(var);
  34. /*
  35. * Mostly I expect that the value will be a string or symbol.
  36. */
  37. #ifdef COMMON
  38. if (complex_stringp(var))
  39. { var = simplify_string(var);
  40. nil = C_nil;
  41. if (exception_pending())
  42. { flip_exception();
  43. return NULL;
  44. }
  45. }
  46. #endif /* COMMON */
  47. if (symbolp(var))
  48. { var = get_pname(var);
  49. nil = C_nil;
  50. if (exception_pending())
  51. { flip_exception();
  52. return NULL;
  53. }
  54. h = vechdr(var);
  55. }
  56. else if (!is_vector(var) ||
  57. type_of_header(h = vechdr(var)) != TYPE_STRING)
  58. return NULL;
  59. len = length_of_header(h) - 4;
  60. /*
  61. * Copy the characters from the string or from the name of the variable
  62. * into the file-name buffer. There could at present be a crash here
  63. * if the expansion was very very long and overflowed my buffer. Tough
  64. * luck for now - people doing that (maybe) get what they (maybe) deserve.
  65. */
  66. memcpy(o, (char *)var + (4L- TAG_VECTOR), (size_t)len);
  67. o = o + len;
  68. return o;
  69. }
  70. }
  71. static void process_file_name(char *filename, char *old, size_t n)
  72. /*
  73. * This procedure maps filenames by expanding some environment
  74. * variables. It is very thoroughly system specific, which is why it
  75. * is in this file. See also LONGEST_LEGAL_FILENAME in "tags.h" for a
  76. * limit on the permitted size of an expanded filename.
  77. * The input (old) is not necessarily properly terminated as a C string,
  78. * so n says how many characters to inspect. Build a converted name
  79. * in filename.
  80. * At present the expansions I allow are:
  81. *
  82. * $xxx (terminated by '.', '/' or '\' with at least one char x)
  83. * ${xxx} (self-terminating)
  84. * First check for a Lisp variable $xxx. If this is set (and is
  85. * a string or a symbol) then its value is used. If not then
  86. * next inspect the environment variable xxx and dump its
  87. * value into the output. If the variable is unset then a check
  88. * is made for the value of a global lisp variable called @xxx,
  89. * and if that exists and is a string or symbol it is used.
  90. * If @xxx is undefined a null string is inserted.
  91. * If one of the variables is defined but has an improper value
  92. * then the whole file-translation fails.
  93. * The use of two Lisp variables makes it possible to control
  94. * precedence between these and shell variables.
  95. *
  96. * ~ ) followed by '.', '/' or '\'
  97. * ~xxx )
  98. * On Unix these try to find home directories using
  99. * getpwuid(getuid()) for '~' and getpwnam() for ~xxx.
  100. * If that fails ~ expands into nothing at all.
  101. * This syntax is only recognised at the very start of a file-name.
  102. * For systems other than Unix this syntax will not be useful and
  103. * should be avoided, however as an experimental place-holder I
  104. * may do things with environment variables called HOME etc.
  105. *
  106. *
  107. * I convert file-names of the form aaa/bbb/ccc.ddd into something
  108. * acceptable to the system being used, even though this may result in
  109. * some native file titles that include '/' characters becoming unavailable.
  110. * The reasoning here is that scripts and programs can then use Unix-like
  111. * names and non-Unix hosts will treat them forgivingly.
  112. *
  113. *
  114. */
  115. #ifdef __vmsvax__
  116. /*
  117. * This is maybe going to be a mess under VAX/VMS, but I will try
  118. * pretending that is still Unix for now since the VMS C runtime system
  119. * seems prepared to help a little in that case.
  120. */
  121. #endif /* __vmsvax__ */
  122. {
  123. int32 i, j;
  124. int c;
  125. char *o, *tail;
  126. if (n == 0)
  127. { *filename = 0;
  128. return; /* deem zero-length name to be illegal */
  129. }
  130. o = filename;
  131. c = *old;
  132. /*
  133. * First I deal with a leading "~"
  134. */
  135. if (c == '~')
  136. { old++;
  137. n--;
  138. while (n != 0)
  139. { c = *old;
  140. if (c == '.' || c == '/' || c == '\\') break;
  141. old++;
  142. n--;
  143. *o++ = c;
  144. }
  145. *o = 0;
  146. /*
  147. * actually deciding what the home directory is is passed down to a
  148. * system-specific call, but it is not to be relied upon especially
  149. * on personal computers.
  150. */
  151. if (o == filename) /* '~' on its own */
  152. { get_home_directory(filename, LONGEST_LEGAL_FILENAME);
  153. o = filename + strlen(filename);
  154. }
  155. else
  156. { get_users_home_directory(filename, LONGEST_LEGAL_FILENAME);
  157. o = filename + strlen(filename);
  158. }
  159. }
  160. /*
  161. * Having copies a user-name across (if there was one) I now copy the
  162. * rest of the file-name, expanding $xxx and ${xxx} as necessary.
  163. */
  164. while (n != 0)
  165. { c = *old++;
  166. n--;
  167. /*
  168. * If I find a "$" that is either at the end of the file-name or that is
  169. * immediately followed by ".", "/" or "\" then I will not use it for
  170. * parameter expansion. This at least gives me some help with the RISCOS
  171. * file-name $.abc.def where the "$" is used to indicate the root of the
  172. * current disc.
  173. */
  174. if (c == '$' && n != 0 &&
  175. (c = *old) != '.' && c != '/' && c != '\\')
  176. { char *p = o, *w;
  177. /*
  178. * I collect the name of the parameter at the end of my file-name buffer,
  179. * but will over-write it later on when I actually do the expansion.
  180. */
  181. if (c == '{')
  182. { old++;
  183. n--;
  184. while (n != 0)
  185. { c = *old++;
  186. n--;
  187. if (c == '}') break;
  188. *p++ = c;
  189. }
  190. }
  191. else
  192. { while (n != 0)
  193. { c = *old;
  194. if (c == '.' || c == '/' || c == '\\') break;
  195. old++;
  196. n--;
  197. *p++ = c;
  198. }
  199. }
  200. *p = 0;
  201. i = strlen(o) + 2;
  202. while (i-- != 0) o[i] = o[i-1];
  203. if ((p = look_in_lisp_variable(o, '$')) != NULL &&
  204. p != o) o = p;
  205. else if ((w = my_getenv(o+1)) != NULL) /* Shell variable? */
  206. { strcpy(o, w);
  207. o = o + strlen(o);
  208. }
  209. else if ((p = look_in_lisp_variable(o, '@')) != NULL)
  210. o = p;
  211. else
  212. { *filename = 0; /* return reporting failure */
  213. return;
  214. }
  215. }
  216. else *o++ = c;
  217. }
  218. *o = 0;
  219. #ifdef NOT_TOTALLY_DEBUGGED
  220. term_printf("[temp trace] File-name expands to \"%s\"\n", filename);
  221. #endif
  222. #ifdef MS_DOS
  223. /*
  224. * Now the filename has had $ and ~ prefix things expanded - I "just"
  225. * need to deal with sub-directory representation issues. Specifically I need
  226. * to map "/" separators into "\" so that if a user presents a file
  227. * name such as aaa/bbb/ccc.d it gets passed to the operating system
  228. * as aaa\bbb\ccc.d
  229. * NOte that I enable this code under the heading MS_DOS but really it
  230. * means any file-system (eg Windows too) that uses "\" as its main
  231. * directory separator character.
  232. */
  233. /*
  234. * I map / characters in MSDOS filenames into \s, so that users
  235. * can give file names with Unix-like slashes as separators if they want.
  236. * People who WANT to use filenames with '/' in them will be hurt.
  237. */
  238. tail = filename;
  239. while ((j = *tail) != 0)
  240. { if (j == '/') *tail = '\\';
  241. tail++;
  242. }
  243. /*
  244. * stat and friends do not like directories referred to as "\foo\", so check
  245. * for a trailing slash, being careful to respect directories with names
  246. * like "\" and "a:\".
  247. */
  248. j = strlen(filename);
  249. if (j > 0 && j != 1 && !(j == 3 && *(filename+1) == ':'))
  250. {
  251. if ( (*(tail - 1) == '\\')) *(tail - 1) = 0;
  252. }
  253. #ifdef __WATCOMC__
  254. /*
  255. * There is a bug in the stat function under some releases of Watcom C, where:
  256. * stat("\\foo\\..", ...);
  257. * fails with errno=-1. So we delete trailing ".." segments.
  258. */
  259. if (filename[0] == '\\' || filename[1] == ':')
  260. { j = strlen(filename);
  261. while (filename[j-1] == '.' && filename[j-2] == '.')
  262. { tail = strrchr(filename, '\\');
  263. /*
  264. * Warning - the aerror0() function sets an internal flag to indicate
  265. * that something went wrong, and then returns. Thus if further processing
  266. * is not valid in one of these cases some explicit control flow (maybe a
  267. * "return") is called for,
  268. */
  269. if (tail == NULL)
  270. aerror0("Unexpected pathname - this error should never happen");
  271. else *tail = '\0';
  272. tail = strrchr(filename, '\\');
  273. if (tail == NULL)
  274. aerror0("Unexpected pathname - this error should never happen");
  275. else *tail = '\0';
  276. j = strlen(filename);
  277. }
  278. /* Make sure we don't have an empty string or just a drive */
  279. if (j == 0) strcpy(filename,"\\");
  280. else if (j==2 && filename[1] == ':') strcat(filename,"\\");
  281. }
  282. #endif /* __WATCOMC__ */
  283. #ifdef EIGHT_PLUS_THREE
  284. /*
  285. * A *NASTY* hack here. I will explicitly truncate the name down to
  286. * and 8+3 format to keep as much DOS compatibility as I (in)conveniently can.
  287. * This is done here because if a user attempts to open a file with a long
  288. * name Windows 95 will try to honour the request and will then get confused
  289. * if old-style W3.x or DOS utilities made the file with a truncated name.
  290. * I rather think that this ought not to be wanted any more, especially if
  291. * it is possible to accept that raw DOS and Windows before 95 need not
  292. * be supported, but I will leave this in the code just in case!
  293. */
  294. tail = filename;
  295. eight_plus_three(tail);
  296. while ((j=*tail++)!=0) if (j=='\\' || j==':') eight_plus_three(tail);
  297. #endif /* EIGHT_PLUS_THREE */
  298. #endif /* MS_DOS */
  299. #ifdef MACINTOSH
  300. /*
  301. * Now the filename has had $ and ~ prefix things expanded - I "just"
  302. * need to deal with sub-directory representation issues. Specifically I need
  303. * to map "/" separators into "\" so that if a user presents a file
  304. * name such as aaa/bbb/ccc.d it gets passed to the operating system
  305. * as aaa\bbb\ccc.d
  306. * NOte that I enable this code under the heading MS_DOS but really it
  307. * means any file-system (eg Windows too) that uses "\" as its main
  308. * directory separator character.
  309. */
  310. /*
  311. * I map '/'characters in Macintosh filenames into ':'s, so that users
  312. * can give file names with Unix-like slashes as separators if they want.
  313. * People who WANT to use filenames with '/' in them will be hurt.
  314. * Furthermore if the name originally had no colons in it a leading colon is
  315. * added, and if it originally started with a '/' (for a Unix fully rooted name)
  316. * then the leading ':' or '/' is removed.
  317. */
  318. tail = filename;
  319. while ((j = *tail) != 0 && j != ':') tail++;
  320. if (j == 0)
  321. { memmove(&filename[1], filename, 1+strlen(filename));
  322. filename[0] = ':';
  323. }
  324. if (filename[0] == '/') memmove(filename, &filename[1], strlen(filename));
  325. tail = filename;
  326. while ((j = *tail) != 0)
  327. { if (j == '/') *tail = ':';
  328. tail++;
  329. }
  330. /*
  331. * I map the string :..: onto just :: to cope with Unix-format references to
  332. * the parent directory
  333. */
  334. i = 0;
  335. while ((c = filename[i]) != 0)
  336. { if (c == ':' &&
  337. filename[i+1] == '.' &&
  338. filename[i+2] == '.' &&
  339. filename[i+3] == ':')
  340. { j = i+1;
  341. do
  342. { c = filename[j+2];
  343. filename[j++] = c;
  344. } while (c != 0);
  345. }
  346. i++;
  347. }
  348. #endif /* MACINTOSH */
  349. #ifdef RISCOS
  350. /*
  351. * Now the filename has had $ and ~ prefix things expanded - I "just"
  352. * need to deal with sub-directory representation issues.
  353. */
  354. /*
  355. * The Archimedes is best coped with by re-mapping file names
  356. * so that xxxx.y sometimes becomes y.xxxx
  357. */
  358. i = strlen(filename);
  359. for (j=i-1; j>=0; j--) if (filename[j] == '.') break;
  360. if (j >= 0) /* No '.' => no possible conversion */
  361. { tail = &filename[j+1];
  362. if (j == i - 2 || /* one character suffix */
  363. /*
  364. * At present my policy is that any file with a one-character final
  365. * component gets mangled, and that as a special case files of
  366. * the form xxx.lsp, xxx.red, xxx.fsl and xxx.log are also flipped.
  367. */
  368. strcmp(tail, "lsp") == 0 ||
  369. strcmp(tail, "red") == 0 ||
  370. strcmp(tail, "fsl") == 0 ||
  371. strcmp(tail, "tst") == 0 ||
  372. strcmp(tail, "sl") == 0 ||
  373. strcmp(tail, "log") == 0)
  374. { int32 k;
  375. char suffix[8];
  376. for (k=j-1; k>=0; k--)
  377. if (filename[k] == '.' || filename[k] == '/') break;
  378. strcpy(suffix, tail);
  379. strcat(suffix, ".");
  380. do filename[--i] = filename[--j]; while (j > k);
  381. memcpy(&filename[k+1], suffix, (size_t)(i - j));
  382. }
  383. }
  384. /*
  385. * Now if in the Unix world I had a component '..' in the file it will
  386. * appear something like //.aaa.bbb or aaa.//.bbb
  387. * Similarly I map an isolated '.' (now an isolated '/') into '@'.
  388. */
  389. { int32 k = 0;
  390. j = -1;
  391. c = '/';
  392. for (;;)
  393. { if (c == '/' || c == 0)
  394. { if (j == k+1 && filename[k] == '.') filename[k] = '@';
  395. else if (j == k + 2 && filename[k] == '.' && filename[k+1] == '.')
  396. { int c1;
  397. filename[k++] = '^';
  398. do
  399. { c1 = filename[k+1];
  400. filename[k++] = c1;
  401. } while (c1 != 0);
  402. }
  403. k = j+1;
  404. }
  405. if (c == 0) break;
  406. j++;
  407. c = filename[j];
  408. }
  409. }
  410. /*
  411. * I map / characters in RISCOS filenames into dots, so that users
  412. * can give file names with Unix-like slashes as separators if they want.
  413. * People who WANT to use filenames with '/' in them will be hurt.
  414. * Note also that when files are created for output an attempt to open
  415. * (e.g.) "arthur.red" will fail unless the directory "red" already
  416. * exists.
  417. */
  418. tail = filename;
  419. while ((j = *tail) != 0)
  420. { if (j == '/') *tail = '.';
  421. tail++;
  422. }
  423. if (*filename == '.') /* Deal with fully-rooted Unix filenames */
  424. { tail[1] = 0;
  425. while (tail != filename)
  426. { tail--;
  427. tail[1] = tail[0];
  428. }
  429. tail[0] = '$';
  430. }
  431. #endif /* RISCOS */
  432. }
  433. FILE *open_file(char *filename, char *old, size_t n,
  434. char *mode, FILE *old_file)
  435. {
  436. /*
  437. * mode is something like "r" or "w" or "rb", as needed by fopen(),
  438. * and old_file is NULL normally, but can be a (FILE *) to indicate
  439. * the use of freopen rather than fopen.
  440. */
  441. process_file_name(filename, old, n);
  442. if (*filename == 0) return NULL;
  443. #ifdef NO_BINARY_FOPEN
  444. /*
  445. * On some Unix implementations (I mean DECs version on its MIPS workstations
  446. * and on the microvax I tried) the library does not support "rb" and "wb"
  447. * modes, so I work around that here. Explicit selection of binary file
  448. * access will be needed on some non-Unix operating systems, but should
  449. * never be relevant under Unix, hence my choice of a flag for the conditional
  450. * compilation here.
  451. */
  452. if (mode[0] == 'w')
  453. { if (mode[1] == '+') mode = "w+";
  454. else mode = "w";
  455. }
  456. else if (mode[1] == '+') mode = "r+";
  457. else mode = "r"; /* That ought to patch it up */
  458. #endif
  459. if (old_file == NULL) return fopen(filename, mode);
  460. else return freopen(filename, mode, old_file);
  461. }
  462. /* end of filename.c */