Skip to content

Commit

Permalink
move update commands to a seperate functions, add func signs to the h…
Browse files Browse the repository at this point in the history
…eader of main.h
  • Loading branch information
BaseMax committed Dec 28, 2024
1 parent a17680a commit d590c8f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 35 deletions.
75 changes: 40 additions & 35 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,8 @@ void run(bool isCode, const char *path, char *content, char *build_dir) {

lexer_lex(lexer);

// lexer_debug(lexer);

// lexer_save(lexer, "tokens.txt");

ast_t *ast = parser_parse(lexer);

// ast_debug(ast);

generator_t *generator = generator_create(ast);

if (isCode == false && build_dir != NULL) {
Expand All @@ -105,8 +99,6 @@ void run(bool isCode, const char *path, char *content, char *build_dir) {

generator_code(generator);

// generator_debug(generator);

if (isCode == true) {
if (build_dir == NULL) {
printf("%s\n", generator->html->data);
Expand Down Expand Up @@ -172,6 +164,45 @@ void help(char *app) {
exit(1);
}

/**
*
* @function update
* @brief Update and download new version
* @params {void}
* @returns {void}
*
*/
void update()
{
printf("Check latest version...\n");

const char *output_file = "update.tmp";
const char *port = "80";
const char *hostname = "versions.salamlang.ir";

#ifdef _WIN32
const char *path = "/latest/windows";
#elif __APPLE__
const char *path = "/latest/macos";
#elif __linux__
const char *path = "/latest/linux";
#else
printf("Unsupported OS\n");
exit(1);
#endif

FILE *fp = fopen(output_file, "wb");

printf("Connecting to the server...\n");
bool res = download(fp, port, hostname, path);

if (res == true) {
printf("Download successful.\n");
} else {
printf("Download failed, something went wrong.\n");
}
}

/**
*
* @function doargs
Expand All @@ -197,33 +228,7 @@ void doargs(int argc, char **argv) {
} else if (strcmp(path, "help") == 0) {
help(argv[0]);
} else if (strcmp(path, "update") == 0) {
printf("Check latest version...\n");

const char *output_file = "update.tmp";
const char *port = "80";
const char *hostname = "versions.salamlang.ir";

#ifdef _WIN32
const char *path = "/latest/windows";
#elif __APPLE__
const char *path = "/latest/macos";
#elif __linux__
const char *path = "/latest/linux";
#else
printf("Unsupported OS\n");
exit(1);
#endif

FILE *fp = fopen(output_file, "wb");

printf("Connecting to the server...\n");
bool res = download(fp, port, hostname, path);

if (res == true) {
printf("Download successful.\n");
} else {
printf("Download failed, something went wrong.\n");
}
update();
} else if (strcmp(path, "lint") == 0) {
if (argc <= 2) {
error(1, "Usage: %s lint <file>\n", argv[0]);
Expand Down
68 changes: 68 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,72 @@
#include "validator.h"
#include "validator_style.h"

/**
*
* @function lint
* @brief Linting the given content and parameters
* @params {bool} isCode - Whether the content is code or file
* @params {const char*} path - Path of the file
* @params {char*} content - Content of the file
* @params {char*} build_file - Build file
* @returns {void}
*
*/
void lint(bool isCode, const char *path, char *content, char *build_file);

/**
*
* @function run
* @brief Running the compiler with the given content and parameters
* @params {bool} isCode - Whether the content is code or file
* @params {const char*} path - Path of the file
* @params {char*} content - Content of the file
* @params {char*} build_dir - Build directory
* @returns {void}
*
*/
void run(bool isCode, const char *path, char *content, char *build_dir);

/**
*
* @function help
* @brief Display help message
* @params {char*} app - Application name
* @returns {void}
*
*/
void help(char *app);

/**
*
* @function update
* @brief Update and download new version
* @params {void}
* @returns {void}
*
*/
void update();

/**
*
* @function doargs
* @brief Handle command line arguments
* @params {int} argc - Number of arguments
* @params {char**} argv - Array of arguments
* @returns {void}
*
*/
void doargs(int argc, char **argv);

/**
*
* @function main
* @brief Main entry point
* @params {int} argc - Number of arguments
* @params {char**} argv - Array of arguments
* @returns {int}
*
*/
int main(int argc, char **argv);

#endif

0 comments on commit d590c8f

Please sign in to comment.