-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_utils.cpp
More file actions
43 lines (36 loc) · 1.48 KB
/
script_utils.cpp
File metadata and controls
43 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "script_utils.hpp"
#include "utils.hpp"
#include <cstdlib>
#include <fstream>
void createScriptFile(std::ofstream& scriptFile, std::string& scriptPath) {
scriptPath = getHomeDir() + "/.temp/temp_auto_script.sh";
scriptFile.open(scriptPath);
}
void appendBashHistoryT(std::ofstream& scriptFile, const std::string& command) {
// Write the script content
scriptFile << "#!/bin/bash\n";
// Use wrapper to capture command history
scriptFile << "wrapper() {\n";
scriptFile << " text=\"$1\"\n";
scriptFile << " read -e -p \"\" -i \"$text\" edited_text\n";
scriptFile << " echo \"$edited_text\" >> ~/.bash_history\n"; // Append edited command to history
scriptFile << " eval \"$edited_text\"\n"; // Execute the command
scriptFile << "}\n";
scriptFile << "wrapper \"" << command << "\"\n"; // Call the wrapper function
scriptFile << "exit\n"; // Ensure the shell exits cleanly after execution
scriptFile.close();
}
void appendBashHistoryC(std::ofstream& scriptFile, const std::string& command) {
scriptFile << "#!/bin/bash\n";
scriptFile << "echo \"" << command << "\" >> ~/.bash_history\n";
scriptFile << "history -a\n";
scriptFile << "exit\n";
scriptFile.close();
}
void executeAndRemoveScript(const std::string& scriptPath) {
std::string chmodCmd = "chmod +x " + scriptPath;
system(chmodCmd.c_str());
system(scriptPath.c_str());
std::string rmCmd = "rm " + scriptPath;
system(rmCmd.c_str());
}