Getopt.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2005 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // alloc two extra getopt -- one for help, one for all zeros
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             // ignore this slot
00104             long_opts[i].name = "help";
00105         }
00106     }
00107                                         
00108     // tack on the help option
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             // missing value to option
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             // end of list
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 } // namespace oasys

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1