bfd:
[binutils.git] / binutils / arsup.c
blobd6809fc47497a652fcfaf6fade10a4a9ef32613e
1 /* arsup.c - Archive support for MRI compatibility
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999
3 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Contributed by Steve Chamberlain
23 sac@cygnus.com
25 This file looks after requests from arparse.y, to provide the MRI
26 style librarian command syntax + 1 word LIST
30 #include "bfd.h"
31 #include "arsup.h"
32 #include "libiberty.h"
33 #include "bucomm.h"
35 static void map_over_list
36 PARAMS ((bfd *, void (*function) (bfd *, bfd *), struct list *));
37 static void ar_directory_doer PARAMS ((bfd *, bfd *));
38 static void ar_addlib_doer PARAMS ((bfd *, bfd *));
40 extern int verbose;
42 static void
43 map_over_list (arch, function, list)
44 bfd *arch;
45 void (*function) PARAMS ((bfd *, bfd *));
46 struct list *list;
48 bfd *head;
50 if (list == NULL)
52 bfd *next;
54 head = arch->next;
55 while (head != NULL)
57 next = head->next;
58 function (head, (bfd *) NULL);
59 head = next;
62 else
64 struct list *ptr;
66 /* This may appear to be a baroque way of accomplishing what we
67 want. however we have to iterate over the filenames in order
68 to notice where a filename is requested but does not exist in
69 the archive. Ditto mapping over each file each time -- we
70 want to hack multiple references. */
71 for (ptr = list; ptr; ptr = ptr->next)
73 boolean found = false;
74 bfd *prev = arch;
76 for (head = arch->next; head; head = head->next)
78 if (head->filename != NULL
79 && strcmp (ptr->name, head->filename) == 0)
81 found = true;
82 function (head, prev);
84 prev = head;
86 if (! found)
87 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
93 FILE *outfile;
95 /*ARGSUSED*/
96 static void
97 ar_directory_doer (abfd, ignore)
98 bfd *abfd;
99 bfd *ignore ATTRIBUTE_UNUSED;
101 print_arelt_descr(outfile, abfd, verbose);
104 void
105 ar_directory (ar_name, list, output)
106 char *ar_name;
107 struct list *list;
108 char *output;
110 bfd *arch;
112 arch = open_inarch (ar_name, (char *) NULL);
113 if (output)
115 outfile = fopen(output,"w");
116 if (outfile == 0)
118 outfile = stdout;
119 fprintf (stderr,_("Can't open file %s\n"), output);
120 output = 0;
123 else
124 outfile = stdout;
126 map_over_list (arch, ar_directory_doer, list);
128 bfd_close (arch);
130 if (output)
131 fclose (outfile);
134 void
135 DEFUN_VOID(prompt)
137 extern int interactive;
138 if (interactive)
140 printf("AR >");
141 fflush(stdout);
145 void
146 maybequit ()
148 if (! interactive)
149 xexit (9);
153 bfd *obfd;
154 char *real_name ;
155 void
156 DEFUN(ar_open,(name, t),
157 char *name AND
158 int t)
161 char *tname = (char *) xmalloc (strlen (name) + 10);
162 real_name = name;
163 sprintf(tname, "%s-tmp", name);
164 obfd = bfd_openw(tname, NULL);
166 if (!obfd) {
167 fprintf(stderr,_("%s: Can't open output archive %s\n"), program_name,
168 tname);
170 maybequit();
172 else {
173 if (!t) {
174 bfd **ptr;
175 bfd *element;
176 bfd *ibfd;
177 ibfd = bfd_openr(name, NULL);
178 if (!ibfd) {
179 fprintf(stderr,_("%s: Can't open input archive %s\n"),
180 program_name, name);
181 maybequit();
182 return;
184 if (bfd_check_format(ibfd, bfd_archive) != true) {
185 fprintf(stderr,_("%s: file %s is not an archive\n"), program_name,
186 name);
187 maybequit();
188 return;
190 ptr = &(obfd->archive_head);
191 element = bfd_openr_next_archived_file(ibfd, NULL);
193 while (element) {
194 *ptr = element;
195 ptr = &element->next;
196 element = bfd_openr_next_archived_file(ibfd, element);
200 bfd_set_format(obfd, bfd_archive);
202 obfd->has_armap = 1;
207 static void
208 ar_addlib_doer (abfd, prev)
209 bfd *abfd;
210 bfd *prev;
212 /* Add this module to the output bfd */
213 if (prev != NULL)
214 prev->next = abfd->next;
215 abfd->next = obfd->archive_head;
216 obfd->archive_head = abfd;
219 void
220 ar_addlib (name, list)
221 char *name;
222 struct list *list;
224 if (obfd == NULL)
226 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
227 maybequit ();
229 else
231 bfd *arch;
233 arch = open_inarch (name, (char *) NULL);
234 if (arch != NULL)
235 map_over_list (arch, ar_addlib_doer, list);
237 /* Don't close the bfd, since it will make the elements disasppear */
241 void
242 DEFUN(ar_addmod, (list),
243 struct list *list)
245 if (!obfd) {
246 fprintf(stderr, _("%s: no open output archive\n"), program_name);
247 maybequit();
249 else
251 while (list) {
252 bfd *abfd = bfd_openr(list->name, NULL);
253 if (!abfd) {
254 fprintf(stderr,_("%s: can't open file %s\n"), program_name,
255 list->name);
256 maybequit();
258 else {
259 abfd->next = obfd->archive_head;
260 obfd->archive_head = abfd;
262 list = list->next;
269 void
270 DEFUN_VOID(ar_clear)
272 if (obfd)
273 obfd->archive_head = 0;
276 void
277 DEFUN(ar_delete, (list),
278 struct list *list)
280 if (!obfd) {
281 fprintf(stderr, _("%s: no open output archive\n"), program_name);
282 maybequit();
284 else
286 while (list) {
287 /* Find this name in the archive */
288 bfd *member = obfd->archive_head;
289 bfd **prev = &(obfd->archive_head);
290 int found = 0;
291 while (member) {
292 if (strcmp(member->filename, list->name) == 0) {
293 *prev = member->next;
294 found = 1;
296 else {
297 prev = &(member->next);
299 member = member->next;
301 if (!found) {
302 fprintf(stderr,_("%s: can't find module file %s\n"), program_name,
303 list->name);
304 maybequit();
306 list = list->next;
312 void
313 DEFUN_VOID(ar_save)
316 if (!obfd) {
317 fprintf(stderr, _("%s: no open output archive\n"), program_name);
318 maybequit();
320 else {
321 char *ofilename = xstrdup (bfd_get_filename (obfd));
322 bfd_close(obfd);
324 rename (ofilename, real_name);
325 obfd = 0;
326 free(ofilename);
332 void
333 DEFUN(ar_replace, (list),
334 struct list *list)
336 if (!obfd) {
337 fprintf(stderr, _("%s: no open output archive\n"), program_name);
338 maybequit();
340 else
342 while (list) {
343 /* Find this name in the archive */
344 bfd *member = obfd->archive_head;
345 bfd **prev = &(obfd->archive_head);
346 int found = 0;
347 while (member)
349 if (strcmp(member->filename, list->name) == 0)
351 /* Found the one to replace */
352 bfd *abfd = bfd_openr(list->name, 0);
353 if (!abfd)
355 fprintf(stderr, _("%s: can't open file %s\n"), program_name, list->name);
356 maybequit();
358 else {
359 *prev = abfd;
360 abfd->next = member->next;
361 found = 1;
364 else {
365 prev = &(member->next);
367 member = member->next;
369 if (!found) {
370 bfd *abfd = bfd_openr(list->name, 0);
371 fprintf(stderr,_("%s: can't find module file %s\n"), program_name,
372 list->name);
373 if (!abfd)
375 fprintf(stderr, _("%s: can't open file %s\n"), program_name, list->name);
376 maybequit();
378 else
380 *prev = abfd;
384 list = list->next;
389 /* And I added this one */
390 void
391 DEFUN_VOID(ar_list)
393 if (!obfd)
395 fprintf(stderr, _("%s: no open output archive\n"), program_name);
396 maybequit();
398 else {
399 bfd *abfd;
400 outfile = stdout;
401 verbose =1 ;
402 printf(_("Current open archive is %s\n"), bfd_get_filename (obfd));
403 for (abfd = obfd->archive_head;
404 abfd != (bfd *)NULL;
405 abfd = abfd->next)
407 ar_directory_doer (abfd, (bfd *) NULL);
413 void
414 DEFUN_VOID(ar_end)
416 if (obfd)
418 fclose((FILE *)(obfd->iostream));
419 unlink(bfd_get_filename (obfd));
422 void
423 DEFUN(ar_extract,(list),
424 struct list *list)
426 if (!obfd)
429 fprintf(stderr, _("%s: no open archive\n"), program_name);
430 maybequit();
432 else
434 while (list) {
435 /* Find this name in the archive */
436 bfd *member = obfd->archive_head;
437 int found = 0;
438 while (member && !found)
440 if (strcmp(member->filename, list->name) == 0)
442 extract_file(member);
443 found = 1;
446 member = member->next;
448 if (!found) {
449 bfd_openr(list->name, 0);
450 fprintf(stderr,_("%s: can't find module file %s\n"), program_name,
451 list->name);
454 list = list->next;