aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--browser.c30
-rw-r--r--util.c25
-rw-r--r--util.h2
3 files changed, 50 insertions, 7 deletions
diff --git a/browser.c b/browser.c
index c2588d3..fe36782 100644
--- a/browser.c
+++ b/browser.c
@@ -8,8 +8,28 @@
#include <stdio.h>
#include <string.h>
+#include <sds/sds.h>
#include <util.h>
+sds get_page(const int fd_socket) {
+ sds page = sdsempty();
+
+ char buff[512];
+ memset(buff, 0, 512);
+ while (read(fd_socket, buff, 512)) {
+ page = sdscat(page, buff);
+ memset(buff, 0, 512);
+ }
+
+ return page;
+}
+
+void renderPage(const sds page) {
+ sds toPrint = gsub(page, "\\*[^*]*\\*", "emph");
+ write(1, toPrint, sdslen(toPrint));
+ sdsfree(toPrint);
+}
+
int main(int argc, char* argv[]) {
int fd_socket;
herr(fd_socket = socket(AF_INET, SOCK_STREAM, 0), "socket");
@@ -25,13 +45,9 @@ int main(int argc, char* argv[]) {
/* char msg[] = "hello@/test.txt"; */
write(fd_socket, argv[1], strlen(argv[1]));
- char buff[256];
- memset(buff, 0, sizeof(buff));
- while (read(fd_socket, buff, 256)) {
- write(1, buff, strlen(buff));
- memset(buff, 0, sizeof(buff));
- }
- read(fd_socket, buff, 256);
+ sds page = get_page(fd_socket);
+ renderPage(page);
+ sdsfree(page);
close(fd_socket);
}
diff --git a/util.c b/util.c
index 6a0c0a3..e210e34 100644
--- a/util.c
+++ b/util.c
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <errno.h>
+#include <regex.h>
uint16_t inet_atop(const char *port) {
return htons(atoi(port));
@@ -21,3 +22,27 @@ void herr(int output, const char* funcName) {
exit(errno);
}
}
+
+sds gsub(const sds str, const char* regex, const char* repl) {
+ regex_t preg;
+ regcomp(&preg, regex, 0);
+
+ int strInd = 0;
+ regmatch_t pmatch[1] = {
+ { .rm_so = 0, .rm_eo = 0, },
+ };
+
+ sds ret = sdsempty();
+ // sdslen is in O(1) time
+ while (strInd < sdslen(str)) {
+ if (regexec(&preg, str + strInd, 1, pmatch, 0) > 0) {
+ ret = sdscat(ret, str + strInd);
+ break;
+ }
+ ret = sdscatlen(ret, str + strInd, pmatch[0].rm_so);
+ ret = sdscat(ret, repl);
+ strInd += pmatch[0].rm_eo;
+ }
+
+ return ret;
+}
diff --git a/util.h b/util.h
index 13ee5e5..7ea5311 100644
--- a/util.h
+++ b/util.h
@@ -2,9 +2,11 @@
#define H_UTIL
#include <inttypes.h>
+#include <sds/sds.h>
uint16_t inet_atop(const char *port);
void herr(int output, const char* funcName);
void herrc(int output, const char* funcName);
+sds gsub(sds str, const char* regex, const char* repl);
#endif