Formatting.

This commit is contained in:
retoor 2025-05-05 14:33:08 +02:00
parent 44c7063033
commit 255e5458be
12 changed files with 618 additions and 471 deletions

117
browse.h
View File

@ -12,15 +12,63 @@ char *web_search_news(char *q) {
const int MAX_RETRIES = 3; const int MAX_RETRIES = 3;
for (int retry = 0; retry < MAX_RETRIES; retry++) { for (int retry = 0; retry < MAX_RETRIES; retry++) {
char *news = malloc(4096); char *news = malloc(4096);
if (!news) return NULL; if (!news)
return NULL;
news[0] = 0; news[0] = 0;
char *q_encoded = url_encode(q); char *q_encoded = url_encode(q);
snprintf(news, 4096, "https://search.molodetz.nl/search?q=%s&format=json&categories=news&preferences=eJx1WMuy4zYO_Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQcPQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3Asf0LZ3tOtJ09g4DIFCibGfG_6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4BOI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2GT519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_rwH9CMqXW6Y58IJPeDXwPVQI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5npuFbLldY7hfo1_z_HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4FqVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV", q_encoded); snprintf(
news, 4096,
"https://search.molodetz.nl/"
"search?q=%s&format=json&categories=news&preferences=eJx1WMuy4zYO_"
"Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQc"
"PQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-"
"faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-"
"eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-"
"jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_"
"mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__"
"MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJ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7gQd7GehKmbIe_"
"NRBSymdvgVawUnxqVKpce3bREefaCubIU0uyICdEXCXy3qMeS63k5ljTeCz3tbYKb2mHj2"
"qTKA7a2dYrmhlfx3LBTRDf8O_Q0bOcth49stoplzwDn5mYnwZWKaq5dV-"
"QI4Ucy7gDpRzLRzK44i79-freYty1bY6M7b_Nc4LH0hkghBeWj5SZc-o9D0z-"
"q0DrFI6ch_wOz3-JBxYF5-jsrzN49HL6WxD9C6-hrqArAPZkP6-"
"N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY"
"1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_"
"HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6"
"OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_"
"y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-"
"_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-"
"gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_"
"3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_"
"cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_"
"hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-"
"k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4F"
"qVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
q_encoded);
free(q_encoded); free(q_encoded);
char *ret = curl_get(news); char *ret = curl_get(news);
free(news); free(news);
if (!ret) continue; if (!ret)
continue;
json_object *json_ret = json_tokener_parse(ret); json_object *json_ret = json_tokener_parse(ret);
if (!json_ret) { if (!json_ret) {
@ -44,15 +92,63 @@ char *web_search(char *q) {
const int MAX_RETRIES = 3; const int MAX_RETRIES = 3;
for (int retry = 0; retry < MAX_RETRIES; retry++) { for (int retry = 0; retry < MAX_RETRIES; retry++) {
char *news = malloc(4096); char *news = malloc(4096);
if (!news) return NULL; if (!news)
return NULL;
news[0] = 0; news[0] = 0;
char *q_encoded = url_encode(q); char *q_encoded = url_encode(q);
snprintf(news, 4096, "https://search.molodetz.nl/search?q=%s&format=json&preferences=eJx1WMuy4zYO_Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQcPQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3Asf0LZ3tOtJ09g4DIFCibGfG_6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4BOI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2GT519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_rwH9CMqXW6Y58IJPeDXwPVQIFw5qtvjQQY6CP32lWXA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CHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4FqVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV", q_encoded); snprintf(
news, 4096,
"https://search.molodetz.nl/"
"search?q=%s&format=json&preferences=eJx1WMuy4zYO_"
"Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQc"
"PQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-"
"faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-"
"eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-"
"jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_"
"mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__"
"MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3As"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gVawUnxqVKpce3bREefaCubIU0uyICdEXCXy3qMeS63k5ljTeCz3tbYKb2mHj2"
"qTKA7a2dYrmhlfx3LBTRDf8O_Q0bOcth49stoplzwDn5mYnwZWKaq5dV-"
"QI4Ucy7gDpRzLRzK44i79-freYty1bY6M7b_Nc4LH0hkghBeWj5SZc-o9D0z-"
"q0DrFI6ch_wOz3-JBxYF5-jsrzN49HL6WxD9C6-hrqArAPZkP6-"
"N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY"
"1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_"
"HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6"
"OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_"
"y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-"
"_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-"
"gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_"
"3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_"
"cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_"
"hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-"
"k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4F"
"qVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
q_encoded);
free(q_encoded); free(q_encoded);
char *ret = curl_get(news); char *ret = curl_get(news);
free(news); free(news);
if (!ret) continue; if (!ret)
continue;
json_object *json_ret = json_tokener_parse(ret); json_object *json_ret = json_tokener_parse(ret);
if (!json_ret) { if (!json_ret) {
@ -76,12 +172,15 @@ char *web_search_engine(char *q) {
const int MAX_RETRIES = 3; const int MAX_RETRIES = 3;
for (int retry = 0; retry < MAX_RETRIES; retry++) { for (int retry = 0; retry < MAX_RETRIES; retry++) {
char *searx = malloc(4096); char *searx = malloc(4096);
if (!searx) return NULL; if (!searx)
return NULL;
searx[0] = 0; searx[0] = 0;
snprintf(searx, 4096, "https://searx.molodetz.nl/search?q=%s&format=json", q); snprintf(searx, 4096, "https://searx.molodetz.nl/search?q=%s&format=json",
q);
char *ret = curl_get(searx); char *ret = curl_get(searx);
free(searx); free(searx);
if (!ret) continue; if (!ret)
continue;
json_object *json_ret = json_tokener_parse(ret); json_object *json_ret = json_tokener_parse(ret);
if (!json_ret) { if (!json_ret) {

View File

@ -1,7 +1,7 @@
#include "db_utils.h" #include "db_utils.h"
#include <sqlite3.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sqlite3.h>
void db_initialize() { void db_initialize() {
sqlite3 *db; sqlite3 *db;

View File

@ -22,8 +22,6 @@ json_object *db_set(const char *key, const char *value);
json_object *db_get(const char *key); json_object *db_get(const char *key);
json_object *db_query(const char *query); json_object *db_query(const char *query);
void db_initialize() { void db_initialize() {
sqlite3 *db; sqlite3 *db;
int rc = sqlite3_open(db_file_expanded(), &db); int rc = sqlite3_open(db_file_expanded(), &db);
@ -32,12 +30,14 @@ void db_initialize() {
return; return;
} }
db_execute("CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);"); db_execute("CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value "
db_execute("CREATE TABLE IF NOT EXISTS file_version_history ( id INTEGER PRIMARY KEY AUTOINCREMENT," "TEXT);");
"path TEXT NOT NULL," db_execute("CREATE TABLE IF NOT EXISTS file_version_history ( id INTEGER "
"content TEXT," "PRIMARY KEY AUTOINCREMENT,"
"date DATETIME DEFAULT CURRENT_TIMESTAMP" "path TEXT NOT NULL,"
");"); "content TEXT,"
"date DATETIME DEFAULT CURRENT_TIMESTAMP"
");");
sqlite3_close(db); sqlite3_close(db);
} }
@ -180,18 +180,20 @@ json_object *db_execute(const char *query) {
sqlite3_close(db); sqlite3_close(db);
return result; return result;
} }
void db_store_file_version(const char * path) { void db_store_file_version(const char *path) {
char * expanded = expand_home_directory(path); char *expanded = expand_home_directory(path);
char * content = read_file(expanded); char *content = read_file(expanded);
if(!content) { if (!content) {
return; return;
} }
fprintf(stderr, "Creating backup:: %s\n", expanded); fprintf(stderr, "Creating backup:: %s\n", expanded);
char * formatted = sqlite3_mprintf("INSERT INTO file_version_history (path, content) VALUES (%Q, %Q)", expanded, content); char *formatted = sqlite3_mprintf(
db_execute(formatted); "INSERT INTO file_version_history (path, content) VALUES (%Q, %Q)",
sqlite3_free(formatted); expanded, content);
free(content); db_execute(formatted);
sqlite3_free(formatted);
free(content);
} }
char *db_get_schema() { char *db_get_schema() {
json_object *tables = json_object *tables =

View File

@ -76,8 +76,8 @@ char *curl_post(const char *url, const char *data) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if (res != CURLE_OK) { if (res != CURLE_OK) {
fprintf(stderr, "Url: %s\n",data); fprintf(stderr, "Url: %s\n", data);
fprintf(stderr, "Data: %s\n",data); fprintf(stderr, "Data: %s\n", data);
fprintf(stderr, "An error occurred: %s\n", curl_easy_strerror(res)); fprintf(stderr, "An error occurred: %s\n", curl_easy_strerror(res));
} }
curl_slist_free_all(headers); curl_slist_free_all(headers);

176
indexer.h
View File

@ -12,101 +12,123 @@
#define MAX_PATH 4096 #define MAX_PATH 4096
static const char *extensions[] = { static const char *extensions[] = {
".c", ".cpp", ".h", ".py", ".java", ".js", ".mk", ".html", ".c", ".cpp", ".h", ".py", ".java", ".js",
"Makefile", ".css", ".json", ".cs", ".csproj", ".sln", ".toml", ".rs", ".mk", ".html", "Makefile", ".css", ".json", ".cs",
".go", ".rb", ".swift", ".php", ".pl", ".sh", ".bash", ".sql", ".csproj", ".sln", ".toml", ".rs", ".go", ".rb",
".xml", ".yaml", ".yml", ".kt", ".dart", ".scala", ".clj", ".asm", ".swift", ".php", ".pl", ".sh", ".bash", ".sql",
".m", ".r", ".lua", ".groovy", ".v", ".pas", ".d", ".f90", ".f95", ".xml", ".yaml", ".yml", ".kt", ".dart", ".scala",
".for", ".s", ".tcl", ".vhdl", ".verilog", ".coffee", ".less", ".scss", ".clj", ".asm", ".m", ".r", ".lua", ".groovy",
".ps1", ".psm1", ".cmd", ".bat", ".json5", ".cxx", ".cc", ".hpp", ".v", ".pas", ".d", ".f90", ".f95", ".for",
".hxx", ".inc", ".nsi", ".ninja", ".cmake", ".cmake.in", ".mk.in", ".s", ".tcl", ".vhdl", ".verilog", ".coffee", ".less",
".make", ".makefile", ".gyp", ".gypi", ".pro", ".qml", ".ui", ".wxs", ".scss", ".ps1", ".psm1", ".cmd", ".bat", ".json5",
".wxl", ".wxi", ".wxl", ".wxs", ".wxi", ".wxl", ".wxs", ".wxi" ".cxx", ".cc", ".hpp", ".hxx", ".inc", ".nsi",
}; ".ninja", ".cmake", ".cmake.in", ".mk.in", ".make", ".makefile",
".gyp", ".gypi", ".pro", ".qml", ".ui", ".wxs",
".wxl", ".wxi", ".wxl", ".wxs", ".wxi", ".wxl",
".wxs", ".wxi"};
static const size_t ext_count = sizeof(extensions) / sizeof(extensions[0]); static const size_t ext_count = sizeof(extensions) / sizeof(extensions[0]);
typedef struct { typedef struct {
char name[MAX_PATH]; char name[MAX_PATH];
char modification_date[20]; char modification_date[20];
char creation_date[20]; char creation_date[20];
char type[10]; char type[10];
size_t size_bytes; size_t size_bytes;
} FileInfo; } FileInfo;
static FileInfo file_list[MAX_FILES]; static FileInfo file_list[MAX_FILES];
static size_t file_count = 0; static size_t file_count = 0;
static int is_valid_extension(const char *filename) { static int is_valid_extension(const char *filename) {
const char *dot = strrchr(filename, '.'); const char *dot = strrchr(filename, '.');
if (!dot) dot = filename; if (!dot)
for (size_t i = 0; i < ext_count; i++) { dot = filename;
if (strcmp(dot, extensions[i]) == 0) return 1; for (size_t i = 0; i < ext_count; i++) {
} if (strcmp(dot, extensions[i]) == 0)
return 0; return 1;
}
return 0;
} }
static int is_ignored_directory(const char *dir_name) { static int is_ignored_directory(const char *dir_name) {
const char *ignored_dirs[] = {"env", ".venv", "node_modules", "venv", "virtualenv"}; const char *ignored_dirs[] = {"env", ".venv", "node_modules", "venv",
for (size_t i = 0; i < sizeof(ignored_dirs) / sizeof(ignored_dirs[0]); i++) { "virtualenv"};
if (strcmp(dir_name, ignored_dirs[i]) == 0) return 1; for (size_t i = 0; i < sizeof(ignored_dirs) / sizeof(ignored_dirs[0]); i++) {
} if (strcmp(dir_name, ignored_dirs[i]) == 0)
return 0; return 1;
}
return 0;
} }
static void get_file_info(const char *path) { static void get_file_info(const char *path) {
struct stat file_stat; struct stat file_stat;
if (stat(path, &file_stat) == 0) { if (stat(path, &file_stat) == 0) {
FileInfo info; FileInfo info;
strncpy(info.name, path, MAX_PATH - 1); strncpy(info.name, path, MAX_PATH - 1);
info.name[MAX_PATH - 1] = '\0'; info.name[MAX_PATH - 1] = '\0';
strftime(info.modification_date, sizeof(info.modification_date), "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_mtime)); strftime(info.modification_date, sizeof(info.modification_date),
strftime(info.creation_date, sizeof(info.creation_date), "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_ctime)); "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_mtime));
strncpy(info.type, S_ISDIR(file_stat.st_mode) ? "directory" : "file", sizeof(info.type) - 1); strftime(info.creation_date, sizeof(info.creation_date),
info.type[sizeof(info.type) - 1] = '\0'; "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_ctime));
info.size_bytes = file_stat.st_size; strncpy(info.type, S_ISDIR(file_stat.st_mode) ? "directory" : "file",
file_list[file_count++] = info; sizeof(info.type) - 1);
} info.type[sizeof(info.type) - 1] = '\0';
info.size_bytes = file_stat.st_size;
file_list[file_count++] = info;
}
} }
char *index_directory(const char *dir_path) { char *index_directory(const char *dir_path) {
DIR *dir = opendir(dir_path); DIR *dir = opendir(dir_path);
if (!dir) { if (!dir) {
perror("Failed to open directory"); perror("Failed to open directory");
return NULL; return NULL;
}
struct dirent *entry;
json_object *jarray = json_object_new_array();
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (entry->d_name[0] == '.' || is_ignored_directory(entry->d_name))
continue;
char full_path[MAX_PATH];
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, entry->d_name);
if (entry->d_type == DT_DIR) {
char *subdir_json = index_directory(full_path);
if (subdir_json) {
json_object *jsubdir = json_object_new_string(subdir_json);
json_object_array_add(jarray, jsubdir);
free(subdir_json);
}
} else if (is_valid_extension(entry->d_name)) {
get_file_info(full_path);
json_object *jfile = json_object_new_object();
json_object_object_add(
jfile, "file_name",
json_object_new_string(file_list[file_count - 1].name));
json_object_object_add(
jfile, "modification_date",
json_object_new_string(file_list[file_count - 1].modification_date));
json_object_object_add(
jfile, "creation_date",
json_object_new_string(file_list[file_count - 1].creation_date));
json_object_object_add(
jfile, "type",
json_object_new_string(file_list[file_count - 1].type));
json_object_object_add(
jfile, "size_bytes",
json_object_new_int64(file_list[file_count - 1].size_bytes));
json_object_array_add(jarray, jfile);
} }
}
closedir(dir);
struct dirent *entry; char *result = strdup(json_object_to_json_string(jarray));
json_object *jarray = json_object_new_array(); json_object_put(jarray);
return result;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
if (entry->d_name[0] == '.' || is_ignored_directory(entry->d_name)) continue;
char full_path[MAX_PATH];
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, entry->d_name);
if (entry->d_type == DT_DIR) {
char *subdir_json = index_directory(full_path);
if (subdir_json) {
json_object *jsubdir = json_object_new_string(subdir_json);
json_object_array_add(jarray, jsubdir);
free(subdir_json);
}
} else if (is_valid_extension(entry->d_name)) {
get_file_info(full_path);
json_object *jfile = json_object_new_object();
json_object_object_add(jfile, "file_name", json_object_new_string(file_list[file_count - 1].name));
json_object_object_add(jfile, "modification_date", json_object_new_string(file_list[file_count - 1].modification_date));
json_object_object_add(jfile, "creation_date", json_object_new_string(file_list[file_count - 1].creation_date));
json_object_object_add(jfile, "type", json_object_new_string(file_list[file_count - 1].type));
json_object_object_add(jfile, "size_bytes", json_object_new_int64(file_list[file_count - 1].size_bytes));
json_object_array_add(jarray, jfile);
}
}
closedir(dir);
char *result = strdup(json_object_to_json_string(jarray));
json_object_put(jarray);
return result;
} }

374
main.c
View File

@ -1,18 +1,18 @@
#include "r.h" #include "r.h"
#include <locale.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <locale.h>
#include <unistd.h> #include <unistd.h>
#include "db_utils.h"
#include "line.h" #include "line.h"
#include "markdown.h" #include "markdown.h"
#include "openai.h" #include "openai.h"
#include "utils.h"
#include "db_utils.h"
#include "tools.h" #include "tools.h"
#include "utils.h"
volatile sig_atomic_t sigint_count = 0; volatile sig_atomic_t sigint_count = 0;
time_t first_sigint_time = 0; time_t first_sigint_time = 0;
@ -25,232 +25,244 @@ bool openai_include(const char *);
char *strreplace(const char *, const char *, const char *); char *strreplace(const char *, const char *, const char *);
char *get_prompt_from_stdin(char *prompt) { char *get_prompt_from_stdin(char *prompt) {
int index = 0; int index = 0;
char c; char c;
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF) {
prompt[index++] = c; prompt[index++] = c;
} }
prompt[index] = '\0'; prompt[index] = '\0';
return prompt; return prompt;
} }
char *get_prompt_from_args(int argc, char **argv) { char *get_prompt_from_args(int argc, char **argv) {
char *prompt = malloc(10 * 1024 * 1024 + 1); char *prompt = malloc(10 * 1024 * 1024 + 1);
char *system = malloc(1024 * 1024); char *system = malloc(1024 * 1024);
if (!prompt || !system) { if (!prompt || !system) {
fprintf(stderr, "Error: Memory allocation failed.\n"); fprintf(stderr, "Error: Memory allocation failed.\n");
free(prompt); free(prompt);
free(system); free(system);
return NULL; return NULL;
} }
bool get_from_std_in = false; bool get_from_std_in = false;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--stdin") == 0) { if (strcmp(argv[i], "--stdin") == 0) {
fprintf(stderr, "Reading from stdin.\n"); fprintf(stderr, "Reading from stdin.\n");
get_from_std_in = true; get_from_std_in = true;
} else if (strcmp(argv[i], "--verbose") == 0) { } else if (strcmp(argv[i], "--verbose") == 0) {
is_verbose = true; is_verbose = true;
} else if (strcmp(argv[i], "--py") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--py") == 0 && i + 1 < argc) {
char *py_file_path = expand_home_directory(argv[++i]); char *py_file_path = expand_home_directory(argv[++i]);
fprintf(stderr, "Including \"%s\".\n", py_file_path); fprintf(stderr, "Including \"%s\".\n", py_file_path);
openai_include(py_file_path); openai_include(py_file_path);
free(py_file_path); free(py_file_path);
} else if (strcmp(argv[i], "--free") == 0) { } else if (strcmp(argv[i], "--free") == 0) {
auth_free(); auth_free();
} else if (strcmp(argv[i], "--context") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--context") == 0 && i + 1 < argc) {
char *context_file_path = argv[++i]; char *context_file_path = argv[++i];
fprintf(stderr, "Including \"%s\".\n", context_file_path); fprintf(stderr, "Including \"%s\".\n", context_file_path);
openai_include(context_file_path); openai_include(context_file_path);
} else if (strcmp(argv[i], "--api") == 0) { } else if (strcmp(argv[i], "--api") == 0) {
API_MODE = true; API_MODE = true;
} else if (strcmp(argv[i], "--nh") == 0) { } else if (strcmp(argv[i], "--nh") == 0) {
SYNTAX_HIGHLIGHT_ENABLED = false; SYNTAX_HIGHLIGHT_ENABLED = false;
fprintf(stderr, "Syntax highlighting disabled.\n"); fprintf(stderr, "Syntax highlighting disabled.\n");
} else {
strcat(system, argv[i]);
strcat(system, (i < argc - 1) ? " " : ".");
}
}
if (get_from_std_in) {
if (*system) openai_system(system);
prompt = get_prompt_from_stdin(prompt);
} else { } else {
free(prompt); strcat(system, argv[i]);
prompt = system; strcat(system, (i < argc - 1) ? " " : ".");
} }
}
if (!*prompt) { if (get_from_std_in) {
free(prompt); if (*system)
return NULL; openai_system(system);
} prompt = get_prompt_from_stdin(prompt);
return prompt; } else {
free(prompt);
prompt = system;
}
if (!*prompt) {
free(prompt);
return NULL;
}
return prompt;
} }
bool try_prompt(int argc, char *argv[]) { bool try_prompt(int argc, char *argv[]) {
char *prompt = get_prompt_from_args(argc, argv); char *prompt = get_prompt_from_args(argc, argv);
if (prompt) { if (prompt) {
char *response = openai_chat("user", prompt); char *response = openai_chat("user", prompt);
if (!response) { if (!response) {
printf("Could not get response from server\n"); printf("Could not get response from server\n");
free(prompt); free(prompt);
return false; return false;
}
render(response);
free(response);
free(prompt);
return true;
} }
return false; render(response);
free(response);
free(prompt);
return true;
}
return false;
} }
char **get_parameters(const char *content, const char *delimiter) { char **get_parameters(const char *content, const char *delimiter) {
char *start = NULL; char *start = NULL;
char **parameters = NULL; char **parameters = NULL;
int count = 0; int count = 0;
while ((start = strstr(content, delimiter)) != NULL) { while ((start = strstr(content, delimiter)) != NULL) {
start += 3; start += 3;
char *end = strstr(start, delimiter); char *end = strstr(start, delimiter);
char *parameter = malloc(end - start + 1); char *parameter = malloc(end - start + 1);
memcpy(parameter, start, end - start); memcpy(parameter, start, end - start);
parameter[end - start] = '\0'; parameter[end - start] = '\0';
content = end + 3; content = end + 3;
count++; count++;
parameters = realloc(parameters, sizeof(char *) * (count + 1)); parameters = realloc(parameters, sizeof(char *) * (count + 1));
parameters[count - 1] = parameter; parameters[count - 1] = parameter;
parameters[count] = NULL; parameters[count] = NULL;
} }
return parameters; return parameters;
} }
void render(const char *content) { void render(const char *content) {
if (SYNTAX_HIGHLIGHT_ENABLED) { if (SYNTAX_HIGHLIGHT_ENABLED) {
parse_markdown_to_ansi(content); parse_markdown_to_ansi(content);
} else { } else {
printf("%s", content); printf("%s", content);
} }
} }
void repl() { void repl() {
line_init(); line_init();
char *line = NULL; char *line = NULL;
while (true) { while (true) {
line = line_read("> "); line = line_read("> ");
if (!line || !*line) continue; if (!line || !*line)
continue;
if (!strncmp(line, "!dump", 5)) { if (!strncmp(line, "!dump", 5)) {
printf("%s\n", message_json()); printf("%s\n", message_json());
continue; continue;
}
if (!strncmp(line, "!verbose", 8)) {
is_verbose = !is_verbose;
fprintf(stderr, "%s\n", is_verbose ? "Verbose mode enabled" : "Verbose mode disabled");
continue;
}
if (line && *line != '\n') line_add_history(line);
if(!strncmp(line, "!tools", 6)) {
printf("Available tools: %s\n", json_object_to_json_string(tools_descriptions()));
continue;
}
if (!strncmp(line, "!models", 7)) {
printf("Current model: %s\n", openai_fetch_models());
continue;
}
if (!strncmp(line, "!model", 6)) {
if (line[6] == ' ') {
set_prompt_model(line + 7);
}
printf("Current model: %s\n", get_prompt_model());
continue;
}
if (!strncmp(line, "exit", 4)) exit(0);
while (line && *line != '\n') {
char *response = openai_chat("user", line);
if (response) {
render(response);
printf("\n");
if (strstr(response, "_STEP_")) {
line = "continue";
} else {
line = NULL;
}
free(response);
} else {
exit(0);
}
}
} }
if (!strncmp(line, "!verbose", 8)) {
is_verbose = !is_verbose;
fprintf(stderr, "%s\n",
is_verbose ? "Verbose mode enabled" : "Verbose mode disabled");
continue;
}
if (line && *line != '\n')
line_add_history(line);
if (!strncmp(line, "!tools", 6)) {
printf("Available tools: %s\n",
json_object_to_json_string(tools_descriptions()));
continue;
}
if (!strncmp(line, "!models", 7)) {
printf("Current model: %s\n", openai_fetch_models());
continue;
}
if (!strncmp(line, "!model", 6)) {
if (line[6] == ' ') {
set_prompt_model(line + 7);
}
printf("Current model: %s\n", get_prompt_model());
continue;
}
if (!strncmp(line, "exit", 4))
exit(0);
while (line && *line != '\n') {
char *response = openai_chat("user", line);
if (response) {
render(response);
printf("\n");
if (strstr(response, "_STEP_")) {
line = "continue";
} else {
line = NULL;
}
free(response);
} else {
exit(0);
}
}
}
} }
char *strreplace(const char *content, const char *what, const char *with) { char *strreplace(const char *content, const char *what, const char *with) {
char *pos = strstr(content, what); char *pos = strstr(content, what);
if (!pos) return strdup(content); if (!pos)
return strdup(content);
size_t result_size = strlen(content) + strlen(with) - strlen(what) + 1; size_t result_size = strlen(content) + strlen(with) - strlen(what) + 1;
char *result = malloc(result_size); char *result = malloc(result_size);
snprintf(result, result_size, "%.*s%s%s", (int)(pos - content), content, with, pos + strlen(what)); snprintf(result, result_size, "%.*s%s%s", (int)(pos - content), content, with,
return result; pos + strlen(what));
return result;
} }
bool openai_include(const char *path) { bool openai_include(const char *path) {
char *file_content = read_file(path); char *file_content = read_file(path);
if (!file_content) return false; if (!file_content)
return false;
openai_system(file_content); openai_system(file_content);
free(file_content); free(file_content);
return true; return true;
} }
void init() { void init() {
setbuf(stdout, NULL); setbuf(stdout, NULL);
line_init(); line_init();
auth_init(); auth_init();
db_initialize(); db_initialize();
char *schema = db_get_schema(); char *schema = db_get_schema();
char payload[1024 * 1024] = {0}; char payload[1024 * 1024] = {0};
snprintf(payload, sizeof(payload), snprintf(payload, sizeof(payload),
"Your have a database that you can mutate using the query tool and the get and set tool. This is the schema in json format: %s. Dialect is sqlite.", "Your have a database that you can mutate using the query tool and "
schema); "the get and set tool. This is the schema in json format: %s. "
free(schema); "Dialect is sqlite.",
schema);
free(schema);
fprintf(stderr, "Loading... 4e6"); fprintf(stderr, "Loading... 4e6");
openai_system(payload); openai_system(payload);
if (!openai_include(".rcontext.txt")) { if (!openai_include(".rcontext.txt")) {
openai_include("~/.rcontext.txt"); openai_include("~/.rcontext.txt");
} }
fprintf(stderr, "\r \r"); fprintf(stderr, "\r \r");
} }
void handle_sigint(int sig) { void handle_sigint(int sig) {
time_t current_time = time(NULL); time_t current_time = time(NULL);
printf("\n"); printf("\n");
if (sigint_count == 0) { if (sigint_count == 0) {
first_sigint_time = current_time; first_sigint_time = current_time;
sigint_count++; sigint_count++;
} else {
if (difftime(current_time, first_sigint_time) <= 1) {
exit(0);
} else { } else {
if (difftime(current_time, first_sigint_time) <= 1) { sigint_count = 1;
exit(0); first_sigint_time = current_time;
} else {
sigint_count = 1;
first_sigint_time = current_time;
}
} }
}
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
signal(SIGINT, handle_sigint); signal(SIGINT, handle_sigint);
init(); init();
if (try_prompt(argc, argv)) return 0; if (try_prompt(argc, argv))
repl();
return 0; return 0;
repl();
return 0;
} }

View File

@ -25,10 +25,10 @@
#ifndef R_MESSAGES_H #ifndef R_MESSAGES_H
#define R_MESSAGES_H #define R_MESSAGES_H
#include "db_utils.h"
#include "json-c/json.h" #include "json-c/json.h"
#include "tools.h" #include "tools.h"
#include <string.h> #include <string.h>
#include "db_utils.h"
struct json_object *message_array = NULL; struct json_object *message_array = NULL;
struct json_object *message_list() { struct json_object *message_list() {
@ -66,7 +66,7 @@ struct json_object *message_add_tool_result(const char *tool_call_id,
json_object_object_add(message, "tool_call_id", json_object_object_add(message, "tool_call_id",
json_object_new_string(tool_call_id)); json_object_new_string(tool_call_id));
if(strlen(tool_result) > 104000){ if (strlen(tool_result) > 104000) {
tool_result[104000] = '\0'; tool_result[104000] = '\0';
} }
@ -82,7 +82,6 @@ void message_add_object(json_object *message) {
json_object_array_add(messages, message); json_object_array_add(messages, message);
} }
struct json_object *message_add(const char *role, const char *content); struct json_object *message_add(const char *role, const char *content);
struct json_object *message_add(const char *role, const char *content) { struct json_object *message_add(const char *role, const char *content) {
@ -90,17 +89,19 @@ struct json_object *message_add(const char *role, const char *content) {
struct json_object *message = json_object_new_object(); struct json_object *message = json_object_new_object();
json_object_object_add(message, "role", json_object_new_string(role)); json_object_object_add(message, "role", json_object_new_string(role));
if(content){ if (content) {
char * formatted_content = strdup(content); char *formatted_content = strdup(content);
if(strlen(formatted_content) > 1048570){ if (strlen(formatted_content) > 1048570) {
formatted_content[1048570] = '\0'; formatted_content[1048570] = '\0';
} }
json_object_object_add(message, "content", json_object_new_string(formatted_content)); json_object_object_add(message, "content",
free(formatted_content); json_object_new_string(formatted_content));
free(formatted_content);
} }
if (!strcmp(role, "user")) { if (!strcmp(role, "user")) {
json_object_object_add(message, "tools", tools_descriptions()); json_object_object_add(message, "tools", tools_descriptions());
json_object_object_add(message, "parallel_tool_calls", json_object_new_boolean(true)); json_object_object_add(message, "parallel_tool_calls",
json_object_new_boolean(true));
} }
json_object_array_add(messages, message); json_object_array_add(messages, message);

View File

@ -46,7 +46,7 @@ bool openai_system(char *message_content) {
struct json_object *openai_process_chat_message(const char *api_url, struct json_object *openai_process_chat_message(const char *api_url,
const char *json_data) { const char *json_data) {
char *response = curl_post(api_url, json_data); char *response = curl_post(api_url, json_data);
if (!response) { if (!response) {
fprintf(stderr, "Failed to get response.\n"); fprintf(stderr, "Failed to get response.\n");
return NULL; return NULL;
@ -128,8 +128,8 @@ char *openai_chat(const char *user_role, const char *message_content) {
message_add_tool_call(tool_call_result); message_add_tool_call(tool_call_result);
} }
char *tool_calls_result_str = chat_json(NULL, NULL); char *tool_calls_result_str = chat_json(NULL, NULL);
message_object = message_object = openai_process_chat_message(get_completions_api_url(),
openai_process_chat_message(get_completions_api_url(), tool_calls_result_str); tool_calls_result_str);
if (message_object == NULL) { if (message_object == NULL) {
return NULL; return NULL;
} }

85
r.h
View File

@ -11,58 +11,57 @@ char *completions_api_url = "https://api.openai.com/v1/chat/completions";
char *advanced_model = "gpt-4o-mini"; char *advanced_model = "gpt-4o-mini";
char *fast_model = "gpt-3.5-turbo"; char *fast_model = "gpt-3.5-turbo";
//char *models_api_url = "https://api.openai.com/v1/models"; // char *models_api_url = "https://api.openai.com/v1/models";
//char *completions_api_url = "https://api.anthropic.com/v1/chat/completions"; // char *completions_api_url = "https://api.anthropic.com/v1/chat/completions";
//char *advanced_model = "claude-3-5-haiku-20241022"; // char *advanced_model = "claude-3-5-haiku-20241022";
//char *advanced_model = "meta-llama/Meta-Llama-3.1-8B-Instruct"; // char *advanced_model = "meta-llama/Meta-Llama-3.1-8B-Instruct";
//char *advanced_model = "google/gemini-1.5-flash"; // char *advanced_model = "google/gemini-1.5-flash";
//char *fast_model = "claude-3-5-haiku-20241022"; // char *fast_model = "claude-3-5-haiku-20241022";
//#endif // #endif
//#ifdef OLLAMA // #ifdef OLLAMA
//char *models_api_url = "https://ollama.molodetz.nl/v1/models"; // char *models_api_url = "https://ollama.molodetz.nl/v1/models";
//char *completions_api_url = "https://ollama.molodetz.nl/v1/chat/completions"; // char *completions_api_url = "https://ollama.molodetz.nl/v1/chat/completions";
//char *advanced_model = "qwen2.5:3b"; // char *advanced_model = "qwen2.5:3b";
//char *advanced_model = "qwen2.5-coder:0.5b"; // char *advanced_model = "qwen2.5-coder:0.5b";
//char *fast_model = "qwen2.5:0.5b"; // char *fast_model = "qwen2.5:0.5b";
//#endif // #endif
char *_model = NULL; char *_model = NULL;
#define DB_FILE "~/.r.db" #define DB_FILE "~/.r.db"
#define PROMPT_TEMPERATURE 0.1 #define PROMPT_TEMPERATURE 0.1
bool get_use_strict(){ bool get_use_strict() {
if(getenv("R_USE_STRICT") != NULL) { if (getenv("R_USE_STRICT") != NULL) {
const char * value = getenv("R_USE_STRICT"); const char *value = getenv("R_USE_STRICT");
if(!strcmp(value, "true")) { if (!strcmp(value, "true")) {
return true; return true;
}
if(!strcmp(value, "false")) {
return false;
}
if(!strcmp(value, "1")) {
return true;
}
if(!strcmp(value, "0")) {
return false;
}
} }
return true; if (!strcmp(value, "false")) {
return false;
}
if (!strcmp(value, "1")) {
return true;
}
if (!strcmp(value, "0")) {
return false;
}
}
return true;
} }
char * get_completions_api_url() { char *get_completions_api_url() {
if(getenv("R_BASE_URL") != NULL) { if (getenv("R_BASE_URL") != NULL) {
return joinpath(getenv("R_BASE_URL"), "v1/chat/completions"); return joinpath(getenv("R_BASE_URL"), "v1/chat/completions");
} }
return completions_api_url; return completions_api_url;
} }
char * get_models_api_url() { char *get_models_api_url() {
if(getenv("R_BASE_URL") != NULL) { if (getenv("R_BASE_URL") != NULL) {
return joinpath(getenv("R_BASE_URL"), "v1/models"); return joinpath(getenv("R_BASE_URL"), "v1/models");
} }
return models_api_url; return models_api_url;
} }
void set_prompt_model(const char *model) { void set_prompt_model(const char *model) {
@ -73,10 +72,10 @@ void set_prompt_model(const char *model) {
} }
const char *get_prompt_model() { const char *get_prompt_model() {
if(_model == NULL && getenv("R_MODEL") != NULL) { if (_model == NULL && getenv("R_MODEL") != NULL) {
_model = getenv("R_MODEL"); _model = getenv("R_MODEL");
} }
if(_model){ if (_model) {
return _model; return _model;
} }
if (auth_type != AUTH_TYPE_API_KEY) { if (auth_type != AUTH_TYPE_API_KEY) {

BIN
rpylib.so

Binary file not shown.

149
tools.h
View File

@ -12,23 +12,21 @@
#ifndef R_TOOLS_H #ifndef R_TOOLS_H
#define R_TOOLS_H #define R_TOOLS_H
#include "http_curl.h" #include "http_curl.h"
#include "r.h" #include "r.h"
#include "utils.h" #include "utils.h"
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <glob.h> #include <glob.h>
#include <json-c/json.h> #include <json-c/json.h>
#include <json-c/json_object.h> #include <json-c/json_object.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "browse.h" #include "browse.h"
#include "db_utils.h" #include "db_utils.h"
@ -120,8 +118,9 @@ struct json_object *tool_description_web_search_news() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -176,8 +175,9 @@ struct json_object *tool_description_web_search() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -224,13 +224,14 @@ struct json_object *tool_description_db_get() {
json_object_array_add(required, json_object_new_string("key")); json_object_array_add(required, json_object_new_string("key"));
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -276,13 +277,14 @@ struct json_object *tool_description_db_query() {
json_object_array_add(required, json_object_new_string("query")); json_object_array_add(required, json_object_new_string("query"));
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -335,13 +337,14 @@ struct json_object *tool_description_db_set() {
json_object_array_add(required, json_object_new_string("value")); json_object_array_add(required, json_object_new_string("value"));
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -367,19 +370,17 @@ char *tool_function_linux_terminal(char *command) {
char stderr_command[1024] = {0}; char stderr_command[1024] = {0};
if(!strstr(command, "2>")) { if (!strstr(command, "2>")) {
snprintf(stderr_command, sizeof(stderr_command), "%s 2>&1", command); snprintf(stderr_command, sizeof(stderr_command), "%s 2>&1", command);
command = stderr_command; command = stderr_command;
} }
fp = popen(command, "r"); fp = popen(command, "r");
if (fp == NULL) { if (fp == NULL) {
perror("popen failed"); perror("popen failed");
return strdup("Popen failed!"); return strdup("Popen failed!");
} }
while (fgets(buffer, sizeof(buffer), fp) != NULL) { while (fgets(buffer, sizeof(buffer), fp) != NULL) {
size_t chunk_size = strlen(buffer); size_t chunk_size = strlen(buffer);
char *new_output = realloc(output, total_size + chunk_size + 1); char *new_output = realloc(output, total_size + chunk_size + 1);
@ -482,8 +483,9 @@ struct json_object *tool_description_chdir() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -519,13 +521,14 @@ struct json_object *tool_description_index_source_directory() {
struct json_object *required = json_object_new_array(); struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("path")); json_object_array_add(required, json_object_new_string("path"));
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -569,8 +572,9 @@ struct json_object *tool_description_linux_terminal_interactive() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -614,8 +618,9 @@ struct json_object *tool_description_directory_rglob() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -651,12 +656,13 @@ struct json_object *tool_description_read_file() {
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -709,8 +715,9 @@ struct json_object *tool_description_write_file() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -721,12 +728,13 @@ char *tool_function_index_source_directory(char *path) {
return index_directory(path); return index_directory(path);
} }
char * tool_function_mkdir(char *path); char *tool_function_mkdir(char *path);
void ensure_parent_directory_exists(char *path_including_file_name) { void ensure_parent_directory_exists(char *path_including_file_name) {
if(!path_including_file_name) return; if (!path_including_file_name)
return;
char *path = strdup(path_including_file_name); char *path = strdup(path_including_file_name);
char *last_slash = strrchr(path, '/'); char *last_slash = strrchr(path, '/');
if (last_slash != NULL) { if (last_slash != NULL) {
*last_slash = '\0'; *last_slash = '\0';
@ -765,8 +773,8 @@ char *tool_function_read_file(char *path) {
char *tool_function_write_file(char *path, char *content) { char *tool_function_write_file(char *path, char *content) {
db_store_file_version(path); db_store_file_version(path);
ensure_parent_directory_exists(path); ensure_parent_directory_exists(path);
FILE *fp = fopen(path, "w+"); FILE *fp = fopen(path, "w+");
if (fp == NULL) { if (fp == NULL) {
perror("fopen failed"); perror("fopen failed");
return strdup("Failed to open file for writing!"); return strdup("Failed to open file for writing!");
@ -884,7 +892,7 @@ char *tool_function_directory_rglob(char *target_dir) {
struct stat file_stat; struct stat file_stat;
char mod_time[20], create_time[20]; char mod_time[20], create_time[20];
if(!strcmp(target_dir, ".")) { if (!strcmp(target_dir, ".")) {
target_dir[0] = '*'; target_dir[0] = '*';
} }
@ -1005,11 +1013,12 @@ struct json_object *tool_description_http_get() {
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -1052,8 +1061,9 @@ struct json_object *tool_description_directory_glob() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -1089,12 +1099,13 @@ struct json_object *tool_description_linux_terminal() {
json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_object_add(parameters, "additionalProperties",
json_object_new_boolean(0)); json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);
@ -1102,26 +1113,26 @@ struct json_object *tool_description_linux_terminal() {
} }
char *tool_function_mkdir(char *path) { char *tool_function_mkdir(char *path) {
char temp[2048]; char temp[2048];
char *p = NULL; char *p = NULL;
size_t len; size_t len;
snprintf(temp, sizeof(temp), "%s", path); snprintf(temp, sizeof(temp), "%s", path);
len = strlen(temp); len = strlen(temp);
if (temp[len - 1] == '/') { if (temp[len - 1] == '/') {
temp[len - 1] = '\0'; temp[len - 1] = '\0';
} }
for (p = temp + 1; *p; p++) { for (p = temp + 1; *p; p++) {
if (*p == '/') { if (*p == '/') {
*p = '\0'; *p = '\0';
if (mkdir(temp, 0777) != 0 && errno != EEXIST) { if (mkdir(temp, 0777) != 0 && errno != EEXIST) {
return strdup("Failed to create directory!"); return strdup("Failed to create directory!");
} }
*p = '/'; *p = '/';
}
} }
}
return strdup("Directory successfully created."); return strdup("Directory successfully created.");
} }
@ -1133,7 +1144,8 @@ struct json_object *tool_description_mkdir() {
json_object_object_add(function, "name", json_object_new_string("mkdir")); json_object_object_add(function, "name", json_object_new_string("mkdir"));
json_object_object_add( json_object_object_add(
function, "description", function, "description",
json_object_new_string("Creates a new directory with the specified path.")); json_object_new_string(
"Creates a new directory with the specified path."));
struct json_object *parameters = json_object_new_object(); struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object")); json_object_object_add(parameters, "type", json_object_new_string("object"));
@ -1157,8 +1169,9 @@ struct json_object *tool_description_mkdir() {
json_object_object_add(function, "parameters", parameters); json_object_object_add(function, "parameters", parameters);
if(get_use_strict()) if (get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict())); json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function); json_object_object_add(root, "function", function);

103
utils.h
View File

@ -21,87 +21,86 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#else #else
#include <unistd.h>
#include <limits.h> #include <limits.h>
#include <pwd.h> #include <pwd.h>
#include <unistd.h>
#endif #endif
void get_current_directory() { void get_current_directory() {
char buffer[PATH_MAX]; char buffer[PATH_MAX];
#ifdef _WIN32 #ifdef _WIN32
DWORD length = GetCurrentDirectory(PATH_MAX, buffer); DWORD length = GetCurrentDirectory(PATH_MAX, buffer);
if (length > 0 && length < PATH_MAX) { if (length > 0 && length < PATH_MAX) {
printf("Current Directory: %s\n", buffer); printf("Current Directory: %s\n", buffer);
} else { } else {
printf("Error getting current directory.\n"); printf("Error getting current directory.\n");
} }
#else #else
if (getcwd(buffer, sizeof(buffer)) != NULL) { if (getcwd(buffer, sizeof(buffer)) != NULL) {
printf("Current Directory: %s\n", buffer); printf("Current Directory: %s\n", buffer);
} else { } else {
perror("Error getting current directory"); perror("Error getting current directory");
} }
#endif #endif
} }
char* expand_home_directory(const char* path) { char *expand_home_directory(const char *path) {
if (path[0] != '~') { if (path[0] != '~') {
return strdup(path); // Return the original path if it doesn't start with ~ return strdup(path); // Return the original path if it doesn't start with ~
} }
char* home_dir; char *home_dir;
#ifdef _WIN32 #ifdef _WIN32
home_dir = getenv("USERPROFILE"); // Get home directory on Windows home_dir = getenv("USERPROFILE"); // Get home directory on Windows
#else #else
struct passwd* pw = getpwuid(getuid()); struct passwd *pw = getpwuid(getuid());
home_dir = pw->pw_dir; // Get home directory on Linux home_dir = pw->pw_dir; // Get home directory on Linux
#endif #endif
if (home_dir == NULL) { if (home_dir == NULL) {
return NULL; // Error getting home directory return NULL; // Error getting home directory
} }
// Allocate memory for the expanded path // Allocate memory for the expanded path
size_t expanded_size = strlen(home_dir) + strlen(path); size_t expanded_size = strlen(home_dir) + strlen(path);
char* expanded_path = (char *)malloc(expanded_size); char *expanded_path = (char *)malloc(expanded_size);
if (expanded_path == NULL) { if (expanded_path == NULL) {
return NULL; // Memory allocation error return NULL; // Memory allocation error
} }
// Construct the expanded path // Construct the expanded path
snprintf(expanded_path, expanded_size, "%s%s", home_dir, path + 1); snprintf(expanded_path, expanded_size, "%s%s", home_dir, path + 1);
return expanded_path; return expanded_path;
} }
unsigned long hash(const char *str) { unsigned long hash(const char *str) {
unsigned long hash = 5381; // Starting value unsigned long hash = 5381; // Starting value
int c; int c;
while ((c = *str++)) { while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; // hash * 33 + c hash = ((hash << 5) + hash) + c; // hash * 33 + c
} }
return hash; return hash;
} }
char * joinpath(const char *base_url, const char *path) { char *joinpath(const char *base_url, const char *path) {
static char result[1024]; static char result[1024];
result[0] = '\0'; result[0] = '\0';
strcat(result, base_url); strcat(result, base_url);
if(base_url[strlen(base_url) - 1] != '/') { if (base_url[strlen(base_url) - 1] != '/') {
strcat(result, "/"); strcat(result, "/");
} }
if(path[0] == '/') { if (path[0] == '/') {
path++; path++;
} }
strcat(result, path); strcat(result, path);
return result; return result;
} }
char *read_file(const char *path) { char *read_file(const char *path) {