2008-02-04 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / binutils / arsup.c
blob49961b0ef1167336f5c475891d13dd55b4fe1de5
1 /* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
3 2004, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* Contributed by Steve Chamberlain
24 sac@cygnus.com
26 This file looks after requests from arparse.y, to provide the MRI
27 style librarian command syntax + 1 word LIST. */
29 #include "sysdep.h"
30 #include "bfd.h"
31 #include "libiberty.h"
32 #include "filenames.h"
33 #include "bucomm.h"
34 #include "arsup.h"
36 static void map_over_list
37 (bfd *, void (*function) (bfd *, bfd *), struct list *);
38 static void ar_directory_doer (bfd *, bfd *);
39 static void ar_addlib_doer (bfd *, bfd *);
41 extern int verbose;
43 static bfd *obfd;
44 static char *real_name;
45 static FILE *outfile;
47 static void
48 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
50 bfd *head;
52 if (list == NULL)
54 bfd *next;
56 head = arch->archive_next;
57 while (head != NULL)
59 next = head->archive_next;
60 function (head, (bfd *) NULL);
61 head = next;
64 else
66 struct list *ptr;
68 /* This may appear to be a baroque way of accomplishing what we
69 want. however we have to iterate over the filenames in order
70 to notice where a filename is requested but does not exist in
71 the archive. Ditto mapping over each file each time -- we
72 want to hack multiple references. */
73 for (ptr = list; ptr; ptr = ptr->next)
75 bfd_boolean found = FALSE;
76 bfd *prev = arch;
78 for (head = arch->archive_next; head; head = head->archive_next)
80 if (head->filename != NULL
81 && FILENAME_CMP (ptr->name, head->filename) == 0)
83 found = TRUE;
84 function (head, prev);
86 prev = head;
88 if (! found)
89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
96 static void
97 ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
99 print_arelt_descr(outfile, abfd, verbose);
102 void
103 ar_directory (char *ar_name, struct list *list, char *output)
105 bfd *arch;
107 arch = open_inarch (ar_name, (char *) NULL);
108 if (output)
110 outfile = fopen(output,"w");
111 if (outfile == 0)
113 outfile = stdout;
114 fprintf (stderr,_("Can't open file %s\n"), output);
115 output = 0;
118 else
119 outfile = stdout;
121 map_over_list (arch, ar_directory_doer, list);
123 bfd_close (arch);
125 if (output)
126 fclose (outfile);
129 void
130 prompt (void)
132 extern int interactive;
134 if (interactive)
136 printf ("AR >");
137 fflush (stdout);
141 void
142 maybequit (void)
144 if (! interactive)
145 xexit (9);
149 void
150 ar_open (char *name, int t)
152 char *tname = (char *) xmalloc (strlen (name) + 10);
153 const char *bname = lbasename (name);
154 real_name = name;
156 /* Prepend tmp- to the beginning, to avoid file-name clashes after
157 truncation on filesystems with limited namespaces (DOS). */
158 sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname);
159 obfd = bfd_openw (tname, NULL);
161 if (!obfd)
163 fprintf (stderr,
164 _("%s: Can't open output archive %s\n"),
165 program_name, tname);
167 maybequit ();
169 else
171 if (!t)
173 bfd **ptr;
174 bfd *element;
175 bfd *ibfd;
177 ibfd = bfd_openr (name, NULL);
179 if (!ibfd)
181 fprintf (stderr,_("%s: Can't open input archive %s\n"),
182 program_name, name);
183 maybequit ();
184 return;
187 if (!bfd_check_format(ibfd, bfd_archive))
189 fprintf (stderr,
190 _("%s: file %s is not an archive\n"),
191 program_name, name);
192 maybequit ();
193 return;
196 ptr = &(obfd->archive_head);
197 element = bfd_openr_next_archived_file (ibfd, NULL);
199 while (element)
201 *ptr = element;
202 ptr = &element->archive_next;
203 element = bfd_openr_next_archived_file (ibfd, element);
207 bfd_set_format (obfd, bfd_archive);
209 obfd->has_armap = 1;
213 static void
214 ar_addlib_doer (bfd *abfd, bfd *prev)
216 /* Add this module to the output bfd. */
217 if (prev != NULL)
218 prev->archive_next = abfd->archive_next;
220 abfd->archive_next = obfd->archive_head;
221 obfd->archive_head = abfd;
224 void
225 ar_addlib (char *name, struct list *list)
227 if (obfd == NULL)
229 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
230 maybequit ();
232 else
234 bfd *arch;
236 arch = open_inarch (name, (char *) NULL);
237 if (arch != NULL)
238 map_over_list (arch, ar_addlib_doer, list);
240 /* Don't close the bfd, since it will make the elements disappear. */
244 void
245 ar_addmod (struct list *list)
247 if (!obfd)
249 fprintf (stderr, _("%s: no open output archive\n"), program_name);
250 maybequit ();
252 else
254 while (list)
256 bfd *abfd = bfd_openr (list->name, NULL);
258 if (!abfd)
260 fprintf (stderr, _("%s: can't open file %s\n"),
261 program_name, list->name);
262 maybequit ();
264 else
266 abfd->archive_next = obfd->archive_head;
267 obfd->archive_head = abfd;
269 list = list->next;
275 void
276 ar_clear (void)
278 if (obfd)
279 obfd->archive_head = 0;
282 void
283 ar_delete (struct list *list)
285 if (!obfd)
287 fprintf (stderr, _("%s: no open output archive\n"), program_name);
288 maybequit ();
290 else
292 while (list)
294 /* Find this name in the archive. */
295 bfd *member = obfd->archive_head;
296 bfd **prev = &(obfd->archive_head);
297 int found = 0;
299 while (member)
301 if (FILENAME_CMP(member->filename, list->name) == 0)
303 *prev = member->archive_next;
304 found = 1;
306 else
307 prev = &(member->archive_next);
309 member = member->archive_next;
312 if (!found)
314 fprintf (stderr, _("%s: can't find module file %s\n"),
315 program_name, list->name);
316 maybequit ();
319 list = list->next;
324 void
325 ar_save (void)
327 if (!obfd)
329 fprintf (stderr, _("%s: no open output archive\n"), program_name);
330 maybequit ();
332 else
334 char *ofilename = xstrdup (bfd_get_filename (obfd));
336 bfd_close (obfd);
338 smart_rename (ofilename, real_name, 0);
339 obfd = 0;
340 free (ofilename);
344 void
345 ar_replace (struct list *list)
347 if (!obfd)
349 fprintf (stderr, _("%s: no open output archive\n"), program_name);
350 maybequit ();
352 else
354 while (list)
356 /* Find this name in the archive. */
357 bfd *member = obfd->archive_head;
358 bfd **prev = &(obfd->archive_head);
359 int found = 0;
361 while (member)
363 if (FILENAME_CMP (member->filename, list->name) == 0)
365 /* Found the one to replace. */
366 bfd *abfd = bfd_openr (list->name, 0);
368 if (!abfd)
370 fprintf (stderr, _("%s: can't open file %s\n"),
371 program_name, list->name);
372 maybequit ();
374 else
376 *prev = abfd;
377 abfd->archive_next = member->archive_next;
378 found = 1;
381 else
383 prev = &(member->archive_next);
385 member = member->archive_next;
388 if (!found)
390 bfd *abfd = bfd_openr (list->name, 0);
392 fprintf (stderr,_("%s: can't find module file %s\n"),
393 program_name, list->name);
394 if (!abfd)
396 fprintf (stderr, _("%s: can't open file %s\n"),
397 program_name, list->name);
398 maybequit ();
400 else
401 *prev = abfd;
404 list = list->next;
409 /* And I added this one. */
410 void
411 ar_list (void)
413 if (!obfd)
415 fprintf (stderr, _("%s: no open output archive\n"), program_name);
416 maybequit ();
418 else
420 bfd *abfd;
422 outfile = stdout;
423 verbose =1 ;
424 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
426 for (abfd = obfd->archive_head;
427 abfd != (bfd *)NULL;
428 abfd = abfd->archive_next)
429 ar_directory_doer (abfd, (bfd *) NULL);
433 void
434 ar_end (void)
436 if (obfd)
438 bfd_cache_close (obfd);
439 unlink (bfd_get_filename (obfd));
443 void
444 ar_extract (struct list *list)
446 if (!obfd)
448 fprintf (stderr, _("%s: no open archive\n"), program_name);
449 maybequit ();
451 else
453 while (list)
455 /* Find this name in the archive. */
456 bfd *member = obfd->archive_head;
457 int found = 0;
459 while (member && !found)
461 if (FILENAME_CMP (member->filename, list->name) == 0)
463 extract_file (member);
464 found = 1;
467 member = member->archive_next;
470 if (!found)
472 bfd_openr (list->name, 0);
473 fprintf (stderr, _("%s: can't find module file %s\n"),
474 program_name, list->name);
477 list = list->next;