xref: /freebsd/usr.bin/ar/acpyacc.y (revision aa0a1e58)
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 extern int	yyparse(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(EX_OK); }
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, 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));
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_finish(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 speicifed 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 spcified, 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