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