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