acpyacc.y revision 235789
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: head/usr.bin/ar/acpyacc.y 235789 2012-05-22 16:33:10Z bapt $");
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
50struct list {
51	char		*str;
52	struct list	*next;
53};
54
55
56extern int	yylex(void);
57
58static void	yyerror(const char *);
59static void	arscp_addlib(char *archive, struct list *list);
60static void	arscp_addmod(struct list *list);
61static void	arscp_clear(void);
62static int	arscp_copy(int ifd, int ofd);
63static void	arscp_create(char *in, char *out);
64static void	arscp_delete(struct list *list);
65static void	arscp_dir(char *archive, struct list *list, char *rlt);
66static void	arscp_end(int eval);
67static void	arscp_extract(struct list *list);
68static void	arscp_free_argv(void);
69static void	arscp_free_mlist(struct list *list);
70static void	arscp_list(void);
71static struct list *arscp_mlist(struct list *list, char *str);
72static void	arscp_mlist2argv(struct list *list);
73static int	arscp_mlist_len(struct list *list);
74static void	arscp_open(char *fname);
75static void	arscp_prompt(void);
76static void	arscp_replace(struct list *list);
77static void	arscp_save(void);
78static int	arscp_target_exist(void);
79
80extern int		 lineno;
81
82static struct bsdar	*bsdar;
83static char		*target;
84static char		*tmpac;
85static int		 interactive;
86static 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
117begin
118	: { arscp_prompt(); } ar_script
119	;
120
121ar_script
122	: cmd_list
123	|
124	;
125
126mod_list
127	: FNAME { $$ = arscp_mlist(NULL, $1); }
128	| mod_list separator FNAME { $$ = arscp_mlist($1, $3); }
129	;
130
131separator
132	: COMMA
133	|
134	;
135
136cmd_list
137	: rawcmd
138	| cmd_list rawcmd
139	;
140
141rawcmd
142	: cmd EOL { arscp_prompt(); }
143	;
144
145cmd
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
164addlib_cmd
165	: ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); }
166	| ADDLIB FNAME { arscp_addlib($2, NULL); }
167	;
168
169addmod_cmd
170	: ADDMOD mod_list { arscp_addmod($2); }
171	;
172
173clear_cmd
174	: CLEAR { arscp_clear(); }
175	;
176
177create_cmd
178	: CREATE FNAME { arscp_create(NULL, $2); }
179	;
180
181delete_cmd
182	: DELETE mod_list { arscp_delete($2); }
183	;
184
185directory_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
191end_cmd
192	: END { arscp_end(EX_OK); }
193	;
194
195extract_cmd
196	: EXTRACT mod_list { arscp_extract($2); }
197	;
198
199list_cmd
200	: LIST { arscp_list(); }
201	;
202
203open_cmd
204	: OPEN FNAME { arscp_open($2); }
205	;
206
207replace_cmd
208	: REPLACE mod_list { arscp_replace($2); }
209	;
210
211save_cmd
212	: SAVE { arscp_save(); }
213	;
214
215verbose_cmd
216	: VERBOSE { verbose = !verbose; }
217	;
218
219empty_cmd
220	:
221	;
222
223invalid_cmd
224	: FNAME { yyerror(NULL); }
225	;
226
227%%
228
229/* ARGSUSED */
230static void
231yyerror(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 */
243static void
244arscp_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 */
269static void
270arscp_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. */
326static int
327arscp_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 */
362static void
363arscp_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. */
376static void
377arscp_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. */
389static void
390arscp_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. */
402static void
403arscp_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) */
415static void
416arscp_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) */
430static void
431arscp_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. */
471static void
472arscp_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. */
484static void
485arscp_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 */
513static void
514arscp_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 */
530static void
531arscp_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 */
550static int
551arscp_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. */
562static struct list *
563arscp_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. */
577static int
578arscp_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. */
589static void
590arscp_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. */
602static void
603arscp_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. */
625static void
626arscp_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 */
637static void
638arscp_prompt(void)
639{
640
641	if (interactive) {
642		printf("AR >");
643		fflush(stdout);
644	}
645}
646
647/* Main function for ar script mode. */
648void
649ar_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