3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Kai Wang
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/queue.h>
38 #include <archive_entry.h>
49 #define TEMPLATE "arscp.XXXXXXXX"
57 extern int yylex(void);
59 static void yyerror(const char *);
60 static void arscp_addlib(char *archive, struct list *list);
61 static void arscp_addmod(struct list *list);
62 static void arscp_clear(void);
63 static int arscp_copy(int ifd, int ofd);
64 static void arscp_create(char *in, char *out);
65 static void arscp_delete(struct list *list);
66 static void arscp_dir(char *archive, struct list *list, char *rlt);
67 static void arscp_end(int eval);
68 static void arscp_extract(struct list *list);
69 static void arscp_free_argv(void);
70 static void arscp_free_mlist(struct list *list);
71 static void arscp_list(void);
72 static struct list *arscp_mlist(struct list *list, char *str);
73 static void arscp_mlist2argv(struct list *list);
74 static int arscp_mlist_len(struct list *list);
75 static void arscp_open(char *fname);
76 static void arscp_prompt(void);
77 static void arscp_replace(struct list *list);
78 static void arscp_save(void);
79 static int arscp_target_exist(void);
83 static struct bsdar *bsdar;
86 static int interactive;
109 %type <list> mod_list
119 : { arscp_prompt(); } ar_script
128 : FNAME { $$ = arscp_mlist(NULL, $1); }
129 | mod_list separator FNAME { $$ = arscp_mlist($1, $3); }
143 : cmd EOL { arscp_prompt(); }
166 : ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); }
167 | ADDLIB FNAME { arscp_addlib($2, NULL); }
171 : ADDMOD mod_list { arscp_addmod($2); }
175 : CLEAR { arscp_clear(); }
179 : CREATE FNAME { arscp_create(NULL, $2); }
183 : DELETE mod_list { arscp_delete($2); }
187 : DIRECTORY FNAME { arscp_dir($2, NULL, NULL); }
188 | DIRECTORY FNAME LP mod_list RP { arscp_dir($2, $4, NULL); }
189 | DIRECTORY FNAME LP mod_list RP FNAME { arscp_dir($2, $4, $6); }
193 : END { arscp_end(EXIT_SUCCESS); }
197 : EXTRACT mod_list { arscp_extract($2); }
201 : LIST { arscp_list(); }
205 : OPEN FNAME { arscp_open($2); }
209 : REPLACE mod_list { arscp_replace($2); }
213 : SAVE { arscp_save(); }
217 : VERBOSE { verbose = !verbose; }
225 : FNAME { yyerror(NULL); }
232 yyerror(const char *s)
236 printf("Syntax error in archive script, line %d\n", lineno);
240 * arscp_open first open an archive and check its validity. If the archive
241 * format is valid, it calls arscp_create to create a temporary copy of
245 arscp_open(char *fname)
248 struct archive_entry *entry;
251 if ((a = archive_read_new()) == NULL)
252 bsdar_errc(bsdar, 0, "archive_read_new failed");
253 archive_read_support_format_ar(a);
254 AC(archive_read_open_filename(a, fname, DEF_BLKSZ));
255 if ((r = archive_read_next_header(a, &entry)))
256 bsdar_warnc(bsdar, archive_errno(a), "%s",
257 archive_error_string(a));
258 AC(archive_read_close(a));
259 AC(archive_read_free(a));
262 arscp_create(fname, fname);
266 * Create archive. in != NULL indicate it's a OPEN cmd, and resulting
267 * archive is based on modification of an existing one. If in == NULL,
268 * we are in CREATE cmd and a new empty archive will be created.
271 arscp_create(char *in, char *out)
276 /* Delete previously created temporary archive, if any. */
278 if (unlink(tmpac) < 0)
279 bsdar_errc(bsdar, errno, "unlink failed");
283 tmpac = strdup(TEMPLATE);
285 bsdar_errc(bsdar, errno, "strdup failed");
286 if ((ofd = mkstemp(tmpac)) < 0)
287 bsdar_errc(bsdar, errno, "mkstemp failed");
291 * Command OPEN creates a temporary copy of the
294 if ((ifd = open(in, O_RDONLY)) < 0) {
295 bsdar_warnc(bsdar, errno, "open failed");
298 if (arscp_copy(ifd, ofd)) {
299 bsdar_warnc(bsdar, 0, "arscp_copy failed");
306 * Command CREATE creates an "empty" archive.
307 * (archive with only global header)
309 if ((a = archive_write_new()) == NULL)
310 bsdar_errc(bsdar, 0, "archive_write_new failed");
311 archive_write_set_format_ar_svr4(a);
312 AC(archive_write_open_fd(a, ofd));
313 AC(archive_write_close(a));
314 AC(archive_write_free(a));
317 /* Override previous target, if any. */
322 bsdar->filename = tmpac;
325 /* A file copying implementation using mmap. */
327 arscp_copy(int ifd, int ofd)
334 if (fstat(ifd, &sb) < 0) {
335 bsdar_warnc(bsdar, errno, "fstate failed");
338 if ((p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd,
339 (off_t)0)) == MAP_FAILED) {
340 bsdar_warnc(bsdar, errno, "mmap failed");
343 for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) {
344 w = write(ofd, buf, bytes);
346 bsdar_warnc(bsdar, errno, "write failed");
350 if (munmap(p, sb.st_size) < 0)
351 bsdar_errc(bsdar, errno, "munmap failed");
359 * Add all modules of archive to current archive, if list != NULL,
360 * only those modules specified in 'list' will be added.
363 arscp_addlib(char *archive, struct list *list)
366 if (!arscp_target_exist())
368 arscp_mlist2argv(list);
369 bsdar->addlib = archive;
370 ar_write_archive(bsdar, 'A');
372 arscp_free_mlist(list);
375 /* Add modules into current archive. */
377 arscp_addmod(struct list *list)
380 if (!arscp_target_exist())
382 arscp_mlist2argv(list);
383 ar_write_archive(bsdar, 'q');
385 arscp_free_mlist(list);
388 /* Delete modules from current archive. */
390 arscp_delete(struct list *list)
393 if (!arscp_target_exist())
395 arscp_mlist2argv(list);
396 ar_write_archive(bsdar, 'd');
398 arscp_free_mlist(list);
401 /* Extract modules from current archive. */
403 arscp_extract(struct list *list)
406 if (!arscp_target_exist())
408 arscp_mlist2argv(list);
409 ar_read_archive(bsdar, 'x', stdout);
411 arscp_free_mlist(list);
414 /* List modules of archive. (Simple Mode) */
419 if (!arscp_target_exist())
423 /* Always verbose. */
424 bsdar->options |= AR_V;
425 ar_read_archive(bsdar, 't', stdout);
426 bsdar->options &= ~AR_V;
429 /* List modules of archive. (Advance Mode) */
431 arscp_dir(char *archive, struct list *list, char *rlt)
435 /* If rlt != NULL, redirect output to it */
438 if ((out = fopen(rlt, "w")) == NULL)
439 bsdar_errc(bsdar, errno, "fopen %s failed", rlt);
442 bsdar->filename = archive;
444 arscp_mlist2argv(list);
450 bsdar->options |= AR_V;
451 ar_read_archive(bsdar, 't', out);
452 bsdar->options &= ~AR_V;
455 if (fclose(out) == EOF)
456 bsdar_errc(bsdar, errno, "fclose %s failed", rlt);
460 bsdar->filename = tmpac;
462 arscp_free_mlist(list);
466 /* Replace modules of current archive. */
468 arscp_replace(struct list *list)
471 if (!arscp_target_exist())
473 arscp_mlist2argv(list);
474 ar_write_archive(bsdar, 'r');
476 arscp_free_mlist(list);
479 /* Rename the temporary archive to the target archive. */
486 if (rename(tmpac, target) < 0)
487 bsdar_errc(bsdar, errno, "rename failed");
489 * mkstemp creates temp files with mode 0600, here we
490 * set target archive mode per process umask.
494 if (chmod(target, 0666 & ~mask) < 0)
495 bsdar_errc(bsdar, errno, "chmod failed");
500 bsdar->filename = NULL;
502 bsdar_warnc(bsdar, 0, "no open output archive");
506 * Discard all the contents of current archive. This is achieved by
507 * invoking CREATE cmd on current archive.
515 new_target = strdup(target);
516 if (new_target == NULL)
517 bsdar_errc(bsdar, errno, "strdup failed");
518 arscp_create(NULL, new_target);
523 * Quit ar(1). Note that END cmd will not SAVE current archive
533 if (unlink(tmpac) == -1)
534 bsdar_errc(bsdar, errno, "unlink %s failed",
543 * Check if target specified, i.e, whether OPEN or CREATE has been
547 arscp_target_exist(void)
553 bsdar_warnc(bsdar, 0, "no open output archive");
557 /* Construct module list. */
559 arscp_mlist(struct list *list, char *str)
563 l = malloc(sizeof(*l));
565 bsdar_errc(bsdar, errno, "malloc failed");
572 /* Calculate the length of a mlist. */
574 arscp_mlist_len(struct list *list)
578 for(len = 0; list; list = list->next)
584 /* Free the space allocated for mod_list. */
586 arscp_free_mlist(struct list *list)
590 /* Note that list->str was freed in arscp_free_argv. */
591 for(; list; list = l) {
597 /* Convert mlist to argv array. */
599 arscp_mlist2argv(struct list *list)
604 n = arscp_mlist_len(list);
605 argv = malloc(n * sizeof(*argv));
607 bsdar_errc(bsdar, errno, "malloc failed");
609 /* Note that module names are stored in reverse order in mlist. */
610 for(i = n - 1; i >= 0; i--, list = list->next) {
612 bsdar_errc(bsdar, errno, "invalid mlist");
620 /* Free space allocated for argv array and its elements. */
622 arscp_free_argv(void)
626 for(i = 0; i < bsdar->argc; i++)
627 free(bsdar->argv[i]);
632 /* Show a prompt if we are in interactive mode */
643 /* Main function for ar script mode. */
645 ar_mode_script(struct bsdar *ar)
649 interactive = isatty(fileno(stdin));
652 arscp_end(EXIT_FAILURE);
655 /* Script ends without END */
656 arscp_end(EXIT_SUCCESS);