code cleanup

This commit is contained in:
mateoferon 2021-05-04 09:32:30 +02:00
parent 58b95c45e7
commit bc215fc994
14 changed files with 144 additions and 127 deletions

View file

@ -6,13 +6,13 @@
#include <map>
#include <set>
typedef struct debashify_params {
struct debashify_params {
std::set<std::string> required_fcts;
void require_fct(std::string const& in) { required_fcts.insert(in); }
// map of detected arrays
// bool value: is associative
std::map<std::string,bool> arrays;
} debashify_params;
};
bool r_debashify(_obj* o, debashify_params* params);

View file

@ -1,9 +1,9 @@
#ifndef ERRCODES_H
#define ERRCODES_H
#define ERR_HELP 1001
#define ERR_OPT 1002
#define ERR_PARSE 1003
#define ERR_RUNTIME 1004
#define ERR_HELP 101
#define ERR_OPT 102
#define ERR_PARSE 103
#define ERR_RUNTIME 104
#endif //ERRCODES_H

View file

@ -7,8 +7,6 @@
#include <utility>
#include <vector>
#include <ztd/filedat.hpp>
#define SPACES " \t"
#define SEPARATORS " \t\n"
#define ARG_END " \t\n;#()&|<>"
@ -27,7 +25,7 @@
#define ARRAY_ARG_END " \t\n;#()&|<>]"
// macros
#define PARSE_ERROR(str, i) ztd::format_error(str, "", in, i)
#define PARSE_ERROR(str, i) format_error(str, "", in, i)
// globals

View file

@ -15,9 +15,9 @@ struct lxsh_fct {
std::vector<std::string> depends_on=std::vector<std::string>();
};
extern const std::map<const std::string, const struct lxsh_fct> lxsh_extend_fcts;
extern const std::map<const std::string, const struct lxsh_fct> lxsh_array_fcts;
extern const std::map<const std::string, const struct lxsh_fct> lxsh_allfcts;
extern const std::map<const std::string, const lxsh_fct> lxsh_extend_fcts;
extern const std::map<const std::string, const lxsh_fct> lxsh_array_fcts;
extern const std::map<const std::string, const lxsh_fct> lxsh_allfcts;
void add_lxsh_fcts(shmain* sh, std::set<std::string> fcts);

View file

@ -62,6 +62,31 @@ subarg: can be one of
*/
// exceptions
class format_error : public std::exception
{
public:
//! @brief Conctructor
inline format_error(const std::string& what, const std::string& origin, const std::string& data, int where) { desc=what; index=where; filename=origin; sdat=data; }
//! @brief Error message
inline const char * what () const throw () {return desc.c_str();}
//! @brief Origin of the data, name of imported file, otherwise empty if generated
inline const char * origin() const throw () {return filename.c_str();}
//! @brief Data causing the exception
inline const char * data() const throw () {return sdat.c_str();}
//! @brief Where the error is located in the data
inline const int where () const throw () {return index;}
private:
std::string desc;
int index;
std::string filename;
std::string sdat;
};
// objects
#define AND_OP false
#define OR_OP true

View file

@ -12,8 +12,6 @@
#include <functional>
#include <regex>
#include <ztd/filedat.hpp>
#include "struc.hpp"
extern std::string indenting_string;
@ -149,7 +147,7 @@ int _exec(std::string const& bin, std::vector<std::string> const& args);
std::string stringReplace(std::string subject, const std::string& search, const std::string& replace);
void printFormatError(ztd::format_error const& e, bool print_line=true);
void printFormatError(format_error const& e, bool print_line=true);
void printErrorIndex(const char* in, const int index, const std::string& message, const std::string& origin, bool print_line=true);
int execute(shmain* sh, std::vector<std::string>& args);

View file

@ -51,9 +51,9 @@ std::vector<condlist*> do_resolve_exec(condlist* cmd, std::string const& filenam
// cd back
_cd(dir);
}
catch(ztd::format_error& e)
catch(format_error& e)
{
throw ztd::format_error(e.what(), '`'+p.first+'`', e.data(), e.where());
throw format_error(e.what(), '`'+p.first+'`', e.data(), e.where());
}
return ret;
@ -187,9 +187,9 @@ void parse_exec(FILE* fd, const char* in, uint32_t size, std::string const& file
delete t_lst;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
throw ztd::format_error(e.what(), filename, in, e.where());
throw format_error(e.what(), filename, in, e.where());
}
#endif
}

View file

@ -1,12 +1,13 @@
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <fstream>
#include <ztd/options.hpp>
#include <ztd/shell.hpp>
#include <unistd.h>
#include "util.hpp"
#include "struc.hpp"
#include "parse.hpp"
@ -29,6 +30,7 @@ int main(int argc, char* argv[])
bool optstop=false;
shmain *sh=nullptr, *tsh=nullptr;
try
{
args=options.process(argc, argv, {.stop_on_argument=true, .output_doubledash=true} );
@ -37,48 +39,41 @@ int main(int argc, char* argv[])
optstop=true;
args.erase(args.begin());
}
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
return ERR_OPT;
}
oneshot_opt_process(argv[0]);
// resolve input
std::string file;
if(args.size() > 0) // argument provided
{
if(args[0] == "-" || args[0] == "/dev/stdin") //stdin
oneshot_opt_process(argv[0]);
// resolve input
std::string file;
if(args.size() > 0) // argument provided
{
file = "/dev/stdin";
if(args[0] == "-" || args[0] == "/dev/stdin") //stdin
{
file = "/dev/stdin";
}
else
{
file=args[0];
}
}
else
{
file=args[0];
if(isatty(fileno(stdin))) // stdin is interactive
{
print_help(argv[0]);
return ERR_HELP;
}
else // is piped
{
file = "/dev/stdin";
args.push_back("/dev/stdin");
}
}
}
else
{
if(isatty(fileno(stdin))) // stdin is interactive
{
print_help(argv[0]);
return ERR_HELP;
}
else // is piped
{
file = "/dev/stdin";
args.push_back("/dev/stdin");
}
}
// parsing
// parsing
sh = new shmain(new list);
shmain* sh = new shmain(new list);
shmain* tsh = nullptr;
try
{
bool is_exec = false;
bool first_run = true;
@ -219,7 +214,7 @@ int main(int argc, char* argv[])
}
}
#ifndef NO_PARSE_CATCH
catch(ztd::format_error& e)
catch(format_error& e)
{
if(tsh != nullptr)
delete tsh;
@ -228,11 +223,17 @@ int main(int argc, char* argv[])
return ERR_PARSE;
}
#endif
catch(ztd::option_error& e)
{
std::cerr << e.what() << std::endl;
return ERR_OPT;
}
catch(std::runtime_error& e)
{
if(tsh != nullptr)
delete tsh;
delete sh;
if(sh != nullptr)
delete sh;
std::cerr << e.what() << std::endl;
return ERR_RUNTIME;
}

View file

@ -7,55 +7,49 @@
#include "version.h"
#include "g_version.h"
ztd::option_set options = gen_options();
bool opt_minify=false;
ztd::option_set options( {
ztd::option("\r [Help]"),
ztd::option('h', "help", false, "Display this help message"),
ztd::option("version", false, "Display version"),
ztd::option("help-link-commands", false, "Print help for linker commands"),
ztd::option("help-extend-fcts", false, "Print help for lxsh extension functions"),
ztd::option("\r [Output]"),
ztd::option('o', "output", true , "Output result script to file", "file"),
ztd::option('c', "stdout", false, "Output result script to stdout"),
ztd::option('e', "exec", false, "Directly execute script"),
ztd::option("no-shebang", false, "Don't output shebang"),
ztd::option('J', "json", false, "Output the json structure"),
ztd::option("\r [Processing]"),
ztd::option('m', "minify", false, "Minify code without changing functionality"),
ztd::option('M', "minify-full", false, "Enable all minifying features: -m --minify-quotes --minify-var --minify-fct --remove-unused"),
ztd::option("minify-quotes", false, "Remove unnecessary quotes"),
ztd::option('C', "no-cd", false, "Don't cd when doing %include and %resolve"),
ztd::option('I', "no-include", false, "Don't resolve %include commands"),
ztd::option('R', "no-resolve", false, "Don't resolve %resolve commands"),
ztd::option("no-extend", false, "Don't add lxsh extension functions"),
ztd::option("debashify", false, "Attempt to turn a bash-specific script into a POSIX shell script"),
ztd::option("\r [var/fct processing]"),
ztd::option("minify-var", false, "Minify variable names"),
ztd::option("minify-fct", false, "Minify function names"),
ztd::option("exclude-var", true, "List of matching regex to ignore for variable processing", "list"),
ztd::option("exclude-fct", true, "List of matching regex to ignore for function processing", "list"),
ztd::option("no-exclude-reserved",false, "Don't exclude reserved variables"),
ztd::option("list-var", false, "List all variables set and invoked in the script"),
ztd::option("list-var-def", false, "List all variables set in the script"),
ztd::option("list-var-call", false, "List all variables invoked in the script"),
ztd::option("list-fct", false, "List all functions defined in the script"),
ztd::option("list-cmd", false, "List all commands invoked in the script"),
ztd::option("remove-unused", false, "Remove unused functions and variables"),
ztd::option("unset-var", false, "Add 'unset' to all vars at the start of the script to avoid environment interference")
} );
bool g_cd=false;
bool g_include=true;
bool g_resolve=true;
bool g_shebang=true;
ztd::option_set gen_options()
{
ztd::option_set ret;
ret.add(
ztd::option("\r [Help]"),
ztd::option('h', "help", false, "Display this help message"),
ztd::option("version", false, "Display version"),
ztd::option("help-link-commands", false, "Print help for linker commands"),
ztd::option("help-extend-fcts", false, "Print help for lxsh extension functions"),
ztd::option("\r [Output]"),
ztd::option('o', "output", true , "Output result script to file", "file"),
ztd::option('c', "stdout", false, "Output result script to stdout"),
ztd::option('e', "exec", false, "Directly execute script"),
ztd::option("no-shebang", false, "Don't output shebang"),
ztd::option('J', "json", false, "Output the json structure"),
ztd::option("\r [Processing]"),
ztd::option('m', "minify", false, "Minify code without changing functionality"),
ztd::option('M', "minify-full", false, "Enable all minifying features: -m --minify-quotes --minify-var --minify-fct --remove-unused"),
ztd::option("minify-quotes", false, "Remove unnecessary quotes"),
ztd::option('C', "no-cd", false, "Don't cd when doing %include and %resolve"),
ztd::option('I', "no-include", false, "Don't resolve %include commands"),
ztd::option('R', "no-resolve", false, "Don't resolve %resolve commands"),
ztd::option("no-extend", false, "Don't add lxsh extension functions"),
ztd::option("debashify", false, "Attempt to turn a bash-specific script into a POSIX shell script"),
ztd::option("\r [var/fct processing]"),
ztd::option("minify-var", false, "Minify variable names"),
ztd::option("minify-fct", false, "Minify function names"),
ztd::option("exclude-var", true, "List of matching regex to ignore for variable processing", "list"),
ztd::option("exclude-fct", true, "List of matching regex to ignore for function processing", "list"),
ztd::option("no-exclude-reserved",false, "Don't exclude reserved variables"),
ztd::option("list-var", false, "List all variables set and invoked in the script"),
ztd::option("list-var-def", false, "List all variables set in the script"),
ztd::option("list-var-call", false, "List all variables invoked in the script"),
ztd::option("list-fct", false, "List all functions defined in the script"),
ztd::option("list-cmd", false, "List all commands invoked in the script"),
ztd::option("remove-unused", false, "Remove unused functions and variables"),
ztd::option("unset-var", false, "Add 'unset' to all vars at the start of the script to avoid environment interference")
);
return ret;
}
void get_opts()
{
g_cd=!options['C'].activated;

View file

@ -259,7 +259,7 @@ std::pair<arithmetic*, uint32_t> parse_arithmetic(const char* in, uint32_t size,
}
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -315,7 +315,7 @@ std::pair<variable*, uint32_t> parse_manipulation(const char* in, uint32_t size,
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
if(ret != nullptr) delete ret;
throw e;
@ -482,7 +482,7 @@ std::pair<arg*, uint32_t> parse_arg(const char* in, uint32_t size, uint32_t star
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -624,7 +624,7 @@ std::pair<redirect*, uint32_t> parse_redirect(const char* in, uint32_t size, uin
}
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
if(ret!=nullptr)
delete ret;
@ -732,7 +732,7 @@ std::pair<arglist*, uint32_t> parse_arglist(const char* in, uint32_t size, uint3
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
if(ret != nullptr)
delete ret;
@ -773,7 +773,7 @@ std::pair<pipeline*, uint32_t> parse_pipeline(const char* in, uint32_t size, uin
}
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -829,7 +829,7 @@ std::pair<condlist*, uint32_t> parse_condlist(const char* in, uint32_t size, uin
}
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -882,7 +882,7 @@ std::pair<list*, uint32_t> parse_list_until(const char* in, uint32_t size, uint3
}
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -936,7 +936,7 @@ std::pair<list*, uint32_t> parse_list_until(const char* in, uint32_t size, uint3
g_expecting=old_expect;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1004,7 +1004,7 @@ std::tuple<list*, uint32_t, std::string> parse_list_until(const char* in, uint32
g_expecting=old_expect;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1033,7 +1033,7 @@ std::pair<subshell*, uint32_t> parse_subshell(const char* in, uint32_t size, uin
i++;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1063,7 +1063,7 @@ std::pair<brace*, uint32_t> parse_brace(const char* in, uint32_t size, uint32_t
i++;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1099,7 +1099,7 @@ std::pair<function*, uint32_t> parse_function(const char* in, uint32_t size, uin
i++;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1236,7 +1236,7 @@ std::pair<cmd*, uint32_t> parse_cmd(const char* in, uint32_t size, uint32_t star
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1322,7 +1322,7 @@ std::pair<case_block*, uint32_t> parse_case(const char* in, uint32_t size, uint3
i+=4;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
if(ret != nullptr) delete ret;
throw e;
@ -1377,7 +1377,7 @@ std::pair<if_block*, uint32_t> parse_if(const char* in, uint32_t size, uint32_t
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1434,7 +1434,7 @@ std::pair<for_block*, uint32_t> parse_for(const char* in, uint32_t size, uint32_
i=lp.second;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1469,7 +1469,7 @@ std::pair<while_block*, uint32_t> parse_while(const char* in, uint32_t size, uin
throw PARSE_ERROR("while is empty", i);
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw e;
@ -1595,7 +1595,7 @@ std::pair<block*, uint32_t> parse_block(const char* in, uint32_t size, uint32_t
}
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
if(ret != nullptr) delete ret;
throw e;
@ -1631,10 +1631,10 @@ shmain* parse_text(const char* in, uint32_t size, std::string const& filename)
i=pp.second;
#ifndef NO_PARSE_CATCH
}
catch(ztd::format_error& e)
catch(format_error& e)
{
delete ret;
throw ztd::format_error(e.what(), filename, e.data(), e.where());
throw format_error(e.what(), filename, e.data(), e.where());
}
#endif
return ret;

View file

@ -463,7 +463,7 @@ bool r_do_string_processor(_obj* in)
require_rescan_all();
t->val='\'' + tsh->generate(false, 0) + '\'';
}
catch(ztd::format_error& e) // if fail: skip processing
catch(format_error& e) // if fail: skip processing
{
std::cerr << "Exception caused in string processing LXSH_PARSE_MINIFY\n";
printFormatError(e);

View file

@ -197,9 +197,9 @@ std::vector<condlist*> do_resolve_parse(condlist* cmd, std::string const& filena
// cd back
_cd(dir);
}
catch(ztd::format_error& e)
catch(format_error& e)
{
throw ztd::format_error(e.what(), '`'+p.first+'`', e.data(), e.where());
throw format_error(e.what(), '`'+p.first+'`', e.data(), e.where());
}
return ret;

View file

@ -4,13 +4,13 @@
#include "processing.hpp"
#include "struc_helper.hpp"
const std::map<const std::string, const struct lxsh_fct> lxsh_extend_fcts = {
const std::map<const std::string, const lxsh_fct> lxsh_extend_fcts = {
{ "_lxsh_random", { "[K]", "Generate a random number between 0 and 2^(k*8). Default 2", RANDOM_SH} },
{ "_lxsh_random_string", { "[N]", "Generate a random alphanumeric string of length N. Default 20", RANDOM_STRING_SH} },
{ "_lxsh_random_tmpfile", { "[N]", "Get a random TMP filepath, with N random chars. Default 20", RANDOM_TMPFILE_SH, {"_lxsh_random_string"} } }
};
const std::map<const std::string, const struct lxsh_fct> lxsh_array_fcts = {
const std::map<const std::string, const lxsh_fct> lxsh_array_fcts = {
{ "_lxsh_array_create", { "<VAL...>", "Create an array out of input arguments", ARRAY_CREATE_SH} },
{ "_lxsh_array_get", { "<ARRAY> <I>", "Get value from array", ARRAY_GET_SH} },
{ "_lxsh_array_set", { "<ARRAY> <I> <VAL>", "Set value of array", ARRAY_SET_SH} },
@ -19,7 +19,7 @@ const std::map<const std::string, const struct lxsh_fct> lxsh_array_fcts = {
{ "_lxsh_map_set", { "<MAP> <KEY> <VAL>", "Set value of map", MAP_SET_SH} }
};
std::map<const std::string, const struct lxsh_fct> create_allfcts()
std::map<const std::string, const lxsh_fct> create_allfcts()
{
auto r = lxsh_array_fcts;
for(auto it: lxsh_extend_fcts)
@ -27,7 +27,7 @@ std::map<const std::string, const struct lxsh_fct> create_allfcts()
return r;
}
const std::map<const std::string, const struct lxsh_fct> lxsh_allfcts = create_allfcts();
const std::map<const std::string, const lxsh_fct> lxsh_allfcts = create_allfcts();
void add_lxsh_fcts(shmain* sh, std::set<std::string> fcts)
{

View file

@ -7,6 +7,7 @@
#include <tuple>
#include <iostream>
#include <fstream>
#include <ztd/shell.hpp>
@ -210,7 +211,7 @@ std::string repeatString(std::string const& str, uint32_t n)
return ret;
}
void printFormatError(ztd::format_error const& e, bool print_line)
void printFormatError(format_error const& e, bool print_line)
{
printErrorIndex(e.data(), e.where(), e.what(), e.origin(), print_line);
}