1 %{ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2008 Kai Wang 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/mman.h> 34 #include <sys/param.h> 35 #include <sys/queue.h> 36 #include <sys/stat.h> 37 #include <archive.h> 38 #include <archive_entry.h> 39 #include <dirent.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <sysexits.h> 46 #include <unistd.h> 47 48 #include "ar.h" 49 50 #define TEMPLATE "arscp.XXXXXXXX" 51 52 struct list { 53 char *str; 54 struct list *next; 55 }; 56 57 58 extern int yylex(void); 59 60 static void yyerror(const char *); 61 static void arscp_addlib(char *archive, struct list *list); 62 static void arscp_addmod(struct list *list); 63 static void arscp_clear(void); 64 static int arscp_copy(int ifd, int ofd); 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); 81 82 extern int lineno; 83 84 static struct bsdar *bsdar; 85 static char *target; 86 static char *tmpac; 87 static int interactive; 88 static int verbose; 89 90 %} 91 92 %token ADDLIB 93 %token ADDMOD 94 %token CLEAR 95 %token CREATE 96 %token DELETE 97 %token DIRECTORY 98 %token END 99 %token EXTRACT 100 %token LIST 101 %token OPEN 102 %token REPLACE 103 %token VERBOSE 104 %token SAVE 105 %token LP 106 %token RP 107 %token COMMA 108 %token EOL 109 %token <str> FNAME 110 %type <list> mod_list 111 112 %union { 113 char *str; 114 struct list *list; 115 } 116 117 %% 118 119 begin 120 : { arscp_prompt(); } ar_script 121 ; 122 123 ar_script 124 : cmd_list 125 | 126 ; 127 128 mod_list 129 : FNAME { $$ = arscp_mlist(NULL, $1); } 130 | mod_list separator FNAME { $$ = arscp_mlist($1, $3); } 131 ; 132 133 separator 134 : COMMA 135 | 136 ; 137 138 cmd_list 139 : rawcmd 140 | cmd_list rawcmd 141 ; 142 143 rawcmd 144 : cmd EOL { arscp_prompt(); } 145 ; 146 147 cmd 148 : addlib_cmd 149 | addmod_cmd 150 | clear_cmd 151 | create_cmd 152 | delete_cmd 153 | directory_cmd 154 | end_cmd 155 | extract_cmd 156 | list_cmd 157 | open_cmd 158 | replace_cmd 159 | verbose_cmd 160 | save_cmd 161 | invalid_cmd 162 | empty_cmd 163 | error 164 ; 165 166 addlib_cmd 167 : ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); } 168 | ADDLIB FNAME { arscp_addlib($2, NULL); } 169 ; 170 171 addmod_cmd 172 : ADDMOD mod_list { arscp_addmod($2); } 173 ; 174 175 clear_cmd 176 : CLEAR { arscp_clear(); } 177 ; 178 179 create_cmd 180 : CREATE FNAME { arscp_create(NULL, $2); } 181 ; 182 183 delete_cmd 184 : DELETE mod_list { arscp_delete($2); } 185 ; 186 187 directory_cmd 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); } 191 ; 192 193 end_cmd 194 : END { arscp_end(EX_OK); } 195 ; 196 197 extract_cmd 198 : EXTRACT mod_list { arscp_extract($2); } 199 ; 200 201 list_cmd 202 : LIST { arscp_list(); } 203 ; 204 205 open_cmd 206 : OPEN FNAME { arscp_open($2); } 207 ; 208 209 replace_cmd 210 : REPLACE mod_list { arscp_replace($2); } 211 ; 212 213 save_cmd 214 : SAVE { arscp_save(); } 215 ; 216 217 verbose_cmd 218 : VERBOSE { verbose = !verbose; } 219 ; 220 221 empty_cmd 222 : 223 ; 224 225 invalid_cmd 226 : FNAME { yyerror(NULL); } 227 ; 228 229 %% 230 231 /* ARGSUSED */ 232 static void 233 yyerror(const char *s) 234 { 235 236 (void) s; 237 printf("Syntax error in archive script, line %d\n", lineno); 238 } 239 240 /* 241 * arscp_open first open an archive and check its validity. If the archive 242 * format is valid, it calls arscp_create to create a temporary copy of 243 * the archive. 244 */ 245 static void 246 arscp_open(char *fname) 247 { 248 struct archive *a; 249 struct archive_entry *entry; 250 int r; 251 252 if ((a = archive_read_new()) == NULL) 253 bsdar_errc(bsdar, EX_SOFTWARE, 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 AC(archive_read_free(a)); 260 if (r != ARCHIVE_OK) 261 return; 262 arscp_create(fname, fname); 263 } 264 265 /* 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. 269 */ 270 static void 271 arscp_create(char *in, char *out) 272 { 273 struct archive *a; 274 int ifd, ofd; 275 276 /* Delete previously created temporary archive, if any. */ 277 if (tmpac) { 278 if (unlink(tmpac) < 0) 279 bsdar_errc(bsdar, EX_IOERR, errno, "unlink failed"); 280 free(tmpac); 281 } 282 283 tmpac = strdup(TEMPLATE); 284 if (tmpac == NULL) 285 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed"); 286 if ((ofd = mkstemp(tmpac)) < 0) 287 bsdar_errc(bsdar, EX_IOERR, errno, "mkstemp failed"); 288 289 if (in) { 290 /* 291 * Command OPEN creates a temporary copy of the 292 * input archive. 293 */ 294 if ((ifd = open(in, O_RDONLY)) < 0) { 295 bsdar_warnc(bsdar, errno, "open failed"); 296 return; 297 } 298 if (arscp_copy(ifd, ofd)) { 299 bsdar_warnc(bsdar, 0, "arscp_copy failed"); 300 return; 301 } 302 close(ifd); 303 close(ofd); 304 } else { 305 /* 306 * Command CREATE creates an "empty" archive. 307 * (archive with only global header) 308 */ 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_free(a)); 316 } 317 318 /* Override previous target, if any. */ 319 if (target) 320 free(target); 321 322 target = out; 323 bsdar->filename = tmpac; 324 } 325 326 /* A file copying implementation using mmap. */ 327 static int 328 arscp_copy(int ifd, int ofd) 329 { 330 struct stat sb; 331 char *buf, *p; 332 ssize_t w; 333 size_t bytes; 334 335 if (fstat(ifd, &sb) < 0) { 336 bsdar_warnc(bsdar, errno, "fstate failed"); 337 return (1); 338 } 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"); 342 return (1); 343 } 344 for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) { 345 w = write(ofd, buf, bytes); 346 if (w <= 0) { 347 bsdar_warnc(bsdar, errno, "write failed"); 348 break; 349 } 350 } 351 if (munmap(p, sb.st_size) < 0) 352 bsdar_errc(bsdar, EX_SOFTWARE, errno, "munmap failed"); 353 if (bytes > 0) 354 return (1); 355 356 return (0); 357 } 358 359 /* 360 * Add all modules of archive to current archive, if list != NULL, 361 * only those modules specified in 'list' will be added. 362 */ 363 static void 364 arscp_addlib(char *archive, struct list *list) 365 { 366 367 if (!arscp_target_exist()) 368 return; 369 arscp_mlist2argv(list); 370 bsdar->addlib = archive; 371 ar_mode_A(bsdar); 372 arscp_free_argv(); 373 arscp_free_mlist(list); 374 } 375 376 /* Add modules into current archive. */ 377 static void 378 arscp_addmod(struct list *list) 379 { 380 381 if (!arscp_target_exist()) 382 return; 383 arscp_mlist2argv(list); 384 ar_mode_q(bsdar); 385 arscp_free_argv(); 386 arscp_free_mlist(list); 387 } 388 389 /* Delete modules from current archive. */ 390 static void 391 arscp_delete(struct list *list) 392 { 393 394 if (!arscp_target_exist()) 395 return; 396 arscp_mlist2argv(list); 397 ar_mode_d(bsdar); 398 arscp_free_argv(); 399 arscp_free_mlist(list); 400 } 401 402 /* Extract modules from current archive. */ 403 static void 404 arscp_extract(struct list *list) 405 { 406 407 if (!arscp_target_exist()) 408 return; 409 arscp_mlist2argv(list); 410 ar_mode_x(bsdar); 411 arscp_free_argv(); 412 arscp_free_mlist(list); 413 } 414 415 /* List modules of archive. (Simple Mode) */ 416 static void 417 arscp_list(void) 418 { 419 420 if (!arscp_target_exist()) 421 return; 422 bsdar->argc = 0; 423 bsdar->argv = NULL; 424 /* Always verbose. */ 425 bsdar->options |= AR_V; 426 ar_mode_t(bsdar); 427 bsdar->options &= ~AR_V; 428 } 429 430 /* List modules of archive. (Advance Mode) */ 431 static void 432 arscp_dir(char *archive, struct list *list, char *rlt) 433 { 434 FILE *out; 435 436 /* If rlt != NULL, redirect output to it */ 437 out = NULL; 438 if (rlt) { 439 out = stdout; 440 if ((stdout = fopen(rlt, "w")) == NULL) 441 bsdar_errc(bsdar, EX_IOERR, errno, 442 "fopen %s failed", rlt); 443 } 444 445 bsdar->filename = archive; 446 if (list) 447 arscp_mlist2argv(list); 448 else { 449 bsdar->argc = 0; 450 bsdar->argv = NULL; 451 } 452 if (verbose) 453 bsdar->options |= AR_V; 454 ar_mode_t(bsdar); 455 bsdar->options &= ~AR_V; 456 457 if (rlt) { 458 if (fclose(stdout) == EOF) 459 bsdar_errc(bsdar, EX_IOERR, errno, 460 "fclose %s failed", rlt); 461 stdout = out; 462 free(rlt); 463 } 464 free(archive); 465 bsdar->filename = tmpac; 466 arscp_free_argv(); 467 arscp_free_mlist(list); 468 } 469 470 471 /* Replace modules of current archive. */ 472 static void 473 arscp_replace(struct list *list) 474 { 475 476 if (!arscp_target_exist()) 477 return; 478 arscp_mlist2argv(list); 479 ar_mode_r(bsdar); 480 arscp_free_argv(); 481 arscp_free_mlist(list); 482 } 483 484 /* Rename the temporary archive to the target archive. */ 485 static void 486 arscp_save(void) 487 { 488 mode_t mask; 489 490 if (target) { 491 if (rename(tmpac, target) < 0) 492 bsdar_errc(bsdar, EX_IOERR, errno, "rename failed"); 493 /* 494 * mkstemp creates temp files with mode 0600, here we 495 * set target archive mode per process umask. 496 */ 497 mask = umask(0); 498 umask(mask); 499 if (chmod(target, 0666 & ~mask) < 0) 500 bsdar_errc(bsdar, EX_IOERR, errno, "chmod failed"); 501 free(tmpac); 502 free(target); 503 tmpac = NULL; 504 target= NULL; 505 bsdar->filename = NULL; 506 } else 507 bsdar_warnc(bsdar, 0, "no open output archive"); 508 } 509 510 /* 511 * Discard all the contents of current archive. This is achieved by 512 * invoking CREATE cmd on current archive. 513 */ 514 static void 515 arscp_clear(void) 516 { 517 char *new_target; 518 519 if (target) { 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); 524 } 525 } 526 527 /* 528 * Quit ar(1). Note that END cmd will not SAVE current archive 529 * before exit. 530 */ 531 static void 532 arscp_end(int eval) 533 { 534 535 if (target) 536 free(target); 537 if (tmpac) { 538 if (unlink(tmpac) == -1) 539 bsdar_errc(bsdar, EX_IOERR, errno, "unlink %s failed", 540 tmpac); 541 free(tmpac); 542 } 543 544 exit(eval); 545 } 546 547 /* 548 * Check if target specified, i.e, whether OPEN or CREATE has been 549 * issued by user. 550 */ 551 static int 552 arscp_target_exist(void) 553 { 554 555 if (target) 556 return (1); 557 558 bsdar_warnc(bsdar, 0, "no open output archive"); 559 return (0); 560 } 561 562 /* Construct module list. */ 563 static struct list * 564 arscp_mlist(struct list *list, char *str) 565 { 566 struct list *l; 567 568 l = malloc(sizeof(*l)); 569 if (l == NULL) 570 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 571 l->str = str; 572 l->next = list; 573 574 return (l); 575 } 576 577 /* Calculate the length of a mlist. */ 578 static int 579 arscp_mlist_len(struct list *list) 580 { 581 int len; 582 583 for(len = 0; list; list = list->next) 584 len++; 585 586 return (len); 587 } 588 589 /* Free the space allocated for mod_list. */ 590 static void 591 arscp_free_mlist(struct list *list) 592 { 593 struct list *l; 594 595 /* Note that list->str was freed in arscp_free_argv. */ 596 for(; list; list = l) { 597 l = list->next; 598 free(list); 599 } 600 } 601 602 /* Convert mlist to argv array. */ 603 static void 604 arscp_mlist2argv(struct list *list) 605 { 606 char **argv; 607 int i, n; 608 609 n = arscp_mlist_len(list); 610 argv = malloc(n * sizeof(*argv)); 611 if (argv == NULL) 612 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); 613 614 /* Note that module names are stored in reverse order in mlist. */ 615 for(i = n - 1; i >= 0; i--, list = list->next) { 616 if (list == NULL) 617 bsdar_errc(bsdar, EX_SOFTWARE, errno, "invalid mlist"); 618 argv[i] = list->str; 619 } 620 621 bsdar->argc = n; 622 bsdar->argv = argv; 623 } 624 625 /* Free space allocated for argv array and its elements. */ 626 static void 627 arscp_free_argv(void) 628 { 629 int i; 630 631 for(i = 0; i < bsdar->argc; i++) 632 free(bsdar->argv[i]); 633 634 free(bsdar->argv); 635 } 636 637 /* Show a prompt if we are in interactive mode */ 638 static void 639 arscp_prompt(void) 640 { 641 642 if (interactive) { 643 printf("AR >"); 644 fflush(stdout); 645 } 646 } 647 648 /* Main function for ar script mode. */ 649 void 650 ar_mode_script(struct bsdar *ar) 651 { 652 653 bsdar = ar; 654 interactive = isatty(fileno(stdin)); 655 while(yyparse()) { 656 if (!interactive) 657 arscp_end(1); 658 } 659 660 /* Script ends without END */ 661 arscp_end(EX_OK); 662 } 663