aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-09-01 05:37:17 +0000
committer_ov4 <ov4@ov4.org>2026-09-01 05:37:17 +0000
commit30726f56c8c522e81e65055ada52f56d95f68575 (patch)
tree000976c50c1fd26f43154db1af8dbcfd6ec50b8e
parent7d18b6dfe664a3b655e7eed4cc94cd7591506ce1 (diff)
string util
-rw-r--r--.gitignore1
-rw-r--r--include/util/string.h16
-rw-r--r--src/main.cc10
-rw-r--r--src/util/string.cc38
4 files changed, 58 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
index 144667a..9922c48 100644
--- a/.gitignore
+++ b/.gitignore
@@ -69,3 +69,4 @@ Testing/
.cache/
include/test.h
+src/test.cc
diff --git a/include/util/string.h b/include/util/string.h
new file mode 100644
index 0000000..85aae8d
--- /dev/null
+++ b/include/util/string.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <cstring>
+#include <string>
+#include <vector>
+#include <iostream>
+
+#include "global.h"
+
+namespace ov4
+{
+
+void string2argv(const std::vector<std::string> * const s, char **argv);
+void argv2string(std::vector<std::string> *s, const char * const * argv);
+
+}
diff --git a/src/main.cc b/src/main.cc
index 04aca2a..d7719b2 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -18,6 +18,8 @@
#include "ground.h"
#include "eval.h"
#include "path.h"
+#include "util/string.h"
+#include "test.h"
using namespace std;
using namespace ov4;
@@ -43,12 +45,6 @@ handler_t *Signal(int signum, handler_t *handler);
// built-in commands
void builtin_command_quit();
-void test()
-{
- cout << find_cmd("ls") << endl;
- exit(0);
-}
-
/*
* main - The shell's main routine
*/
@@ -59,7 +55,7 @@ You are using ccshell. \n\
This is free software; see the source for copying conditions. \
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \n\
Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl;
- //test();
+ //test(argv);
init();
char c;
char cmdline[MAXLINE];
diff --git a/src/util/string.cc b/src/util/string.cc
new file mode 100644
index 0000000..8c6c86a
--- /dev/null
+++ b/src/util/string.cc
@@ -0,0 +1,38 @@
+#include "util/string.h"
+
+namespace ov4
+{
+
+using namespace std;
+
+void string2argv(const vector<string> * const s, char **argv)
+{
+
+ // pointers [0, size)
+ // nullptr [size]
+ // data [size+1, ...)
+
+ char *p = (char*)(argv + s->size() + 1);
+
+ argv[s->size()] = nullptr;
+
+ for (size_t i = 0; i < s->size(); i++)
+ {
+ strcpy(p, (*s)[i].c_str());
+ argv[i] = p;
+ p += (*s)[i].size() + 1; // for '\0'
+ }
+}
+
+void argv2string(vector<string> *s, const char * const * argv)
+{
+ if (argv == nullptr) return;
+
+ for (size_t i = 0; true; i++)
+ {
+ if (argv[i] == nullptr) return;
+ s->push_back(argv[i]);
+ }
+}
+
+}