00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include <unistd.h>
00040
00041 #include "config.h"
00042
00043 #ifdef HAVE_GETOPT_LONG
00044 #include <getopt.h>
00045 #else
00046 #include "compat/getopt.h"
00047 #endif
00048
00049 #include "Getopt.h"
00050
00051 namespace oasys {
00052
00053 Opt* Getopt::opts_[256];
00054 Getopt::OptList Getopt::allopts_;
00055
00056 void
00057 Getopt::addopt(Opt* opt)
00058 {
00059 if (opt->shortopt_ != 0) {
00060 int c = opt->shortopt_;
00061 if (opts_[c]) {
00062 fprintf(stderr,
00063 "FATAL ERROR: multiple addopt calls for char '%c'\n", c);
00064 abort();
00065 }
00066
00067 opts_[c] = opt;
00068 }
00069 allopts_.push_back(opt);
00070 }
00071
00072 int
00073 Getopt::getopt(const char* progname, int argc, char* const argv[],
00074 const char* extra_usage)
00075 {
00076 Opt* opt;
00077 char short_opts[256];
00078 char* optstring = short_opts;
00079 int c, i;
00080 struct option* long_opts;
00081
00082 int nopts = allopts_.size();
00083
00084
00085 long_opts = (struct option*) malloc(sizeof(struct option) * (nopts + 2));
00086 memset(long_opts, 0, sizeof(struct option) * (nopts + 2));
00087
00088 for (i = 0; i < nopts; ++i)
00089 {
00090 opt = allopts_[i];
00091
00092 if (opt->shortopt_) {
00093 *optstring++ = opt->shortopt_;
00094 if (opt->needval_) {
00095 *optstring++ = ':';
00096 }
00097 }
00098
00099 if (opt->longopt_) {
00100 long_opts[i].name = opt->longopt_;
00101 long_opts[i].has_arg = opt->needval_;
00102 } else {
00103
00104 long_opts[i].name = "help";
00105 }
00106 }
00107
00108
00109 *optstring++ = 'h';
00110 *optstring++ = 'H';
00111 long_opts[nopts].name = "help";
00112
00113 while (1) {
00114 c = ::getopt_long(argc, argv, short_opts, long_opts, &i);
00115 switch(c) {
00116 case 0:
00117 if (!strcmp(long_opts[i].name, "help"))
00118 {
00119 usage(progname, extra_usage);
00120 exit(0);
00121 }
00122
00123 opt = allopts_[i];
00124
00125 if (opt->set(optarg, optarg ? strlen(optarg) : 0) != 0) {
00126 fprintf(stderr, "invalid value '%s' for option '--%s'\n",
00127 optarg, opt->longopt_);
00128 exit(1);
00129 }
00130
00131 break;
00132 case ':':
00133
00134 fprintf(stderr, "option %s requires a value\n", long_opts[i].name);
00135 usage(progname, extra_usage);
00136 exit(0);
00137
00138 case '?':
00139 case 'h':
00140 case 'H':
00141 usage(progname, extra_usage);
00142 exit(0);
00143
00144 case -1:
00145
00146 goto done;
00147
00148 default:
00149 if (c < 0 || c > 256) {
00150 fprintf(stderr, "FATAL ERROR: %d returned from getopt\n", c);
00151 abort();
00152 }
00153 opt = opts_[c];
00154
00155 if (!opt) {
00156 fprintf(stderr, "unknown char '%c' returned from getopt\n", c);
00157 exit(1);
00158 }
00159
00160 if (opt->set(optarg, optarg ? strlen(optarg) : 0) != 0) {
00161 fprintf(stderr, "invalid value '%s' for option '-%c'\n",
00162 optarg, c);
00163 exit(1);
00164 }
00165
00166 if (opt->setp_)
00167 *opt->setp_ = true;
00168
00169 }
00170 }
00171
00172 done:
00173 free(long_opts);
00174 return optind;
00175 }
00176
00177 void
00178 Getopt::usage(const char* progname, const char* extra_usage)
00179 {
00180 OptList::iterator iter;
00181 char opts[128];
00182
00183 const char* s = strrchr(progname, '/');
00184 if (s != NULL) {
00185 progname = s + 1;
00186 }
00187 fprintf(stderr, "usage: %s [opts] %s\n\nopts:\n",
00188 progname, extra_usage);
00189
00190 snprintf(opts, sizeof(opts), "-h, --help");
00191 fprintf(stderr, " %-24s%s\n", opts, "show usage");
00192
00193 for (iter = allopts_.begin(); iter != allopts_.end(); ++iter)
00194 {
00195 Opt* opt = *iter;
00196
00197 if (opt->shortopt_ && opt->longopt_)
00198 {
00199 snprintf(opts, sizeof(opts), "-%c, --%s %s",
00200 opt->shortopt_, opt->longopt_, opt->valdesc_);
00201 }
00202 else if (opt->shortopt_)
00203 {
00204 snprintf(opts, sizeof(opts), "-%c %s",
00205 opt->shortopt_, opt->valdesc_);
00206 } else {
00207 snprintf(opts, sizeof(opts), "--%s %s ",
00208 opt->longopt_, opt->valdesc_);
00209 }
00210
00211 if (strlen(opts) <= 24) {
00212 fprintf(stderr, " %-24s%s\n", opts, opt->desc_);
00213 } else {
00214 fprintf(stderr, " %s\n", opts);
00215 fprintf(stderr, " %s\n", opt->desc_);
00216 }
00217 }
00218 }
00219
00220 }