ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/getopt.c
(Generate patch)

Comparing:
ircd-hybrid-7.2/src/getopt.c (file contents), Revision 786 by michael, Sun Aug 27 08:48:04 2006 UTC vs.
ircd-hybrid/trunk/src/getopt.c (file contents), Revision 8603 by michael, Sun Oct 28 19:43:30 2018 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  getopt.c: Uses getopt to fetch the command line options.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 2001-2018 ircd-hybrid development team
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 16 | Line 15
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18 < *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19   *  USA
20 < *
21 < *  $Id$
20 > */
21 >
22 > /*! \file getopt.c
23 > * \brief Uses getopt to fetch the command line options.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
28   #include "ircd_getopt.h"
29 + #include "memory.h"
30  
31   #define OPTCHAR '-'
32  
33 < static void usage(const char *name);
33 >
34 > static void
35 > usage(const char *name, const struct lgetopt *opts)
36 > {
37 >  fprintf(stderr, "Usage: %s [options]\n", name);
38 >  fprintf(stderr, "Where valid options are:\n");
39 >
40 >  for (; opts->opt; ++opts)
41 >    fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, opts->opt,
42 >            (opts->argtype == YESNO || opts->argtype == USAGE) ? "" :
43 >            opts->argtype == INTEGER ? "<number>" : "<string>",
44 >            opts->desc);
45 >
46 >  exit(EXIT_FAILURE);
47 > }
48  
49   void
50   parseargs(int *argc, char ***argv, struct lgetopt *opts)
51   {
52 <  int i;
53 <  char *progname = (*argv)[0];
52 >  const char *progname = (*argv)[0];
53 >
54 >  /* Loop through each argument */
55 >  while (1)
56 >  {
57 >    int found = 0;
58 >    const char *opt = NULL;
59 >
60 >    (*argc)--;
61 >    (*argv)++;
62  
63 <  /* loop through each argument */
64 <  for (;;)
63 >    if (*argc < 1 || (*argv)[0][0] != OPTCHAR)
64 >      return;
65 >
66 >    opt = &(*argv)[0][1];
67 >
68 >    /* Search through our argument list, and see if it matches */
69 >    for (unsigned int i = 0; opts[i].opt; ++i)
70      {
71 <      int found = 0;
71 >      if (strcmp(opts[i].opt, opt) == 0)
72 >      {
73 >        /* Found our argument */
74 >        found = 1;
75 >
76 >        switch (opts[i].argtype)
77 >        {
78 >          case YESNO:
79 >            *((int *)opts[i].argloc) = 1;
80 >            break;
81 >
82 >          case INTEGER:
83 >            if (*argc < 2)
84 >            {
85 >              fprintf(stderr, "Error: option '%c%s' requires an argument\n",
86 >                      OPTCHAR, opts[i].opt);
87 >              usage((*argv)[0], opts);
88 >            }
89  
90 <      (*argc)--;
44 <      (*argv)++;
45 <      
46 <      if (*argc < 1)
47 <        {
48 <          return;
49 <        }
50 <      
51 <      /* check if it *is* an arg.. */
52 <      if ((*argv)[0][0] != OPTCHAR)
53 <        {
54 <          return;
55 <        }
56 <      
57 <      (*argv)[0]++;
58 <
59 <      /* search through our argument list, and see if it matches */
60 <      for (i = 0; opts[i].opt; i++)
61 <        {
62 <          if (!strcmp(opts[i].opt, (*argv)[0]))
63 <            {
64 <              /* found our argument */
65 <              found = 1;
66 <
67 <              switch (opts[i].argtype)
68 <                {
69 <                case YESNO:
70 <                  *((int *)opts[i].argloc) = 1;
71 <                  break;
72 <                case INTEGER:
73 <                  if (*argc < 2)
74 <                    {
75 <                      fprintf(stderr, "Error: option '%c%s' requires an argument\n",
76 <                              OPTCHAR, opts[i].opt);
77 <                      usage((*argv)[0]);
78 <                    }
79 <                  
80 <                  *((int *)opts[i].argloc) = atoi((*argv)[1]);
90 >            *((int *)opts[i].argloc) = atoi((*argv)[1]);
91              (*argc)--;
92              (*argv)++;
93 <                  break;
94 <                case STRING:
95 <                  if (*argc < 2)
96 <                    {
97 <                      fprintf(stderr, "error: option '%c%s' requires an argument\n",
98 <                              OPTCHAR, opts[i].opt);
99 <                      usage(progname);
100 <                    }
101 <                  
102 <                  *((char**)opts[i].argloc) = malloc(strlen((*argv)[1]) + 1);
103 <                  strcpy(*((char**)opts[i].argloc), (*argv)[1]);
93 >            break;
94 >
95 >          case STRING:
96 >            if (*argc < 2)
97 >            {
98 >              fprintf(stderr, "Error: option '%c%s' requires an argument\n",
99 >                      OPTCHAR, opts[i].opt);
100 >              usage(progname, opts);
101 >            }
102 >
103 >            *((char **)opts[i].argloc) = xstrdup((*argv)[1]);
104              (*argc)--;
105              (*argv)++;
106 <                  break;
106 >            break;
107  
108 <                case USAGE:
109 <                  usage(progname);
110 <                  /*NOTREACHED*/
111 <
112 <                default:
113 <                  fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n",
114 <                          __FILE__, __LINE__);
115 <                  exit(EXIT_FAILURE);
116 <                }
117 <            }
108 <        }
109 <        if (!found)
110 <          {
111 <            fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]);
112 <            usage(progname);
113 <          }
108 >          case USAGE:
109 >            usage(progname, opts);
110 >            /* NOTREACHED */
111 >
112 >          default:
113 >            fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n",
114 >                    __FILE__, __LINE__);
115 >            exit(EXIT_FAILURE);
116 >        }
117 >      }
118      }
115 }
119  
120 < static void
121 < usage(const char *name)
122 < {
123 <  int i;
124 <  
125 <  fprintf(stderr, "Usage: %s [options]\n", name);
123 <  fprintf(stderr, "Where valid options are:\n");
124 <  
125 <  for (i = 0; myopts[i].opt; i++)
126 <  {
127 <    fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, myopts[i].opt,
128 <            (myopts[i].argtype == YESNO || myopts[i].argtype == USAGE) ? "" :
129 <            myopts[i].argtype == INTEGER ? "<number>" : "<string>",
130 <            myopts[i].desc);
120 >    if (found == 0)
121 >    {
122 >      fprintf(stderr, "Error: unknown argument '%c%s'\n",
123 >              OPTCHAR, opt);
124 >      usage(progname, opts);
125 >    }
126    }
132
133  exit(EXIT_FAILURE);
127   }
135

Comparing:
ircd-hybrid-7.2/src/getopt.c (property svn:keywords), Revision 786 by michael, Sun Aug 27 08:48:04 2006 UTC vs.
ircd-hybrid/trunk/src/getopt.c (property svn:keywords), Revision 8603 by michael, Sun Oct 28 19:43:30 2018 UTC

# Line 1 | Line 1
1 < Id Revision
1 > Id

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)