Home | History | Annotate | Line # | Download | only in vndcompress
main.c revision 1.3.8.2
      1  1.3.8.2  martin /*	$NetBSD: main.c,v 1.3.8.2 2014/12/07 13:57:58 martin Exp $	*/
      2  1.3.8.2  martin 
      3  1.3.8.2  martin /*-
      4  1.3.8.2  martin  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.3.8.2  martin  * All rights reserved.
      6  1.3.8.2  martin  *
      7  1.3.8.2  martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.8.2  martin  * by Taylor R. Campbell.
      9  1.3.8.2  martin  *
     10  1.3.8.2  martin  * Redistribution and use in source and binary forms, with or without
     11  1.3.8.2  martin  * modification, are permitted provided that the following conditions
     12  1.3.8.2  martin  * are met:
     13  1.3.8.2  martin  * 1. Redistributions of source code must retain the above copyright
     14  1.3.8.2  martin  *    notice, this list of conditions and the following disclaimer.
     15  1.3.8.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.8.2  martin  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.8.2  martin  *    documentation and/or other materials provided with the distribution.
     18  1.3.8.2  martin  *
     19  1.3.8.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3.8.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3.8.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3.8.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3.8.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3.8.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3.8.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3.8.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3.8.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3.8.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3.8.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     30  1.3.8.2  martin  */
     31  1.3.8.2  martin 
     32  1.3.8.2  martin #include <sys/cdefs.h>
     33  1.3.8.2  martin __RCSID("$NetBSD: main.c,v 1.3.8.2 2014/12/07 13:57:58 martin Exp $");
     34  1.3.8.2  martin 
     35  1.3.8.2  martin #include <assert.h>
     36  1.3.8.2  martin #include <err.h>
     37  1.3.8.2  martin #include <getopt.h>
     38  1.3.8.2  martin #include <stdio.h>
     39  1.3.8.2  martin #include <stdlib.h>
     40  1.3.8.2  martin #include <string.h>
     41  1.3.8.2  martin 
     42  1.3.8.2  martin #include "common.h"
     43  1.3.8.2  martin 
     44  1.3.8.2  martin static int (*operation)(int, char **, const struct options *) = &vndcompress;
     45  1.3.8.2  martin 
     46  1.3.8.2  martin int
     47  1.3.8.2  martin main(int argc, char **argv)
     48  1.3.8.2  martin {
     49  1.3.8.2  martin 	static const struct options zero_options;
     50  1.3.8.2  martin 	struct options options = zero_options, *O = &options;
     51  1.3.8.2  martin 	int ch;
     52  1.3.8.2  martin 
     53  1.3.8.2  martin 	setprogname(argv[0]);
     54  1.3.8.2  martin 
     55  1.3.8.2  martin 	if (strcmp(getprogname(), "vndcompress") == 0)
     56  1.3.8.2  martin 		operation = &vndcompress;
     57  1.3.8.2  martin 	else if (strcmp(getprogname(), "vnduncompress") == 0)
     58  1.3.8.2  martin 		operation = &vnduncompress;
     59  1.3.8.2  martin 	else
     60  1.3.8.2  martin 		warnx("unknown program name, defaulting to vndcompress: %s",
     61  1.3.8.2  martin 		    getprogname());
     62  1.3.8.2  martin 
     63  1.3.8.2  martin 	while ((ch = getopt(argc, argv, "b:cdk:l:p:rRw:")) != -1) {
     64  1.3.8.2  martin 		switch (ch) {
     65  1.3.8.2  martin 		case 'b':
     66  1.3.8.2  martin 			if (ISSET(O->flags, FLAG_b)) {
     67  1.3.8.2  martin 				warnx("-b may be supplied only once");
     68  1.3.8.2  martin 				usage();
     69  1.3.8.2  martin 			}
     70  1.3.8.2  martin 			O->flags |= FLAG_b;
     71  1.3.8.2  martin 			__CTASSERT(MIN_BLOCKSIZE <= MAX_BLOCKSIZE);
     72  1.3.8.2  martin 			__CTASSERT(MAX_BLOCKSIZE <= LLONG_MAX);
     73  1.3.8.2  martin 			O->blocksize = strsuftoll("block size", optarg,
     74  1.3.8.2  martin 			    MIN_BLOCKSIZE, MAX_BLOCKSIZE);
     75  1.3.8.2  martin 			break;
     76  1.3.8.2  martin 
     77  1.3.8.2  martin 		case 'c':
     78  1.3.8.2  martin 			if (ISSET(O->flags, FLAG_d)) {
     79  1.3.8.2  martin 				warnx("-c and -d are mutually exclusive");
     80  1.3.8.2  martin 				usage();
     81  1.3.8.2  martin 			}
     82  1.3.8.2  martin 			O->flags |= FLAG_c;
     83  1.3.8.2  martin 			operation = &vndcompress;
     84  1.3.8.2  martin 			break;
     85  1.3.8.2  martin 
     86  1.3.8.2  martin 		case 'd':
     87  1.3.8.2  martin 			if (ISSET(O->flags, FLAG_c)) {
     88  1.3.8.2  martin 				warnx("-c and -d are mutually exclusive");
     89  1.3.8.2  martin 				usage();
     90  1.3.8.2  martin 			}
     91  1.3.8.2  martin 			O->flags |= FLAG_d;
     92  1.3.8.2  martin 			operation = &vnduncompress;
     93  1.3.8.2  martin 			break;
     94  1.3.8.2  martin 
     95  1.3.8.2  martin 		case 'k':
     96  1.3.8.2  martin 			if (ISSET(O->flags, FLAG_k)) {
     97  1.3.8.2  martin 				warnx("-k may be supplied only once");
     98  1.3.8.2  martin 				usage();
     99  1.3.8.2  martin 			}
    100  1.3.8.2  martin 			O->flags |= FLAG_k;
    101  1.3.8.2  martin 			O->checkpoint_blocks = strsuftoll("checkpoint blocks",
    102  1.3.8.2  martin 			    optarg,
    103  1.3.8.2  martin 			    0, MIN(UINT32_MAX, (OFF_MAX / MIN_BLOCKSIZE)));
    104  1.3.8.2  martin 			break;
    105  1.3.8.2  martin 
    106  1.3.8.2  martin 		case 'l':
    107  1.3.8.2  martin 			if (ISSET(O->flags, FLAG_l)) {
    108  1.3.8.2  martin 				warnx("-l may be supplied only once");
    109  1.3.8.2  martin 				usage();
    110  1.3.8.2  martin 			}
    111  1.3.8.2  martin 			O->flags |= FLAG_l;
    112  1.3.8.2  martin 			O->length = strsuftoll("length", optarg,
    113  1.3.8.2  martin 			    0, MIN(OFF_MAX, UINT64_MAX));
    114  1.3.8.2  martin 			break;
    115  1.3.8.2  martin 
    116  1.3.8.2  martin 		case 'p':
    117  1.3.8.2  martin 			O->flags |= FLAG_p;
    118  1.3.8.2  martin 			__CTASSERT(OFF_MAX <= LLONG_MAX);
    119  1.3.8.2  martin 			O->end_block = strsuftoll("end block", optarg,
    120  1.3.8.2  martin 			    0, MIN(UINT32_MAX, (OFF_MAX / MIN_BLOCKSIZE)));
    121  1.3.8.2  martin 			break;
    122  1.3.8.2  martin 
    123  1.3.8.2  martin 		case 'r':
    124  1.3.8.2  martin 			O->flags |= FLAG_r;
    125  1.3.8.2  martin 			break;
    126  1.3.8.2  martin 
    127  1.3.8.2  martin 		case 'R':
    128  1.3.8.2  martin 			O->flags |= FLAG_R;
    129  1.3.8.2  martin 			break;
    130  1.3.8.2  martin 
    131  1.3.8.2  martin 		case 'w':
    132  1.3.8.2  martin 			if (ISSET(O->flags, FLAG_w)) {
    133  1.3.8.2  martin 				warnx("-w may be supplied only once");
    134  1.3.8.2  martin 				usage();
    135  1.3.8.2  martin 			}
    136  1.3.8.2  martin 			O->flags |= FLAG_w;
    137  1.3.8.2  martin 			O->window_size = strsuftoll("window size", optarg,
    138  1.3.8.2  martin 			    0, MAX_WINDOW_SIZE);
    139  1.3.8.2  martin 			break;
    140  1.3.8.2  martin 
    141  1.3.8.2  martin 		case '?':
    142  1.3.8.2  martin 		default:
    143  1.3.8.2  martin 			usage();
    144  1.3.8.2  martin 		}
    145  1.3.8.2  martin 	}
    146  1.3.8.2  martin 
    147  1.3.8.2  martin 	argc -= optind;
    148  1.3.8.2  martin 	argv += optind;
    149  1.3.8.2  martin 
    150  1.3.8.2  martin 	if (operation == &vnduncompress) {
    151  1.3.8.2  martin 		if (ISSET(O->flags, ~(FLAG_d | FLAG_w)))
    152  1.3.8.2  martin 			usage();
    153  1.3.8.2  martin 	} else {
    154  1.3.8.2  martin 		assert(operation == &vndcompress);
    155  1.3.8.2  martin 		if (ISSET(O->flags, ~(FLAG_b | FLAG_c | FLAG_k | FLAG_l |
    156  1.3.8.2  martin 			    FLAG_p | FLAG_r | FLAG_R | FLAG_w)))
    157  1.3.8.2  martin 			usage();
    158  1.3.8.2  martin 		if (ISSET(O->flags, FLAG_R) && !ISSET(O->flags, FLAG_r)) {
    159  1.3.8.2  martin 			warnx("-R makes no sense without -r");
    160  1.3.8.2  martin 			usage();
    161  1.3.8.2  martin 		}
    162  1.3.8.2  martin 	}
    163  1.3.8.2  martin 
    164  1.3.8.2  martin 	return (*operation)(argc, argv, O);
    165  1.3.8.2  martin }
    166  1.3.8.2  martin 
    167  1.3.8.2  martin void __dead
    168  1.3.8.2  martin usage(void)
    169  1.3.8.2  martin {
    170  1.3.8.2  martin 
    171  1.3.8.2  martin 	(void)fprintf(stderr,
    172  1.3.8.2  martin 	    "Usage: %s -c [-rR] [-b <blocksize>] [-k <checkpoint-blocks>]\n"
    173  1.3.8.2  martin 	    "          [-l <length>] [-p <partial-offset>] [-w <winsize>]\n"
    174  1.3.8.2  martin 	    "          <image> <compressed-image> [<blocksize>]\n"
    175  1.3.8.2  martin 	    "       %s -d <compressed-image> <image>\n",
    176  1.3.8.2  martin 	    getprogname(), getprogname());
    177  1.3.8.2  martin 	exit(1);
    178  1.3.8.2  martin }
    179