PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/freebsd/contrib/texinfo/makeinfo/files.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 784 lines | 566 code | 114 blank | 104 comment | 136 complexity | 2ad7faae3c7b43d4ffce7ed4ee622d70 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* files.c -- file-related functions for makeinfo.
  2. $Id: files.c,v 1.5 2004/07/27 00:06:31 karl Exp $
  3. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
  4. Foundation, Inc.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "system.h"
  17. #include "files.h"
  18. #include "html.h"
  19. #include "index.h"
  20. #include "macro.h"
  21. #include "makeinfo.h"
  22. #include "node.h"
  23. FSTACK *filestack = NULL;
  24. static int node_filename_stack_index = 0;
  25. static int node_filename_stack_size = 0;
  26. static char **node_filename_stack = NULL;
  27. /* Looking for include files. */
  28. /* Given a string containing units of information separated by colons,
  29. return the next one pointed to by INDEX, or NULL if there are no more.
  30. Advance INDEX to the character after the colon. */
  31. static char *
  32. extract_colon_unit (char *string, int *index)
  33. {
  34. int start;
  35. int path_sep_char = PATH_SEP[0];
  36. int i = *index;
  37. if (!string || (i >= strlen (string)))
  38. return NULL;
  39. /* Each call to this routine leaves the index pointing at a colon if
  40. there is more to the path. If i > 0, then increment past the
  41. `:'. If i == 0, then the path has a leading colon. Trailing colons
  42. are handled OK by the `else' part of the if statement; an empty
  43. string is returned in that case. */
  44. if (i && string[i] == path_sep_char)
  45. i++;
  46. start = i;
  47. while (string[i] && string[i] != path_sep_char) i++;
  48. *index = i;
  49. if (i == start)
  50. {
  51. if (string[i])
  52. (*index)++;
  53. /* Return "" in the case of a trailing `:'. */
  54. return xstrdup ("");
  55. }
  56. else
  57. {
  58. char *value;
  59. value = xmalloc (1 + (i - start));
  60. memcpy (value, &string[start], (i - start));
  61. value [i - start] = 0;
  62. return value;
  63. }
  64. }
  65. /* Return the full pathname for FILENAME by searching along PATH.
  66. When found, return the stat () info for FILENAME in FINFO.
  67. If PATH is NULL, only the current directory is searched.
  68. If the file could not be found, return a NULL pointer. */
  69. char *
  70. get_file_info_in_path (char *filename, char *path, struct stat *finfo)
  71. {
  72. char *dir;
  73. int result, index = 0;
  74. if (path == NULL)
  75. path = ".";
  76. /* Handle absolute pathnames. */
  77. if (IS_ABSOLUTE (filename)
  78. || (*filename == '.'
  79. && (IS_SLASH (filename[1])
  80. || (filename[1] == '.' && IS_SLASH (filename[2])))))
  81. {
  82. if (stat (filename, finfo) == 0)
  83. return xstrdup (filename);
  84. else
  85. return NULL;
  86. }
  87. while ((dir = extract_colon_unit (path, &index)))
  88. {
  89. char *fullpath;
  90. if (!*dir)
  91. {
  92. free (dir);
  93. dir = xstrdup (".");
  94. }
  95. fullpath = xmalloc (2 + strlen (dir) + strlen (filename));
  96. sprintf (fullpath, "%s/%s", dir, filename);
  97. free (dir);
  98. result = stat (fullpath, finfo);
  99. if (result == 0)
  100. return fullpath;
  101. else
  102. free (fullpath);
  103. }
  104. return NULL;
  105. }
  106. /* Prepend and append new paths to include_files_path. */
  107. void
  108. prepend_to_include_path (char *path)
  109. {
  110. if (!include_files_path)
  111. {
  112. include_files_path = xstrdup (path);
  113. include_files_path = xrealloc (include_files_path,
  114. strlen (include_files_path) + 3); /* 3 for ":.\0" */
  115. strcat (strcat (include_files_path, PATH_SEP), ".");
  116. }
  117. else
  118. {
  119. char *tmp = xstrdup (include_files_path);
  120. include_files_path = xrealloc (include_files_path,
  121. strlen (include_files_path) + strlen (path) + 2); /* 2 for ":\0" */
  122. strcpy (include_files_path, path);
  123. strcat (include_files_path, PATH_SEP);
  124. strcat (include_files_path, tmp);
  125. free (tmp);
  126. }
  127. }
  128. void
  129. append_to_include_path (char *path)
  130. {
  131. if (!include_files_path)
  132. include_files_path = xstrdup (".");
  133. include_files_path = (char *) xrealloc (include_files_path,
  134. 2 + strlen (include_files_path) + strlen (path));
  135. strcat (include_files_path, PATH_SEP);
  136. strcat (include_files_path, path);
  137. }
  138. /* Remove the first path from the include_files_path. */
  139. void
  140. pop_path_from_include_path (void)
  141. {
  142. int i = 0;
  143. char *tmp;
  144. if (include_files_path)
  145. for (i = 0; i < strlen (include_files_path)
  146. && include_files_path[i] != ':'; i++);
  147. /* Advance include_files_path to the next char from ':' */
  148. tmp = (char *) xmalloc (strlen (include_files_path) - i);
  149. strcpy (tmp, (char *) include_files_path + i + 1);
  150. free (include_files_path);
  151. include_files_path = tmp;
  152. }
  153. /* Find and load the file named FILENAME. Return a pointer to
  154. the loaded file, or NULL if it can't be loaded. If USE_PATH is zero,
  155. just look for the given file (this is used in handle_delayed_writes),
  156. else search along include_files_path. */
  157. char *
  158. find_and_load (char *filename, int use_path)
  159. {
  160. struct stat fileinfo;
  161. long file_size;
  162. int file = -1, count = 0;
  163. char *fullpath, *result;
  164. int n, bytes_to_read;
  165. result = fullpath = NULL;
  166. fullpath
  167. = get_file_info_in_path (filename, use_path ? include_files_path : NULL,
  168. &fileinfo);
  169. if (!fullpath)
  170. goto error_exit;
  171. filename = fullpath;
  172. file_size = (long) fileinfo.st_size;
  173. file = open (filename, O_RDONLY);
  174. if (file < 0)
  175. goto error_exit;
  176. /* Load the file, with enough room for a newline and a null. */
  177. result = xmalloc (file_size + 2);
  178. /* VMS stat lies about the st_size value. The actual number of
  179. readable bytes is always less than this value. The arcane
  180. mysteries of VMS/RMS are too much to probe, so this hack
  181. suffices to make things work. It's also needed on Cygwin. And so
  182. we might as well use it everywhere. */
  183. bytes_to_read = file_size;
  184. while ((n = read (file, result + count, bytes_to_read)) > 0)
  185. {
  186. count += n;
  187. bytes_to_read -= n;
  188. }
  189. if (0 < count && count < file_size)
  190. result = xrealloc (result, count + 2); /* why waste the slack? */
  191. else if (n == -1)
  192. error_exit:
  193. {
  194. if (result)
  195. free (result);
  196. if (fullpath)
  197. free (fullpath);
  198. if (file != -1)
  199. close (file);
  200. return NULL;
  201. }
  202. close (file);
  203. /* Set the globals to the new file. */
  204. input_text = result;
  205. input_text_length = count;
  206. input_filename = fullpath;
  207. node_filename = xstrdup (fullpath);
  208. input_text_offset = 0;
  209. line_number = 1;
  210. /* Not strictly necessary. This magic prevents read_token () from doing
  211. extra unnecessary work each time it is called (that is a lot of times).
  212. INPUT_TEXT_LENGTH is one past the actual end of the text. */
  213. input_text[input_text_length] = '\n';
  214. /* This, on the other hand, is always necessary. */
  215. input_text[input_text_length+1] = 0;
  216. return result;
  217. }
  218. /* Pushing and popping files. */
  219. static void
  220. push_node_filename (void)
  221. {
  222. if (node_filename_stack_index + 1 > node_filename_stack_size)
  223. node_filename_stack = xrealloc
  224. (node_filename_stack, (node_filename_stack_size += 10) * sizeof (char *));
  225. node_filename_stack[node_filename_stack_index] = node_filename;
  226. node_filename_stack_index++;
  227. }
  228. static void
  229. pop_node_filename (void)
  230. {
  231. node_filename = node_filename_stack[--node_filename_stack_index];
  232. }
  233. /* Save the state of the current input file. */
  234. void
  235. pushfile (void)
  236. {
  237. FSTACK *newstack = xmalloc (sizeof (FSTACK));
  238. newstack->filename = input_filename;
  239. newstack->text = input_text;
  240. newstack->size = input_text_length;
  241. newstack->offset = input_text_offset;
  242. newstack->line_number = line_number;
  243. newstack->next = filestack;
  244. filestack = newstack;
  245. push_node_filename ();
  246. }
  247. /* Make the current file globals be what is on top of the file stack. */
  248. void
  249. popfile (void)
  250. {
  251. FSTACK *tos = filestack;
  252. if (!tos)
  253. abort (); /* My fault. I wonder what I did? */
  254. if (macro_expansion_output_stream)
  255. {
  256. maybe_write_itext (input_text, input_text_offset);
  257. forget_itext (input_text);
  258. }
  259. /* Pop the stack. */
  260. filestack = filestack->next;
  261. /* Make sure that commands with braces have been satisfied. */
  262. if (!executing_string && !me_executing_string)
  263. discard_braces ();
  264. /* Get the top of the stack into the globals. */
  265. input_filename = tos->filename;
  266. input_text = tos->text;
  267. input_text_length = tos->size;
  268. input_text_offset = tos->offset;
  269. line_number = tos->line_number;
  270. free (tos);
  271. /* Go back to the (now) current node. */
  272. pop_node_filename ();
  273. }
  274. /* Flush all open files on the file stack. */
  275. void
  276. flush_file_stack (void)
  277. {
  278. while (filestack)
  279. {
  280. char *fname = input_filename;
  281. char *text = input_text;
  282. popfile ();
  283. free (fname);
  284. free (text);
  285. }
  286. }
  287. /* Return the index of the first character in the filename
  288. which is past all the leading directory characters. */
  289. static int
  290. skip_directory_part (char *filename)
  291. {
  292. int i = strlen (filename) - 1;
  293. while (i && !IS_SLASH (filename[i]))
  294. i--;
  295. if (IS_SLASH (filename[i]))
  296. i++;
  297. else if (filename[i] && HAVE_DRIVE (filename))
  298. i = 2;
  299. return i;
  300. }
  301. static char *
  302. filename_non_directory (char *name)
  303. {
  304. return xstrdup (name + skip_directory_part (name));
  305. }
  306. /* Return just the simple part of the filename; i.e. the
  307. filename without the path information, or extensions.
  308. This conses up a new string. */
  309. char *
  310. filename_part (char *filename)
  311. {
  312. char *basename = filename_non_directory (filename);
  313. #ifdef REMOVE_OUTPUT_EXTENSIONS
  314. /* See if there is an extension to remove. If so, remove it. */
  315. {
  316. char *temp = strrchr (basename, '.');
  317. if (temp)
  318. *temp = 0;
  319. }
  320. #endif /* REMOVE_OUTPUT_EXTENSIONS */
  321. return basename;
  322. }
  323. /* Return the pathname part of filename. This can be NULL. */
  324. char *
  325. pathname_part (char *filename)
  326. {
  327. char *result = NULL;
  328. int i;
  329. filename = expand_filename (filename, "");
  330. i = skip_directory_part (filename);
  331. if (i)
  332. {
  333. result = xmalloc (1 + i);
  334. strncpy (result, filename, i);
  335. result[i] = 0;
  336. }
  337. free (filename);
  338. return result;
  339. }
  340. /* Return the full path to FILENAME. */
  341. static char *
  342. full_pathname (char *filename)
  343. {
  344. int initial_character;
  345. char *result;
  346. /* No filename given? */
  347. if (!filename || !*filename)
  348. return xstrdup ("");
  349. /* Already absolute? */
  350. if (IS_ABSOLUTE (filename) ||
  351. (*filename == '.' &&
  352. (IS_SLASH (filename[1]) ||
  353. (filename[1] == '.' && IS_SLASH (filename[2])))))
  354. return xstrdup (filename);
  355. initial_character = *filename;
  356. if (initial_character != '~')
  357. {
  358. char *localdir = xmalloc (1025);
  359. #ifdef HAVE_GETCWD
  360. if (!getcwd (localdir, 1024))
  361. #else
  362. if (!getwd (localdir))
  363. #endif
  364. {
  365. fprintf (stderr, _("%s: getwd: %s, %s\n"),
  366. progname, filename, localdir);
  367. xexit (1);
  368. }
  369. strcat (localdir, "/");
  370. strcat (localdir, filename);
  371. result = xstrdup (localdir);
  372. free (localdir);
  373. }
  374. else
  375. { /* Does anybody know why WIN32 doesn't want to support $HOME?
  376. If the reason is they don't have getpwnam, they should
  377. only disable the else clause below. */
  378. #ifndef WIN32
  379. if (IS_SLASH (filename[1]))
  380. {
  381. /* Return the concatenation of the environment variable HOME
  382. and the rest of the string. */
  383. char *temp_home;
  384. temp_home = (char *) getenv ("HOME");
  385. result = xmalloc (strlen (&filename[1])
  386. + 1
  387. + temp_home ? strlen (temp_home)
  388. : 0);
  389. *result = 0;
  390. if (temp_home)
  391. strcpy (result, temp_home);
  392. strcat (result, &filename[1]);
  393. }
  394. else
  395. {
  396. struct passwd *user_entry;
  397. int i, c;
  398. char *username = xmalloc (257);
  399. for (i = 1; (c = filename[i]); i++)
  400. {
  401. if (IS_SLASH (c))
  402. break;
  403. else
  404. username[i - 1] = c;
  405. }
  406. if (c)
  407. username[i - 1] = 0;
  408. user_entry = getpwnam (username);
  409. if (!user_entry)
  410. return xstrdup (filename);
  411. result = xmalloc (1 + strlen (user_entry->pw_dir)
  412. + strlen (&filename[i]));
  413. strcpy (result, user_entry->pw_dir);
  414. strcat (result, &filename[i]);
  415. }
  416. #endif /* not WIN32 */
  417. }
  418. return result;
  419. }
  420. /* Return the expansion of FILENAME. */
  421. char *
  422. expand_filename (char *filename, char *input_name)
  423. {
  424. int i;
  425. if (filename)
  426. {
  427. filename = full_pathname (filename);
  428. if (IS_ABSOLUTE (filename)
  429. || (*filename == '.' &&
  430. (IS_SLASH (filename[1]) ||
  431. (filename[1] == '.' && IS_SLASH (filename[2])))))
  432. return filename;
  433. }
  434. else
  435. {
  436. filename = filename_non_directory (input_name);
  437. if (!*filename)
  438. {
  439. free (filename);
  440. filename = xstrdup ("noname.texi");
  441. }
  442. for (i = strlen (filename) - 1; i; i--)
  443. if (filename[i] == '.')
  444. break;
  445. if (!i)
  446. i = strlen (filename);
  447. if (i + 6 > (strlen (filename)))
  448. filename = xrealloc (filename, i + 6);
  449. strcpy (filename + i, html ? ".html" : ".info");
  450. return filename;
  451. }
  452. if (IS_ABSOLUTE (input_name))
  453. {
  454. /* Make it so that relative names work. */
  455. char *result;
  456. i = strlen (input_name) - 1;
  457. result = xmalloc (1 + strlen (input_name) + strlen (filename));
  458. strcpy (result, input_name);
  459. while (!IS_SLASH (result[i]) && i)
  460. i--;
  461. if (IS_SLASH (result[i]))
  462. i++;
  463. strcpy (&result[i], filename);
  464. free (filename);
  465. return result;
  466. }
  467. return filename;
  468. }
  469. char *
  470. output_name_from_input_name (char *name)
  471. {
  472. return expand_filename (NULL, name);
  473. }
  474. /* Modify the file name FNAME so that it fits the limitations of the
  475. underlying filesystem. In particular, truncate the file name as it
  476. would be truncated by the filesystem. We assume the result can
  477. never be longer than the original, otherwise we couldn't be sure we
  478. have enough space in the original string to modify it in place. */
  479. char *
  480. normalize_filename (char *fname)
  481. {
  482. int maxlen;
  483. char orig[PATH_MAX + 1];
  484. int i;
  485. char *lastdot, *p;
  486. #ifdef _PC_NAME_MAX
  487. maxlen = pathconf (fname, _PC_NAME_MAX);
  488. if (maxlen < 1)
  489. #endif
  490. maxlen = PATH_MAX;
  491. i = skip_directory_part (fname);
  492. if (fname[i] == '\0')
  493. return fname; /* only a directory name -- don't modify */
  494. strcpy (orig, fname + i);
  495. switch (maxlen)
  496. {
  497. case 12: /* MS-DOS 8+3 filesystem */
  498. if (orig[0] == '.') /* leading dots are not allowed */
  499. orig[0] = '_';
  500. lastdot = strrchr (orig, '.');
  501. if (!lastdot)
  502. lastdot = orig + strlen (orig);
  503. strncpy (fname + i, orig, lastdot - orig);
  504. for (p = fname + i;
  505. p < fname + i + (lastdot - orig) && p < fname + i + 8;
  506. p++)
  507. if (*p == '.')
  508. *p = '_';
  509. *p = '\0';
  510. if (*lastdot == '.')
  511. strncat (fname + i, lastdot, 4);
  512. break;
  513. case 14: /* old Unix systems with 14-char limitation */
  514. strcpy (fname + i, orig);
  515. if (strlen (fname + i) > 14)
  516. fname[i + 14] = '\0';
  517. break;
  518. default:
  519. strcpy (fname + i, orig);
  520. if (strlen (fname) > maxlen - 1)
  521. fname[maxlen - 1] = '\0';
  522. break;
  523. }
  524. return fname;
  525. }
  526. /* Delayed writing functions. A few of the commands
  527. needs to be handled at the end, namely @contents,
  528. @shortcontents, @printindex and @listoffloats.
  529. These functions take care of that. */
  530. static DELAYED_WRITE *delayed_writes = NULL;
  531. int handling_delayed_writes = 0;
  532. void
  533. register_delayed_write (char *delayed_command)
  534. {
  535. DELAYED_WRITE *new;
  536. if (!current_output_filename || !*current_output_filename)
  537. {
  538. /* Cannot register if we don't know what the output file is. */
  539. warning (_("`%s' omitted before output filename"), delayed_command);
  540. return;
  541. }
  542. if (STREQ (current_output_filename, "-"))
  543. {
  544. /* Do not register a new write if the output file is not seekable.
  545. Let the user know about it first, though. */
  546. warning (_("`%s' omitted since writing to stdout"), delayed_command);
  547. return;
  548. }
  549. /* Don't complain if the user is writing /dev/null, since surely they
  550. don't care, but don't register the delayed write, either. */
  551. if (FILENAME_CMP (current_output_filename, NULL_DEVICE) == 0
  552. || FILENAME_CMP (current_output_filename, ALSO_NULL_DEVICE) == 0)
  553. return;
  554. /* We need the HTML header in the output,
  555. to get a proper output_position. */
  556. if (!executing_string && html)
  557. html_output_head ();
  558. /* Get output_position updated. */
  559. flush_output ();
  560. new = xmalloc (sizeof (DELAYED_WRITE));
  561. new->command = xstrdup (delayed_command);
  562. new->filename = xstrdup (current_output_filename);
  563. new->input_filename = xstrdup (input_filename);
  564. new->position = output_position;
  565. new->calling_line = line_number;
  566. new->node = current_node ? xstrdup (current_node): "";
  567. new->node_order = node_order;
  568. new->index_order = index_counter;
  569. new->next = delayed_writes;
  570. delayed_writes = new;
  571. }
  572. void
  573. handle_delayed_writes (void)
  574. {
  575. DELAYED_WRITE *temp = (DELAYED_WRITE *) reverse_list
  576. ((GENERIC_LIST *) delayed_writes);
  577. int position_shift_amount, line_number_shift_amount;
  578. char *delayed_buf;
  579. handling_delayed_writes = 1;
  580. while (temp)
  581. {
  582. delayed_buf = find_and_load (temp->filename, 0);
  583. if (output_paragraph_offset > 0)
  584. {
  585. error (_("Output buffer not empty."));
  586. return;
  587. }
  588. if (!delayed_buf)
  589. {
  590. fs_error (temp->filename);
  591. return;
  592. }
  593. output_stream = fopen (temp->filename, "w");
  594. if (!output_stream)
  595. {
  596. fs_error (temp->filename);
  597. return;
  598. }
  599. if (fwrite (delayed_buf, 1, temp->position, output_stream) != temp->position)
  600. {
  601. fs_error (temp->filename);
  602. return;
  603. }
  604. {
  605. int output_position_at_start = output_position;
  606. int line_number_at_start = output_line_number;
  607. /* In order to make warnings and errors
  608. refer to the correct line number. */
  609. input_filename = temp->input_filename;
  610. line_number = temp->calling_line;
  611. execute_string ("%s", temp->command);
  612. flush_output ();
  613. /* Since the output file is modified, following delayed writes
  614. need to be updated by this amount. */
  615. position_shift_amount = output_position - output_position_at_start;
  616. line_number_shift_amount = output_line_number - line_number_at_start;
  617. }
  618. if (fwrite (delayed_buf + temp->position, 1,
  619. input_text_length - temp->position, output_stream)
  620. != input_text_length - temp->position
  621. || fclose (output_stream) != 0)
  622. fs_error (temp->filename);
  623. /* Done with the buffer. */
  624. free (delayed_buf);
  625. /* Update positions in tag table for nodes that are defined after
  626. the line this delayed write is registered. */
  627. if (!html && !xml)
  628. {
  629. TAG_ENTRY *node;
  630. for (node = tag_table; node; node = node->next_ent)
  631. if (node->order > temp->node_order)
  632. node->position += position_shift_amount;
  633. }
  634. /* Something similar for the line numbers in all of the defined
  635. indices. */
  636. {
  637. int i;
  638. for (i = 0; i < defined_indices; i++)
  639. if (name_index_alist[i])
  640. {
  641. char *name = ((INDEX_ALIST *) name_index_alist[i])->name;
  642. INDEX_ELT *index;
  643. for (index = index_list (name); index; index = index->next)
  644. if ((no_headers || STREQ (index->node, temp->node))
  645. && index->entry_number > temp->index_order)
  646. index->output_line += line_number_shift_amount;
  647. }
  648. }
  649. /* Shift remaining delayed positions
  650. by the length of this write. */
  651. {
  652. DELAYED_WRITE *future_write = temp->next;
  653. while (future_write)
  654. {
  655. if (STREQ (temp->filename, future_write->filename))
  656. future_write->position += position_shift_amount;
  657. future_write = future_write->next;
  658. }
  659. }
  660. temp = temp->next;
  661. }
  662. }