From 01412686f2aa064299c97697c4d9efe03d7492b8 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 20 Dec 2023 18:12:27 +0200 Subject: [browser] Renamed browser-stdio to browser-cli --- Makefile | 2 +- browser-cli.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ browser-cli.h | 12 ++++++ browser-stdio.c | 127 -------------------------------------------------------- browser-stdio.h | 12 ------ browser.c | 2 +- 6 files changed, 141 insertions(+), 141 deletions(-) create mode 100644 browser-cli.c create mode 100644 browser-cli.h delete mode 100644 browser-stdio.c delete mode 100644 browser-stdio.h diff --git a/Makefile b/Makefile index 87b2596..cc77da1 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,4 @@ server: .PHONY: browser browser: - gcc -g -o browser -I. sds/sds.c util.c browser.c browser-stdio.c + gcc -g -o browser -I. sds/sds.c util.c browser.c browser-cli.c diff --git a/browser-cli.c b/browser-cli.c new file mode 100644 index 0000000..4f2ff3b --- /dev/null +++ b/browser-cli.c @@ -0,0 +1,127 @@ +#include +#include +#include + +#include +#include +#include +#include + +struct md_syntax { + regex_t anchor; +}; + +struct md_syntax syntax = { + .anchor = NULL, +}; + +int* anchorsIndecies; +int anchorsCount = 0; + +void initRendering() { + /* + * Compile regexes used in rendering + */ + + herr(regcomp(&syntax.anchor, "\\[\\([^]]*\\)\\](\\([^)]*\\))", 0), "regcomp"); +} + +void freeRendering() { + regfree(&syntax.anchor); + + free(anchorsIndecies); +} + +void renderPage(const sds page) { + if (sdslen(page) == 0) { + printf("Server didn't return page!\n"); + return; + } + + sds toPrint = sdsdup(page); + + /* + * Parse Markdown constructs + */ + + /* Substitute and store anchorsIndecies */ + if (anchorsIndecies != NULL) { + free(anchorsIndecies); + anchorsCount = 0; + anchorsIndecies = NULL; + } + toPrint = gsub_getm(toPrint, &syntax.anchor, "\033[4m\1\033[0m\16", &anchorsIndecies, &anchorsCount); + + sds newPrint; + for (int i = 0, anchorInd = 0; i < anchorsCount; i++) { + anchorInd = strchr(toPrint, '\16') - toPrint; + + /* In toPrint, replace '\16' with "\033[30;46m%d\033[0m", where %d is the variable i */ + toPrint[anchorInd] = '\0'; + newPrint = sdsgrowzero(sdsempty(), sdslen(toPrint) + digits(i) + 8 + 4); + sprintf(newPrint, "%s\033[30;46m%d\033[0m%s", toPrint, i, toPrint + anchorInd + 1); + + sdsfree(toPrint); + toPrint = newPrint; + } + + /* + * Print page on stdout + */ + write(1, toPrint, sdslen(toPrint)); + + sdsfree(toPrint); +} + +#define MAX_LEN_COMMAND 16 +#define COMMAND_FORMAT ": %16s" + +int handleCLI(sds authority, sds *address, const sds page) { + // Get a line + char line[1024]; + fgets(line, 1024, stdin); + + // Nothing + if (line[0] == '\0') { + printf("Please enter a valid command!\n"); + return 0; + } + + // Number or URL + if (line[0] != ':') { + // Index of anchor + if (strchr(line, '/') == NULL) { + int gotoIndex = 0; + sscanf(line, "%d", &gotoIndex); + + if (gotoIndex < 0 || gotoIndex >= anchorsCount) { + printf("Invalid anchor index!\n"); + return 0; + } + + char* newplace = strchr(page + anchorsIndecies[gotoIndex], '(') + 1; + sdsfree(*address); + *address = sdscatlen(sdsdup(authority), newplace, strchr(newplace, ')') - newplace); + } + // New address + else { + sdsfree(*address); + *address = sdsnewlen(line, strlen(line)-1 /* skip newline */); + } + return 0; + } + + // Special command + + // Get command name and it's arguments + // Currently no command takes arguments + char name[MAX_LEN_COMMAND+1] = { '\0' }; + int argsAssigned = sscanf(line, COMMAND_FORMAT, name); + + if (streq(name, "q") || streq(name, "e") || streq(name, "quit") || streq(name, "exit")) { + return 1; + } + + printf("Invalid command %s!\n", name); + return 0; +} diff --git a/browser-cli.h b/browser-cli.h new file mode 100644 index 0000000..39f89bf --- /dev/null +++ b/browser-cli.h @@ -0,0 +1,12 @@ +#ifndef BROWSER_CLI +#define BROWSER_CLI + +#include + +void initRendering(); +void freeRendering(); + +void renderPage(const sds page); +int handleCLI(sds authority, sds *address, const sds page); + +#endif diff --git a/browser-stdio.c b/browser-stdio.c deleted file mode 100644 index 4f2ff3b..0000000 --- a/browser-stdio.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -struct md_syntax { - regex_t anchor; -}; - -struct md_syntax syntax = { - .anchor = NULL, -}; - -int* anchorsIndecies; -int anchorsCount = 0; - -void initRendering() { - /* - * Compile regexes used in rendering - */ - - herr(regcomp(&syntax.anchor, "\\[\\([^]]*\\)\\](\\([^)]*\\))", 0), "regcomp"); -} - -void freeRendering() { - regfree(&syntax.anchor); - - free(anchorsIndecies); -} - -void renderPage(const sds page) { - if (sdslen(page) == 0) { - printf("Server didn't return page!\n"); - return; - } - - sds toPrint = sdsdup(page); - - /* - * Parse Markdown constructs - */ - - /* Substitute and store anchorsIndecies */ - if (anchorsIndecies != NULL) { - free(anchorsIndecies); - anchorsCount = 0; - anchorsIndecies = NULL; - } - toPrint = gsub_getm(toPrint, &syntax.anchor, "\033[4m\1\033[0m\16", &anchorsIndecies, &anchorsCount); - - sds newPrint; - for (int i = 0, anchorInd = 0; i < anchorsCount; i++) { - anchorInd = strchr(toPrint, '\16') - toPrint; - - /* In toPrint, replace '\16' with "\033[30;46m%d\033[0m", where %d is the variable i */ - toPrint[anchorInd] = '\0'; - newPrint = sdsgrowzero(sdsempty(), sdslen(toPrint) + digits(i) + 8 + 4); - sprintf(newPrint, "%s\033[30;46m%d\033[0m%s", toPrint, i, toPrint + anchorInd + 1); - - sdsfree(toPrint); - toPrint = newPrint; - } - - /* - * Print page on stdout - */ - write(1, toPrint, sdslen(toPrint)); - - sdsfree(toPrint); -} - -#define MAX_LEN_COMMAND 16 -#define COMMAND_FORMAT ": %16s" - -int handleCLI(sds authority, sds *address, const sds page) { - // Get a line - char line[1024]; - fgets(line, 1024, stdin); - - // Nothing - if (line[0] == '\0') { - printf("Please enter a valid command!\n"); - return 0; - } - - // Number or URL - if (line[0] != ':') { - // Index of anchor - if (strchr(line, '/') == NULL) { - int gotoIndex = 0; - sscanf(line, "%d", &gotoIndex); - - if (gotoIndex < 0 || gotoIndex >= anchorsCount) { - printf("Invalid anchor index!\n"); - return 0; - } - - char* newplace = strchr(page + anchorsIndecies[gotoIndex], '(') + 1; - sdsfree(*address); - *address = sdscatlen(sdsdup(authority), newplace, strchr(newplace, ')') - newplace); - } - // New address - else { - sdsfree(*address); - *address = sdsnewlen(line, strlen(line)-1 /* skip newline */); - } - return 0; - } - - // Special command - - // Get command name and it's arguments - // Currently no command takes arguments - char name[MAX_LEN_COMMAND+1] = { '\0' }; - int argsAssigned = sscanf(line, COMMAND_FORMAT, name); - - if (streq(name, "q") || streq(name, "e") || streq(name, "quit") || streq(name, "exit")) { - return 1; - } - - printf("Invalid command %s!\n", name); - return 0; -} diff --git a/browser-stdio.h b/browser-stdio.h deleted file mode 100644 index 3785c10..0000000 --- a/browser-stdio.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef BROWSER_STDIO -#define BROWSER_STDIO - -#include - -void initRendering(); -void freeRendering(); - -void renderPage(const sds page); -int handleCLI(sds authority, sds *address, const sds page); - -#endif diff --git a/browser.c b/browser.c index 4b9e853..4d29478 100644 --- a/browser.c +++ b/browser.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #define READ_BUFFER_SIZE 512 -- cgit v1.2.3