commit 5ab44094c8d9dc2736501ecff5a7bae648759c29
parent c12a9d015ed92c1b16b6e8ffaef2f0cd132860bc
Author: Nathaniel Chappelle <nathaniel@chappelle.dev>
Date: Sat, 24 Jan 2026 15:10:29 -0800
Callback creates linked list (thread?)
Diffstat:
| M | stamail.c | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++-- |
| A | stamail.h | | | 11 | +++++++++++ |
2 files changed, 62 insertions(+), 2 deletions(-)
diff --git a/stamail.c b/stamail.c
@@ -1,8 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "json.h"
+#include "stamail.h"
-/* Current callback handler */
+/* Callback handler */
+/*
static void print_message(struct message *m, void *ud) {
(void)ud;
@@ -15,6 +17,33 @@ static void print_message(struct message *m, void *ud) {
}
printf("--------------------------------------------------\n");
}
+*/
+
+static struct message_node *head = NULL;
+static struct message_node *tail = NULL;
+
+/* Current callback handler */
+static void collect_message(struct message *m, void *ud) {
+ (void)ud;
+
+ struct message_node *n = malloc(sizeof(struct message_node));
+ if (!n) {
+ perror("malloc");
+ exit(1);
+ }
+
+ /* shallow copy: pointers still reference JSON buffer */
+ n->m = *m;
+ n->next = NULL;
+
+ if (!head) {
+ head = tail = n;
+ } else {
+ tail->next = n;
+ tail = n;
+ }
+}
+
int main(void) {
/* Slurp stdin */
@@ -36,9 +65,29 @@ int main(void) {
if (!buf) return 0;
- parse_mail_json(buf, len, print_message, NULL);
+ parse_mail_json(buf, len, collect_message, NULL);
+
+ for (struct message_node *n = head; n; n = n->next) {
+ struct message *m = &n->m;
+
+ printf("Message-ID: %.*s\n", m->id_len, m->id);
+ printf("From: %.*s\n", m->from_len, m->from);
+ printf("Subject: %.*s\n", m->subject_len, m->subject);
+ printf("Date: %.*s\n", m->date_len, m->date);
+ if (m->body) {
+ printf("Body:\n%.*s\n", m->body_len, m->body);
+ }
+ printf("--------------------------------------------------\n");
+ }
free(buf);
+ struct message_node *n = head;
+ while (n) {
+ struct message_node *next = n->next;
+ free(n);
+ n = next;
+ }
+
return 0;
}
diff --git a/stamail.h b/stamail.h
@@ -0,0 +1,11 @@
+#ifndef STAMAIL_H
+#define STAMAIL_H
+
+#include "json.h"
+
+struct message_node {
+ struct message m;
+ struct message_node *next;
+};
+
+#endif