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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
36 #include <archive_entry.h>
48 #define TEMPLATE "arscp.XXXXXXXX"
56 extern int yylex(void);
57 extern int yyparse(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(EX_OK); }
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, EX_SOFTWARE, 0, "archive_read_new failed");
253 archive_read_support_compression_none(a);
254 archive_read_support_format_ar(a);
255 AC(archive_read_open_file(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 AC(archive_read_finish(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, EX_IOERR, errno, "unlink failed");
283 tmpac = strdup(TEMPLATE);
285 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
286 if ((ofd = mkstemp(tmpac)) < 0)
287 bsdar_errc(bsdar, EX_IOERR, 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, EX_SOFTWARE, 0,
311 "archive_write_new failed");
312 archive_write_set_format_ar_svr4(a);
313 AC(archive_write_open_fd(a, ofd));
314 AC(archive_write_close(a));
315 AC(archive_write_finish(a));
318 /* Override previous target, if any. */
323 bsdar->filename = tmpac;
326 /* A file copying implementation using mmap. */
328 arscp_copy(int ifd, int ofd)
335 if (fstat(ifd, &sb) < 0) {
336 bsdar_warnc(bsdar, errno, "fstate failed");
339 if ((p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd,
340 (off_t)0)) == MAP_FAILED) {
341 bsdar_warnc(bsdar, errno, "mmap failed");
344 for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) {
345 w = write(ofd, buf, bytes);
347 bsdar_warnc(bsdar, errno, "write failed");
351 if (munmap(p, sb.st_size) < 0)
352 bsdar_errc(bsdar, EX_SOFTWARE, errno, "munmap failed");
360 * Add all modules of archive to current archive, if list != NULL,
361 * only those modules specified in 'list' will be added.
364 arscp_addlib(char *archive, struct list *list)
367 if (!arscp_target_exist())
369 arscp_mlist2argv(list);
370 bsdar->addlib = archive;
373 arscp_free_mlist(list);
376 /* Add modules into current archive. */
378 arscp_addmod(struct list *list)
381 if (!arscp_target_exist())
383 arscp_mlist2argv(list);
386 arscp_free_mlist(list);
389 /* Delete modules from current archive. */
391 arscp_delete(struct list *list)
394 if (!arscp_target_exist())
396 arscp_mlist2argv(list);
399 arscp_free_mlist(list);
402 /* Extract modules from current archive. */
404 arscp_extract(struct list *list)
407 if (!arscp_target_exist())
409 arscp_mlist2argv(list);
412 arscp_free_mlist(list);
415 /* List modules of archive. (Simple Mode) */
420 if (!arscp_target_exist())
424 /* Always verbose. */
425 bsdar->options |= AR_V;
427 bsdar->options &= ~AR_V;
430 /* List modules of archive. (Advance Mode) */
432 arscp_dir(char *archive, struct list *list, char *rlt)
436 /* If rlt != NULL, redirect output to it */
440 if ((stdout = fopen(rlt, "w")) == NULL)
441 bsdar_errc(bsdar, EX_IOERR, errno,
442 "fopen %s failed", rlt);
445 bsdar->filename = archive;
447 arscp_mlist2argv(list);
453 bsdar->options |= AR_V;
455 bsdar->options &= ~AR_V;
458 if (fclose(stdout) == EOF)
459 bsdar_errc(bsdar, EX_IOERR, errno,
460 "fclose %s failed", rlt);
465 bsdar->filename = tmpac;
467 arscp_free_mlist(list);
471 /* Replace modules of current archive. */
473 arscp_replace(struct list *list)
476 if (!arscp_target_exist())
478 arscp_mlist2argv(list);
481 arscp_free_mlist(list);
484 /* Rename the temporary archive to the target archive. */
491 if (rename(tmpac, target) < 0)
492 bsdar_errc(bsdar, EX_IOERR, errno, "rename failed");
494 * mkstemp creates temp files with mode 0600, here we
495 * set target archive mode per process umask.
499 if (chmod(target, 0666 & ~mask) < 0)
500 bsdar_errc(bsdar, EX_IOERR, errno, "chmod failed");
505 bsdar->filename = NULL;
507 bsdar_warnc(bsdar, 0, "no open output archive");
511 * Discard all the contents of current archive. This is achieved by
512 * invoking CREATE cmd on current archive.
520 new_target = strdup(target);
521 if (new_target == NULL)
522 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
523 arscp_create(NULL, new_target);
528 * Quit ar(1). Note that END cmd will not SAVE current archive
538 if (unlink(tmpac) == -1)
539 bsdar_errc(bsdar, EX_IOERR, errno, "unlink %s failed",
548 * Check if target specified, i.e, whether OPEN or CREATE has been
558 bsdar_warnc(bsdar, 0, "no open output archive");
562 /* Construct module list. */
564 arscp_mlist(struct list *list, char *str)
568 l = malloc(sizeof(*l));
570 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
577 /* Calculate the length of a mlist. */
579 arscp_mlist_len(struct list *list)
583 for(len = 0; list; list = list->next)
589 /* Free the space allocated for mod_list. */
591 arscp_free_mlist(struct list *list)
595 /* Note that list->str was freed in arscp_free_argv. */
596 for(; list; list = l) {
602 /* Convert mlist to argv array. */
604 arscp_mlist2argv(struct list *list)
609 n = arscp_mlist_len(list);
610 argv = malloc(n * sizeof(*argv));
612 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
614 /* Note that module names are stored in reverse order in mlist. */
615 for(i = n - 1; i >= 0; i--, list = list->next) {
617 bsdar_errc(bsdar, EX_SOFTWARE, errno, "invalid mlist");
625 /* Free space allocated for argv array and its elements. */
631 for(i = 0; i < bsdar->argc; i++)
632 free(bsdar->argv[i]);
637 /* Show a prompt if we are in interactive mode */
648 /* Main function for ar script mode. */
650 ar_mode_script(struct bsdar *ar)
654 interactive = isatty(fileno(stdin));
660 /* Script ends without END */