xref: /freebsd/usr.bin/ar/acpyacc.y (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
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');
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');
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 = NULL;
437 	if (rlt) {
438 		out = stdout;
439 		if ((stdout = fopen(rlt, "w")) == NULL)
440 			bsdar_errc(bsdar, errno, "fopen %s failed", rlt);
441 	}
442 
443 	bsdar->filename = archive;
444 	if (list)
445 		arscp_mlist2argv(list);
446 	else {
447 		bsdar->argc = 0;
448 		bsdar->argv = NULL;
449 	}
450 	if (verbose)
451 		bsdar->options |= AR_V;
452 	ar_read_archive(bsdar, 't');
453 	bsdar->options &= ~AR_V;
454 
455 	if (rlt) {
456 		if (fclose(stdout) == EOF)
457 			bsdar_errc(bsdar, errno, "fclose %s failed", rlt);
458 		stdout = out;
459 		free(rlt);
460 	}
461 	free(archive);
462 	bsdar->filename = tmpac;
463 	arscp_free_argv();
464 	arscp_free_mlist(list);
465 }
466 
467 
468 /* Replace modules of current archive. */
469 static void
470 arscp_replace(struct list *list)
471 {
472 
473 	if (!arscp_target_exist())
474 		return;
475 	arscp_mlist2argv(list);
476 	ar_write_archive(bsdar, 'r');
477 	arscp_free_argv();
478 	arscp_free_mlist(list);
479 }
480 
481 /* Rename the temporary archive to the target archive. */
482 static void
483 arscp_save(void)
484 {
485 	mode_t mask;
486 
487 	if (target) {
488 		if (rename(tmpac, target) < 0)
489 			bsdar_errc(bsdar, errno, "rename failed");
490 		/*
491 		 * mkstemp creates temp files with mode 0600, here we
492 		 * set target archive mode per process umask.
493 		 */
494 		mask = umask(0);
495 		umask(mask);
496 		if (chmod(target, 0666 & ~mask) < 0)
497 			bsdar_errc(bsdar, errno, "chmod failed");
498 		free(tmpac);
499 		free(target);
500 		tmpac = NULL;
501 		target= NULL;
502 		bsdar->filename = NULL;
503 	} else
504 		bsdar_warnc(bsdar, 0, "no open output archive");
505 }
506 
507 /*
508  * Discard all the contents of current archive. This is achieved by
509  * invoking CREATE cmd on current archive.
510  */
511 static void
512 arscp_clear(void)
513 {
514 	char		*new_target;
515 
516 	if (target) {
517 		new_target = strdup(target);
518 		if (new_target == NULL)
519 			bsdar_errc(bsdar, errno, "strdup failed");
520 		arscp_create(NULL, new_target);
521 	}
522 }
523 
524 /*
525  * Quit ar(1). Note that END cmd will not SAVE current archive
526  * before exit.
527  */
528 static void
529 arscp_end(int eval)
530 {
531 
532 	if (target)
533 		free(target);
534 	if (tmpac) {
535 		if (unlink(tmpac) == -1)
536 			bsdar_errc(bsdar, errno, "unlink %s failed",
537 			    tmpac);
538 		free(tmpac);
539 	}
540 
541 	exit(eval);
542 }
543 
544 /*
545  * Check if target specified, i.e, whether OPEN or CREATE has been
546  * issued by user.
547  */
548 static int
549 arscp_target_exist(void)
550 {
551 
552 	if (target)
553 		return (1);
554 
555 	bsdar_warnc(bsdar, 0, "no open output archive");
556 	return (0);
557 }
558 
559 /* Construct module list. */
560 static struct list *
561 arscp_mlist(struct list *list, char *str)
562 {
563 	struct list *l;
564 
565 	l = malloc(sizeof(*l));
566 	if (l == NULL)
567 		bsdar_errc(bsdar, errno, "malloc failed");
568 	l->str = str;
569 	l->next = list;
570 
571 	return (l);
572 }
573 
574 /* Calculate the length of a mlist. */
575 static int
576 arscp_mlist_len(struct list *list)
577 {
578 	int len;
579 
580 	for(len = 0; list; list = list->next)
581 		len++;
582 
583 	return (len);
584 }
585 
586 /* Free the space allocated for mod_list. */
587 static void
588 arscp_free_mlist(struct list *list)
589 {
590 	struct list *l;
591 
592 	/* Note that list->str was freed in arscp_free_argv. */
593 	for(; list; list = l) {
594 		l = list->next;
595 		free(list);
596 	}
597 }
598 
599 /* Convert mlist to argv array. */
600 static void
601 arscp_mlist2argv(struct list *list)
602 {
603 	char	**argv;
604 	int	  i, n;
605 
606 	n = arscp_mlist_len(list);
607 	argv = malloc(n * sizeof(*argv));
608 	if (argv == NULL)
609 		bsdar_errc(bsdar, errno, "malloc failed");
610 
611 	/* Note that module names are stored in reverse order in mlist. */
612 	for(i = n - 1; i >= 0; i--, list = list->next) {
613 		if (list == NULL)
614 			bsdar_errc(bsdar, errno, "invalid mlist");
615 		argv[i] = list->str;
616 	}
617 
618 	bsdar->argc = n;
619 	bsdar->argv = argv;
620 }
621 
622 /* Free space allocated for argv array and its elements. */
623 static void
624 arscp_free_argv(void)
625 {
626 	int i;
627 
628 	for(i = 0; i < bsdar->argc; i++)
629 		free(bsdar->argv[i]);
630 
631 	free(bsdar->argv);
632 }
633 
634 /* Show a prompt if we are in interactive mode */
635 static void
636 arscp_prompt(void)
637 {
638 
639 	if (interactive) {
640 		printf("AR >");
641 		fflush(stdout);
642 	}
643 }
644 
645 /* Main function for ar script mode. */
646 void
647 ar_mode_script(struct bsdar *ar)
648 {
649 
650 	bsdar = ar;
651 	interactive = isatty(fileno(stdin));
652 	while(yyparse()) {
653 		if (!interactive)
654 			arscp_end(EXIT_FAILURE);
655 	}
656 
657 	/* Script ends without END */
658 	arscp_end(EXIT_SUCCESS);
659 }
660