-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
362 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/bin | ||
**/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |