| 1 | /* Interface to `ar' archives for GNU Make. | 
|---|
| 2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1997, | 
|---|
| 3 | 2002 Free Software Foundation, Inc. | 
|---|
| 4 | This file is part of GNU Make. | 
|---|
| 5 |  | 
|---|
| 6 | GNU Make 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, or (at your option) | 
|---|
| 9 | any later version. | 
|---|
| 10 |  | 
|---|
| 11 | GNU Make 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 GNU Make; see the file COPYING.  If not, write to | 
|---|
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 
|---|
| 19 | Boston, MA 02111-1307, USA.  */ | 
|---|
| 20 |  | 
|---|
| 21 | #include "make.h" | 
|---|
| 22 |  | 
|---|
| 23 | #ifndef NO_ARCHIVES | 
|---|
| 24 |  | 
|---|
| 25 | #include "filedef.h" | 
|---|
| 26 | #include "dep.h" | 
|---|
| 27 | #include <fnmatch.h> | 
|---|
| 28 |  | 
|---|
| 29 | /* Defined in arscan.c.  */ | 
|---|
| 30 | extern long int ar_scan PARAMS ((char *archive, long int (*function) (), long int arg)); | 
|---|
| 31 | extern int ar_name_equal PARAMS ((char *name, char *mem, int truncated)); | 
|---|
| 32 | #ifndef VMS | 
|---|
| 33 | extern int ar_member_touch PARAMS ((char *arname, char *memname)); | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | /* Return nonzero if NAME is an archive-member reference, zero if not. | 
|---|
| 37 | An archive-member reference is a name like `lib(member)'. | 
|---|
| 38 | If a name like `lib((entry))' is used, a fatal error is signaled at | 
|---|
| 39 | the attempt to use this unsupported feature.  */ | 
|---|
| 40 |  | 
|---|
| 41 | int | 
|---|
| 42 | ar_name (char *name) | 
|---|
| 43 | { | 
|---|
| 44 | char *p = strchr (name, '('); | 
|---|
| 45 | char *end; | 
|---|
| 46 |  | 
|---|
| 47 | if (p == 0 || p == name) | 
|---|
| 48 | return 0; | 
|---|
| 49 |  | 
|---|
| 50 | end = p + strlen (p) - 1; | 
|---|
| 51 | if (*end != ')') | 
|---|
| 52 | return 0; | 
|---|
| 53 |  | 
|---|
| 54 | if (p[1] == '(' && end[-1] == ')') | 
|---|
| 55 | fatal (NILF, _("attempt to use unsupported feature: `%s'"), name); | 
|---|
| 56 |  | 
|---|
| 57 | return 1; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /* Parse the archive-member reference NAME into the archive and member names. | 
|---|
| 62 | Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil; | 
|---|
| 63 | put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil.  */ | 
|---|
| 64 |  | 
|---|
| 65 | void | 
|---|
| 66 | ar_parse_name (char *name, char **arname_p, char **memname_p) | 
|---|
| 67 | { | 
|---|
| 68 | char *p = strchr (name, '('), *end = name + strlen (name) - 1; | 
|---|
| 69 |  | 
|---|
| 70 | if (arname_p != 0) | 
|---|
| 71 | *arname_p = savestring (name, p - name); | 
|---|
| 72 |  | 
|---|
| 73 | if (memname_p != 0) | 
|---|
| 74 | *memname_p = savestring (p + 1, end - (p + 1)); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | static long int ar_member_date_1 PARAMS ((int desc, char *mem, int truncated, long int hdrpos, | 
|---|
| 79 | long int datapos, long int size, long int date, int uid, int gid, int mode, char *name)); | 
|---|
| 80 |  | 
|---|
| 81 | /* Return the modtime of NAME.  */ | 
|---|
| 82 |  | 
|---|
| 83 | time_t | 
|---|
| 84 | ar_member_date (char *name) | 
|---|
| 85 | { | 
|---|
| 86 | char *arname; | 
|---|
| 87 | int arname_used = 0; | 
|---|
| 88 | char *memname; | 
|---|
| 89 | long int val; | 
|---|
| 90 |  | 
|---|
| 91 | ar_parse_name (name, &arname, &memname); | 
|---|
| 92 |  | 
|---|
| 93 | /* Make sure we know the modtime of the archive itself because we are | 
|---|
| 94 | likely to be called just before commands to remake a member are run, | 
|---|
| 95 | and they will change the archive itself. | 
|---|
| 96 |  | 
|---|
| 97 | But we must be careful not to enter_file the archive itself if it does | 
|---|
| 98 | not exist, because pattern_search assumes that files found in the data | 
|---|
| 99 | base exist or can be made.  */ | 
|---|
| 100 | { | 
|---|
| 101 | struct file *arfile; | 
|---|
| 102 | arfile = lookup_file (arname); | 
|---|
| 103 | if (arfile == 0 && file_exists_p (arname)) | 
|---|
| 104 | { | 
|---|
| 105 | arfile = enter_file (arname); | 
|---|
| 106 | arname_used = 1; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | if (arfile != 0) | 
|---|
| 110 | (void) f_mtime (arfile, 0); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | val = ar_scan (arname, ar_member_date_1, (long int) memname); | 
|---|
| 114 |  | 
|---|
| 115 | if (!arname_used) | 
|---|
| 116 | free (arname); | 
|---|
| 117 | free (memname); | 
|---|
| 118 |  | 
|---|
| 119 | return (val <= 0 ? (time_t) -1 : (time_t) val); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /* This function is called by `ar_scan' to find which member to look at.  */ | 
|---|
| 123 |  | 
|---|
| 124 | /* ARGSUSED */ | 
|---|
| 125 | static long int | 
|---|
| 126 | ar_member_date_1 (int desc, char *mem, int truncated, | 
|---|
| 127 | long int hdrpos, long int datapos, long int size, | 
|---|
| 128 | long int date, int uid, int gid, int mode, char *name) | 
|---|
| 129 | { | 
|---|
| 130 | return ar_name_equal (name, mem, truncated) ? date : 0; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | /* Set the archive-member NAME's modtime to now.  */ | 
|---|
| 135 |  | 
|---|
| 136 | #ifdef VMS | 
|---|
| 137 | int | 
|---|
| 138 | ar_touch (char *name) | 
|---|
| 139 | { | 
|---|
| 140 | error (NILF, _("touch archive member is not available on VMS")); | 
|---|
| 141 | return -1; | 
|---|
| 142 | } | 
|---|
| 143 | #else | 
|---|
| 144 | int | 
|---|
| 145 | ar_touch (char *name) | 
|---|
| 146 | { | 
|---|
| 147 | char *arname, *memname; | 
|---|
| 148 | int arname_used = 0; | 
|---|
| 149 | register int val; | 
|---|
| 150 |  | 
|---|
| 151 | ar_parse_name (name, &arname, &memname); | 
|---|
| 152 |  | 
|---|
| 153 | /* Make sure we know the modtime of the archive itself before we | 
|---|
| 154 | touch the member, since this will change the archive itself.  */ | 
|---|
| 155 | { | 
|---|
| 156 | struct file *arfile; | 
|---|
| 157 | arfile = lookup_file (arname); | 
|---|
| 158 | if (arfile == 0) | 
|---|
| 159 | { | 
|---|
| 160 | arfile = enter_file (arname); | 
|---|
| 161 | arname_used = 1; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | (void) f_mtime (arfile, 0); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | val = 1; | 
|---|
| 168 | switch (ar_member_touch (arname, memname)) | 
|---|
| 169 | { | 
|---|
| 170 | case -1: | 
|---|
| 171 | error (NILF, _("touch: Archive `%s' does not exist"), arname); | 
|---|
| 172 | break; | 
|---|
| 173 | case -2: | 
|---|
| 174 | error (NILF, _("touch: `%s' is not a valid archive"), arname); | 
|---|
| 175 | break; | 
|---|
| 176 | case -3: | 
|---|
| 177 | perror_with_name ("touch: ", arname); | 
|---|
| 178 | break; | 
|---|
| 179 | case 1: | 
|---|
| 180 | error (NILF, | 
|---|
| 181 | _("touch: Member `%s' does not exist in `%s'"), memname, arname); | 
|---|
| 182 | break; | 
|---|
| 183 | case 0: | 
|---|
| 184 | val = 0; | 
|---|
| 185 | break; | 
|---|
| 186 | default: | 
|---|
| 187 | error (NILF, | 
|---|
| 188 | _("touch: Bad return code from ar_member_touch on `%s'"), name); | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | if (!arname_used) | 
|---|
| 192 | free (arname); | 
|---|
| 193 | free (memname); | 
|---|
| 194 |  | 
|---|
| 195 | return val; | 
|---|
| 196 | } | 
|---|
| 197 | #endif /* !VMS */ | 
|---|
| 198 |  | 
|---|
| 199 |  | 
|---|
| 200 | /* State of an `ar_glob' run, passed to `ar_glob_match'.  */ | 
|---|
| 201 |  | 
|---|
| 202 | struct ar_glob_state | 
|---|
| 203 | { | 
|---|
| 204 | char *arname; | 
|---|
| 205 | char *pattern; | 
|---|
| 206 | unsigned int size; | 
|---|
| 207 | struct nameseq *chain; | 
|---|
| 208 | unsigned int n; | 
|---|
| 209 | }; | 
|---|
| 210 |  | 
|---|
| 211 | /* This function is called by `ar_scan' to match one archive | 
|---|
| 212 | element against the pattern in STATE.  */ | 
|---|
| 213 |  | 
|---|
| 214 | static long int | 
|---|
| 215 | ar_glob_match (int desc, char *mem, int truncated, | 
|---|
| 216 | long int hdrpos, long int datapos, long int size, | 
|---|
| 217 | long int date, int uid, int gid, int mode, | 
|---|
| 218 | struct ar_glob_state *state) | 
|---|
| 219 | { | 
|---|
| 220 | if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0) | 
|---|
| 221 | { | 
|---|
| 222 | /* We have a match.  Add it to the chain.  */ | 
|---|
| 223 | struct nameseq *new = (struct nameseq *) xmalloc (state->size); | 
|---|
| 224 | new->name = concat (state->arname, mem, ")"); | 
|---|
| 225 | new->next = state->chain; | 
|---|
| 226 | state->chain = new; | 
|---|
| 227 | ++state->n; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | return 0L; | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | /* Return nonzero if PATTERN contains any metacharacters. | 
|---|
| 234 | Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */ | 
|---|
| 235 | static int | 
|---|
| 236 | glob_pattern_p (const char *pattern, int quote) | 
|---|
| 237 | { | 
|---|
| 238 | const char *p; | 
|---|
| 239 | int open = 0; | 
|---|
| 240 |  | 
|---|
| 241 | for (p = pattern; *p != '\0'; ++p) | 
|---|
| 242 | switch (*p) | 
|---|
| 243 | { | 
|---|
| 244 | case '?': | 
|---|
| 245 | case '*': | 
|---|
| 246 | return 1; | 
|---|
| 247 |  | 
|---|
| 248 | case '\\': | 
|---|
| 249 | if (quote) | 
|---|
| 250 | ++p; | 
|---|
| 251 | break; | 
|---|
| 252 |  | 
|---|
| 253 | case '[': | 
|---|
| 254 | open = 1; | 
|---|
| 255 | break; | 
|---|
| 256 |  | 
|---|
| 257 | case ']': | 
|---|
| 258 | if (open) | 
|---|
| 259 | return 1; | 
|---|
| 260 | break; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | return 0; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | /* Glob for MEMBER_PATTERN in archive ARNAME. | 
|---|
| 267 | Return a malloc'd chain of matching elements (or nil if none).  */ | 
|---|
| 268 |  | 
|---|
| 269 | struct nameseq * | 
|---|
| 270 | ar_glob (char *arname, char *member_pattern, unsigned int size) | 
|---|
| 271 | { | 
|---|
| 272 | struct ar_glob_state state; | 
|---|
| 273 | char **names; | 
|---|
| 274 | struct nameseq *n; | 
|---|
| 275 | unsigned int i; | 
|---|
| 276 |  | 
|---|
| 277 | if (! glob_pattern_p (member_pattern, 1)) | 
|---|
| 278 | return 0; | 
|---|
| 279 |  | 
|---|
| 280 | /* Scan the archive for matches. | 
|---|
| 281 | ar_glob_match will accumulate them in STATE.chain.  */ | 
|---|
| 282 | i = strlen (arname); | 
|---|
| 283 | state.arname = (char *) alloca (i + 2); | 
|---|
| 284 | bcopy (arname, state.arname, i); | 
|---|
| 285 | state.arname[i] = '('; | 
|---|
| 286 | state.arname[i + 1] = '\0'; | 
|---|
| 287 | state.pattern = member_pattern; | 
|---|
| 288 | state.size = size; | 
|---|
| 289 | state.chain = 0; | 
|---|
| 290 | state.n = 0; | 
|---|
| 291 | (void) ar_scan (arname, ar_glob_match, (long int) &state); | 
|---|
| 292 |  | 
|---|
| 293 | if (state.chain == 0) | 
|---|
| 294 | return 0; | 
|---|
| 295 |  | 
|---|
| 296 | /* Now put the names into a vector for sorting.  */ | 
|---|
| 297 | names = (char **) alloca (state.n * sizeof (char *)); | 
|---|
| 298 | i = 0; | 
|---|
| 299 | for (n = state.chain; n != 0; n = n->next) | 
|---|
| 300 | names[i++] = n->name; | 
|---|
| 301 |  | 
|---|
| 302 | /* Sort them alphabetically.  */ | 
|---|
| 303 | qsort ((char *) names, i, sizeof (*names), alpha_compare); | 
|---|
| 304 |  | 
|---|
| 305 | /* Put them back into the chain in the sorted order.  */ | 
|---|
| 306 | i = 0; | 
|---|
| 307 | for (n = state.chain; n != 0; n = n->next) | 
|---|
| 308 | n->name = names[i++]; | 
|---|
| 309 |  | 
|---|
| 310 | return state.chain; | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | #endif  /* Not NO_ARCHIVES.  */ | 
|---|