jcf-path.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* Handle CLASSPATH, -classpath, and path searching.
  2. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>.
  15. Java and all Java-based marks are trademarks or registered trademarks
  16. of Sun Microsystems, Inc. in the United States and other countries.
  17. The Free Software Foundation is independent of Sun Microsystems, Inc. */
  18. /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include <dirent.h>
  23. #include "jcf.h"
  24. #ifndef DIR_UP
  25. #define DIR_UP ".."
  26. #endif
  27. /* Possible flag values. */
  28. #define FLAG_SYSTEM 1
  29. #define FLAG_ZIP 2
  30. /* We keep linked lists of directory names. A ``directory'' can be
  31. either an ordinary directory or a .zip file. */
  32. struct entry
  33. {
  34. char *name;
  35. int flags;
  36. struct entry *next;
  37. };
  38. static void free_entry (struct entry **);
  39. static void append_entry (struct entry **, struct entry *);
  40. static void add_entry (struct entry **, const char *, int);
  41. static void add_path (struct entry **, const char *, int);
  42. /* We support several different ways to set the class path.
  43. built-in system directory (only libgcj.jar)
  44. CLASSPATH environment variable
  45. -classpath option overrides $CLASSPATH
  46. -CLASSPATH option is a synonym for -classpath (for compatibility)
  47. -bootclasspath overrides built-in
  48. -extdirs sets the extensions directory path (overrides built-in)
  49. -I prepends path to list
  50. We implement this by keeping several path lists, and then simply
  51. ignoring the ones which are not relevant. */
  52. /* This holds all the -I directories. */
  53. static struct entry *include_dirs;
  54. /* This holds the CLASSPATH environment variable. */
  55. static struct entry *classpath_env;
  56. /* This holds the -classpath command-line option. */
  57. static struct entry *classpath_user;
  58. /* This holds the default directories. Some of these will have the
  59. "system" flag set. */
  60. static struct entry *sys_dirs;
  61. /* This holds the extensions path entries. */
  62. static struct entry *extensions;
  63. /* This is the sealed list. It is just a combination of other lists. */
  64. static struct entry *sealed;
  65. /* We keep track of the longest path we've seen. */
  66. static int longest_path = 0;
  67. static void
  68. free_entry (struct entry **entp)
  69. {
  70. struct entry *e, *n;
  71. for (e = *entp; e; e = n)
  72. {
  73. n = e->next;
  74. free (e->name);
  75. free (e);
  76. }
  77. *entp = NULL;
  78. }
  79. static void
  80. append_entry (struct entry **entp, struct entry *ent)
  81. {
  82. /* It doesn't matter if this is slow, since it is run only at
  83. startup, and then infrequently. */
  84. struct entry *e;
  85. /* Find end of list. */
  86. for (e = *entp; e && e->next; e = e->next)
  87. ;
  88. if (e)
  89. e->next = ent;
  90. else
  91. *entp = ent;
  92. }
  93. static void
  94. add_entry (struct entry **entp, const char *filename, int is_system)
  95. {
  96. int len;
  97. struct entry *n;
  98. n = XNEW (struct entry);
  99. n->flags = is_system ? FLAG_SYSTEM : 0;
  100. n->next = NULL;
  101. len = strlen (filename);
  102. if (len > 4 && (FILENAME_CMP (filename + len - 4, ".zip") == 0
  103. || FILENAME_CMP (filename + len - 4, ".jar") == 0))
  104. {
  105. n->flags |= FLAG_ZIP;
  106. /* If the user uses -classpath then he'll have to include
  107. libgcj.jar in the value. We check for this in a simplistic
  108. way. Symlinks will fool this test. This is only used for
  109. -MM and -MMD, so it probably isn't terribly important. */
  110. if (! FILENAME_CMP (filename, LIBGCJ_ZIP_FILE))
  111. n->flags |= FLAG_SYSTEM;
  112. }
  113. /* Note that we add a trailing separator to `.zip' names as well.
  114. This is a little hack that lets the searching code in jcf-io.c
  115. work more easily. Eww. */
  116. if (! IS_DIR_SEPARATOR (filename[len - 1]))
  117. {
  118. char *f2 = (char *) alloca (len + 2);
  119. strcpy (f2, filename);
  120. f2[len] = DIR_SEPARATOR;
  121. f2[len + 1] = '\0';
  122. n->name = xstrdup (f2);
  123. ++len;
  124. }
  125. else
  126. n->name = xstrdup (filename);
  127. if (len > longest_path)
  128. longest_path = len;
  129. append_entry (entp, n);
  130. }
  131. static void
  132. add_path (struct entry **entp, const char *cp, int is_system)
  133. {
  134. const char *startp, *endp;
  135. if (cp)
  136. {
  137. char *buf = (char *) alloca (strlen (cp) + 3);
  138. startp = endp = cp;
  139. while (1)
  140. {
  141. if (! *endp || *endp == PATH_SEPARATOR)
  142. {
  143. if (endp == startp)
  144. {
  145. buf[0] = '.';
  146. buf[1] = DIR_SEPARATOR;
  147. buf[2] = '\0';
  148. }
  149. else
  150. {
  151. strncpy (buf, startp, endp - startp);
  152. buf[endp - startp] = '\0';
  153. }
  154. add_entry (entp, buf, is_system);
  155. if (! *endp)
  156. break;
  157. ++endp;
  158. startp = endp;
  159. }
  160. else
  161. ++endp;
  162. }
  163. }
  164. }
  165. static int init_done = 0;
  166. /* Initialize the path module. */
  167. void
  168. jcf_path_init (void)
  169. {
  170. char *cp;
  171. char *attempt, sep[2];
  172. struct stat stat_b;
  173. int found = 0, len;
  174. if (init_done)
  175. return;
  176. init_done = 1;
  177. sep[0] = DIR_SEPARATOR;
  178. sep[1] = '\0';
  179. cp = getenv ("GCC_EXEC_PREFIX");
  180. if (cp)
  181. {
  182. attempt = (char *) alloca (strlen (cp) + 50);
  183. /* The exec prefix can be something like
  184. /usr/local/bin/../lib/gcc-lib/. We want to change this
  185. into a pointer to the share/java directory. We support two
  186. configurations: one where prefix and exec-prefix are the
  187. same, and one where exec-prefix is `prefix/SOMETHING'. */
  188. strcpy (attempt, cp);
  189. strcat (attempt, DIR_UP);
  190. strcat (attempt, sep);
  191. strcat (attempt, DIR_UP);
  192. strcat (attempt, sep);
  193. len = strlen (attempt);
  194. strcpy (attempt + len, "share");
  195. strcat (attempt, sep);
  196. strcat (attempt, "java");
  197. strcat (attempt, sep);
  198. strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
  199. if (! stat (attempt, &stat_b))
  200. {
  201. add_entry (&sys_dirs, attempt, 1);
  202. found = 1;
  203. strcpy (&attempt[strlen (attempt)
  204. - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
  205. sep);
  206. strcat (attempt, "ext");
  207. strcat (attempt, sep);
  208. if (! stat (attempt, &stat_b))
  209. jcf_path_extdirs_arg (attempt);
  210. }
  211. else
  212. {
  213. strcpy (attempt + len, DIR_UP);
  214. strcat (attempt, sep);
  215. strcat (attempt, "share");
  216. strcat (attempt, sep);
  217. strcat (attempt, "java");
  218. strcat (attempt, sep);
  219. strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
  220. if (! stat (attempt, &stat_b))
  221. {
  222. add_entry (&sys_dirs, attempt, 1);
  223. found = 1;
  224. strcpy (&attempt[strlen (attempt)
  225. - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
  226. sep);
  227. strcat (attempt, "ext");
  228. strcat (attempt, sep);
  229. if (! stat (attempt, &stat_b))
  230. jcf_path_extdirs_arg (attempt);
  231. }
  232. }
  233. }
  234. if (! found)
  235. {
  236. /* Desperation: use the installed one. */
  237. char *extdirs;
  238. add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
  239. extdirs = (char *) alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
  240. strcpy (extdirs, LIBGCJ_ZIP_FILE);
  241. strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
  242. - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
  243. "ext");
  244. strcat (extdirs, sep);
  245. if (! stat (extdirs, &stat_b))
  246. jcf_path_extdirs_arg (extdirs);
  247. }
  248. cp = getenv ("CLASSPATH");
  249. add_path (&classpath_env, cp, 0);
  250. }
  251. /* Call this when -classpath is seen on the command line.
  252. This overrides only the $CLASSPATH environment variable.
  253. */
  254. void
  255. jcf_path_classpath_arg (const char *path)
  256. {
  257. free_entry (&classpath_user);
  258. add_path (&classpath_user, path, 0);
  259. }
  260. /* Call this when -bootclasspath is seen on the command line.
  261. */
  262. void
  263. jcf_path_bootclasspath_arg (const char *path)
  264. {
  265. free_entry (&sys_dirs);
  266. add_path (&sys_dirs, path, 1);
  267. }
  268. /* Call this when -extdirs is seen on the command line.
  269. */
  270. void
  271. jcf_path_extdirs_arg (const char *cp)
  272. {
  273. const char *startp, *endp;
  274. free_entry (&extensions);
  275. if (cp)
  276. {
  277. char *buf = (char *) alloca (strlen (cp) + 3);
  278. startp = endp = cp;
  279. while (1)
  280. {
  281. if (! *endp || *endp == PATH_SEPARATOR)
  282. {
  283. if (endp == startp)
  284. return;
  285. strncpy (buf, startp, endp - startp);
  286. buf[endp - startp] = '\0';
  287. {
  288. DIR *dirp = NULL;
  289. int dirname_length = strlen (buf);
  290. dirp = opendir (buf);
  291. if (dirp == NULL)
  292. return;
  293. for (;;)
  294. {
  295. struct dirent *direntp = readdir (dirp);
  296. if (!direntp)
  297. break;
  298. if (direntp->d_name[0] != '.')
  299. {
  300. char *name = (char *) alloca (dirname_length
  301. + strlen (direntp->d_name) + 2);
  302. strcpy (name, buf);
  303. if (! IS_DIR_SEPARATOR (name[dirname_length-1]))
  304. {
  305. name[dirname_length] = DIR_SEPARATOR;
  306. name[dirname_length+1] = 0;
  307. }
  308. strcat (name, direntp->d_name);
  309. add_entry (&extensions, name, 0);
  310. }
  311. }
  312. if (dirp)
  313. closedir (dirp);
  314. }
  315. if (! *endp)
  316. break;
  317. ++endp;
  318. startp = endp;
  319. }
  320. else
  321. ++endp;
  322. }
  323. }
  324. }
  325. /* Call this when -I is seen on the command line. */
  326. void
  327. jcf_path_include_arg (const char *path)
  328. {
  329. add_entry (&include_dirs, path, 0);
  330. }
  331. /* We `seal' the path by linking everything into one big list. Then
  332. we provide a way to iterate through the sealed list. If PRINT is
  333. true then we print the final class path to stderr. */
  334. void
  335. jcf_path_seal (int print)
  336. {
  337. struct entry *secondary;
  338. sealed = include_dirs;
  339. include_dirs = NULL;
  340. if (classpath_user)
  341. {
  342. secondary = classpath_user;
  343. classpath_user = NULL;
  344. }
  345. else
  346. {
  347. if (! classpath_env)
  348. add_entry (&classpath_env, ".", 0);
  349. secondary = classpath_env;
  350. classpath_env = NULL;
  351. }
  352. free_entry (&classpath_user);
  353. free_entry (&classpath_env);
  354. append_entry (&sealed, secondary);
  355. append_entry (&sealed, sys_dirs);
  356. append_entry (&sealed, extensions);
  357. sys_dirs = NULL;
  358. extensions = NULL;
  359. if (print)
  360. {
  361. struct entry *ent;
  362. fprintf (stderr, "Class path starts here:\n");
  363. for (ent = sealed; ent; ent = ent->next)
  364. {
  365. fprintf (stderr, " %s", ent->name);
  366. if ((ent->flags & FLAG_SYSTEM))
  367. fprintf (stderr, " (system)");
  368. if ((ent->flags & FLAG_ZIP))
  369. fprintf (stderr, " (zip)");
  370. fprintf (stderr, "\n");
  371. }
  372. }
  373. }
  374. void *
  375. jcf_path_start (void)
  376. {
  377. return (void *) sealed;
  378. }
  379. void *
  380. jcf_path_next (void *x)
  381. {
  382. struct entry *ent = (struct entry *) x;
  383. return (void *) ent->next;
  384. }
  385. static const char
  386. PATH_SEPARATOR_STR[] = {PATH_SEPARATOR, '\0'};
  387. char *
  388. jcf_path_compute (const char *prefix)
  389. {
  390. struct entry *iter;
  391. char *result;
  392. int length = strlen (prefix) + 1;
  393. int first;
  394. for (iter = sealed; iter != NULL; iter = iter->next)
  395. length += strlen (iter->name) + 1;
  396. result = (char *) xmalloc (length);
  397. strcpy (result, prefix);
  398. first = 1;
  399. for (iter = sealed; iter != NULL; iter = iter->next)
  400. {
  401. if (! first)
  402. strcat (result, PATH_SEPARATOR_STR);
  403. first = 0;
  404. strcat (result, iter->name);
  405. /* Ugly: we want to strip the '/' from zip entries when
  406. computing a string classpath. */
  407. if ((iter->flags & FLAG_ZIP) != 0)
  408. result[strlen (result) - 1] = '\0';
  409. }
  410. return result;
  411. }
  412. /* We guarantee that the return path will either be a zip file, or it
  413. will end with a directory separator. */
  414. char *
  415. jcf_path_name (void *x)
  416. {
  417. struct entry *ent = (struct entry *) x;
  418. return ent->name;
  419. }
  420. int
  421. jcf_path_is_zipfile (void *x)
  422. {
  423. struct entry *ent = (struct entry *) x;
  424. return (ent->flags & FLAG_ZIP);
  425. }
  426. int
  427. jcf_path_is_system (void *x)
  428. {
  429. struct entry *ent = (struct entry *) x;
  430. return (ent->flags & FLAG_SYSTEM);
  431. }
  432. int
  433. jcf_path_max_len (void)
  434. {
  435. return longest_path;
  436. }