Mixe for Privacy and Anonymity in the Internet
Macros | Functions
poptparse.cpp File Reference
#include "../StdAfx.h"
Include dependency graph for poptparse.cpp:

Go to the source code of this file.

Macros

#define POPT_ARGV_ARRAY_GROW_DELTA   5
 

Functions

int poptDupArgv (int argc, const char **argv, int *argcPtr, const char ***argvPtr)
 
int poptParseArgvString (const char *s, int *argcPtr, const char ***argvPtr)
 

Macro Definition Documentation

◆ POPT_ARGV_ARRAY_GROW_DELTA

#define POPT_ARGV_ARRAY_GROW_DELTA   5

Definition at line 8 of file poptparse.cpp.

Function Documentation

◆ poptDupArgv()

int poptDupArgv ( int  argc,
const char **  argv,
int *  argcPtr,
const char ***  argvPtr 
)

Definition at line 10 of file poptparse.cpp.

12 {
13  size_t nb = (argc + 1) * sizeof(*argv);
14  const char ** argv2;
15  char * dst;
16  int i;
17 
18  for (i = 0; i < argc; i++) {
19  if (argv[i] == NULL)
20  return POPT_ERROR_NOARG;
21  nb += strlen(argv[i]) + 1;
22  }
23 
24  dst = (char *)malloc(nb);
25  argv2 = (const char**) dst;
26  dst += (argc + 1) * sizeof(*argv);
27 
28  for (i = 0; i < argc; i++) {
29  argv2[i] = dst;
30  dst += strlen(strcpy(dst, argv[i])) + 1;
31  }
32  argv2[argc] = NULL;
33 
34  *argvPtr = argv2;
35  *argcPtr = argc;
36  return 0;
37 }
#define POPT_ERROR_NOARG
Definition: popt.cpp:717

References POPT_ERROR_NOARG.

Referenced by poptParseArgvString(), and poptStuffArgs().

Here is the caller graph for this function:

◆ poptParseArgvString()

int poptParseArgvString ( const char *  s,
int *  argcPtr,
const char ***  argvPtr 
)

Definition at line 39 of file poptparse.cpp.

40 {
41  const char * src;
42  char quote = '\0';
43  int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
44  const char ** argv = (const char **)malloc(sizeof(*argv) * argvAlloced);
45  int argc = 0;
46  int buflen = strlen(s) + 1;
47  char * buf = (char*)memset(alloca(buflen), 0, buflen);
48 
49  argv[argc] = buf;
50 
51  for (src = s; *src; src++) {
52  if (quote == *src) {
53  quote = '\0';
54  } else if (quote) {
55  if (*src == '\\') {
56  src++;
57  if (!*src) {
58  free((void*)argv);
59  return POPT_ERROR_BADQUOTE;
60  }
61  if (*src != quote) *buf++ = '\\';
62  }
63  *buf++ = *src;
64  } else if (isspace(*src)) {
65  if (*argv[argc]) {
66  buf++, argc++;
67  if (argc == argvAlloced) {
68  argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
69  argv = (const char**)realloc((void*)argv, sizeof(*argv) * argvAlloced);
70  }
71  argv[argc] = buf;
72  }
73  } else switch (*src) {
74  case '"':
75  case '\'':
76  quote = *src;
77  break;
78  case '\\':
79  src++;
80  if (!*src) {
81  free((void*)argv);
82  return POPT_ERROR_BADQUOTE;
83  }
84  /*@fallthrough@*/
85  default:
86  *buf++ = *src;
87  break;
88  }
89  }
90 
91  if (strlen(argv[argc])) {
92  argc++, buf++;
93  }
94 
95  (void) poptDupArgv(argc, argv, argcPtr, argvPtr);
96 
97  free((void*)argv);
98 
99  return 0;
100 }
#define POPT_ERROR_BADQUOTE
Definition: popt.cpp:720
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
Definition: poptparse.cpp:10
#define POPT_ARGV_ARRAY_GROW_DELTA
Definition: poptparse.cpp:8
char * alloca()

References alloca(), POPT_ARGV_ARRAY_GROW_DELTA, POPT_ERROR_BADQUOTE, and poptDupArgv().

Here is the call graph for this function: