lcpex rewrite - alpha

This commit is contained in:
phanes
2023-02-15 18:44:21 -05:00
parent bb85754dc0
commit 7ed6e13fa5
82 changed files with 4510 additions and 2592 deletions

View File

@@ -0,0 +1,24 @@
#include "string_expansion.h"
// convert a string to a char** representing our artificial argv to be consumed by execvp
char ** expand_env(const std::string& var, int flags )
{
std::vector<std::string> vars;
wordexp_t p;
if(!wordexp(var.c_str(), &p, flags))
{
if(p.we_wordc)
for(char** exp = p.we_wordv; *exp; ++exp)
vars.push_back(exp[0]);
wordfree(&p);
}
char ** arr = new char*[vars.size() + 1];
for(size_t i = 0; i < vars.size(); i++){
arr[i] = new char[vars[i].size() + 1];
strcpy(arr[i], vars[i].c_str());
}
arr[vars.size()] = (char * ) nullptr;
return arr;
}

View File

@@ -0,0 +1,29 @@
#ifndef LCPEX_STRING_EXPANSION_H
#define LCPEX_STRING_EXPANSION_H
#include <wordexp.h>
#include <vector>
#include <cstring>
#include <iostream>
/**
* @brief Convert a string to a 'char**' representing an argument list
*
* This function takes a string 'var' and converts it into a 'char**'
* representing an argument list. The argument list can be consumed by
* functions like 'execvp'.
*
* @param[in] var The string to be converted
* @param[in] flags Flags controlling the behavior of the conversion
*
* @return A 'char**' representing the argument list
*
* The function uses the 'wordexp' function to perform the conversion. The
* 'flags' argument controls the behavior of the 'wordexp' function. The
* returned 'char**' is dynamically allocated, and must be freed by the
* caller using 'delete[]'.
*/
char ** expand_env(const std::string& var, int flags = 0);
#endif //LCPEX_STRING_EXPANSION_H