|
@@ -1,5 +1,5 @@
|
|
|
|
|
|
- * argv_new.c
|
|
|
+ * argv.c
|
|
|
*/
|
|
|
#include "private.h"
|
|
|
#include "lub/string.h"
|
|
@@ -7,6 +7,7 @@
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <assert.h>
|
|
|
+#include <ctype.h>
|
|
|
|
|
|
|
|
|
static void lub_argv_init(lub_argv_t * this, const char *line, size_t offset)
|
|
@@ -92,3 +93,107 @@ void lub_argv_delete(lub_argv_t * this)
|
|
|
}
|
|
|
|
|
|
|
|
|
+char *lub_argv__get_line(const lub_argv_t * this)
|
|
|
+{
|
|
|
+ int space = 0;
|
|
|
+ const char *p;
|
|
|
+ unsigned i;
|
|
|
+ char *line = NULL;
|
|
|
+
|
|
|
+ for (i = 0; i < this->argc; i++) {
|
|
|
+ if (i != 0)
|
|
|
+ lub_string_cat(&line, " ");
|
|
|
+ space = 0;
|
|
|
+
|
|
|
+ for (p = this->argv[i].arg; *p; p++) {
|
|
|
+ if (isspace(*p)) {
|
|
|
+ space = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (space)
|
|
|
+ lub_string_cat(&line, "\"");
|
|
|
+ lub_string_cat(&line, this->argv[i].arg);
|
|
|
+ if (space)
|
|
|
+ lub_string_cat(&line, "\"");
|
|
|
+ }
|
|
|
+
|
|
|
+ return line;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+char **lub_argv__get_argv(const lub_argv_t * this, const char *argv0)
|
|
|
+{
|
|
|
+ char **result = NULL;
|
|
|
+ unsigned i;
|
|
|
+ unsigned a = 0;
|
|
|
+
|
|
|
+ if (argv0)
|
|
|
+ a = 1;
|
|
|
+
|
|
|
+ result = malloc(sizeof(char *) * (this->argc + 1 + a));
|
|
|
+
|
|
|
+ if (argv0)
|
|
|
+ result[0] = strdup(argv0);
|
|
|
+ for (i = 0; i < this->argc; i++)
|
|
|
+ result[i + a] = strdup(this->argv[i].arg);
|
|
|
+ result[i + a] = NULL;
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void lub_argv__free_argv(char **argv)
|
|
|
+{
|
|
|
+ unsigned i;
|
|
|
+
|
|
|
+ if (!argv)
|
|
|
+ return;
|
|
|
+
|
|
|
+ for (i = 0; argv[i]; i++)
|
|
|
+ free(argv[i]);
|
|
|
+ free(argv);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const char *lub_argv__get_arg(const lub_argv_t *this, unsigned int index)
|
|
|
+{
|
|
|
+ const char *result = NULL;
|
|
|
+
|
|
|
+ if (!this)
|
|
|
+ return NULL;
|
|
|
+ if (this->argc > index)
|
|
|
+ result = this->argv[index].arg;
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+unsigned lub_argv__get_count(const lub_argv_t * this)
|
|
|
+{
|
|
|
+ return this->argc;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+size_t lub_argv__get_offset(const lub_argv_t * this, unsigned index)
|
|
|
+{
|
|
|
+ size_t result = 0;
|
|
|
+
|
|
|
+ if (this->argc > index)
|
|
|
+ result = this->argv[index].offset;
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool_t lub_argv__get_quoted(const lub_argv_t * this, unsigned index)
|
|
|
+{
|
|
|
+ bool_t result = BOOL_FALSE;
|
|
|
+
|
|
|
+ if (this->argc > index)
|
|
|
+ result = this->argv[index].quoted;
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|