1 /* 2 * Copyright (c) 2010, 2011 Ryan Flannery <ryan.flannery@gmail.com> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef STR2ARGV_H 18 #define STR2ARGV_H 19 20 #include <err.h> 21 #include <stdbool.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "debug.h" 27 28 #include "compat.h" 29 30 /* hard limits on the size of an argv and each entry/token w/in an argv */ 31 #define ARGV_MAX_ENTRIES 255 32 #define ARGV_MAX_TOKEN_LEN 255 33 34 /* 35 * Given a string (str), it parses it taking into account escape sequence (\), 36 * quoting, etc., and builds an argc/argv style set of parameters that are 37 * suitable for passing to any of the cmd_* or ecmd_* functions. 38 */ 39 int str2argv(char *str, int *argc, char ***argv, const char **errmsg); 40 41 /* 42 * After the above function is used to build an argc/argv set of parameters, 43 * this function should be used to free() all of the allocated memory. 44 */ 45 void argv_free(int *argc, char ***argv); 46 47 /* 48 * This is used to un-tokenize an argv array. Given argc/argv, this 49 * constructs a string containing all of the tokens in order, with a single 50 * space between each. Tokens with multiple words are quoted. 51 */ 52 char *argv2str(int argc, char *argv[]); 53 54 #endif 55