aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--browser.c80
-rw-r--r--util.c8
-rw-r--r--util.h6
3 files changed, 66 insertions, 28 deletions
diff --git a/browser.c b/browser.c
index 5e0396f..f5a10cc 100644
--- a/browser.c
+++ b/browser.c
@@ -13,36 +13,48 @@
#include <regex.h>
#include <util.h>
-int create_socket(const char* ip, const char* port) {
+#define READ_BUFFER_SIZE 512
+
+sds get_page(const char* ip, const char* port, const char* URL) {
/*
* Create socket for connecting with server
*/
- int fd_socket;
- herr(fd_socket = socket(AF_INET, SOCK_STREAM, 0), "socket");
+ int fd_socket = socket(AF_INET, SOCK_STREAM, 0);
+ herr(fd_socket, "socket");
+ int aton_status = 0;
struct sockaddr_in sa_server = {
.sin_family = AF_INET,
- .sin_port = inet_atop(port),
+ .sin_port = atop(port),
+ .sin_addr = aton(ip, &aton_status),
};
- herr(inet_aton(ip, &sa_server.sin_addr.s_addr), "inet_aton");
-
- herr(connect(fd_socket, (struct sockaddr*)&sa_server, sizeof(struct sockaddr_in)), "connect");
+ herr(aton_status, "inet_aton");
- return fd_socket;
-}
+ /*
+ * Request page
+ */
-sds get_page(const int fd_socket, const char* URL) {
+ herr(connect(fd_socket, (struct sockaddr*)&sa_server, sizeof(struct sockaddr_in)), "connect");
write(fd_socket, URL, strlen(URL));
+ /*
+ * Receive page
+ */
+
sds page = sdsempty();
- char buff[512];
- memset(buff, 0, 512);
- while (read(fd_socket, buff, 512)) {
+ char buff[READ_BUFFER_SIZE];
+ clear_arr(buff);
+ while (read(fd_socket, buff, READ_BUFFER_SIZE)) {
page = sdscat(page, buff);
- memset(buff, 0, 512);
+ clear_arr(buff);
}
+
+ /*
+ * Final
+ */
+ close(fd_socket);
return page;
}
@@ -52,15 +64,19 @@ struct md_syntax {
void renderPage(const sds page, const struct md_syntax* syntax, int* *matches, int *matchesCount) {
sds toPrint = sdsdup(page);
+
+ /*
+ * Parse Markdown constructs
+ */
- /* Substitute and register anchors */
+ /* Substitute and store anchors */
toPrint = gsub_getm(toPrint, &syntax->anchor, "\033[4m\1\033[0m\16", matches, matchesCount);
- int anchorInd = 0;
sds newPrint;
- for (int i = 0; i < *matchesCount; i++) {
+ 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);
@@ -69,13 +85,17 @@ void renderPage(const sds page, const struct md_syntax* syntax, int* *matches, i
toPrint = newPrint;
}
+ /*
+ * Print page on stdout
+ */
write(1, toPrint, sdslen(toPrint));
+
sdsfree(toPrint);
}
int main(int argc, char* argv[]) {
/*
- * Preparation for rendering
+ * Compile regexes used in rendering
*/
struct md_syntax md = {
@@ -87,32 +107,40 @@ int main(int argc, char* argv[]) {
* Server-client communication
*/
- sds address = sdsnew(argv[1]);
- int count = 0;
sds page;
+ sds address = sdsnew(argv[1]);
+
int* anchorIndecies = NULL;
int anchorCount = 0;
int gotoIndex = 0;
- int fd_socket;
+ int count = 0;
+
while (count < 2) {
- fd_socket = create_socket("127.0.0.1", "8080");
- page = get_page(fd_socket, address);
+ /*
+ * Get the page
+ */
+
+ page = get_page("127.0.0.1", "8080", address);
+
free(anchorIndecies);
anchorCount = 0;
renderPage(page, &md, &anchorIndecies, &anchorCount);
+ /*
+ * Handle user input
+ */
scanf("%d", &gotoIndex);
+
sdsfree(address);
char* newplace = strchr(page + anchorIndecies[gotoIndex], '(') + 1;
address = sdscatlen(sdsnew(argv[1]), newplace, strchr(newplace, ')') - newplace);
- count++;
- close(fd_socket);
+ count++;
}
+ regfree(&md.anchor);
sdsfree(page);
sdsfree(address);
free(anchorIndecies);
- regfree(&md.anchor);
}
diff --git a/util.c b/util.c
index 439bcf7..f2c464e 100644
--- a/util.c
+++ b/util.c
@@ -7,10 +7,16 @@
#include <errno.h>
#include <regex.h>
-uint16_t inet_atop(const char *port) {
+uint16_t atop(const char *port) {
return htons(atoi(port));
}
+struct in_addr aton(const char* cp, int* output) {
+ struct in_addr inp;
+ *output = inet_aton(cp, &inp);
+ return inp;
+}
+
void herrc(int output, const char* funcName) {
if (output < 0 && errno != EINTR) {
perror(funcName);
diff --git a/util.h b/util.h
index b8936ab..bfa95f7 100644
--- a/util.h
+++ b/util.h
@@ -3,9 +3,12 @@
#include <inttypes.h>
#include <sds/sds.h>
+#include <arpa/inet.h>
#include <regex.h>
-uint16_t inet_atop(const char *port);
+uint16_t atop(const char *port);
+struct in_addr aton(const char* cp, int* output);
+
void herr(int output, const char* funcName);
void herrc(int output, const char* funcName);
@@ -13,5 +16,6 @@ sds gsub(sds str, const regex_t* regex, const char* repl);
sds gsub_getm(sds str, const regex_t *regex, const char* repl, int* *matches, int *matchesCount);
int digits(int num);
+#define clear_arr(arr) memset(arr, 0, sizeof(arr)/sizeof(*arr))
#endif