2022-11-03 22:30:21 +03:00
|
|
|
#include "main.h"
|
2022-11-04 03:15:28 +03:00
|
|
|
#include <sys/stat.h>
|
2022-11-02 22:17:51 +03:00
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
extern "C" {
|
2022-11-02 18:25:09 +03:00
|
|
|
|
2022-11-04 03:15:28 +03:00
|
|
|
inline bool path_exists (const std::string& name) {
|
|
|
|
struct stat buffer;
|
|
|
|
return (stat (name.c_str(), &buffer) == 0);
|
2022-11-03 22:30:21 +03:00
|
|
|
}
|
2022-11-02 22:13:01 +03:00
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
CLAPI void* initAug(augSettings settings, int flags) {
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
if(!path_exists(std::string(settings.root))) {
|
|
|
|
std::cout << "error: root path is invalid." << settings.root << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
return aug_init(settings.root, settings.loadPath, flags);
|
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
CLAPI void closeAug(void* aug) {
|
|
|
|
aug_close((augeas*)aug);
|
2022-11-03 22:30:21 +03:00
|
|
|
}
|
2022-11-05 00:10:00 +03:00
|
|
|
|
|
|
|
// Wrapper of aug_preview
|
|
|
|
CLAPI void printPreview(augeas* aug, const char* matchPath, const char* filePath) {
|
|
|
|
|
|
|
|
if(aug == nullptr) {
|
|
|
|
std::cout << "error: augeas is not initialized." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
int r;
|
|
|
|
char *s;
|
|
|
|
char *etc_hosts_fn = nullptr;
|
|
|
|
FILE *hosts_fp = nullptr;
|
|
|
|
char *hosts_txt = nullptr;
|
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
|
|
|
|
r = aug_load_file(aug,filePath);
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
if(r != 0) {
|
|
|
|
std::cout << "Error loading file." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = aug_preview(aug,matchPath,&s);
|
|
|
|
|
|
|
|
if(r != 0) {
|
|
|
|
std::cout << "Failure previewing." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
|
|
|
std::cout << s << std::endl;
|
|
|
|
|
|
|
|
free(hosts_txt);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
// Print tree of the matchPath
|
2022-11-04 03:15:28 +03:00
|
|
|
CLAPI void printAugTree(
|
2022-11-05 00:10:00 +03:00
|
|
|
augeas *aug,
|
2022-11-04 03:15:28 +03:00
|
|
|
const char* matchPath,
|
|
|
|
const char* filePath
|
2022-11-03 22:30:21 +03:00
|
|
|
) {
|
2022-11-05 00:10:00 +03:00
|
|
|
|
|
|
|
if(aug == nullptr) {
|
|
|
|
std::cout << "error: augeas is not initialized." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
int r;
|
2022-11-05 00:10:00 +03:00
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
FILE *out = tmpfile();
|
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
r = aug_load_file(aug, filePath);
|
|
|
|
|
|
|
|
if(r != 0) {
|
|
|
|
std::cout << "error: can't load file." << std::endl;
|
|
|
|
return;
|
2022-11-04 03:15:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
r = aug_print(aug, out,matchPath);
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
if(r != 0) {
|
|
|
|
std::cout << "error: aug_print failure." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
std::map <std::string, std::string> stdBindList;
|
2022-11-04 03:15:28 +03:00
|
|
|
std::map <std::string ,std::string>::iterator pos;
|
2022-11-03 22:30:21 +03:00
|
|
|
|
|
|
|
char line[256];
|
|
|
|
rewind(out);
|
|
|
|
while (fgets(line, 256, out) != nullptr) {
|
|
|
|
// remove end of line
|
|
|
|
line[strlen(line) - 1] = '\0';
|
2022-11-04 03:15:28 +03:00
|
|
|
std::string str_matchPath = matchPath;
|
2022-11-03 22:30:21 +03:00
|
|
|
std::string s = line;
|
|
|
|
// skip comments
|
|
|
|
if (s.find("#comment") != std::string::npos)
|
|
|
|
continue;
|
2022-11-04 03:15:28 +03:00
|
|
|
s = s.substr(str_matchPath.length() - 1);
|
2022-11-03 22:30:21 +03:00
|
|
|
// split by '=' sign
|
|
|
|
size_t eqpos = s.find(" = ");
|
|
|
|
if (eqpos == std::string::npos)
|
|
|
|
continue;
|
|
|
|
// extract key and value
|
|
|
|
std::string key = s.substr(0, eqpos);
|
|
|
|
std::string value = s.substr(eqpos + 3);
|
|
|
|
// remove '"' sign from around value
|
|
|
|
value.erase(value.begin());
|
|
|
|
value.erase(value.end() - 1);
|
|
|
|
stdBindList.insert(std::pair<std::string,std::string>(key,value)); // 2
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
for (pos = stdBindList.begin();pos!=stdBindList.end();pos++)
|
|
|
|
{
|
|
|
|
std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
|
|
|
}
|