1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "@(#)args.h 1.28 09/05/26 SMI" 28 29 #ifndef _DISKOMIZER_ARGS_H 30 #define _DISKOMIZER_ARGS_H 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 #define LONGLONG_MAX 0x7fffffffffffffffLL 37 #include <ctype.h> 38 #include <stdio.h> 39 #include <sys/types.h> 40 #define KILO_BYTE(A) (1024*A) 41 #define MEGA_BYTE(A) (1024*1024*A) 42 #define DAYS(A) (24*3600*A) 43 #define MINUTE 60 44 #define MINS(A) (MINUTE * A) 45 struct ilist { 46 int wlen; 47 int *vals; 48 int *weightings; 49 }; 50 /* 51 * All these defines are done do the structure has all the 64 bit 52 * quantites, then the 32 bit quantities then the 8 bit quantities. 53 * so that we don't waste space with too much padding, but the order 54 * of the file arg_vals.h defines the order in which things get printed 55 */ 56 #define START_SECTION(A, B) 57 #define END_SECTION(A) 58 #define SET_OPENING_BLURB(A) 59 struct options { 60 #define ILIST(B, C, D) struct ilist B; 61 #define STARG(B, D, C) 62 #define SARG(B, D, C) 63 #define LARG(B, D, C) 64 #define IARG(B, D, C) 65 #define CARG(B, D, C) 66 #define BARG(B, D, C) 67 #define LLARG(B, D, C) 68 #include "arg_vals.h" 69 #undef ILIST 70 #undef LLARG 71 #define ILIST(A, B, C) 72 #define LLARG(B, D, C) long long B; 73 #include "arg_vals.h" 74 #undef LLARG 75 #define LLARG(B, D, C) 76 #undef STARG 77 #undef LARG 78 #define STARG(B, D, C) char *B; 79 #define LARG(B, D, C) ulong_t B; 80 #include "arg_vals.h" 81 #undef STARG 82 #undef LARG 83 #undef LLARG 84 #undef IARG 85 #define STARG(B, D, C) 86 #define LLARG(B, D, C) 87 #define LARG(B, D, C) 88 #define IARG(B, D, C) int B; 89 #include "arg_vals.h" 90 #undef IARG 91 #define IARG(B, D, C) 92 #undef SARG 93 #define SARG(B, D, C) ushort_t B; 94 #include "arg_vals.h" 95 #undef SARG 96 #define SARG(B, D, C) 97 #undef CARG 98 #define CARG(B, D, C) uchar_t B; 99 #include "arg_vals.h" 100 #undef CARG 101 #undef BARG 102 #define CARG(B, D, C) 103 #define BARG(B, D, C) uint_t B:1; 104 #include "arg_vals.h" 105 #undef STARG 106 #undef SARG 107 #undef IARG 108 #undef LLARG 109 #undef LARG 110 #undef CARG 111 #undef BARG 112 #undef ILIST 113 }; 114 #undef START_SECTION 115 #undef END_SECTION 116 #undef SET_OPENING_BLURB 117 118 extern struct options opts; 119 #define OPTION(A) opts.A 120 121 /* 122 * Option Store. 123 * 124 * The option store can be used by a shared libary so that the library 125 * can access options. 126 * 127 * The library calls opts_init which returns a pointer to an option_opts 128 * structure. Each of the option opts takes 2 arguments, the first being 129 * the key and the second a pointer to the variable into which to write 130 * the result. They all return OPT_OK on success and OPT_FAILED on 131 * failure. 132 * 133 * On success it writes the value into the variable pointed to by the 134 * second argument. 135 * 136 * Options should be processed at the start of the program and cached 137 * as the option store is not "super fast". Once all the options are 138 * processed opts_fini() should be called and then option routines 139 * should not be used again. 140 * 141 * Example: 142 * 143 * char *name; 144 * int count; 145 * struct option_ops *ops; 146 * 147 * ops = opts_init(); 148 * if (ops->opt_str("TEST_OPTION", &name) == OPT_OK) { 149 * name = strdup(name); 150 * } 151 * if (ops->opt_int("TEST_INT", &test_int) != OPT_OK) { 152 * fprintf(stderr, "Unknown option \"TEST_INT\"\n"); 153 * } 154 * 155 */ 156 enum option_status { 157 OPT_FAILED = 0, 158 OPT_OK = 1 159 }; 160 typedef enum option_status option_status_t; 161 162 struct option_ops { 163 option_status_t (*opt_bool)(const char *, char *); 164 option_status_t (*opt_char)(const char *, char *); 165 option_status_t (*opt_short)(const char *, short *); 166 option_status_t (*opt_int)(const char *, int *); 167 option_status_t (*opt_long)(const char *, long *); 168 option_status_t (*opt_long_long)(const char *, long long *); 169 option_status_t (*opt_str)(const char *, char **); 170 }; 171 172 extern const struct option_ops *opts_init(void); 173 extern int opts_fini(void); /* returns a count of the unused options */ 174 extern int do_args(int argc, char **argv, void (*pprintf)(char *fmt, ...), 175 const char *path); 176 extern void print_args(int argc, char **argv, 177 void (*pprintf)(const char *fmt, ...)); 178 extern int print_options(FILE *f); 179 extern void usage(const char *prog); 180 extern char *map_file(char *file, time_t *last_readp, size_t *lenp, 181 void (*pprintf)(char *fmt, ...), const char *path); 182 extern int save_options(const char *file); 183 extern int read_config_file(char *file, void (pprintf)(char *, ...), 184 const char *path); 185 extern int load_ilist(struct ilist *ilp, int *vals, int len); 186 extern const char *set_diskomizer_path(void); 187 extern const struct option_ops option_ops; 188 #ifdef __cplusplus 189 } 190 #endif 191 192 #endif /* _DISKOMIZER_ARGS_H */ 193