| 1 | /* GNU m4 -- A simple macro processor
|
|---|
| 2 |
|
|---|
| 3 | Copyright (C) 1989, 1990, 1991, 1992, 1993, 2004, 2006 Free Software
|
|---|
| 4 | Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program; if not, write to the Free Software
|
|---|
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|---|
| 19 | 02110-1301 USA
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | /* Handling of path search of included files via the builtins "include"
|
|---|
| 23 | and "sinclude". */
|
|---|
| 24 |
|
|---|
| 25 | #include "m4.h"
|
|---|
| 26 |
|
|---|
| 27 | struct includes
|
|---|
| 28 | {
|
|---|
| 29 | struct includes *next; /* next directory to search */
|
|---|
| 30 | const char *dir; /* directory */
|
|---|
| 31 | int len;
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | typedef struct includes includes;
|
|---|
| 35 |
|
|---|
| 36 | static includes *dir_list; /* the list of path directories */
|
|---|
| 37 | static includes *dir_list_end; /* the end of same */
|
|---|
| 38 | static int dir_max_length; /* length of longest directory name */
|
|---|
| 39 |
|
|---|
| 40 | |
|---|
| 41 |
|
|---|
| 42 | void
|
|---|
| 43 | include_init (void)
|
|---|
| 44 | {
|
|---|
| 45 | dir_list = NULL;
|
|---|
| 46 | dir_list_end = NULL;
|
|---|
| 47 | dir_max_length = 0;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | void
|
|---|
| 51 | include_env_init (void)
|
|---|
| 52 | {
|
|---|
| 53 | char *path;
|
|---|
| 54 | char *path_end;
|
|---|
| 55 | char *env_path;
|
|---|
| 56 |
|
|---|
| 57 | if (no_gnu_extensions)
|
|---|
| 58 | return;
|
|---|
| 59 |
|
|---|
| 60 | env_path = getenv ("M4PATH");
|
|---|
| 61 | if (env_path == NULL)
|
|---|
| 62 | return;
|
|---|
| 63 |
|
|---|
| 64 | env_path = xstrdup (env_path);
|
|---|
| 65 | path = env_path;
|
|---|
| 66 |
|
|---|
| 67 | do
|
|---|
| 68 | {
|
|---|
| 69 | path_end = strchr (path, ':');
|
|---|
| 70 | if (path_end)
|
|---|
| 71 | *path_end = '\0';
|
|---|
| 72 | add_include_directory (path);
|
|---|
| 73 | path = path_end + 1;
|
|---|
| 74 | }
|
|---|
| 75 | while (path_end);
|
|---|
| 76 | free (env_path);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void
|
|---|
| 80 | add_include_directory (const char *dir)
|
|---|
| 81 | {
|
|---|
| 82 | includes *incl;
|
|---|
| 83 |
|
|---|
| 84 | if (no_gnu_extensions)
|
|---|
| 85 | return;
|
|---|
| 86 |
|
|---|
| 87 | if (*dir == '\0')
|
|---|
| 88 | dir = ".";
|
|---|
| 89 |
|
|---|
| 90 | incl = (includes *) xmalloc (sizeof (struct includes));
|
|---|
| 91 | incl->next = NULL;
|
|---|
| 92 | incl->len = strlen (dir);
|
|---|
| 93 | incl->dir = xstrdup (dir);
|
|---|
| 94 |
|
|---|
| 95 | if (incl->len > dir_max_length) /* remember len of longest directory */
|
|---|
| 96 | dir_max_length = incl->len;
|
|---|
| 97 |
|
|---|
| 98 | if (dir_list_end == NULL)
|
|---|
| 99 | dir_list = incl;
|
|---|
| 100 | else
|
|---|
| 101 | dir_list_end->next = incl;
|
|---|
| 102 | dir_list_end = incl;
|
|---|
| 103 |
|
|---|
| 104 | #ifdef DEBUG_INCL
|
|---|
| 105 | fprintf (stderr, "add_include_directory (%s);\n", dir);
|
|---|
| 106 | #endif
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /* Search for FILE, first in `.', then according to -I options. If
|
|---|
| 110 | successful, return the open file, and if RESULT is not NULL, set
|
|---|
| 111 | *RESULT to a malloc'd string that represents the file found with
|
|---|
| 112 | respect to the current working directory. */
|
|---|
| 113 |
|
|---|
| 114 | FILE *
|
|---|
| 115 | m4_path_search (const char *file, char **result)
|
|---|
| 116 | {
|
|---|
| 117 | FILE *fp;
|
|---|
| 118 | includes *incl;
|
|---|
| 119 | char *name; /* buffer for constructed name */
|
|---|
| 120 | int e;
|
|---|
| 121 |
|
|---|
| 122 | if (result)
|
|---|
| 123 | *result = NULL;
|
|---|
| 124 |
|
|---|
| 125 | /* Reject empty file. */
|
|---|
| 126 | if (!*file)
|
|---|
| 127 | {
|
|---|
| 128 | errno = ENOENT;
|
|---|
| 129 | return NULL;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /* Look in current working directory first. */
|
|---|
| 133 | fp = fopen (file, "r");
|
|---|
| 134 | if (fp != NULL)
|
|---|
| 135 | {
|
|---|
| 136 | if (set_cloexec_flag (fileno (fp), true) != 0)
|
|---|
| 137 | M4ERROR ((warning_status, errno,
|
|---|
| 138 | "Warning: cannot protect input file across forks"));
|
|---|
| 139 | if (result)
|
|---|
| 140 | *result = xstrdup (file);
|
|---|
| 141 | return fp;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /* If file not found, and filename absolute, fail. */
|
|---|
| 145 | if (*file == '/' || no_gnu_extensions)
|
|---|
| 146 | return NULL;
|
|---|
| 147 | e = errno;
|
|---|
| 148 |
|
|---|
| 149 | name = (char *) xmalloc (dir_max_length + 1 + strlen (file) + 1);
|
|---|
| 150 |
|
|---|
| 151 | for (incl = dir_list; incl != NULL; incl = incl->next)
|
|---|
| 152 | {
|
|---|
| 153 | strncpy (name, incl->dir, incl->len);
|
|---|
| 154 | name[incl->len] = '/';
|
|---|
| 155 | strcpy (name + incl->len + 1, file);
|
|---|
| 156 |
|
|---|
| 157 | #ifdef DEBUG_INCL
|
|---|
| 158 | fprintf (stderr, "m4_path_search (%s) -- trying %s\n", file, name);
|
|---|
| 159 | #endif
|
|---|
| 160 |
|
|---|
| 161 | fp = fopen (name, "r");
|
|---|
| 162 | if (fp != NULL)
|
|---|
| 163 | {
|
|---|
| 164 | if (debug_level & DEBUG_TRACE_PATH)
|
|---|
| 165 | DEBUG_MESSAGE2 ("path search for `%s' found `%s'", file, name);
|
|---|
| 166 | if (set_cloexec_flag (fileno (fp), true) != 0)
|
|---|
| 167 | M4ERROR ((warning_status, errno,
|
|---|
| 168 | "Warning: cannot protect input file across forks"));
|
|---|
| 169 | if (result)
|
|---|
| 170 | *result = name;
|
|---|
| 171 | else
|
|---|
| 172 | free (name);
|
|---|
| 173 | errno = e;
|
|---|
| 174 | return fp;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | free (name);
|
|---|
| 178 | errno = e;
|
|---|
| 179 | return fp;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | #ifdef DEBUG_INCL
|
|---|
| 183 |
|
|---|
| 184 | static void M4_GNUC_UNUSED
|
|---|
| 185 | include_dump (void)
|
|---|
| 186 | {
|
|---|
| 187 | includes *incl;
|
|---|
| 188 |
|
|---|
| 189 | fprintf (stderr, "include_dump:\n");
|
|---|
| 190 | for (incl = dir_list; incl != NULL; incl = incl->next)
|
|---|
| 191 | fprintf (stderr, "\t%s\n", incl->dir);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | #endif /* DEBUG_INCL */
|
|---|