From b64a17d77f8d8f6013b7936f4a1d7012969e6c87 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Thu, 3 Nov 2022 19:30:21 +0000 Subject: [PATCH] Interop working --- AugFuncs.h | 105 --------------------------------- AugManaged.h | 30 ---------- AugSettings.h | 8 --- CMakeLists.txt | 16 ++++- include/AugSettings.cpp | 0 include/AugSettings.h | 4 ++ main.cpp | 126 ++++++++++++++++++++++++++++++++++------ main.h | 22 +++++++ 8 files changed, 147 insertions(+), 164 deletions(-) delete mode 100644 AugFuncs.h delete mode 100644 AugManaged.h delete mode 100644 AugSettings.h create mode 100644 include/AugSettings.cpp create mode 100644 include/AugSettings.h create mode 100644 main.h diff --git a/AugFuncs.h b/AugFuncs.h deleted file mode 100644 index fff082c..0000000 --- a/AugFuncs.h +++ /dev/null @@ -1,105 +0,0 @@ -#include "AugSettings.h" -#include "augeas.h" -#include "map" - -#ifndef BLUEWEST_AUGEAS_BLUEAUG_H -#define BLUEWEST_AUGEAS_BLUEAUG_H - -class AugFuncs { - - static void testSource(const AugSettings& settings) { - int r; - struct augeas *aug; - char *s; - - aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); - - r = aug_load_file(aug, "/etc/hosts"); - r = aug_source(aug, "/files/etc/hosts/1/ipaddr", nullptr); - - std::cout << "The value read from node: " << s << std::endl; - - aug_close(aug); - } - - -/// Get the tree of the /etc/hosts file printed in the console -/// Implementation based on https://github.com/Nexenta/node-augeas/blob/756276c432cc9e506836800ac9d217bedbbef55c/libaugeas.cc#L784 - -public: -// This prints the actual file - void printPreview(const AugSettings& settings, const std::string& matchPath, const std::string& filePath) { - struct augeas *aug; - int r; - char *s; - char *etc_hosts_fn = nullptr; - FILE *hosts_fp = nullptr; - char *hosts_txt = nullptr; - - aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); - - r = aug_load_file(aug, filePath.c_str()); - r = aug_preview(aug, matchPath.c_str(), &s); - - std::cout << s << std::endl; - - free(hosts_txt); - free(s); - aug_close(aug); - } - - void printAugTree( - const AugSettings& settings, - const std::string& matchPath, - const std::string& filePath - ) { - struct augeas *aug; - int r; - FILE *out = tmpfile(); - - aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); - - r = aug_load_file(aug, filePath.c_str()); - r = aug_print(aug, out,matchPath.c_str()); - - std::map stdBindList; - std::map ::iterator pos; - - char line[256]; - rewind(out); - while (fgets(line, 256, out) != nullptr) { - // remove end of line - line[strlen(line) - 1] = '\0'; - std::string s = line; - - // skip comments - if (s.find("#comment") != std::string::npos) - continue; - s = s.substr(matchPath.length() - 1); - // 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(key,value)); // 2 - - } - - fclose(out); - - for (pos = stdBindList.begin();pos!=stdBindList.end();pos++) - { - std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; - } - - aug_close(aug); - } -}; - - -#endif //BLUEWEST_AUGEAS_BLUEAUG_H diff --git a/AugManaged.h b/AugManaged.h deleted file mode 100644 index 74ce297..0000000 --- a/AugManaged.h +++ /dev/null @@ -1,30 +0,0 @@ -#include "AugFuncs.h" -#include - -#ifndef BLUEWEST_AUGEAS_MANAGEDAUG_H -#define BLUEWEST_AUGEAS_MANAGEDAUG_H - - - // Managed type to expose the public library to other languages - - ref class AugManaged -{ - AugFuncs* NativePtr; - -public: - AugManaged() : NativePtr(new AugFuncs()) {} - ~AugManaged() { delete NativePtr; } - - void TestAugTree() - { - auto testSettings = AugSettings { - .root = "../root", - .loadPath = "/opt/homebrew/share/augeas/lenses/dist", - }; - - NativePtr->printPreview(testSettings, "/files/etc/hosts/1", "/etc/hosts"); - } -}; - - -#endif //BLUEWEST_AUGEAS_MANAGEDAUG_H diff --git a/AugSettings.h b/AugSettings.h deleted file mode 100644 index 00c031f..0000000 --- a/AugSettings.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef BLUEWEST_AUGEAS_AUGSETTINGS_H -#define BLUEWEST_AUGEAS_AUGSETTINGS_H - -struct AugSettings { - const char *root; - const char *loadPath; -}; -#endif //BLUEWEST_AUGEAS_AUGSETTINGS_H diff --git a/CMakeLists.txt b/CMakeLists.txt index c6969f0..e8305a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,22 @@ cmake_minimum_required(VERSION 3.21) -project(BlueWest.Augeas) +project(BlueAug VERSION 1.0.1 DESCRIPTION "BlueAug description") + include_directories(/opt/homebrew/opt/augeas/include/) # Headers para auto-complete? link_directories(/opt/homebrew/opt/augeas/lib) + # Os binarios com as libraries, como é mac .dylib, linux: .SO, etc # tive que usar HOMEBREW_NO_INSTALL_CLEANUP=1 "brew install augeas --build-from-source" # para ele armazenar o codigo compilado. set(CMAKE_CXX_STANDARD 14) +set(GCC_COVERAGE_COMPILE_FLAGS "-fdeclspec") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") + + +add_library(BlueAug SHARED include/AugSettings.cpp main.cpp main.h) + +target_link_libraries(BlueAug augeas) # Se não ele não sabe que é para compilar para ARM64 + + +#add_executable(BlueAug main.cpp AugFuncs.h AugManaged.h AugSettings.h) -add_executable(BlueWest.Augeas main.cpp AugFuncs.h AugManaged.h AugSettings.h) -target_link_libraries(BlueWest.Augeas augeas) # Se não ele não sabe que é para compilar para ARM64 diff --git a/include/AugSettings.cpp b/include/AugSettings.cpp new file mode 100644 index 0000000..e69de29 diff --git a/include/AugSettings.h b/include/AugSettings.h new file mode 100644 index 0000000..cd53d0c --- /dev/null +++ b/include/AugSettings.h @@ -0,0 +1,4 @@ +struct AugSettings { + const char *root; + const char *loadPath; +}; diff --git a/main.cpp b/main.cpp index 067660d..e5ac083 100644 --- a/main.cpp +++ b/main.cpp @@ -1,27 +1,117 @@ -#include -#include "AugSettings.h" -#include "AugFuncs.h" +#include "iostream" +#include "augeas.h" +#include "include/AugSettings.h" +#include "map" +#include "main.h" -// Testing and training using augeas with C++ -// https://github.com/hercules-team/augeas/blob/master/tests/test-api.c +extern "C" { + +RMDEF int32_t getFour() { + + return 4; + +} + +// Testing interop +RMDEF int32_t getThree() { + + return 3333; + +} + +// Testing interop +RMDEF void printStringExample(char* someString) { + std::cout << someString << std::endl; +} -int main(int argc, char *argv[]) { +RMDEF void testSource(const AugSettings& settings) { + int r; + struct augeas *aug; + char *s; - //testGet(); + aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); - auto testSettings = AugSettings { - .root = "../root", - .loadPath = "/opt/homebrew/share/augeas/lenses/dist", - }; + r = aug_load_file(aug, "/etc/hosts"); + r = aug_source(aug, "/files/etc/hosts/1/ipaddr", nullptr); - auto instance = AugFuncs(); + std::cout << "The value read from node: " << s << std::endl; - instance.printPreview(testSettings, "/files/etc/hosts/1", "/etc/hosts"); - //printPreview("/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config"); + aug_close(aug); +} +// This prints the actual file +RMDEF void printPreview(const AugSettings& settings, const std::string& matchPath, const std::string& filePath) { + struct augeas *aug; + int r; + char *s; + char *etc_hosts_fn = nullptr; + FILE *hosts_fp = nullptr; + char *hosts_txt = nullptr; - instance.printAugTree(testSettings, "/files/etc/hosts/*", "/etc/hosts"); - instance.printAugTree(testSettings, "/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config"); + aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); - return 0; -} \ No newline at end of file + r = aug_load_file(aug, filePath.c_str()); + r = aug_preview(aug, matchPath.c_str(), &s); + + std::cout << s << std::endl; + + free(hosts_txt); + free(s); + aug_close(aug); +} + + +RMDEF void printAugTree( + const AugSettings& settings, + const std::string& matchPath, + const std::string& filePath +) { + struct augeas *aug; + int r; + FILE *out = tmpfile(); + + aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); + + r = aug_load_file(aug, filePath.c_str()); + r = aug_print(aug, out,matchPath.c_str()); + + std::map stdBindList; + std::map ::iterator pos; + + char line[256]; + rewind(out); + while (fgets(line, 256, out) != nullptr) { + // remove end of line + line[strlen(line) - 1] = '\0'; + std::string s = line; + + // skip comments + if (s.find("#comment") != std::string::npos) + continue; + s = s.substr(matchPath.length() - 1); + // 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(key,value)); // 2 + + } + + fclose(out); + + for (pos = stdBindList.begin();pos!=stdBindList.end();pos++) + { + std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; + } + + aug_close(aug); +} + + +} diff --git a/main.h b/main.h new file mode 100644 index 0000000..dd251ba --- /dev/null +++ b/main.h @@ -0,0 +1,22 @@ +#if defined(_WIN32) +#define RMDEF __declspec(dllexport) extern "C" inline +#else +#define RMDEF +#endif + +extern "C" { +RMDEF int getThree (); +RMDEF int getFour (); + +RMDEF void testSource (const AugSettings& settings); + +RMDEF void printPreview (const AugSettings& settings, + const std::string& matchPath, + const std::string& filePath); + +RMDEF void printStringExample (char* someString); + +RMDEF void printAugTree (const AugSettings& settings, + const std::string& matchPath, + const std::string& filePath); +}