convert string to argv in c++ - c++

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();

Related

Include LEVEL into SysLog log file

Is it possibile to show the level (LOG_INFO, LOG_ERROR, ...) information passed to the "syslog" function into each log line?
Thank you!
Antonio
You can write a wrapper function like this
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
int
mysyslog(int __level, const char *const syslogformat, ...)
{
va_list args;
char *format;
size_t length;
char *level;
ssize_t result;
va_start(args,syslogformat);
level = "?";
switch (__level)
{
case LOG_ALERT:
level = "LOG_ALERT";
break;
case LOG_INFO:
level = "LOG_INFO";
break;
case LOG_EMERG:
level = "LOG_EMERG";
break;
case LOG_CRIT:
level = "LOG_CRIT";
break;
case LOG_ERR:
level = "LOG_ERR";
break;
case LOG_WARNING:
level = "LOG_WARNING";
break;
case LOG_NOTICE:
level = "LOG_NOTICE";
break;
case LOG_DEBUG:
level = "LOG_DEBUG";
break;
}
length = strlen(syslogformat) + strlen(level) + 2;
format = malloc(length + 1);
result = snprintf(format, length + 1, "%s: %s", level, syslogformat);
if ((result < 0) || (result >= length + 1))
return -1;
vsyslog(__level, format, args);
va_end(args);
return 0;
}
int main(void)
{
mysyslog(LOG_NOTICE, "This is a log message `%d' with an integer as parameter\n", 100);
return 0;
}
Just use a template for your destination file in which you specifie how your log message will be displayed/written; something like this shows only syslog level and message:
template t_example { template("${LEVEL}" "${MSG}"); };
destination d_example { file("/var/log/example.log" template(t_example) ); };

Heap error in VS2010

I'm trying to implement the program, which recognizes Windows file attributes. I have a code, but sometimes, I receive heap error after passing return in the main block. Thank you for your attention and help!
#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
_TCHAR* getStringAttributes(int value, _TCHAR* str[])
{
DWORD d = GetFileAttributes(str[value]);
_TCHAR* temp;
switch(d)
{
case 2048: temp = L"Compressed\n"; break;
case 32: temp = L"Archive\n"; break;
case 16: temp = L"Directory\n"; break;
case 16384: temp = L"Encrypted\n"; break;
case 2: temp = L"Hidden\n"; break;
case 128: temp = L"Normal\n"; break;
case 1: temp = L"Readonly\n"; break;
case 4: temp = L"System\n"; break;
case 256: temp = L"Temporary\n"; break;
default: temp = L"Error or unsupported attribute\n"; break;
}
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR* attString = new _TCHAR();
char* ynAnswer = new char();
if(argv[1] == NULL)
{
printf("%s\n","You didn't type file path. Try again?[Y/N]");
gets_s(ynAnswer,10);
if(*ynAnswer == 'y' || *ynAnswer == 'Y')
{
printf("%s\n","Type in a path to the file");
argv[1] = new _TCHAR();
_getws(argv[1]);
if(argv[1] != L"")
{
printf("%s","Attribute: ");
attString = getStringAttributes(1,argv);
_tprintf(attString);
printf("%s","for\n");
_tprintf(argv[1]);
}
}
}
else
{
printf("%s","Attribute: ");
attString = getStringAttributes(1,argv);
_tprintf(attString);
}
printf("%s","Goodbye");
getch();
delete[] ynAnswer;
delete[] attString;
return 0;
}
you input whole string into 1 or 2 bytes strings.
These allocations:
_TCHAR* attString = new _TCHAR();
char* ynAnswer = new char();
Allocate only 1 item array.
it should be:
_TCHAR* attString = new _TCHAR[MAX_SIZE];
char* ynAnswer = new char[MAX_SIZE];
When MAX_SIZE must beeb defuned as macro.
even better is to use:
wstring attString ;
string ynAnswer;
wcin>> attString;
cin>> ynAnswer;
More problem is the allocation in the line:
argv[1] = new _TCHAR();
in addition to the previous answered problem, in this line argv[1] may not even exists.
You should input to other buffer.
it can be like this:
wstring argv1;
if(argc <2){
wcin>>argv1;
}else{
argv1=argv[1];
}
now use argv1 instead of argv[1]
One more thing:
You are using _TCHAR as WCHAR.
when you use _TCHAR strings should be decalared as:
_T("some string")
not as:
L"some string".

Escape all special characters in printf()

Is there an easy way to escape all special characters in the printf() function?
The reason why I would like to know how to do this is because I am printing a number of characters which may include special characters such as the null character (\0) and the beep character and I just want to see the contents of the string.
Currently I am using the following code
It works for null characters. What would be the easiest way to escape all special characters?
int length;
char* data = GetData( length ); // Fills the length as reference
for( int i = 0; i < length; i++ )
{
char c = data[ i ];
printf( "%c", ( c == 0 ? '\\0' : data[ i ] ) );
}
First of all, '\\0' is a two-character literal, which should really be a two-character string. As for printing all special characters as escape code, you need some more code:
switch (data[i])
{
case '\0':
printf("\\0");
break;
case '\n':
printf("\\n");
break;
/* Etc. */
default:
/* Now comes the "hard" part, because not all characters here
* are actually printable
*/
if (isprint(data[i]))
printf("%c", data[i]); /* Printable character, print it as usual */
else
printf("\\x%02x", data[i]); /* Non-printable character, print as hex value */
break;
}
Use the isprint library function to determine if the character is printable:
#include <ctype.h>
...
if (isprint(data[i]))
printf(" %c", data[i]); // prints character
else
printf(" %d", data[i]); // prints code value for character
In case code needs to write with no ambiguity, using C syntax:
#include <ctype.h>
#include <string.h>
#include <stdio.h>
void EscapePrint(int ch) {
// Delete or adjust these 2 arrays per code's goals
// All simple-escape-sequence C11 6.4.4.4
static const char *escapev = "\a\b\t\n\v\f\r\"\'\?\\";
static const char *escapec = "abtnvfr\"\'\?\\";
char *p = strchr(escapev, ch);
if (p && *p) {
printf("\\%c", escapec[p - escapev]);
} else if (isprint(ch)) {
fputc(ch, stdout);
} else {
// Use octal as hex is problematic reading back
printf("\\%03o", ch);
}
}
void EscapePrints(const char *data, int length) {
while (length-- > 0) {
EscapePrint((unsigned char) *data++);
}
}
Alternatively, code could
void EscapePrint(char sch) {
int ch = (unsigned char) sch;
...
}
void EscapePrints(const char *data, int length) {
while (length-- > 0) {
EscapePrint(*data++);
}
}
To use a hexadecimal-escape-sequence or a shorten octal-escape-sequence, code needs to insure that the next character does not create ambiguity. That complication does not occur in the above code as it uses 3-digit octal-escape-sequences. Amended code would be something like:
} else {
if ((ch == 0) && (nextch < '0' || nextch > '7')) {
fputs("\\0", stdout);
}
else if (!isxdigit((unsigned char) nextch)) {
printf("\\x%X", ch);
}
else {
// Use octal as hex is problematic reading back
printf("\\%03o", ch);
}
}
#include <stdio.h>
#include <ctype.h>
/* Converts a buffer of specified lenth to
* ASCII representation as it was a C string literal.
* Returns how much bytes from source was processed
* (ideally ret == src_sz)
*/
int binbuf_to_escaped_C_literal(const char *src_buf, size_t src_sz, char *dst_str, size_t dst_sz)
{
const char *src = src_buf;
char *dst = dst_str;
while (src < src_buf + src_sz)
{
if (*src == '\\')
{
*dst++ = '\\';
*dst++ = *src++;
}
else if (isprint(*src))
{
*dst++ = *src++;
}
else
{
switch(*src)
{
case '\n':
*dst++ = '\\';
*dst++ = 'n';
break;
case '\r':
*dst++ = '\\';
*dst++ = 'r';
break;
case '\t':
*dst++ = '\\';
*dst++ = 't';
break;
case '\0':
*dst++ = '\\';
*dst++ = '0';
break;
default:
sprintf(dst, "0x%x", *src);
dst += 4;
}
src++;
}
// next iteration requires up to 5 chars in dst buffer, for ex. "0xab\0"
if (dst > (dst_str + dst_sz - 5)) {
break;
}
}
*dst = '\0';
return src - src_buf;
}
int main(int argc, char **argv)
{
const char binbuf[] = "strange \n\r\t\0\0\0\0\0\\\\ string";
size_t sz = sizeof(binbuf) - 1; // drop trailing nul terminator
char escaped[128];
if (binbuf_to_escaped_C_literal(binbuf, sz, escaped, sizeof(escaped)) != sz) {
fprintf(stderr, "Destination string buffer is too small\n");
return 1;
}
printf("Escaped: %s\n", escaped);
// $ ./escape-binary-buf //
// Escaped: strange \n\r\t\0\0\0\0\0\\\\ string //
return 0;
}

How to (un)escape strings in C/C++?

Given a counted string (either an array of characters, or a wrapper like std::string), is there a "proper" way to escape and/or unescape it in C or C++, such that "special" characters (like the null character) become C-style-escaped and "normal" characters stay the way they are?
Or do I have to do it by hand?
This is a function to process a single character:
/*
** Does not generate hex character constants.
** Always generates triple-digit octal constants.
** Always generates escapes in preference to octal.
** Escape question mark to ensure no trigraphs are generated by repetitive use.
** Handling of 0x80..0xFF is locale-dependent (might be octal, might be literal).
*/
void chr_cstrlit(unsigned char u, char *buffer, size_t buflen)
{
if (buflen < 2)
*buffer = '\0';
else if (isprint(u) && u != '\'' && u != '\"' && u != '\\' && u != '\?')
sprintf(buffer, "%c", u);
else if (buflen < 3)
*buffer = '\0';
else
{
switch (u)
{
case '\a': strcpy(buffer, "\\a"); break;
case '\b': strcpy(buffer, "\\b"); break;
case '\f': strcpy(buffer, "\\f"); break;
case '\n': strcpy(buffer, "\\n"); break;
case '\r': strcpy(buffer, "\\r"); break;
case '\t': strcpy(buffer, "\\t"); break;
case '\v': strcpy(buffer, "\\v"); break;
case '\\': strcpy(buffer, "\\\\"); break;
case '\'': strcpy(buffer, "\\'"); break;
case '\"': strcpy(buffer, "\\\""); break;
case '\?': strcpy(buffer, "\\\?"); break;
default:
if (buflen < 5)
*buffer = '\0';
else
sprintf(buffer, "\\%03o", u);
break;
}
}
}
And this is the code to handle a null-terminated string (using the function above):
void str_cstrlit(const char *str, char *buffer, size_t buflen)
{
unsigned char u;
size_t len;
while ((u = (unsigned char)*str++) != '\0')
{
chr_cstrlit(u, buffer, buflen);
if ((len = strlen(buffer)) == 0)
return;
buffer += len;
buflen -= len;
}
*buffer = '\0';
}
Rather than allocating a new buffer to contain the escaped string I like to escape my string while I write it to a stream.
The following function makes for readable and concise code.
struct Escaped
{
const char* str;
friend inline std::ostream& operator<<(std::ostream& os, const Escaped& e)
{
for (const char* char_p = e.str; *char_p != '\0'; char_p++)
{
switch (*char_p)
{
case '\a': os << "\\a"; break;
case '\b': os << "\\b"; break;
case '\f': os << "\\f"; break;
case '\n': os << "\\n"; break;
case '\r': os << "\\r"; break;
case '\t': os << "\\t"; break;
case '\v': os << "\\v"; break;
case '\\': os << "\\\\"; break;
case '\'': os << "\\'"; break;
case '\"': os << "\\\""; break;
case '\?': os << "\\\?"; break;
default: os << *char_p;
}
}
return os;
}
};
int main()
{
std::cout << Escaped{ "foo\n\tbar" } << std::endl;
}
Produces
foo\n bar
//convert '\n' literal to escape code for '\n'
#define STRING "hello\\\\\nworld\\n"
char *p = malloc(strlen(STRING) + 1);
strcpy(p,STRING);
char *s = p;
char c;
for(;*p;++p)
{
while(*p == '\\')
{
++p;
switch(*p){
case '\\':
c = '\\';
goto gstat;
case 'n':
c = '\n';
default:
{
gstat:
strcpy(p-1,p);
*(p-1) = c;
}
break;
}
}
}
printf("%s",s);

string manipulating in C?

I want to print an array of characters, these characters are underscores first.
Then the user can write characters on these underscores.I used gotoxy() but it doesn't work properly.
That is what i wrote:
int main(void)
{
char arr[20];
int i;
char ch;
clrscr();
for(i=0;i<=20;i++)
{
textattr(0x07);
cprintf("_");
}
do
{
for(i=0;i<=20;i++)
{
//gotoxy(i,0);
//ch = getche();
if( isprint(ch) == 1)
{
arr[i] = ch;
gotoxy(i,0);
//printf("%c",ch);
}
}
} while(i == 20);
getch();
return 0;
}
The first thing is this: You probably don't want to have all those calls to gotoxy, textattr and cprintf in your main function, since that is not what the main function is supposed to do.
It is much more likely that the main function's purpose is "to read some text from the user, presented nicely in an input field". So you should make this a function:
static int
nice_input_field(char *buf, size_t bufsize, int x, int y) {
int i, ch;
gotoxy(x, y);
for (i = 0; i < bufsize - 1; i++) {
cprintf("_");
}
i = 0;
gotoxy(x, y);
while ((ch = readkey()) != EOF) {
switch (ch) {
case '...': /* ... */
break;
case '\b': /* backspace */
cprintf("_");
i--;
gotoxy(x + i, y);
break;
case '\t': /* tabulator */
case '\n': /* enter, return */
buf[i] = '\0';
return 0; /* ok */
default: /* some hopefully printable character */
if (i == bufsize - 1) {
cprintf("\a"); /* beep */
} else {
buf[i++] = ch;
gotoxy(x + i, y);
cprintf("%c", buf[i]);
}
}
}
/* TODO: null-terminate the buffer */
return 0;
}
Printing an array of characters is fairly easy:
char* str = your_array;
while(*str) {
putc(*str++);
}
From memory that should print a string out to the screen.
Your code is very DOS-specific. There is not a good general solution to the problem of reading immediate input in a portable way. It does get asked quite often, so I think the C FAQ broke down and included an answer which you might want to seek out.
That said, I think your bug is that gotoxy(1, 1) is the upper corner of the screen, not 0,0. So you want gotoxy(i, 1)