aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2023-12-20 18:02:24 +0200
committerSyndamia <kamen@syndamia.com>2023-12-20 18:02:24 +0200
commite029c14c984d50f3aa182abf9ac5bbc2d188e2a1 (patch)
tree0f6d6f1b0906a7e74b7742ba5c879b03d69f8c51
parentd9d5d333d041130d4ec3105c641d6dc019398d57 (diff)
downloadpico-web-e029c14c984d50f3aa182abf9ac5bbc2d188e2a1.tar
pico-web-e029c14c984d50f3aa182abf9ac5bbc2d188e2a1.tar.gz
pico-web-e029c14c984d50f3aa182abf9ac5bbc2d188e2a1.zip
[browser] Separated renderpage and handlecli in it's own source file
-rw-r--r--Makefile2
-rw-r--r--browser-stdio.c127
-rw-r--r--browser-stdio.h12
-rw-r--r--browser.c116
4 files changed, 145 insertions, 112 deletions
diff --git a/Makefile b/Makefile
index 0a0f6c5..87b2596 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
+ gcc -g -o browser -I. sds/sds.c util.c browser.c browser-stdio.c
diff --git a/browser-stdio.c b/browser-stdio.c
new file mode 100644
index 0000000..4f2ff3b
--- /dev/null
+++ b/browser-stdio.c
@@ -0,0 +1,127 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <string.h>
+#include <sds/sds.h>
+#include <regex.h>
+#include <util.h>
+
+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
new file mode 100644
index 0000000..3785c10
--- /dev/null
+++ b/browser-stdio.h
@@ -0,0 +1,12 @@
+#ifndef BROWSER_STDIO
+#define BROWSER_STDIO
+
+#include <sds/sds.h>
+
+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 fde1d1d..4b9e853 100644
--- a/browser.c
+++ b/browser.c
@@ -10,8 +10,8 @@
#include <string.h>
#include <sds/sds.h>
-#include <regex.h>
#include <util.h>
+#include <browser-stdio.h>
#define READ_BUFFER_SIZE 512
@@ -58,108 +58,8 @@ sds get_page(const char* ip, const char* port, const char* URL) {
return page;
}
-struct md_syntax {
- regex_t anchor;
-};
-
-void renderPage(const sds page, const struct md_syntax* syntax, int* *matches, int *matchesCount) {
- if (sdslen(page) == 0) {
- printf("Server didn't return page!\n");
- return;
- }
-
- sds toPrint = sdsdup(page);
-
- /*
- * Parse Markdown constructs
- */
-
- /* Substitute and store anchors */
- toPrint = gsub_getm(toPrint, &syntax->anchor, "\033[4m\1\033[0m\16", matches, matchesCount);
-
- sds newPrint;
- for (int i = 0, anchorInd = 0; i < *matchesCount; 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, int* *anchorIndecies, int *anchorCount) {
- // 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 >= *anchorCount) {
- printf("Invalid anchor index!\n");
- return 0;
- }
-
- char* newplace = strchr(page + (*anchorIndecies)[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, "quit") || streq(name, "exit")) {
- return 1;
- }
-
- printf("Invalid command %s!\n", name);
- return 0;
-}
-
int main(int argc, char* argv[]) {
- /*
- * Compile regexes used in rendering
- */
-
- struct md_syntax md = {
- .anchor = NULL,
- };
- herr(regcomp(&md.anchor, "\\[\\([^]]*\\)\\](\\([^)]*\\))", 0), "regcomp");
+ initRendering();
/*
* Server-client communication
@@ -169,9 +69,6 @@ int main(int argc, char* argv[]) {
sds authority = sdsnew(argv[1]);
sds address = sdsdup(authority);
- int* anchorIndecies = NULL;
- int anchorCount = 0;
-
int stopProgram = 0;
while (!stopProgram) {
/*
@@ -180,19 +77,16 @@ int main(int argc, char* argv[]) {
printf("\033[30;107m%s\033[0m\n", address);
page = get_page("127.0.0.1", "8080", address);
- renderPage(page, &md, &anchorIndecies, &anchorCount);
+ renderPage(page);
/*
* Handle user input
*/
- stopProgram = handleCLI(authority, &address, page, &anchorIndecies, &anchorCount);
- free(anchorIndecies);
- anchorIndecies = NULL;
- anchorCount = 0;
+ stopProgram = handleCLI(authority, &address, page);
sdsfree(page);
}
- regfree(&md.anchor);
+ freeRendering();
sdsfree(address);
sdsfree(authority);
}