Skip to content

Commit

Permalink
Begin store
Browse files Browse the repository at this point in the history
  • Loading branch information
konect-V committed Jun 11, 2024
1 parent b73a5b4 commit 1dcac4c
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 1 deletion.
25 changes: 25 additions & 0 deletions bootstrap/sys-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,31 @@ packages:
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@/bin/.'
- '@SOURCE_ROOT@/target/boot_disk_kot_mount/.'

- name: store
source:
subdir: 'sources/core/apps'
tools_required:
- host-gcc
pkgs_required:
- mlibc
- curl
build:
- args:
- 'make'
- '-C'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@'
- 'build'
environ:
CC: "@OPTION:cc@"
CXX: "@OPTION:cxx@"
LD: "@OPTION:ld@"
ASMC: "@OPTION:asmc@"
- args:
- 'cp'
- '-r'
- '@THIS_SOURCE_DIR@/target/@OPTION:arch_name@/bin/.'
- '@SOURCE_ROOT@/target/boot_disk_kot_mount/.'

- name: bash
source:
git: 'https://github.com/bminor/bash'
Expand Down
31 changes: 30 additions & 1 deletion bootstrap/sys-libs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,33 @@ packages:
done
rm -vf @THIS_COLLECT_DIR@/usr/lib/libcursesw.so
echo "INPUT(-lncursesw)" > @THIS_COLLECT_DIR@/usr/lib/libcursesw.so
ln -sfv libncurses.so @THIS_COLLECT_DIR@/usr/lib/libcurses.so
ln -sfv libncurses.so @THIS_COLLECT_DIR@/usr/lib/libcurses.so
- name: cjson
source:
subdir: 'bundled'
git: 'https://github.com/DaveGamble/cJSON'
tag: 'v1.7.18'
version: '1.7.18'
tools_required:
- host-gcc
pkgs_required:
- mlibc
revision: 4
configure:
- args:
- 'cmake'
- '@THIS_SOURCE_DIR@'
- '-DENABLE_CJSON_UTILS=On'
- '-DENABLE_CJSON_TEST=Off'
- '-DCMAKE_INSTALL_PREFIX=/usr'
- '-DBUILD_SHARED_LIBS=Off'
environ:
CC: "@OPTION:cc@"
CXX: "@OPTION:cxx@"
LD: "@OPTION:ld@"
ASMC: "@OPTION:asmc@"

build:
- args: ['make', '-j@PARALLELISM@']
- args: ['make', 'DESTDIR=@THIS_COLLECT_DIR@', 'install']
126 changes: 126 additions & 0 deletions sources/core/apps/store/source/apps/apps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>

#include "apps.h"

static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp){
fetch_apps_data_t* buffer_info = (fetch_apps_data_t*)userp;
size_t real_size = size * nmemb;

buffer_info->size += real_size;
buffer_info->buffer = realloc(buffer_info->buffer, buffer_info->size);

memcpy(&(buffer_info->buffer[buffer_info->size - real_size]), contents, real_size);

return real_size;
}

fetch_apps_data_t* fetch_apps_data(void){
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl){
fetch_apps_data_t* buffer_info = malloc(sizeof(fetch_apps_data_t));
buffer_info->buffer = NULL;
buffer_info->size = 0;
curl_easy_setopt(curl, CURLOPT_URL, "https://kot-store.github.io/apps/");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer_info);
curl_easy_setopt(curl, CURLOPT_CAINFO, "/usr/etc/ssl/cert.pem");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);

if(res != CURLE_OK){
fprintf(stderr, "Error : curl_%s\n", curl_easy_strerror(res));
free(buffer_info->buffer);
free(buffer_info);
return NULL;
}else{
return buffer_info;
}

curl_easy_cleanup(curl);
}
curl_global_cleanup();

return NULL;
}

char* find_apps_url_by_name(char* name){
fetch_apps_data_t* data_info = fetch_apps_data();
if(data_info != NULL){
cJSON* root = cJSON_Parse(data_info->buffer);
cJSON* applications = cJSON_GetObjectItem(root, "applications");

for(int i = 0 ; i < cJSON_GetArraySize(applications); i++){
cJSON* application = cJSON_GetArrayItem(applications, i);
cJSON* application_name = cJSON_GetObjectItem(application, "name");
if(cJSON_IsString(application_name) && (application_name->valuestring != NULL)){
if(!strcmp(name, application_name->valuestring)){
cJSON* json_link_application_url = cJSON_GetObjectItem(application, "json_link");
if(cJSON_IsString(json_link_application_url) && (json_link_application_url->valuestring != NULL)){
char* return_value = malloc(strlen(json_link_application_url->valuestring) + 1);
strcpy(return_value, json_link_application_url->valuestring);
cJSON_Delete(root);

free(data_info->buffer);
free(data_info);

return return_value;
}
}
}
}

cJSON_Delete(root);

free(data_info->buffer);
free(data_info);
}

return NULL;
}

char** find_apps_url_by_tag(char* tag){
fetch_apps_data_t* data_info = fetch_apps_data();
char** return_value = NULL;

if(data_info != NULL){
cJSON* root = cJSON_Parse(data_info->buffer);
cJSON* applications = cJSON_GetObjectItem(root, "applications");

size_t return_value_count_url = 0;
for(int i = 0 ; i < cJSON_GetArraySize(applications); i++){
cJSON* application = cJSON_GetArrayItem(applications, i);
cJSON* application_tags = cJSON_GetObjectItem(application, "tags");

for(int y = 0 ; y < cJSON_GetArraySize(applications); y++){
cJSON* application_tag = cJSON_GetArrayItem(application_tags, y);
if(cJSON_IsString(application_tag) && (application_tag->valuestring != NULL)){
if(!strcmp(tag, application_tag->valuestring)){
cJSON* json_link_application_url = cJSON_GetObjectItem(application, "json_link");
if(cJSON_IsString(json_link_application_url) && (json_link_application_url->valuestring != NULL)){
return_value = realloc(return_value, (return_value_count_url + 2) * sizeof(char*));
return_value[return_value_count_url + 1] = NULL;
return_value[return_value_count_url] = malloc(strlen(json_link_application_url->valuestring) + 1);
strcpy(return_value[return_value_count_url], json_link_application_url->valuestring);
return_value_count_url++;
}
}
}
}
}

cJSON_Delete(root);

free(data_info->buffer);
free(data_info);
}

return return_value;
}
15 changes: 15 additions & 0 deletions sources/core/apps/store/source/apps/apps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef APPS_H
#define APPS_H

#include <stddef.h>

typedef struct{
char* buffer;
size_t size;
}fetch_apps_data_t;

fetch_apps_data_t* fetch_apps_data(void);
char* find_apps_url_by_name(char* name);
char** find_apps_url_by_tag(char* tag);

#endif // APPS_H
125 changes: 125 additions & 0 deletions sources/core/apps/store/source/core/core.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "../apps/apps.h"

void print_help(){
printf("For help: ./store --help\n");
printf("To install an application: ./store --install [app name]\n");
}

int main(int argc, char *argv[]){
CURL *curl;
CURLcode res;

if(argc > 1){
if(!strcmp(argv[1], "--help")){
print_help();
return 0;
}else if(!strcmp(argv[1], "--install") && argc == 2){
char search_mode[3];
printf("Search with > T(tag)/N(name)\n");
fgets(search_mode, sizeof(search_mode), stdin);
search_mode[strcspn(search_mode, "\n")] = 0;
if(!strcmp("T", search_mode)){
char tag[512];
printf("What is the tag of the application you want to install?\n");
fgets(tag, sizeof(tag), stdin);
tag[strcspn(tag, "\n")] = 0;

char** url = find_apps_url_by_tag(tag);

if(url != NULL){
int i = 0;
while(url[i] != NULL){
printf("%d) %s\n", i, url[i]);
i++;
}

char link_index[10];
printf("Select the index you want to install :\n");
fgets(link_index, sizeof(link_index), stdin);
link_index[strcspn(link_index, "\n")] = 0;

int index_to_install = atoi(link_index);

if(index_to_install >= 0 && index_to_install < i){
char allow_install[3];
printf("%s > Would you like to install it? (Y/N)\n", url[index_to_install]);
fgets(allow_install, sizeof(allow_install), stdin);
allow_install[strcspn(allow_install, "\n")] = 0;
if(!strcmp("Y", allow_install)){
// TODO
}else{
printf("Cancel the installation\n");
}
}else{
printf("Unknow index !\n");
}

free(url);
return 0;
}else{
printf("No application found with tag: %s. Please check the spelling or try a different tag.\n", tag);
return 0;
}
}else if(!strcmp("N", search_mode)){
char name[512];
printf("What is the name of the application you want to install?\n");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;

char* url = find_apps_url_by_name(name);

if(url != NULL){
char allow_install[3];
printf("%s found in the store. Would you like to install it? (Y/N)\n", name);
fgets(allow_install, sizeof(allow_install), stdin);
allow_install[strcspn(allow_install, "\n")] = 0;
if(!strcmp("Y", allow_install)){
// TODO
}else{
printf("Cancel the installation\n");
}
free(url);
return 0;
}else{
printf("Can't find %s in the store. Did you spell it correctly?\n", name);
return 0;
}
}else{
printf("Unknow search method !\n");
return 0;
}
}else if(!strcmp(argv[1], "--install") && argc == 3){
char* name = argv[2];

char* url = find_apps_url_by_name(name);

if(url != NULL){
char allow_install[3];
printf("%s found in the store. Would you like to install it? (Y/N)\n", name);
fgets(allow_install, sizeof(allow_install), stdin);
allow_install[strcspn(allow_install, "\n")] = 0;
if(!strcmp("Y", allow_install)){
// TODO
}else{
printf("Cancel the installation\n");
}
free(url);
return 0;
}else{
printf("Can't find %s in the store. Did you spell it correctly?\n", name);
return 0;
}
}else{
print_help();
}
}else{
print_help();
}

return 0;
}
2 changes: 2 additions & 0 deletions sources/core/apps/store/target/amd64/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/bin
**/lib
39 changes: 39 additions & 0 deletions sources/core/apps/store/target/amd64/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cyan = /bin/echo -e "\x1b[36m\#\# $1\x1b[0m"

# Project Root
override HOME = ../..

# Project Resources
SYSROOT = $(HOME)/../../../../sysroot
INCLUDE = $(SYSROOT)/usr/include
LIBRARIES = $(SYSROOT)/usr/lib
SOURCE = $(HOME)/source
TOOLS = $(HOME)/../../tools
BIN = bin/usr/bin
LIB = lib

# Tools Config
CFLAGS =

LDFLAGS = -Wall \
-lc

# Recursive Wild Card
rwildcard = $(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))

# Source Files
C_SRC = $(call rwildcard,$(SOURCE),*.c)

OBJS = $(patsubst $(SOURCE)/%.c,$(LIB)/%_c.o,$(C_SRC))

# Target
$(LIB)/%_c.o: $(SOURCE)/%.c
@ mkdir -m 777 -p $(@D)
@ $(call cyan,"$(subst ../,,$^)")
@ $(CC) $(CFLAGS) -c $^ -o $@

link:
@ mkdir -m 777 -p $(BIN)
@ $(CC) $(LDFLAGS) -o $(BIN)/store $(OBJS) $(LIBRARIES)/libcurl.a $(LIBRARIES)/libtls.a $(LIBRARIES)/libssl.a $(LIBRARIES)/libcjson.a

build: $(OBJS) link

0 comments on commit 1dcac4c

Please sign in to comment.