Add or1k target to --enable-targets=all
[deliverable/binutils-gdb.git] / binutils / arsup.c
CommitLineData
252b5132 1/* arsup.c - Archive support for MRI compatibility
219d1afa 2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
252b5132 3
3a1a2036 4 This file is part of GNU Binutils.
252b5132 5
3a1a2036
NC
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
32866df7 8 the Free Software Foundation; either version 3 of the License, or
3a1a2036 9 (at your option) any later version.
252b5132 10
3a1a2036
NC
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.
252b5132 15
3a1a2036
NC
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
32866df7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132
RH
20
21
22/* Contributed by Steve Chamberlain
3a1a2036 23 sac@cygnus.com
252b5132 24
3a1a2036
NC
25 This file looks after requests from arparse.y, to provide the MRI
26 style librarian command syntax + 1 word LIST. */
252b5132 27
3db64b00 28#include "sysdep.h"
252b5132 29#include "bfd.h"
252b5132 30#include "libiberty.h"
5af11cab 31#include "filenames.h"
3db64b00
AM
32#include "bucomm.h"
33#include "arsup.h"
252b5132
RH
34
35static void map_over_list
2da42df6
AJ
36 (bfd *, void (*function) (bfd *, bfd *), struct list *);
37static void ar_directory_doer (bfd *, bfd *);
38static void ar_addlib_doer (bfd *, bfd *);
252b5132
RH
39
40extern int verbose;
5cd84a72 41extern int deterministic;
252b5132 42
85b1c36d
BE
43static bfd *obfd;
44static char *real_name;
45static FILE *outfile;
46
252b5132 47static void
2da42df6 48map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
252b5132
RH
49{
50 bfd *head;
51
52 if (list == NULL)
53 {
54 bfd *next;
55
cc481421 56 head = arch->archive_next;
252b5132
RH
57 while (head != NULL)
58 {
cc481421 59 next = head->archive_next;
252b5132
RH
60 function (head, (bfd *) NULL);
61 head = next;
62 }
63 }
64 else
65 {
66 struct list *ptr;
67
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)
74 {
b34976b6 75 bfd_boolean found = FALSE;
252b5132
RH
76 bfd *prev = arch;
77
cc481421 78 for (head = arch->archive_next; head; head = head->archive_next)
252b5132
RH
79 {
80 if (head->filename != NULL
5af11cab 81 && FILENAME_CMP (ptr->name, head->filename) == 0)
252b5132 82 {
b34976b6 83 found = TRUE;
252b5132
RH
84 function (head, prev);
85 }
86 prev = head;
87 }
88 if (! found)
89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
90 }
91 }
92}
93
94
252b5132 95
252b5132 96static void
2da42df6 97ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
252b5132 98{
3a1a2036 99 print_arelt_descr(outfile, abfd, verbose);
252b5132
RH
100}
101
102void
2da42df6 103ar_directory (char *ar_name, struct list *list, char *output)
252b5132
RH
104{
105 bfd *arch;
106
107 arch = open_inarch (ar_name, (char *) NULL);
108 if (output)
109 {
110 outfile = fopen(output,"w");
111 if (outfile == 0)
112 {
113 outfile = stdout;
114 fprintf (stderr,_("Can't open file %s\n"), output);
115 output = 0;
116 }
117 }
f462a9ea 118 else
252b5132
RH
119 outfile = stdout;
120
121 map_over_list (arch, ar_directory_doer, list);
122
123 bfd_close (arch);
124
125 if (output)
126 fclose (outfile);
127}
128
129void
2da42df6 130prompt (void)
252b5132
RH
131{
132 extern int interactive;
3a1a2036 133
f462a9ea 134 if (interactive)
3a1a2036
NC
135 {
136 printf ("AR >");
137 fflush (stdout);
138 }
252b5132
RH
139}
140
141void
2da42df6 142maybequit (void)
252b5132 143{
f462a9ea 144 if (! interactive)
252b5132
RH
145 xexit (9);
146}
147
148
3a1a2036 149void
2da42df6 150ar_open (char *name, int t)
252b5132
RH
151{
152 char *tname = (char *) xmalloc (strlen (name) + 10);
5e9520c8 153 const char *bname = lbasename (name);
252b5132 154 real_name = name;
3a1a2036 155
5af11cab
AM
156 /* Prepend tmp- to the beginning, to avoid file-name clashes after
157 truncation on filesystems with limited namespaces (DOS). */
3a1a2036
NC
158 sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname);
159 obfd = bfd_openw (tname, NULL);
160
161 if (!obfd)
162 {
163 fprintf (stderr,
164 _("%s: Can't open output archive %s\n"),
165 program_name, tname);
166
167 maybequit ();
252b5132 168 }
3a1a2036
NC
169 else
170 {
171 if (!t)
172 {
173 bfd **ptr;
174 bfd *element;
175 bfd *ibfd;
252b5132 176
3a1a2036 177 ibfd = bfd_openr (name, NULL);
252b5132 178
3a1a2036
NC
179 if (!ibfd)
180 {
181 fprintf (stderr,_("%s: Can't open input archive %s\n"),
182 program_name, name);
183 maybequit ();
184 return;
185 }
186
b34976b6 187 if (!bfd_check_format(ibfd, bfd_archive))
3a1a2036
NC
188 {
189 fprintf (stderr,
190 _("%s: file %s is not an archive\n"),
191 program_name, name);
192 maybequit ();
193 return;
194 }
195
196 ptr = &(obfd->archive_head);
197 element = bfd_openr_next_archived_file (ibfd, NULL);
198
199 while (element)
200 {
201 *ptr = element;
cc481421 202 ptr = &element->archive_next;
3a1a2036
NC
203 element = bfd_openr_next_archived_file (ibfd, element);
204 }
205 }
206
207 bfd_set_format (obfd, bfd_archive);
252b5132 208
3a1a2036 209 obfd->has_armap = 1;
a8da6403 210 obfd->is_thin_archive = 0;
3a1a2036
NC
211 }
212}
252b5132
RH
213
214static void
2da42df6 215ar_addlib_doer (bfd *abfd, bfd *prev)
252b5132 216{
3a1a2036 217 /* Add this module to the output bfd. */
252b5132 218 if (prev != NULL)
cc481421 219 prev->archive_next = abfd->archive_next;
3a1a2036 220
cc481421 221 abfd->archive_next = obfd->archive_head;
252b5132
RH
222 obfd->archive_head = abfd;
223}
224
225void
2da42df6 226ar_addlib (char *name, struct list *list)
252b5132
RH
227{
228 if (obfd == NULL)
229 {
230 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
231 maybequit ();
232 }
233 else
234 {
235 bfd *arch;
236
237 arch = open_inarch (name, (char *) NULL);
238 if (arch != NULL)
239 map_over_list (arch, ar_addlib_doer, list);
240
50c2245b 241 /* Don't close the bfd, since it will make the elements disappear. */
252b5132
RH
242 }
243}
244
245void
2da42df6 246ar_addmod (struct list *list)
252b5132 247{
3a1a2036
NC
248 if (!obfd)
249 {
250 fprintf (stderr, _("%s: no open output archive\n"), program_name);
251 maybequit ();
252 }
f462a9ea 253 else
3a1a2036
NC
254 {
255 while (list)
256 {
70b0cf90 257 bfd *abfd;
3a1a2036 258
70b0cf90
NC
259#if BFD_SUPPORTS_PLUGINS
260 abfd = bfd_openr (list->name, "plugin");
261#else
262 abfd = bfd_openr (list->name, NULL);
263#endif
3a1a2036
NC
264 if (!abfd)
265 {
266 fprintf (stderr, _("%s: can't open file %s\n"),
267 program_name, list->name);
268 maybequit ();
269 }
270 else
271 {
cc481421 272 abfd->archive_next = obfd->archive_head;
3a1a2036
NC
273 obfd->archive_head = abfd;
274 }
275 list = list->next;
276 }
252b5132 277 }
252b5132
RH
278}
279
280
252b5132 281void
2da42df6 282ar_clear (void)
252b5132 283{
3a1a2036
NC
284 if (obfd)
285 obfd->archive_head = 0;
252b5132
RH
286}
287
288void
2da42df6 289ar_delete (struct list *list)
252b5132 290{
3a1a2036
NC
291 if (!obfd)
292 {
293 fprintf (stderr, _("%s: no open output archive\n"), program_name);
294 maybequit ();
295 }
f462a9ea 296 else
3a1a2036
NC
297 {
298 while (list)
299 {
300 /* Find this name in the archive. */
301 bfd *member = obfd->archive_head;
302 bfd **prev = &(obfd->archive_head);
303 int found = 0;
304
305 while (member)
306 {
307 if (FILENAME_CMP(member->filename, list->name) == 0)
308 {
cc481421 309 *prev = member->archive_next;
3a1a2036
NC
310 found = 1;
311 }
312 else
cc481421 313 prev = &(member->archive_next);
3a1a2036 314
cc481421 315 member = member->archive_next;
3a1a2036
NC
316 }
317
318 if (!found)
319 {
320 fprintf (stderr, _("%s: can't find module file %s\n"),
321 program_name, list->name);
322 maybequit ();
323 }
324
325 list = list->next;
252b5132 326 }
252b5132 327 }
252b5132
RH
328}
329
252b5132 330void
2da42df6 331ar_save (void)
252b5132 332{
3a1a2036
NC
333 if (!obfd)
334 {
335 fprintf (stderr, _("%s: no open output archive\n"), program_name);
336 maybequit ();
337 }
338 else
339 {
340 char *ofilename = xstrdup (bfd_get_filename (obfd));
252b5132 341
5cd84a72
NC
342 if (deterministic > 0)
343 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
344
3a1a2036 345 bfd_close (obfd);
252b5132 346
1ba93119 347 smart_rename (ofilename, real_name, 0);
3a1a2036
NC
348 obfd = 0;
349 free (ofilename);
350 }
351}
252b5132
RH
352
353void
2da42df6 354ar_replace (struct list *list)
252b5132 355{
3a1a2036
NC
356 if (!obfd)
357 {
358 fprintf (stderr, _("%s: no open output archive\n"), program_name);
359 maybequit ();
360 }
f462a9ea 361 else
3a1a2036
NC
362 {
363 while (list)
252b5132 364 {
3a1a2036
NC
365 /* Find this name in the archive. */
366 bfd *member = obfd->archive_head;
367 bfd **prev = &(obfd->archive_head);
368 int found = 0;
369
370 while (member)
371 {
372 if (FILENAME_CMP (member->filename, list->name) == 0)
373 {
374 /* Found the one to replace. */
70b0cf90 375 bfd *abfd = bfd_openr (list->name, NULL);
3a1a2036
NC
376
377 if (!abfd)
378 {
379 fprintf (stderr, _("%s: can't open file %s\n"),
380 program_name, list->name);
381 maybequit ();
382 }
383 else
384 {
385 *prev = abfd;
cc481421 386 abfd->archive_next = member->archive_next;
3a1a2036
NC
387 found = 1;
388 }
389 }
390 else
391 {
cc481421 392 prev = &(member->archive_next);
3a1a2036 393 }
cc481421 394 member = member->archive_next;
3a1a2036
NC
395 }
396
397 if (!found)
398 {
70b0cf90 399 bfd *abfd = bfd_openr (list->name, NULL);
252b5132 400
3a1a2036
NC
401 fprintf (stderr,_("%s: can't find module file %s\n"),
402 program_name, list->name);
403 if (!abfd)
404 {
405 fprintf (stderr, _("%s: can't open file %s\n"),
406 program_name, list->name);
407 maybequit ();
408 }
409 else
410 *prev = abfd;
411 }
412
413 list = list->next;
414 }
252b5132 415 }
252b5132
RH
416}
417
3a1a2036 418/* And I added this one. */
252b5132 419void
2da42df6 420ar_list (void)
252b5132 421{
f462a9ea 422 if (!obfd)
252b5132 423 {
3a1a2036
NC
424 fprintf (stderr, _("%s: no open output archive\n"), program_name);
425 maybequit ();
252b5132 426 }
3a1a2036
NC
427 else
428 {
429 bfd *abfd;
430
431 outfile = stdout;
432 verbose =1 ;
433 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
252b5132 434
3a1a2036
NC
435 for (abfd = obfd->archive_head;
436 abfd != (bfd *)NULL;
cc481421 437 abfd = abfd->archive_next)
3a1a2036
NC
438 ar_directory_doer (abfd, (bfd *) NULL);
439 }
440}
252b5132 441
f462a9ea 442void
2da42df6 443ar_end (void)
252b5132
RH
444{
445 if (obfd)
3a1a2036 446 {
c92c35e7 447 bfd_cache_close (obfd);
3a1a2036
NC
448 unlink (bfd_get_filename (obfd));
449 }
252b5132 450}
3a1a2036 451
252b5132 452void
2da42df6 453ar_extract (struct list *list)
252b5132 454{
f462a9ea 455 if (!obfd)
3a1a2036
NC
456 {
457 fprintf (stderr, _("%s: no open archive\n"), program_name);
458 maybequit ();
459 }
f462a9ea 460 else
3a1a2036
NC
461 {
462 while (list)
252b5132 463 {
3a1a2036
NC
464 /* Find this name in the archive. */
465 bfd *member = obfd->archive_head;
466 int found = 0;
467
468 while (member && !found)
469 {
470 if (FILENAME_CMP (member->filename, list->name) == 0)
471 {
472 extract_file (member);
473 found = 1;
474 }
475
cc481421 476 member = member->archive_next;
3a1a2036
NC
477 }
478
479 if (!found)
480 {
70b0cf90 481 bfd_openr (list->name, NULL);
3a1a2036
NC
482 fprintf (stderr, _("%s: can't find module file %s\n"),
483 program_name, list->name);
484 }
485
486 list = list->next;
487 }
252b5132 488 }
252b5132 489}
This page took 1.014428 seconds and 4 git commands to generate.