Go Back

0 /*

1 * ISC License

2 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>

3 */

4 #include <stdio.h>

5 #include <stdlib.h>

6 #include <string.h>

7 #include <stdint.h>

8 #include "macro.h"

9 #include "strlcpy.h"

10 #include "error.h"

11 #include "config.h"

12 #define ABOUT_INTERNAL

13 #include "about.h"

14 #define HISTORY_INTERNAL

15 #include "history.h"

16 #include "utf8.h"

17

18 #define MAXIMUM_LIST_LENGTH 2000

19

20 int about_history_param(const char *param) {

21 if (strcmp(param, "clear")) return ERROR_INVALID_ARGUMENT;

22 return history_clear();

23 }

24

25 int about_history(char **out, size_t *length_out) {

26

27 size_t length = 0, i = 0;

28 char *data = NULL;

29 struct history_entry *entry;

30 const char title[] = "# History\n\n=>clear Clear History\n\n";

31

32 if (!(data = dyn_strcat(NULL, &length, V(header)))) goto fail;

33 if (!(data = dyn_strcat(data, &length, V(title)))) goto fail;

34 if (!config.enableHistory) {

35 const char str[] = "History is disabled\n";

36 if (!(data = dyn_strcat(data, &length, V(str)))) goto fail;

37 }

38

39 for (entry = history; entry; entry = entry->next) {

40 char buf[sizeof(*entry)];

41 /* TODO: handle title as utf8 */

42 int len = snprintf(V(buf), "=>%s %s\n",

43 entry->url, entry->title) + 1;

44 if (!(data = dyn_strcat(data, &length, buf, len))) goto fail;

45 if (i++ == MAXIMUM_LIST_LENGTH) break;

46 }

47

48 *out = data;

49 *length_out = length;

50 return 0;

51 fail:

52 free(data);

53 return ERROR_MEMORY_FAILURE;

54 }

55