I'm trying to make a generic function taking a variable argument list. A part of the design is that some of these functions call each other. Unfortunately it doesn't seem to work. As you can see if you run the simple code below, the call to command() always fails, but the direct call to marshal_size() succeeds in decoding the two strings "FIRST_STR_ARG" and "SECOND_STR_ARG" according to the format string "FORMAT_STRING".
What is wrong in my reasoning?
The sample code compiles equally well with "g++ main.cpp" or "gcc main.c".
Thanks,
jules
#include <stdarg.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#define MARSHAL_FORMAT "%s%s"
#define FIRST_STR_ARG "THIS_IS_ARG_ONE"
#define SECOND_STR_ARG "THIS_IS_ARG_TWO"
#define d(msg__, ...) do { printf("%s#%d: "msg__"\n", __FILE__, __LINE__, ## __VA_ARGS__); } while (0)
static uint32_t
marshal_size(const char *format, ...)
{
uint32_t retv = 0;
uint8_t ub;
uint16_t uw;
uint32_t ul;
char *s;
va_list ap;
if (!format || !strlen(format))
return 0;
d("format = %s \n", format);
va_start(ap, format);
for (; '\0' != *format; format++) {
d("*format = %c \n", *format);
if ('%' == *format) {
format++;
if ('u' == *format)
format++;
} else {
d("FORMAT ERROR\n");
continue;
}
d("*format = %c \n", *format);
switch (*format) {
case 's':
s = va_arg(ap, char*);
d("va_arg = %s\n", (s ? s : "NULL"));
if (s)
retv += strlen(s) + 1;
break;
case 'l':
ul = va_arg(ap, uint32_t);
retv += sizeof(uint32_t);
break;
case 'w':
uw = (uint16_t)va_arg(ap, int);
retv += sizeof(uint16_t);
break;
case 'b':
ub = (uint8_t)va_arg(ap, int);
retv += sizeof(uint8_t);
break;
default:
goto exit;
}
continue;
exit:
break;
}
va_end(ap);
return retv;
}
static uint32_t
command(const char * const format,
...)
{
uint32_t retv;
va_list ap;
va_start(ap, format);
retv = marshal_size(format, ap);
va_end(ap);
return retv;
}
int
main(int argc, char *argv)
{
uint32_t size;
size = command(MARSHAL_FORMAT, FIRST_STR_ARG, SECOND_STR_ARG);
d("size = %d", size);
size = marshal_size(MARSHAL_FORMAT, FIRST_STR_ARG, SECOND_STR_ARG);
d("size = %d", size);
return EXIT_SUCCESS;
}
You have to make marshal_size take a va_list rather than .... See Question 15.12 in the FAQ at c-faq.com.
I don't think you can pass a va_list to a function that takes variable arguments. I believe you need another version of marshal_size that takes a va_list as a hard parameter.
I'm a little rusty on the details of how this all works underneath. So I won't attempt an explanation. But as support, I'd suggest this is why we have the vprintf, vfprintf, and vsprintf versions of such functions.
Most likely you could make marshal_size(const char *format, ...) call the marshal_size(const char *format, va_list arg_ptr) to actually perform its functionality, thus not having to duplicate any code. Then command could call the va_list version as well, and everything should work.
The return value of va_start (which is actually a macro) is a pointer to an element on the stack, so the address of something on the stack. If you pass this as argument to another function, you don't pass the stack value, but the pointer to the value on the stack.
To get this working, the command function should use va_arg to get the actual stack value and pass this to the marshal_size function, however this cannot be done if you don't know the type of the value on the stack. In that case, pass ap as va_list argument and change the argument of marshall_size to get a va_list argument and continue the stack value processing there.
As the other answers point out, if you want to "pass through" variable arguments from one function to another, you need to write your function to take an explicit va_list argument. This is quite easy: you just need to rewrite your marshal_size() function as vmarshal_size() that takes a va_list, then turn marshal_size() itself into a wrapper:
static uint32_t
marshal_size(const char *format, ...)
{
va_list ap;
uint32_t retval;
va_start(ap, format);
retval = vmarshal_size(format, ap);
va_end(ap);
return retval;
}
static uint32_t
vmarshal_size(const char *format, va_list ap)
{
uint32_t retv = 0;
uint8_t ub;
uint16_t uw;
uint32_t ul;
char *s;
if (!format || !strlen(format))
return 0;
d("format = %s \n", format);
/* No va_start() */
for (; '\0' != *format; format++) {
d("*format = %c \n", *format);
if ('%' == *format) {
format++;
if ('u' == *format)
format++;
} else {
d("FORMAT ERROR\n");
continue;
}
d("*format = %c \n", *format);
switch (*format) {
case 's':
s = va_arg(ap, char*);
d("va_arg = %s\n", (s ? s : "NULL"));
if (s)
retv += strlen(s) + 1;
break;
case 'l':
ul = va_arg(ap, uint32_t);
retv += sizeof(uint32_t);
break;
case 'w':
uw = (uint16_t)va_arg(ap, int);
retv += sizeof(uint16_t);
break;
case 'b':
ub = (uint8_t)va_arg(ap, int);
retv += sizeof(uint8_t);
break;
default:
goto exit;
}
continue;
exit:
break;
}
/* No va_end() */
return retv;
}
Your command() function can then call vmarshal_size() in the same way (although I note that your command() function is exactly the same as the new marshal_size(), presumably you actually want to do something different there).
Related
I found a way to pass a variable length array in C++. But it fails 'wrap' function in below code. Actually I want to wrap format function in my project.
What am i doing wrong in my code?
test code
#include <iostream>
#include <stdarg.h>
#include <string>
void log(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
int length = _vscprintf(format, argptr);
char* buf_ = new char [length + 1];
int ret = vsnprintf(buf_, 1000, format, argptr);
if (ret >= 0) {
std::cout << buf_ << std::endl;
}
delete[] buf_;
va_end(argptr);
}
void wrap(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log(format, ap);
va_end(ap);
}
int main()
{
log( "direct = %d", 1);
wrap("wrap = %d", 1);
return 0;
}
the result is here.
direct = 1
wrap = 15137088 // what's happen?
I found a way to pass a variable length array in C++
That isn't a variable-length array, and it isn't really idiomatic C++. The ... is a variable-length argument list, and is available in C.
The simplest reasonable way to wrap your log function is the variadic template one, which can simply be written as:
template <typename... Args>
void wrap(const char *format, Args&&... args) {
log(format, std::forward<Args>(args)...);
}
In the log function itself, vsnprintf returns the number of bytes that would have been written, in the event it fills the buffer. So, you can always just call it once with an optimistic buffer size, and grow the buffer if necessary: you don't need the non-standard _vscprintf. That would look something like:
void log(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
static const size_t DefaultSize = 200;
// pick some value that makes sense ^^ here
char buf[DefaultSize];
int rv = vsnprintf(buf, DefaultSize, format, argptr);
if (rv < 0) {
// we can't return errors with this prototype:
// should it throw?
return;
}
if (rv >= DefaultSize) {
vector<char> dynbuf(rv+1);
rv = vsnprintf(&dynbuf[0], dynbuf.size(), format, argptr);
std::cout << &dynbuf[0] << std::endl;
} else {
std::cout << buf << std::endl;
}
va_end(argptr);
}
Note also that wrap knows the types of all its arguments, but that information is discarded when you call the C-style variadic function log. You might consider Boost.Format as a type-safe alternative - as a bonus, it will manage the buffer for you.
Passing a va_list where a variable number of argumets (x, y, z) is expected isn't designed to work.
To achieve what you want, you need to do something like this:
void log_args(const char* format, va_list& argptr)
{
// I'm unsure on this... you may possibly need to make a separate
// copy of the va_list to pass in to each of _vscprintf and vsnprintf.
va_list second;
va_copy(second, argptr);
int length = _vscprintf(format, argptr);
char* buf_ = new char [length + 1];
int ret = vsnprintf(buf_, 1000, format, second);
if (ret >= 0) {
std::cout << buf_ << std::endl;
}
delete[] buf_;
}
void log(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
log_args(argptr);
va_end(argptr);
}
void wrap(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_args(format, ap);
va_end(ap);
}
In this example 'wrap' and 'log' appear the same... but I presume you want to do something additional in your real wrap function otherwise why would you be asking this question.
In c++11, you can use variadic templates
#include <iostream>
void tprintf(const char* format) // base function
{
std::cout << format;
}
template<typename T, typename... Targs>
void tprintf(const char* format, T value, Targs... Fargs) // recursive variadic function
{
for ( ; *format != '\0'; format++ ) {
if ( *format == '%' ) {
std::cout << value;
tprintf(format+1, Fargs...); // recursive call
return;
}
std::cout << *format;
}
}
int main()
{
tprintf("direct = %\n", 1);
tprintf("wrap = %\n", 1);
return 0;
}
I've written a custom print function. My problem is that I need to return a const char* as this has to be used in another function. I simply have no idea how to manage that...
anotherFunction (const char* text /*Here*/, unsigned __int32 value, unsigned __int64 bigVal);
I know the following example/s do/es not work as it should. That's what I've tried so far.
const char* CatchMessage (const char *message, ...)
{
va_list args;
va_start (args, message);
/*?*/
va_end (args);
return message;
}
I've yet only managed to get the correct output in cmd, but I actually need it as return value.
void CatchMessage (const char *message, ...)
{
va_list args;
va_start (args, message);
vfprintf (stdout, message, args);
va_end (args);
}
Call:
CatchMessage ("Some Input %s and %d equals to %d", randString, randNumber, secRandNumber);
Should return:
"Some Input stuff and 12 equals to 6"
I have not been able to find a solution. Any help would be appreciated.
Q: How do I get this CatchMessage function to return the correctly formatted const char* ?
It sounds like CatchMessage should take a pointer to a char buffer (and its size), and vsnprintf() into that buffer.
Since you're using C++ (at least according to the tags on the question) why not just return the string in a std::string ?
The problem with returning a (const) char * is that you have to have a buffer somewhere.
There are several ways to accomplish this:
The caller has to provide that buffer
You have to malloc() it
The function itself as a static buffer, but that would make it non-reentrant - which would be bad for multithreading etc.
Ad 1:
void CatchMessage(char * result, size_t maxlen, const char *message, ...)
{
va_list ap;
va_start(ap, message);
vsnprintf(result, maxlen, message, ap);
va_end(ap);
}
called with
char buffer[500];
CatchMessage(buffer, sizeof buffer, "Some Input %s and %d equals to %d", randString, randNumber, secRandNumber);
anotherfunction(buffer, ...)
ad 2:
char * CatchMessage(const char *message, ...)
{
size_t size = 500;
char * result = malloc(size);
if (!result) return NULL; // error handling!
while (1) {
va_list ap;
va_start(ap, message);
size_t used = vsnprintf(result, size, message, ap);
va_end(ap);
char * newptr = realloc(result, size);
if (!newptr) { // error
free(result);
return NULL;
}
result = newptr;
if (used <= size) break;
size = used;
}
return result;
}
called with
char * buffer = CatchMessage(buffer, sizeof buffer, "Some Input %s and %d equals to %d", randString, randNumber, secRandNumber);
if (!buffer) { /* error handling: no memory! */ }
anotherfunction(buffer, ...)
free(buffer); // important for avoiding memory leaks
ad 3:
char * CatchMessage(const char *message, ...)
{
static char result[500]; // static is important here! Otherwise the memory will be freed immediately after returning.
va_list ap;
va_start(ap, message);
vsnprintf(result, sizeof result, message, ap);
va_end(ap);
return result;
}
called with
char * buffer = CatchMessage(buffer, sizeof buffer, "Some Input %s and %d equals to %d", randString, randNumber, secRandNumber);
anotherfunction(buffer, ...)
There is no other option, especially not defining
char result[500];
in the function and then returning it: this array lives on the stack and is freed immediately after return. It cannot be safely accessed by the caller; its contents are just undefined.
If you do not care abour re-entrancy, you could return pointer to static buffer:
#define MESSAGE_MAX 1024
const char *
CatchMessage (const char *message, ...)
{
static buffer[MESSAGE_MAX];
va_list args;
va_start (args, message);
vsnprintf (buffer, MESSAGE_MAX, message, args);
va_end (args);
return buffer;
}
Notes:
This implementation is not thread-safe. If you care about thread safety, use thread local storage instead of static buffer
This implementation has hardcoded upper limit on message length. If that's not desirable, and your compiler is C99 compliant, you could call first vsprintf with NULL as first argument to know result string length, then allocate buffer of that side.
In C++, the use of variadic arguments is bad because there is a certain emphasis on type safety (and memory safety). So, you may want to propose a version that is actually type safe (and yes, that is possible), and yet proposes a similar interface:
template <typename H, typename... Args>
void format(std::ostream& out,
char const* format,
size_t len,
H const& head,
Args const&... args);
// Variations with 'char const (&)[N]' and 'std::string' formats
// as well as variations returning directly a 'std::string'.
Now, the implementation of format is not too difficult; especially without support for positional arguments. A simplistic one can be found below:
inline void format_string(std::ostream& out, char const* const* c) { out << *c; }
inline void format_string(std::ostream& out, std::string const* s) { out << *s; }
inline void format_string(std::ostream& out, void const*); // will throw
template <typename Integral, typename = enable_integral<Integral>::type>
inline void format_integral(std::ostream& out, Integral const* i) { out << *i; }
inline void format_integral(std::ostream& out, void const*); // will throw
inline size_t format_consume(std::ostream& out,
char const* const format,
size_t const length)
{
char const* end = format + length;
char const* current = format;
do {
// 1. Find first "format identifier", output stuff in-between
char const* perc = std::find(current, end, '%');
if (perc != current) { out.write(current, perc - current); }
current = perc;
// 2. %% is % escaped by %, so output it directly
while (*current == '%' and *(current + 1) == '%') {
out.put('%');
current += 2;
}
} while (current != end and *current != '%');
// 3. Return number of characters of format parameter consumed
return current - format;
} // format_consume
inline void format(std::ostream& out, char const* format, size_t len) {
size_t const consumed = format_consume(out, format, len);
if (consumed != len) { throw std::runtime_exception("Missing arguments"); }
} // format
template <typename H, typename typename... Args>
void format(std::ostream& out,
char const* format,
size_t len,
H const& head,
Args const&... args)
{
size_t const consumed = format_consume(out, format, len);
if (consumed == len) { throw std::runtime_exception("Extraneous arguments"); }
format += consumed;
len -= consumed;
assert(*format == '%');
switch(*(format+1)) {
case 's': format_string(out, &head); break;
case 'd': format_integral(out, &head); break;
default: throw std::runtime_exception("Invalid specifier");
}
format(out, format+2, len-2, args...);
} // format
In the general case, it is slightly more hairy, as you need to parse the modifiers, etc... but then, for a production-ready implementation, I advise a look at Boost.Format.
char* CatchMessage (size_t size, const char *message, ...)
{
char result[size];
va_list args;
va_start (args, message);
vsprintf (result, message, args);
va_end (args);
return result;
}
See this for reference.
And for glglgl: If you want to use the clean way, use vsnprintf(result, size, message, args)
if I have this function:
printAll(const char *message, ...)
{
va_list argptr = NULL;
va_start(argptr, message);
// todo: how to printf all the arguments in the message?
va_end(argptr);
}
Suppose I call the function like this:
printAll("My info: Value1 = %d, Value 2=%d", 1, 2);
In this line: // todo: how to printf all the arguments in the message?
How can I print them all in order to have:
My info: Value1 = 1, Value 2=2
You're looking for the vprintf() function which was designed to do exactly this:
vprintf(message, argptr);
The v*printf() family of functions work basically in the same way as their normal counterparts, except they take a va_list instead of varargs. They don't call va_end() for you, so the way you have it now is correct.
Here's a sketch of the general idea (though a finished version, has to deal with quite a few more things such as field width, precision, more conversions, etc.
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
int my_vfprintf(FILE *file, char const *fmt, va_list arg) {
int int_temp;
char char_temp;
char *string_temp;
char ch;
int length = 0;
char buffer[512];
while ( ch = *fmt++) {
if ( '%' == ch ) {
switch (ch = *fmt++) {
/* %% - print out a single % */
case '%':
fputc('%', file);
length++;
break;
/* %c: print out a character */
case 'c':
char_temp = va_arg(arg, int);
fputc(char_temp, file);
length++;
break;
/* %s: print out a string */
case 's':
string_temp = va_arg(arg, char *);
fputs(string_temp, file);
length += strlen(string_temp);
break;
/* %d: print out an int */
case 'd':
int_temp = va_arg(arg, int);
itoa(int_temp, buffer, 10);
fputs(buffer, file);
length += strlen(buffer);
break;
/* %x: print out an int in hex */
case 'x':
int_temp = va_arg(arg, int);
itoa(int_temp, buffer, 16);
fputs(buffer, file);
length += strlen(buffer);
break;
}
}
else {
putc(ch, file);
length++;
}
}
return length;
}
int my_printf(char const *fmt, ...) {
va_list arg;
int length;
va_start(arg, fmt);
length = my_vfprintf(stdout, fmt, arg);
va_end(arg);
return length;
}
int my_fprintf(FILE *file, char const *fmt, ...) {
va_list arg;
int length;
va_start(arg, fmt);
length = my_vfprintf(file, fmt, arg);
va_end(arg);
return length;
}
#ifdef TEST
int main() {
my_printf("%s", "Some string");
return 0;
}
#endif
Here is a printAll() that will do exactly as you would like...
#ifdef __cplusplus
#include <cstring>
#include <cstdio>
#include <cstdarg>
using namespace std;
#else
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#endif
void printAll(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
const size_t MAX_LEN = 1023;
#ifdef __cplusplus
char msg[MAX_LEN+1] = {0};
#else
char msg[MAX_LEN+1];
for (int i = 0; i < MAX_LEN+1; i++)
{
msg[i] = 0;
}
#endif
vsnprintf(msg, MAX_LEN, fmt, ap);
printf("%s\n", msg);
va_end(ap);
}
int main() {
printAll("My info: Value1 = %d, Value 2=%d", 1, 2);
return 0;
}
My info: Value1 = 1, Value 2=2
I have this method (by using code from vsnprintf man page):
MYSQL_RES *nsDatabase::queryf(const char *fmt,...){
int n, size = 1024;
char *query=NULL,*np;
va_list ap;
if (this->dbLink == NULL){
return NULL;
}
query = (char *) malloc (size);
if (query == NULL) return NULL;//memory error
while (1) {
va_start(ap, fmt);
n=vsnprintf(query,size,fmt,ap);
va_end(ap);
if (n > -1 && n < size){// format string succeeded
break;
}
if (n > -1) /* glibc 2.1 */
size = n+1; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2; /* twice the old size */
np = (char *) realloc (query, size);
if (np == NULL) {
printf("memory error\n");
FREE(query);
return NULL;//again memory error
} else {
query = np;
}
}
MYSQL_RES *r = this->query(query);
FREE(query);
if(r == NULL){
return NULL; //mysql error
}
return mysql_store_result(this->dbLink);
}
I want to make general function queryFormat which will calculate the size needed for formatted string, allocate memory, print format in it and return the string (not real code, pseudo code):
char *queryFormat(const char *fmt, va_list ap){
// allocate memory
// problem - can I use vsnprintf multiple times here (to determine the size of formatted string and allocate memory)
return <formatted string>;
}
Then I need to call it from all functions that format queries (not real code):
queryRow(const char *fmt,...){
va_start(ap, fmt);
// I need to call vsnprintf many times in queryFormat ... Do I need to call va_start() before every call to fsnprintf ?
char * formattedQuery = queryFormat(query, size, fmt, ap);
va_end(ap);
MYSQL_RES *r = this->query(formattedQuery);
free(formattedQuery);
...
}
I write for GCC under linux, but the code should be runnable under MinGW and CygWin.
See this - http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html.There is a good example in the book of Kernighan and Ritchie book.
EDIT
Its the same in C++ too . you can have a look at this too. - http://www.cplusplus.com/reference/clibrary/cstdarg/va_start/
EDIT 2
http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/. I think this shall help you.
I believe this should help anyone who googled here in search of solution to the problem of how to "reuse" a va_list in a function that had it already passed as parameter:
// Incorrect:
/*void g(int foo, va_list ap) {
vprintf("%s %s %s\n", ap);
vprintf("%s %s %s\n", ap);
}*/
void g(int foo, va_list ap) {
va_list copy;
va_copy(copy, ap);
vprintf("%s %s %s\n", copy);
va_copy(copy, ap);
vprintf("%s %s %s\n", copy);
}
void f(int foo, ...) {
va_list ap;
va_start(ap, foo);
g(foo, ap);
va_end(ap);
}
int main() {
f(42, "aaa", "bbb", "ccc");
return 0;
}
I have an std::string containing a command to be executed with execv, what is the best "C++" way to convert it to the "char *argv[]" that is required by the second parameter of execv()?
To clarify:
std::string cmd = "mycommand arg1 arg2";
char *cmd_argv[];
StrToArgv(cmd, cmd_argv); // how do I write this function?
execv(cmd_argv[0], cmd_argv);
Very non-unixy answers here. What's wrong with:
std::string cmd = "echo hello world";
execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), NULL);
Why bother writing a command line parser when there's a perfectly good one already on the system?
(Note: one good reason is because you don't trust the string you're about to execute. One hopes that this is already true, but the shell will do "more" with that string than a naive whitespace-splitter will and thus open more security holes if you aren't careful.)
std::vector<char *> args;
std::istringstream iss(cmd);
std::string token;
while(iss >> token) {
char *arg = new char[token.size() + 1];
copy(token.begin(), token.end(), arg);
arg[token.size()] = '\0';
args.push_back(arg);
}
args.push_back(0);
// now exec with &args[0], and then:
for(size_t i = 0; i < args.size(); i++)
delete[] args[i];
Of course, this won't work with commans that use quoting like rm "a file.mp3". You can consider the POSIX function wordexp which cares about that and much more.
Perhaps split_winmain from Boost.ProgramOptions. Boost is a good choice in most cases.
http://www.boost.org/doc/libs/1_40_0/doc/html/program_options/howto.html#id1396212
If you are only interested in Windows (other kernels generally don't know about command lines in the Windows sense), you can use the API function CommandLineToArgvW which uses the same conventions as the MS C runtime.
In general it depends on the quoting style of the platform and/or the shell. The Microsoft C Runtime uses a quite different style than e.g. bash!
A combination of the c_str() string method and strtok() to split it up by spaces should get you the array of strings you need to pass to exec() and its related functions.
OK, I've been stumbling over this myself enough times. This is straight "C", so it can be plugged into either C or C++. It treats single and double quote strings differently. The caller is responsible for deallocating argv[0] (if not NULL) and argv.
#include
#include
#include
#include
typedef enum {
STR2AV_OK = 0,
STR2AV_UNBALANCED_QUOTE
} str_to_argv_err_t;
#ifndef NUL
#define NUL '\0'
#endif
static char const nomem[] = "no memory for %d byte allocation\n";
static str_to_argv_err_t
copy_raw_string(char ** dest_p, char ** src_p);
static str_to_argv_err_t
copy_cooked_string(char ** dest_p, char ** src_p);
static inline void *
Xmalloc(size_t sz)
{
void * res = malloc(sz);
if (res == NULL) {
fprintf(stderr, nomem, sz);
exit(EXIT_FAILURE);
}
return res;
}
static inline void *
Xrealloc(void * ptr, size_t sz)
{
void * res = realloc(ptr, sz);
if (res == NULL) {
fprintf(stderr, nomem, sz);
exit(EXIT_FAILURE);
}
return res;
}
str_to_argv_err_t
string_to_argv(char const * str, int * argc_p, char *** argv_p)
{
int argc = 0;
int act = 10;
char ** res = Xmalloc(sizeof(char *) * 10);
char ** argv = res;
char * scan;
char * dest;
str_to_argv_err_t err;
while (isspace((unsigned char)*str)) str++;
str = scan = strdup(str);
for (;;) {
while (isspace((unsigned char)*scan)) scan++;
if (*scan == NUL)
break;
if (++argc >= act) {
act += act / 2;
res = Xrealloc(res, act * sizeof(char *));
argv = res + (argc - 1);
}
*(argv++) = dest = scan;
for (;;) {
char ch = *(scan++);
switch (ch) {
case NUL:
goto done;
case '\\':
if ( (*(dest++) = *(scan++)) == NUL)
goto done;
break;
case '\'':
err = copy_raw_string(&dest, &scan);
if (err != STR2AV_OK)
goto error_leave;
break;
case '"':
err = copy_cooked_string(&dest, &scan);
if (err != STR2AV_OK)
goto error_leave;
break;
case ' ':
case '\t':
case '\n':
case '\f':
case '\r':
case '\v':
case '\b':
goto token_done;
default:
*(dest++) = ch;
}
}
token_done:
*dest = NUL;
}
done:
*argv_p = res;
*argc_p = argc;
*argv = NULL;
if (argc == 0)
free((void *)str);
return STR2AV_OK;
error_leave:
free(res);
free((void *)str);
return err;
}
static str_to_argv_err_t
copy_raw_string(char ** dest_p, char ** src_p)
{
for (;;) {
char ch = *((*src_p)++);
switch (ch) {
case NUL: return STR2AV_UNBALANCED_QUOTE;
case '\'':
*(*dest_p) = NUL;
return STR2AV_OK;
case '\\':
ch = *((*src_p)++);
switch (ch) {
case NUL:
return STR2AV_UNBALANCED_QUOTE;
default:
/*
* unknown/invalid escape. Copy escape character.
*/
*((*dest_p)++) = '\\';
break;
case '\\':
case '\'':
break;
}
/* FALLTHROUGH */
default:
*((*dest_p)++) = ch;
break;
}
}
}
static char
escape_convt(char ** src_p)
{
char ch = *((*src_p)++);
/*
* Escape character is always eaten. The next character is sometimes
* treated specially.
*/
switch (ch) {
case 'a': ch = '\a'; break;
case 'b': ch = '\b'; break;
case 't': ch = '\t'; break;
case 'n': ch = '\n'; break;
case 'v': ch = '\v'; break;
case 'f': ch = '\f'; break;
case 'r': ch = '\r'; break;
}
return ch;
}
static str_to_argv_err_t
copy_cooked_string(char ** dest_p, char ** src_p)
{
for (;;) {
char ch = *((*src_p)++);
switch (ch) {
case NUL: return STR2AV_UNBALANCED_QUOTE;
case '"':
*(*dest_p) = NUL;
return STR2AV_OK;
case '\\':
ch = escape_convt(src_p);
if (ch == NUL)
return STR2AV_UNBALANCED_QUOTE;
/* FALLTHROUGH */
default:
*((*dest_p)++) = ch;
break;
}
}
}
This is a variation on litb's answer, but without all the manual memory allocation. It still won't handle quoting.
#include <vector>
#include <string>
#include <sstream>
std::string cmd = "mycommand arg1 arg2";
std::istringstream ss(cmd);
std::string arg;
std::list<std::string> ls;
std::vector<char*> v;
while (ss >> arg)
{
ls.push_back(arg);
v.push_back(const_cast<char*>(ls.back().c_str()));
}
v.push_back(0); // need terminating null pointer
execv(v[0], &v[0]);
I feel kind of dirty about the const_cast<>, but programs really shouldn't be modifying the contents of the argv strings.
You can use the c_str() function of std::string to convert to char*.
The strtok function will split the string using the ' ' delimiter.
Matt Peitrek's LIBTINYC has a module called argcargv.cpp that takes a string and parses it out to the argument array taking quoted arguments into account. Note that it's Windows-specific, but it's pretty simple so should be easy to move to whatever platform you want.
If you do that, also change it to take as parameters the loaction to put the count and the a pointer to the argv array instead of using externs (just my little bit of advice). Matt didn't need that because LIBTINYC was the runtime.
Alternatively, you can look in your compiler's runtime source (nearly all provide it) to see what they do to parse the commandline and either call that directly (if that turns out to be workable) or borrow the ideas from that bit of code.
May be it is too late to answer on this question but you could use standart POSIX functions glob or wordexp:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
int
main(int argc, char **argv)
{
wordexp_t p;
char *exec_path = "/bin/ls";
p.we_offs = 1;
wordexp("-l -t /etc", &p, WRDE_DOOFFS);
p.we_wordv[ 0 ] = exec_path;
execv(exec_path, p.we_wordv);
/* This code is unreachable */
exit(EXIT_SUCCESS);
}
It would prepare 3 parameters: -l (long listing format), -t (sort by modification time) and directory /etc to list, and run /bin/ls. Call wordexp() gives you exactly the same result as call /bin/sh -c recomended previously but spawaned process would have parent process not /bin/sh.
As it turned out a function exist somewhat hidden in boost program options for this.
The split_unix() function works with escaped and quoted command lines.
#include "boost/program_options/parsers.hpp"
auto parts = boost::program_options::split_unix(commandLine);
std::vector<char*> cstrings ;
for(auto& str : parts){
cstrings.push_back(const_cast<char*> (str.c_str()));
}
int argc = (int)cstrings.size();
char** argv = cstrings.data();