3 * SPDX-License-Identifier: BSD-2-Clause
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>
32 #include <sys/param.h>
33 #include <sys/queue.h>
36 #include <archive_entry.h>
47 #define TEMPLATE "arscp.XXXXXXXX"
55 extern int yylex(void);
57 static void yyerror(const char *);
58 static void arscp_addlib(char *archive, struct list *list);
59 static void arscp_addmod(struct list *list);
60 static void arscp_clear(void);
61 static int arscp_copy(int ifd, int ofd);
62 static void arscp_create(char *in, char *out);
63 static void arscp_delete(struct list *list);
64 static void arscp_dir(char *archive, struct list *list, char *rlt);
65 static void arscp_end(int eval);
66 static void arscp_extract(struct list *list);
67 static void arscp_free_argv(void);
68 static void arscp_free_mlist(struct list *list);
69 static void arscp_list(void);
70 static struct list *arscp_mlist(struct list *list, char *str);
71 static void arscp_mlist2argv(struct list *list);
72 static int arscp_mlist_len(struct list *list);
73 static void arscp_open(char *fname);
74 static void arscp_prompt(void);
75 static void arscp_replace(struct list *list);
76 static void arscp_save(void);
77 static int arscp_target_exist(void);
81 static struct bsdar *bsdar;
84 static int interactive;
107 %type <list> mod_list
117 : { arscp_prompt(); } ar_script
126 : FNAME { $$ = arscp_mlist(NULL, $1); }
127 | mod_list separator FNAME { $$ = arscp_mlist($1, $3); }
141 : cmd EOL { arscp_prompt(); }
164 : ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); }
165 | ADDLIB FNAME { arscp_addlib($2, NULL); }
169 : ADDMOD mod_list { arscp_addmod($2); }
173 : CLEAR { arscp_clear(); }
177 : CREATE FNAME { arscp_create(NULL, $2); }
181 : DELETE mod_list { arscp_delete($2); }
185 : DIRECTORY FNAME { arscp_dir($2, NULL, NULL); }
186 | DIRECTORY FNAME LP mod_list RP { arscp_dir($2, $4, NULL); }
187 | DIRECTORY FNAME LP mod_list RP FNAME { arscp_dir($2, $4, $6); }
191 : END { arscp_end(EXIT_SUCCESS); }
195 : EXTRACT mod_list { arscp_extract($2); }
199 : LIST { arscp_list(); }
203 : OPEN FNAME { arscp_open($2); }
207 : REPLACE mod_list { arscp_replace($2); }
211 : SAVE { arscp_save(); }
215 : VERBOSE { verbose = !verbose; }
223 : FNAME { yyerror(NULL); }
230 yyerror(const char *s)
234 printf("Syntax error in archive script, line %d\n", lineno);
238 * arscp_open first open an archive and check its validity. If the archive
239 * format is valid, it calls arscp_create to create a temporary copy of
243 arscp_open(char *fname)
246 struct archive_entry *entry;
249 if ((a = archive_read_new()) == NULL)
250 bsdar_errc(bsdar, 0, "archive_read_new failed");
251 archive_read_support_format_ar(a);
252 AC(archive_read_open_filename(a, fname, DEF_BLKSZ));
253 if ((r = archive_read_next_header(a, &entry)))
254 bsdar_warnc(bsdar, archive_errno(a), "%s",
255 archive_error_string(a));
256 AC(archive_read_close(a));
257 AC(archive_read_free(a));
260 arscp_create(fname, fname);
264 * Create archive. in != NULL indicate it's a OPEN cmd, and resulting
265 * archive is based on modification of an existing one. If in == NULL,
266 * we are in CREATE cmd and a new empty archive will be created.
269 arscp_create(char *in, char *out)
274 /* Delete previously created temporary archive, if any. */
276 if (unlink(tmpac) < 0)
277 bsdar_errc(bsdar, errno, "unlink failed");
281 tmpac = strdup(TEMPLATE);
283 bsdar_errc(bsdar, errno, "strdup failed");
284 if ((ofd = mkstemp(tmpac)) < 0)
285 bsdar_errc(bsdar, errno, "mkstemp failed");
289 * Command OPEN creates a temporary copy of the
292 if ((ifd = open(in, O_RDONLY)) < 0) {
293 bsdar_warnc(bsdar, errno, "open failed");
296 if (arscp_copy(ifd, ofd)) {
297 bsdar_warnc(bsdar, 0, "arscp_copy failed");
304 * Command CREATE creates an "empty" archive.
305 * (archive with only global header)
307 if ((a = archive_write_new()) == NULL)
308 bsdar_errc(bsdar, 0, "archive_write_new failed");
309 archive_write_set_format_ar_svr4(a);
310 AC(archive_write_open_fd(a, ofd));
311 AC(archive_write_close(a));
312 AC(archive_write_free(a));
315 /* Override previous target, if any. */
320 bsdar->filename = tmpac;
323 /* A file copying implementation using mmap. */
325 arscp_copy(int ifd, int ofd)
332 if (fstat(ifd, &sb) < 0) {
333 bsdar_warnc(bsdar, errno, "fstate failed");
336 if ((p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd,
337 (off_t)0)) == MAP_FAILED) {
338 bsdar_warnc(bsdar, errno, "mmap failed");
341 for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) {
342 w = write(ofd, buf, bytes);
344 bsdar_warnc(bsdar, errno, "write failed");
348 if (munmap(p, sb.st_size) < 0)
349 bsdar_errc(bsdar, errno, "munmap failed");
357 * Add all modules of archive to current archive, if list != NULL,
358 * only those modules specified in 'list' will be added.
361 arscp_addlib(char *archive, struct list *list)
364 if (!arscp_target_exist())
366 arscp_mlist2argv(list);
367 bsdar->addlib = archive;
368 ar_write_archive(bsdar, 'A');
370 arscp_free_mlist(list);
373 /* Add modules into current archive. */
375 arscp_addmod(struct list *list)
378 if (!arscp_target_exist())
380 arscp_mlist2argv(list);
381 ar_write_archive(bsdar, 'q');
383 arscp_free_mlist(list);
386 /* Delete modules from current archive. */
388 arscp_delete(struct list *list)
391 if (!arscp_target_exist())
393 arscp_mlist2argv(list);
394 ar_write_archive(bsdar, 'd');
396 arscp_free_mlist(list);
399 /* Extract modules from current archive. */
401 arscp_extract(struct list *list)
404 if (!arscp_target_exist())
406 arscp_mlist2argv(list);
407 ar_read_archive(bsdar, 'x', stdout);
409 arscp_free_mlist(list);
412 /* List modules of archive. (Simple Mode) */
417 if (!arscp_target_exist())
421 /* Always verbose. */
422 bsdar->options |= AR_V;
423 ar_read_archive(bsdar, 't', stdout);
424 bsdar->options &= ~AR_V;
427 /* List modules of archive. (Advance Mode) */
429 arscp_dir(char *archive, struct list *list, char *rlt)
433 /* If rlt != NULL, redirect output to it */
436 if ((out = fopen(rlt, "w")) == NULL)
437 bsdar_errc(bsdar, errno, "fopen %s failed", rlt);
440 bsdar->filename = archive;
442 arscp_mlist2argv(list);
448 bsdar->options |= AR_V;
449 ar_read_archive(bsdar, 't', out);
450 bsdar->options &= ~AR_V;
453 if (fclose(out) == EOF)
454 bsdar_errc(bsdar, errno, "fclose %s failed", rlt);
458 bsdar->filename = tmpac;
460 arscp_free_mlist(list);
464 /* Replace modules of current archive. */
466 arscp_replace(struct list *list)
469 if (!arscp_target_exist())
471 arscp_mlist2argv(list);
472 ar_write_archive(bsdar, 'r');
474 arscp_free_mlist(list);
477 /* Rename the temporary archive to the target archive. */
484 if (rename(tmpac, target) < 0)
485 bsdar_errc(bsdar, errno, "rename failed");
487 * mkstemp creates temp files with mode 0600, here we
488 * set target archive mode per process umask.
492 if (chmod(target, 0666 & ~mask) < 0)
493 bsdar_errc(bsdar, errno, "chmod failed");
498 bsdar->filename = NULL;
500 bsdar_warnc(bsdar, 0, "no open output archive");
504 * Discard all the contents of current archive. This is achieved by
505 * invoking CREATE cmd on current archive.
513 new_target = strdup(target);
514 if (new_target == NULL)
515 bsdar_errc(bsdar, errno, "strdup failed");
516 arscp_create(NULL, new_target);
521 * Quit ar(1). Note that END cmd will not SAVE current archive
531 if (unlink(tmpac) == -1)
532 bsdar_errc(bsdar, errno, "unlink %s failed",
541 * Check if target specified, i.e, whether OPEN or CREATE has been
545 arscp_target_exist(void)
551 bsdar_warnc(bsdar, 0, "no open output archive");
555 /* Construct module list. */
557 arscp_mlist(struct list *list, char *str)
561 l = malloc(sizeof(*l));
563 bsdar_errc(bsdar, errno, "malloc failed");
570 /* Calculate the length of a mlist. */
572 arscp_mlist_len(struct list *list)
576 for(len = 0; list; list = list->next)
582 /* Free the space allocated for mod_list. */
584 arscp_free_mlist(struct list *list)
588 /* Note that list->str was freed in arscp_free_argv. */
589 for(; list; list = l) {
595 /* Convert mlist to argv array. */
597 arscp_mlist2argv(struct list *list)
602 n = arscp_mlist_len(list);
603 argv = malloc(n * sizeof(*argv));
605 bsdar_errc(bsdar, errno, "malloc failed");
607 /* Note that module names are stored in reverse order in mlist. */
608 for(i = n - 1; i >= 0; i--, list = list->next) {
610 bsdar_errc(bsdar, errno, "invalid mlist");
618 /* Free space allocated for argv array and its elements. */
620 arscp_free_argv(void)
624 for(i = 0; i < bsdar->argc; i++)
625 free(bsdar->argv[i]);
630 /* Show a prompt if we are in interactive mode */
641 /* Main function for ar script mode. */
643 ar_mode_script(struct bsdar *ar)
647 interactive = isatty(fileno(stdin));
650 arscp_end(EXIT_FAILURE);
653 /* Script ends without END */
654 arscp_end(EXIT_SUCCESS);