xref: /freebsd/usr.bin/ar/acpyacc.y (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 %{
2 /*-
3  * Copyright (c) 2008 Kai Wang
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *
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.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/mman.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/stat.h>
35 #include <archive.h>
36 #include <archive_entry.h>
37 #include <dirent.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sysexits.h>
44 #include <unistd.h>
45 
46 #include "ar.h"
47 
48 #define TEMPLATE "arscp.XXXXXXXX"
49 
50 struct list {
51 	char		*str;
52 	struct list	*next;
53 };
54 
55 
56 extern int	yylex(void);
57 
58 static void	yyerror(const char *);
59 static void	arscp_addlib(char *archive, struct list *list);
60 static void	arscp_addmod(struct list *list);
61 static void	arscp_clear(void);
62 static int	arscp_copy(int ifd, int ofd);
63 static void	arscp_create(char *in, char *out);
64 static void	arscp_delete(struct list *list);
65 static void	arscp_dir(char *archive, struct list *list, char *rlt);
66 static void	arscp_end(int eval);
67 static void	arscp_extract(struct list *list);
68 static void	arscp_free_argv(void);
69 static void	arscp_free_mlist(struct list *list);
70 static void	arscp_list(void);
71 static struct list *arscp_mlist(struct list *list, char *str);
72 static void	arscp_mlist2argv(struct list *list);
73 static int	arscp_mlist_len(struct list *list);
74 static void	arscp_open(char *fname);
75 static void	arscp_prompt(void);
76 static void	arscp_replace(struct list *list);
77 static void	arscp_save(void);
78 static int	arscp_target_exist(void);
79 
80 extern int		 lineno;
81 
82 static struct bsdar	*bsdar;
83 static char		*target;
84 static char		*tmpac;
85 static int		 interactive;
86 static int		 verbose;
87 
88 %}
89 
90 %token ADDLIB
91 %token ADDMOD
92 %token CLEAR
93 %token CREATE
94 %token DELETE
95 %token DIRECTORY
96 %token END
97 %token EXTRACT
98 %token LIST
99 %token OPEN
100 %token REPLACE
101 %token VERBOSE
102 %token SAVE
103 %token LP
104 %token RP
105 %token COMMA
106 %token EOL
107 %token <str> FNAME
108 %type <list> mod_list
109 
110 %union {
111 	char		*str;
112 	struct list	*list;
113 }
114 
115 %%
116 
117 begin
118 	: { arscp_prompt(); } ar_script
119 	;
120 
121 ar_script
122 	: cmd_list
123 	|
124 	;
125 
126 mod_list
127 	: FNAME { $$ = arscp_mlist(NULL, $1); }
128 	| mod_list separator FNAME { $$ = arscp_mlist($1, $3); }
129 	;
130 
131 separator
132 	: COMMA
133 	|
134 	;
135 
136 cmd_list
137 	: rawcmd
138 	| cmd_list rawcmd
139 	;
140 
141 rawcmd
142 	: cmd EOL { arscp_prompt(); }
143 	;
144 
145 cmd
146 	: addlib_cmd
147 	| addmod_cmd
148 	| clear_cmd
149 	| create_cmd
150 	| delete_cmd
151 	| directory_cmd
152 	| end_cmd
153 	| extract_cmd
154 	| list_cmd
155 	| open_cmd
156 	| replace_cmd
157 	| verbose_cmd
158 	| save_cmd
159 	| invalid_cmd
160 	| empty_cmd
161 	| error
162 	;
163 
164 addlib_cmd
165 	: ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); }
166 	| ADDLIB FNAME { arscp_addlib($2, NULL); }
167 	;
168 
169 addmod_cmd
170 	: ADDMOD mod_list { arscp_addmod($2); }
171 	;
172 
173 clear_cmd
174 	: CLEAR { arscp_clear(); }
175 	;
176 
177 create_cmd
178 	: CREATE FNAME { arscp_create(NULL, $2); }
179 	;
180 
181 delete_cmd
182 	: DELETE mod_list { arscp_delete($2); }
183 	;
184 
185 directory_cmd
186 	: DIRECTORY FNAME { arscp_dir($2, NULL, NULL); }
187 	| DIRECTORY FNAME LP mod_list RP { arscp_dir($2, $4, NULL); }
188 	| DIRECTORY FNAME LP mod_list RP FNAME { arscp_dir($2, $4, $6); }
189 	;
190 
191 end_cmd
192 	: END { arscp_end(EX_OK); }
193 	;
194 
195 extract_cmd
196 	: EXTRACT mod_list { arscp_extract($2); }
197 	;
198 
199 list_cmd
200 	: LIST { arscp_list(); }
201 	;
202 
203 open_cmd
204 	: OPEN FNAME { arscp_open($2); }
205 	;
206 
207 replace_cmd
208 	: REPLACE mod_list { arscp_replace($2); }
209 	;
210 
211 save_cmd
212 	: SAVE { arscp_save(); }
213 	;
214 
215 verbose_cmd
216 	: VERBOSE { verbose = !verbose; }
217 	;
218 
219 empty_cmd
220 	:
221 	;
222 
223 invalid_cmd
224 	: FNAME { yyerror(NULL); }
225 	;
226 
227 %%
228 
229 /* ARGSUSED */
230 static void
231 yyerror(const char *s)
232 {
233 
234 	(void) s;
235 	printf("Syntax error in archive script, line %d\n", lineno);
236 }
237 
238 /*
239  * arscp_open first open an archive and check its validity. If the archive
240  * format is valid, it calls arscp_create to create a temporary copy of
241  * the archive.
242  */
243 static void
244 arscp_open(char *fname)
245 {
246 	struct archive		*a;
247 	struct archive_entry	*entry;
248 	int			 r;
249 
250 	if ((a = archive_read_new()) == NULL)
251 		bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
252 	archive_read_support_compression_none(a);
253 	archive_read_support_format_ar(a);
254 	AC(archive_read_open_file(a, fname, DEF_BLKSZ));
255 	if ((r = archive_read_next_header(a, &entry)))
256 		bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
257 	AC(archive_read_close(a));
258 	AC(archive_read_finish(a));
259 	if (r != ARCHIVE_OK)
260 		return;
261 	arscp_create(fname, fname);
262 }
263 
264 /*
265  * Create archive. in != NULL indicate it's a OPEN cmd, and resulting
266  * archive is based on modification of an existing one. If in == NULL,
267  * we are in CREATE cmd and a new empty archive will be created.
268  */
269 static void
270 arscp_create(char *in, char *out)
271 {
272 	struct archive		*a;
273 	int			 ifd, ofd;
274 
275 	/* Delete previously created temporary archive, if any. */
276 	if (tmpac) {
277 		if (unlink(tmpac) < 0)
278 			bsdar_errc(bsdar, EX_IOERR, errno, "unlink failed");
279 		free(tmpac);
280 	}
281 
282 	tmpac = strdup(TEMPLATE);
283 	if (tmpac == NULL)
284 		bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
285 	if ((ofd = mkstemp(tmpac)) < 0)
286 		bsdar_errc(bsdar, EX_IOERR, errno, "mkstemp failed");
287 
288 	if (in) {
289 		/*
290 		 * Command OPEN creates a temporary copy of the
291 		 * input archive.
292 		 */
293 		if ((ifd = open(in, O_RDONLY)) < 0) {
294 			bsdar_warnc(bsdar, errno, "open failed");
295 			return;
296 		}
297 		if (arscp_copy(ifd, ofd)) {
298 			bsdar_warnc(bsdar, 0, "arscp_copy failed");
299 			return;
300 		}
301 		close(ifd);
302 		close(ofd);
303 	} else {
304 		/*
305 		 * Command CREATE creates an "empty" archive.
306 		 * (archive with only global header)
307 		 */
308 		if ((a = archive_write_new()) == NULL)
309 			bsdar_errc(bsdar, EX_SOFTWARE, 0,
310 			    "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_finish(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, EX_SOFTWARE, 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_mode_A(bsdar);
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_mode_q(bsdar);
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_mode_d(bsdar);
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_mode_x(bsdar);
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_mode_t(bsdar);
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, EX_IOERR, errno,
441 			    "fopen %s failed", rlt);
442 	}
443 
444 	bsdar->filename = archive;
445 	if (list)
446 		arscp_mlist2argv(list);
447 	else {
448 		bsdar->argc = 0;
449 		bsdar->argv = NULL;
450 	}
451 	if (verbose)
452 		bsdar->options |= AR_V;
453 	ar_mode_t(bsdar);
454 	bsdar->options &= ~AR_V;
455 
456 	if (rlt) {
457 		if (fclose(stdout) == EOF)
458 			bsdar_errc(bsdar, EX_IOERR, errno,
459 			    "fclose %s failed", rlt);
460 		stdout = out;
461 		free(rlt);
462 	}
463 	free(archive);
464 	bsdar->filename = tmpac;
465 	arscp_free_argv();
466 	arscp_free_mlist(list);
467 }
468 
469 
470 /* Replace modules of current archive. */
471 static void
472 arscp_replace(struct list *list)
473 {
474 
475 	if (!arscp_target_exist())
476 		return;
477 	arscp_mlist2argv(list);
478 	ar_mode_r(bsdar);
479 	arscp_free_argv();
480 	arscp_free_mlist(list);
481 }
482 
483 /* Rename the temporary archive to the target archive. */
484 static void
485 arscp_save(void)
486 {
487 	mode_t mask;
488 
489 	if (target) {
490 		if (rename(tmpac, target) < 0)
491 			bsdar_errc(bsdar, EX_IOERR, errno, "rename failed");
492 		/*
493 		 * mkstemp creates temp files with mode 0600, here we
494 		 * set target archive mode per process umask.
495 		 */
496 		mask = umask(0);
497 		umask(mask);
498 		if (chmod(target, 0666 & ~mask) < 0)
499 			bsdar_errc(bsdar, EX_IOERR, errno, "chmod failed");
500 		free(tmpac);
501 		free(target);
502 		tmpac = NULL;
503 		target= NULL;
504 		bsdar->filename = NULL;
505 	} else
506 		bsdar_warnc(bsdar, 0, "no open output archive");
507 }
508 
509 /*
510  * Discard all the contents of current archive. This is achieved by
511  * invoking CREATE cmd on current archive.
512  */
513 static void
514 arscp_clear(void)
515 {
516 	char		*new_target;
517 
518 	if (target) {
519 		new_target = strdup(target);
520 		if (new_target == NULL)
521 			bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
522 		arscp_create(NULL, new_target);
523 	}
524 }
525 
526 /*
527  * Quit ar(1). Note that END cmd will not SAVE current archive
528  * before exit.
529  */
530 static void
531 arscp_end(int eval)
532 {
533 
534 	if (target)
535 		free(target);
536 	if (tmpac) {
537 		if (unlink(tmpac) == -1)
538 			bsdar_errc(bsdar, EX_IOERR, errno, "unlink %s failed",
539 			    tmpac);
540 		free(tmpac);
541 	}
542 
543 	exit(eval);
544 }
545 
546 /*
547  * Check if target specified, i.e, whether OPEN or CREATE has been
548  * issued by user.
549  */
550 static int
551 arscp_target_exist(void)
552 {
553 
554 	if (target)
555 		return (1);
556 
557 	bsdar_warnc(bsdar, 0, "no open output archive");
558 	return (0);
559 }
560 
561 /* Construct module list. */
562 static struct list *
563 arscp_mlist(struct list *list, char *str)
564 {
565 	struct list *l;
566 
567 	l = malloc(sizeof(*l));
568 	if (l == NULL)
569 		bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
570 	l->str = str;
571 	l->next = list;
572 
573 	return (l);
574 }
575 
576 /* Calculate the length of a mlist. */
577 static int
578 arscp_mlist_len(struct list *list)
579 {
580 	int len;
581 
582 	for(len = 0; list; list = list->next)
583 		len++;
584 
585 	return (len);
586 }
587 
588 /* Free the space allocated for mod_list. */
589 static void
590 arscp_free_mlist(struct list *list)
591 {
592 	struct list *l;
593 
594 	/* Note that list->str was freed in arscp_free_argv. */
595 	for(; list; list = l) {
596 		l = list->next;
597 		free(list);
598 	}
599 }
600 
601 /* Convert mlist to argv array. */
602 static void
603 arscp_mlist2argv(struct list *list)
604 {
605 	char	**argv;
606 	int	  i, n;
607 
608 	n = arscp_mlist_len(list);
609 	argv = malloc(n * sizeof(*argv));
610 	if (argv == NULL)
611 		bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
612 
613 	/* Note that module names are stored in reverse order in mlist. */
614 	for(i = n - 1; i >= 0; i--, list = list->next) {
615 		if (list == NULL)
616 			bsdar_errc(bsdar, EX_SOFTWARE, errno, "invalid mlist");
617 		argv[i] = list->str;
618 	}
619 
620 	bsdar->argc = n;
621 	bsdar->argv = argv;
622 }
623 
624 /* Free space allocated for argv array and its elements. */
625 static void
626 arscp_free_argv(void)
627 {
628 	int i;
629 
630 	for(i = 0; i < bsdar->argc; i++)
631 		free(bsdar->argv[i]);
632 
633 	free(bsdar->argv);
634 }
635 
636 /* Show a prompt if we are in interactive mode */
637 static void
638 arscp_prompt(void)
639 {
640 
641 	if (interactive) {
642 		printf("AR >");
643 		fflush(stdout);
644 	}
645 }
646 
647 /* Main function for ar script mode. */
648 void
649 ar_mode_script(struct bsdar *ar)
650 {
651 
652 	bsdar = ar;
653 	interactive = isatty(fileno(stdin));
654 	while(yyparse()) {
655 		if (!interactive)
656 			arscp_end(1);
657 	}
658 
659 	/* Script ends without END */
660 	arscp_end(EX_OK);
661 }
662