3 * Copyright (c) 2008 Kai Wang
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/queue.h>
34 #include <archive_entry.h>
50 #define TEMPLATE "arscp.XXXXXXXX"
58 extern int yylex(void);
59 extern int yyparse(void);
61 static void yyerror(const char *);
62 static void arscp_addlib(char *archive, struct list *list);
63 static void arscp_addmod(struct list *list);
64 static void arscp_clear(void);
65 static void arscp_create(char *in, char *out);
66 static void arscp_delete(struct list *list);
67 static void arscp_dir(char *archive, struct list *list, char *rlt);
68 static void arscp_end(int eval);
69 static void arscp_extract(struct list *list);
70 static void arscp_free_argv(void);
71 static void arscp_free_mlist(struct list *list);
72 static void arscp_list(void);
73 static struct list *arscp_mlist(struct list *list, char *str);
74 static void arscp_mlist2argv(struct list *list);
75 static int arscp_mlist_len(struct list *list);
76 static void arscp_open(char *fname);
77 static void arscp_prompt(void);
78 static void arscp_replace(struct list *list);
79 static void arscp_save(void);
80 static int arscp_target_exist(void);
84 static struct bsdar *bsdar;
87 static int interactive;
110 %type <list> mod_list
120 : { arscp_prompt(); } ar_script
129 : FNAME { $$ = arscp_mlist(NULL, $1); }
130 | mod_list separator FNAME { $$ = arscp_mlist($1, $3); }
144 : cmd EOL { arscp_prompt(); }
167 : ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); }
168 | ADDLIB FNAME { arscp_addlib($2, NULL); }
172 : ADDMOD mod_list { arscp_addmod($2); }
176 : CLEAR { arscp_clear(); }
180 : CREATE FNAME { arscp_create(NULL, $2); }
184 : DELETE mod_list { arscp_delete($2); }
188 : DIRECTORY FNAME { arscp_dir($2, NULL, NULL); }
189 | DIRECTORY FNAME LP mod_list RP { arscp_dir($2, $4, NULL); }
190 | DIRECTORY FNAME LP mod_list RP FNAME { arscp_dir($2, $4, $6); }
194 : END { arscp_end(EXIT_SUCCESS); }
198 : EXTRACT mod_list { arscp_extract($2); }
202 : LIST { arscp_list(); }
206 : OPEN FNAME { arscp_open($2); }
210 : REPLACE mod_list { arscp_replace($2); }
214 : SAVE { arscp_save(); }
218 : VERBOSE { verbose = !verbose; }
226 : FNAME { yyerror(NULL); }
233 yyerror(const char *s)
237 printf("Syntax error in archive script, line %d\n", lineno);
241 * The arscp_open() function will first open an archive and check its
242 * validity. If the archive format is valid, it will call
243 * arscp_create() to create a temporary copy of the archive.
246 arscp_open(char *fname)
249 struct archive_entry *entry;
252 if ((a = archive_read_new()) == NULL)
253 bsdar_errc(bsdar, 0, "archive_read_new failed");
254 archive_read_support_format_ar(a);
255 AC(archive_read_open_filename(a, fname, DEF_BLKSZ));
256 if ((r = archive_read_next_header(a, &entry)))
257 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
258 AC(archive_read_close(a));
259 ACV(archive_read_free(a));
262 arscp_create(fname, fname);
268 * If the parameter 'in' is NULL (the 'CREATE' command), a new empty
269 * archive will be created. If the parameter 'in' is not NULL (the
270 * 'OPEN' command), the resulting archive will be a modified version
271 * of the existing archive.
274 arscp_create(char *in, char *out)
279 /* Delete the previously created temporary archive, if any. */
281 if (unlink(tmpac) < 0)
282 bsdar_errc(bsdar, errno, "unlink failed");
286 tmpac = strdup(TEMPLATE);
288 bsdar_errc(bsdar, errno, "strdup failed");
289 if ((ofd = mkstemp(tmpac)) < 0)
290 bsdar_errc(bsdar, errno, "mkstemp failed");
294 * The 'OPEN' command creates a temporary copy of the
297 if ((ifd = open(in, O_RDONLY)) < 0 ||
298 elftc_copyfile(ifd, ofd) < 0) {
299 bsdar_warnc(bsdar, errno, "'OPEN' failed");
309 * The 'CREATE' command creates an "empty" archive (an
310 * archive consisting only of the archive header).
312 if ((a = archive_write_new()) == NULL)
313 bsdar_errc(bsdar, 0, "archive_write_new failed");
314 archive_write_set_format_ar_svr4(a);
315 AC(archive_write_open_fd(a, ofd));
316 AC(archive_write_close(a));
317 ACV(archive_write_free(a));
320 /* Override the previous target, if any. */
325 bsdar->filename = tmpac;
329 * Add all modules of an archive to the current archive. If the
330 * parameter 'list' is not NULL, only those modules specified by
331 * 'list' will be added.
334 arscp_addlib(char *archive, struct list *list)
337 if (!arscp_target_exist())
339 arscp_mlist2argv(list);
340 bsdar->addlib = archive;
341 ar_write_archive(bsdar, 'A');
343 arscp_free_mlist(list);
347 * Add modules to the current archive.
350 arscp_addmod(struct list *list)
353 if (!arscp_target_exist())
355 arscp_mlist2argv(list);
356 ar_write_archive(bsdar, 'q');
358 arscp_free_mlist(list);
362 * Delete modules from the current archive.
365 arscp_delete(struct list *list)
368 if (!arscp_target_exist())
370 arscp_mlist2argv(list);
371 ar_write_archive(bsdar, 'd');
373 arscp_free_mlist(list);
377 * Extract modules from the current archive.
380 arscp_extract(struct list *list)
383 if (!arscp_target_exist())
385 arscp_mlist2argv(list);
386 ar_read_archive(bsdar, 'x');
388 arscp_free_mlist(list);
392 * List the contents of an archive (simple mode).
398 if (!arscp_target_exist())
402 /* Always verbose. */
403 bsdar->options |= AR_V;
404 ar_read_archive(bsdar, 't');
405 bsdar->options &= ~AR_V;
409 * List the contents of an archive (advanced mode).
412 arscp_dir(char *archive, struct list *list, char *rlt)
416 /* If rlt != NULL, redirect the output to it. */
420 if ((bsdar->output = fopen(rlt, "w")) == NULL)
421 bsdar_errc(bsdar, errno, "fopen %s failed", rlt);
424 bsdar->filename = archive;
426 arscp_mlist2argv(list);
432 bsdar->options |= AR_V;
433 ar_read_archive(bsdar, 't');
434 bsdar->options &= ~AR_V;
437 if (fclose(bsdar->output) == EOF)
438 bsdar_errc(bsdar, errno, "fclose %s failed", rlt);
443 bsdar->filename = tmpac;
445 arscp_free_mlist(list);
450 * Replace modules in the current archive.
453 arscp_replace(struct list *list)
456 if (!arscp_target_exist())
458 arscp_mlist2argv(list);
459 ar_write_archive(bsdar, 'r');
461 arscp_free_mlist(list);
465 * Rename the temporary archive to the target archive.
473 if (rename(tmpac, target) < 0)
474 bsdar_errc(bsdar, errno, "rename failed");
476 * Because mkstemp() creates temporary files with mode
477 * 0600, we set target archive's mode as per the
482 if (chmod(target, 0666 & ~mask) < 0)
483 bsdar_errc(bsdar, errno, "chmod failed");
488 bsdar->filename = NULL;
490 bsdar_warnc(bsdar, 0, "no open output archive");
494 * Discard the contents of the current archive. This is achieved by
495 * invoking the 'CREATE' cmd on the current archive.
503 new_target = strdup(target);
504 if (new_target == NULL)
505 bsdar_errc(bsdar, errno, "strdup failed");
506 arscp_create(NULL, new_target);
511 * Quit ar(1). Note that the 'END' cmd will not 'SAVE' the current
512 * archive before exiting.
521 if (unlink(tmpac) == -1)
522 bsdar_errc(bsdar, errno, "unlink %s failed", tmpac);
530 * Check if a target was specified, i.e, whether an 'OPEN' or 'CREATE'
531 * had been issued by the user.
534 arscp_target_exist(void)
540 bsdar_warnc(bsdar, 0, "no open output archive");
545 * Construct the list of modules.
548 arscp_mlist(struct list *list, char *str)
552 l = malloc(sizeof(*l));
554 bsdar_errc(bsdar, errno, "malloc failed");
562 * Calculate the length of an mlist.
565 arscp_mlist_len(struct list *list)
569 for(len = 0; list; list = list->next)
576 * Free the space allocated for a module list.
579 arscp_free_mlist(struct list *list)
583 /* Note: list->str was freed in arscp_free_argv(). */
584 for(; list; list = l) {
591 * Convert a module list to an 'argv' array.
594 arscp_mlist2argv(struct list *list)
599 n = arscp_mlist_len(list);
600 argv = malloc(n * sizeof(*argv));
602 bsdar_errc(bsdar, errno, "malloc failed");
604 /* Note that module names are stored in reverse order. */
605 for(i = n - 1; i >= 0; i--, list = list->next) {
607 bsdar_errc(bsdar, errno, "invalid mlist");
616 * Free the space allocated for an argv array and its elements.
619 arscp_free_argv(void)
623 for(i = 0; i < bsdar->argc; i++)
624 free(bsdar->argv[i]);
630 * Show a prompt if we are in interactive mode.
643 * The main function implementing script mode.
646 ar_mode_script(struct bsdar *ar)
650 interactive = isatty(fileno(stdin));
653 arscp_end(EXIT_FAILURE);
656 /* Script ends without END */
657 arscp_end(EXIT_SUCCESS);