Rizin
unix-like reverse engineering framework and cli tools
getopt.c File Reference
#include <rz_util.h>

Go to the source code of this file.

Macros

#define BADCH   (int)'?'
 
#define BADARG   (int)':'
 
#define EMSG   ""
 

Functions

RZ_API void rz_getopt_init (RzGetopt *opt, int argc, const char **argv, const char *ostr)
 
RZ_API int rz_getopt_next (RzGetopt *opt)
 

Macro Definition Documentation

◆ BADARG

#define BADARG   (int)':'

Definition at line 14 of file getopt.c.

◆ BADCH

#define BADCH   (int)'?'

Definition at line 13 of file getopt.c.

◆ EMSG

#define EMSG   ""

Definition at line 15 of file getopt.c.

Function Documentation

◆ rz_getopt_init()

RZ_API void rz_getopt_init ( RzGetopt opt,
int  argc,
const char **  argv,
const char *  ostr 
)

Definition at line 17 of file getopt.c.

17  {
18  memset(opt, 0, sizeof(RzGetopt));
19  opt->err = 1;
20  opt->ind = 1;
21  opt->opt = 0;
22  opt->reset = 0;
23  opt->arg = NULL;
24  opt->argc = argc;
25  opt->argv = argv;
26  opt->ostr = ostr;
27 }
#define NULL
Definition: cris-opc.c:27
return memset(p, 0, total)
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
const char * ostr
Definition: rz_getopt.h:19
const char ** argv
Definition: rz_getopt.h:18
const char * arg
Definition: rz_getopt.h:15

References rz_getopt_t::arg, rz_getopt_t::argc, rz_getopt_t::argv, argv, rz_getopt_t::err, rz_getopt_t::ind, memset(), NULL, rz_getopt_t::opt, rz_getopt_t::ostr, and rz_getopt_t::reset.

Referenced by hash_parse_cmdline(), rz_diff_parse_arguments(), rz_main_rizin(), rz_main_rz_agent(), rz_main_rz_asm(), rz_main_rz_bin(), rz_main_rz_find(), rz_main_rz_gg(), rz_main_rz_sign(), rz_test_main(), and windbg_open().

◆ rz_getopt_next()

RZ_API int rz_getopt_next ( RzGetopt opt)

Definition at line 29 of file getopt.c.

29  {
30  static const char *place = EMSG; // option letter processing
31  const char *oli; // option letter list index
32 
33  if (opt->reset || !*place) { // update scanning pointer
34  opt->reset = 0;
35  if (opt->ind >= opt->argc || *(place = opt->argv[opt->ind]) != '-') {
36  place = EMSG;
37  return -1;
38  }
39  if (place[1] && *++place == '-') { // found "--"
40  opt->ind++;
41  place = EMSG;
42  return -1;
43  }
44  }
45  /* option letter okay? */
46  if ((opt->opt = (int)*place++) == (int)':' || !(oli = strchr(opt->ostr, opt->opt))) {
47  /*
48  * if the user didn't specify '-' as an option,
49  * assume it means -1.
50  */
51  if (opt->opt == (int)'-') {
52  opt->ind++;
53  if (rz_getopt_next(opt) == -1) {
54  opt->ind--;
55  return -1;
56  }
57 
58  return '-';
59  }
60  if (!*place) {
61  opt->ind++;
62  }
63  if (opt->err && *opt->ostr != ':') {
64  (void)eprintf("%s: illegal option -- %c\n", opt->argv[0], opt->opt);
65  }
66  return BADCH;
67  }
68  if (*++oli == ':') { /* need argument */
69  if (*place) { /* no white space */
70  opt->arg = place;
71  } else if (opt->argc <= ++opt->ind) { /* no arg */
72  place = EMSG;
73  if (*opt->ostr == ':') {
74  return BADARG;
75  }
76  if (opt->err) {
77  (void)eprintf("%s: option requires an argument -- %c\n", opt->argv[0], opt->opt);
78  }
79  return BADCH;
80  } else { /* white space */
81  opt->arg = opt->argv[opt->ind];
82  }
83  place = EMSG;
84  opt->ind++;
85  } else {
86  opt->arg = NULL;
87  if (!*place) {
88  opt->ind++;
89  }
90  }
91  // dump back option letter
92  return opt->opt;
93 }
#define BADCH
Definition: getopt.c:13
RZ_API int rz_getopt_next(RzGetopt *opt)
Definition: getopt.c:29
#define BADARG
Definition: getopt.c:14
#define EMSG
Definition: getopt.c:15
#define eprintf(x, y...)
Definition: rlcc.c:7

References rz_getopt_t::arg, rz_getopt_t::argc, rz_getopt_t::argv, BADARG, BADCH, EMSG, eprintf, rz_getopt_t::err, rz_getopt_t::ind, NULL, rz_getopt_t::opt, rz_getopt_t::ostr, rz_getopt_t::reset, and rz_getopt_next().

Referenced by hash_parse_cmdline(), rz_diff_parse_arguments(), rz_getopt_next(), rz_main_rizin(), rz_main_rz_agent(), rz_main_rz_asm(), rz_main_rz_bin(), rz_main_rz_find(), rz_main_rz_gg(), rz_main_rz_sign(), rz_test_main(), and windbg_open().