commit 954c5e11f88b8b4ee5ed64e9c7ea5930dc69dfe5
parent 90b5d3f382af8c90a5e31c132059b1be209d92c3
Author: aabacchus <ben@bvnf.space>
Date: Mon, 5 Dec 2022 16:28:15 +0000
history
Diffstat:
| M | xmhtml.c | | | 54 | +++++++++++++++++++++++++++++++++--------------------- |
1 file changed, 33 insertions(+), 21 deletions(-)
diff --git a/xmhtml.c b/xmhtml.c
@@ -53,10 +53,13 @@
*
*****/
+#define _XOPEN_SOURCE 500
+
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Xm/Xm.h>
@@ -71,9 +74,10 @@ static Widget html;
struct history {
struct history *prev;
- char href[];
+ char *href;
};
static struct history *history_top;
+static char *current_page;
/*****
* Change this to change the application class of the examples
@@ -100,34 +104,43 @@ exitCB(Widget widget, XtPointer client_data, XtPointer call_data)
static void
history_init(void) {
+ current_page = NULL;
history_top = NULL;
}
static void
history_push(String href) {
- struct history *h = calloc(sizeof (struct history) + strlen(href) + 1, 1);
+ struct history *h = calloc(sizeof (struct history), 1);
if (h == NULL) {
perror("malloc");
exit(1);
}
- strcpy(h->href, href);
+ h->href = strdup(href);
+ if (h->href == NULL) {
+ free(h);
+ perror("malloc");
+ exit(1);
+ }
+
h->prev = history_top;
history_top = h;
+
+ fprintf(stdout, "added history \"%s\"\n", h->href);
}
+/*
+ * Caller must free the returned String.
+ */
static String
history_pop(void) {
+ String s;
+ struct history *old = history_top;
if (history_top == NULL)
return NULL;
- struct history *old = history_top;
- String s = malloc(strlen(history_top->href) + 1);
- if (s == NULL) {
- perror("malloc");
- exit(1);
- }
- strcpy(s, history_top->href);
+
history_top = history_top->prev;
+ s = old->href;
free(old);
return s;
}
@@ -158,11 +171,14 @@ anchorCB(Widget widget, XtPointer client_data,
cbs->doit = True;
cbs->visited = True;
- fprintf(stderr, "clicked link \"%s\"\n", cbs->href);
+ fprintf(stdout, "clicked link \"%s\"\n", cbs->href);
String content = loadFile(cbs->href);
if (content != NULL) {
+ history_push(current_page);
+ free(current_page);
+ current_page = strdup(cbs->href);
+ /* XmHTMLTextSetString will mess up cbs, so need to use cbs first */
XmHTMLTextSetString(html, content);
- history_push(cbs->href);
}
free(content);
}
@@ -170,10 +186,10 @@ anchorCB(Widget widget, XtPointer client_data,
static void
history_print(void) {
struct history *h = history_top;
- fprintf(stderr, "History:\n");
+ fprintf(stdout, "History:\n");
for (; h != NULL; h = h->prev)
- fprintf(stderr, "%s\n", h->href);
- fprintf(stderr, "\n");
+ fprintf(stdout, "%s\n", h->href);
+ fprintf(stdout, "\n");
}
static void
@@ -182,7 +198,7 @@ backCB(Widget widget, XtPointer client_data, XtPointer call_data) {
String b = history_pop();
if (b == NULL)
return;
- fprintf(stderr, "loading history \"%s\"\n", b);
+ fprintf(stdout, "loading history \"%s\"\n", b);
String content = loadFile(b);
if (content != NULL)
XmHTMLTextSetString(html, content);
@@ -221,8 +237,6 @@ loadFile(String filename)
file_to_open = "index.html";
}
- size = sb.st_size;
-
/* open the given file */
if((file = fopen(file_to_open, "r")) == NULL)
{
@@ -237,8 +251,6 @@ loadFile(String filename)
if (size == -1)
perror("ftell");
rewind(file);
- if (size != sb.st_size)
- fprintf(stderr, "size %d != st_size %ld\n", size, sb.st_size);
/* allocate a buffer large enough to contain the entire file */
if((content = malloc(size+1)) == NULL)
@@ -355,7 +367,7 @@ main(int argc, char **argv)
"file</body></html>");
else
{
- history_push(argv[1]);
+ current_page = strdup(argv[1]);
XmHTMLTextSetString(html, content);
free(content);
}