error append

This commit is contained in:
vrd
2023-10-09 14:04:57 +03:00
parent d7aafbeb71
commit 105d9e5802
5 changed files with 130 additions and 104 deletions
+17 -1
View File
@@ -1,2 +1,18 @@
Library for easy shell like syntax and command execution in C++. Cppipe allows you to write and execute script like C++ programs.
It offers a shell like synthax for seamless command execution.
In this way your scripts can benefit from increased control and speed, while
maintaining a lot of the potential for simple command callsynthax.
INSTALLATION:
In terminal use:
sudo ./install.sh
HOW TO WRITE A CPPIPE PROGRAM:
Just include <cppipe/commands.hpp>
HOW TO EXECUTE A CPPIPE PROGRAM:
In terminal use:
cppipe my_cppipe_program.cpp
+13 -1
View File
@@ -130,7 +130,8 @@ str $(const PendingCmd& cmd)
// check output size (not portable?) // check output size (not portable?)
int pipe_size; int pipe_size;
int rc = ioctl(p.out, FIONREAD, &pipe_size); assert(rc==0); // int rc = ioctl(p.out, FIONREAD, &pipe_size); assert(rc==0);
ioctl(p.out, FIONREAD, &pipe_size);
// write to the string directly, todo: find a better way // write to the string directly, todo: find a better way
str output; str output;
@@ -244,6 +245,16 @@ PendingCmd& operator>=(const PendingCmd& ccmd, fd_t fd)
return cmd; return cmd;
} }
PendingCmd& operator>>=(const PendingCmd& cmd, const char* file)
{
fd_t fd = open_or_die(file, O_WRONLY | O_CREAT | O_APPEND);
return cmd >= fd;
}
PendingCmd& operator>>=(const PendingCmd& cmd, fd_t fd)
{
return cmd >= fd;
}
PendingCmd& operator<(const PendingCmd& cmd, const char* file) PendingCmd& operator<(const PendingCmd& cmd, const char* file)
{ {
fd_t fd = open_or_die(file, O_RDONLY); fd_t fd = open_or_die(file, O_RDONLY);
@@ -253,6 +264,7 @@ PendingCmd& operator<(const PendingCmd& ccmd, fd_t fd)
{ {
auto& cmd = const_cast<PendingCmd&>(ccmd); auto& cmd = const_cast<PendingCmd&>(ccmd);
assert(cmd.in == 0 || !"ERROR: Input is already redirected!"); assert(cmd.in == 0 || !"ERROR: Input is already redirected!");
cmd.in = fd; cmd.in = fd;
return cmd; return cmd;
} }
+7 -4
View File
@@ -125,10 +125,13 @@ PendingCmd& operator>(const PendingCmd&, fd_t);
/* Same but append rather then truncate the file */ /* Same but append rather then truncate the file */
PendingCmd& operator>>(const PendingCmd&, const char* file); PendingCmd& operator>>(const PendingCmd&, const char* file);
PendingCmd& operator>>(const PendingCmd&, fd_t); PendingCmd& operator>>(const PendingCmd&, fd_t);
/* Redirect errors to file */ /* Redirect errors to file */
PendingCmd& operator>=(const PendingCmd&, const char* file); PendingCmd& operator>=(const PendingCmd&, const char* file);
PendingCmd& operator>=(const PendingCmd&, fd_t); PendingCmd& operator>=(const PendingCmd&, fd_t);
/* todo >>= */ /* Same but append rather then truncate the file */
PendingCmd& operator>>=(const PendingCmd&, const char* file);
PendingCmd& operator>>=(const PendingCmd&, fd_t);
/* Shell oprator < use file as input */ /* Shell oprator < use file as input */
PendingCmd& operator<(const PendingCmd&, const char* file); PendingCmd& operator<(const PendingCmd&, const char* file);
@@ -144,7 +147,7 @@ PendingCmd operator&(const PendingCmd&, const Cmd&);
to allow for simpler synthax, speed and safety to allow for simpler synthax, speed and safety
e.g: echo + some_string e.g: echo + some_string
rather then: echo + some_string.c_str() */ rather then: echo + some_string.c_str() */
class str: public std::basic_string<char> /* todo: allocator */ class LeakStr: public std::basic_string<char> /* todo: allocator */
{ {
public: public:
using std::basic_string<char>::basic_string; using std::basic_string<char>::basic_string;
@@ -155,6 +158,6 @@ public:
shell: shell:
var=$(ls) var=$(ls)
becomes: becomes:
string var = $(ls); auto var = $(ls);
*/ */
str $(const PendingCmd&); LeakStr $(const PendingCmd&);
+93 -93
View File
@@ -13,116 +13,116 @@ static bool compile_file(const char* file, path cache, bool debug); // return tr
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
if(argc < 2) if(argc < 2)
{ {
cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n"; cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n";
return 1; return 1;
} }
if(!strcmp(argv[1], "--help")) if(!strcmp(argv[1], "--help"))
{ {
cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n" cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n"
<< "Compile and run C++ source FILE that uses the cppipe library.\n" << "Compile and run C++ source FILE that uses the cppipe library.\n"
"Pass the ARGUMENTS to he compiled binary.\n" "Pass the ARGUMENTS to he compiled binary.\n"
"The binaries are cached and recompiled only if the source is newer.\n" "The binaries are cached and recompiled only if the source is newer.\n"
"-g debug the binary, asserts are also enabled\n"; "-g debug the binary, asserts are also enabled\n";
return 0; return 0;
} }
bool debug = false; bool debug = false;
int file_arg = 1; int file_arg = 1;
if(!strcmp(argv[1], "-g")) if(!strcmp(argv[1], "-g"))
{ {
if(argc < 3) if(argc < 3)
{ {
cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n"; cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n";
return 1; return 1;
} }
debug = true; debug = true;
file_arg = 2; file_arg = 2;
} }
const char* file = argv[file_arg]; const char* file = argv[file_arg];
if( !exists(file) ) if( !exists(file) )
{ {
cerr << "File: " << file << " doesn't exist\n"; cerr << "File: " << file << " doesn't exist\n";
exit(1); exit(1);
} }
path cache( get_cache_path(file) ); // cache bins to avoid recompiles path cache( get_cache_path(file) ); // cache bins to avoid recompiles
if(debug) if(debug)
cache += "_dbg"; cache += "_dbg";
// Compile the passed file // Compile the passed file
if( !compile_file(file, cache, debug) ) if( !compile_file(file, cache, debug) )
return 1; return 1;
// Run the file without forking // Run the file without forking
Cmd run; Cmd run;
if(debug) // debug it with gdb if(debug) // debug it with gdb
{ {
run += "gdb"; run += "gdb";
run += "--args"; run += "--args";
} }
run += cache.c_str(); run += cache.c_str();
for(int i = file_arg; i < argc; ++i) for(int i = file_arg; i < argc; ++i)
run += argv[i]; run += argv[i];
exec(run); exec(run);
} }
static path get_cache_path(const char* file) static path get_cache_path(const char* file)
{ {
path cache; path cache;
if(char* xdg_cache = getenv("XDG_CACHE_HOME")) if(char* xdg_cache = getenv("XDG_CACHE_HOME"))
{ {
cache = xdg_cache; cache = xdg_cache;
cache /= "cppipe"; cache /= "cppipe";
} }
else else
{ {
char* home( getenv("HOME") ); char* home( getenv("HOME") );
cache = home; cache = home;
cache /= ".cache/cppipe"; cache /= ".cache/cppipe";
} }
cache += absolute(file); cache += absolute(file);
create_directories(cache.parent_path()); create_directories(cache.parent_path());
return cache; return cache;
} }
static bool compile_file(const char* file, path cache, bool debug) static bool compile_file(const char* file, path cache, bool debug)
{ {
// Only compile if the source is newer then the bin or we are in debug // Only compile if the source is newer then the bin or we are in debug
error_code ec; error_code ec;
if(last_write_time(file) > last_write_time(cache, ec)) if(last_write_time(file) > last_write_time(cache, ec))
{ {
Cmd compile( Cmd compile(
"g++", "g++",
"-o", cache.c_str(), "-o", cache.c_str(),
"-pipe", "-std=c++17", "-march=native", "-pipe", "-std=c++17", "-march=native",
// "-Wall", "-Wextra", "-Wno-parentheses", // "-Wall", "-Wextra", "-Wno-parentheses",
file, file,
"-lcppipe" "-lcppipe"
); );
if(debug) if(debug)
{ {
compile.append_args({ compile.append_args({
"-g", "-g",
"-Wall", "-Wextra", "-Wno-parentheses" "-Wall", "-Wextra", "-Wno-parentheses"
}); });
} }
else else
{ {
compile.append_args({ compile.append_args({
"-Ofast", "-flto", "-DNDEBUG", "-s" "-Ofast", "-flto", "-DNDEBUG", "-s"
}); });
} }
if( !compile() ) // if failed to compile if( !compile() ) // if failed to compile
return false; return false;
} }
return true; return true;
} }
-5
View File
@@ -3,13 +3,8 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <iostream> #include <iostream>
// tab vs space
// shaded obj? // shaded obj?
// fix warning
/* close files on destruction ?*/ /* close files on destruction ?*/
/* -> for error ? */
// check if processes normal_exit
using namespace std; using namespace std;
int main() int main()
{ {