sxirc

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1d4045a1532e5f66b2758417b925c646d943852b
parent 2b9b38098412ce5fcea2fd38417511d983abbcb6
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 29 Nov 2022 15:07:49 +0000

use one function to add text, poll stdin

Diffstat:
MMakefile | 4++--
Ma.c | 66++++++++++++++++++++++++++++++++++++++++++------------------------
2 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,8 +1,8 @@ .POSIX: BIN = sxtest -LIBS = -lsx -CFLAGS = -Wall -Wextra -pedantic -Og -g +LIBS = -lsx -lXt +CFLAGS = -Wall -Wextra -pedantic -Wno-unused-parameter -Og -g $(BIN): a.c $(CC) a.c $(CFLAGS) -o $(BIN) $(LIBS) diff --git a/a.c b/a.c @@ -1,5 +1,6 @@ #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <libsx.h> struct textpair { @@ -7,44 +8,49 @@ struct textpair { }; void -submit_cb(Widget w, void *d) { - struct textpair *tp = (struct textpair *)d; - char *new, *old, *s; - new = GetTextWidgetText(tp->in); - old = GetTextWidgetText(tp->out); - if (new == NULL || old == NULL) { - fprintf(stderr, "error getting text\n"); +add_text_to_display_newline(char *s, Widget w) { + size_t len; + char *old, *new; + + old = GetTextWidgetText(w); + if (old == NULL) { + fprintf(stderr, "error getting old text\n"); exit(1); } - size_t len = strlen(old); - s = malloc(strlen(new) + len + 2); - if (s == NULL) { + len = strlen(old); + new = calloc(strlen(s) + len + 2, 1); + if (new == NULL) { perror("malloc"); exit(1); } - strcpy(s, old); + strcpy(new, old); if (len > 0 && old[len - 1] != '\n') - strcat(s, "\n"); - strcat(s, new); + strcat(new, "\n"); + strcat(new, s); + SetTextWidgetText(w, new, 0); + free(new); +} + +void +submit_cb(Widget w, void *d) { + struct textpair *tp = (struct textpair *)d; + char *new; + new = GetTextWidgetText(tp->in); + if (new == NULL) { + fprintf(stderr, "error getting text\n"); + exit(1); + } + + add_text_to_display_newline(new, tp->out); SetStringEntry(tp->in, ""); - SetTextWidgetText(tp->out, s, 0); - free(s); } void string_cb(Widget w, char *s, void *data) { - char *old = GetTextWidgetText((Widget)data); - size_t len = strlen(old); - char *new = malloc(strlen(s) + len + 2); - strcpy(new, old); - if (len > 0 && old[len - 1] != '\n') - strcat(new, "\n"); - strcat(new, s); - SetTextWidgetText((Widget)data, new, 0); + add_text_to_display_newline(s, (Widget)data); SetStringEntry(w, ""); - free(new); } void @@ -52,6 +58,16 @@ quit(Widget w, void *d) { exit(0); } +void +pollin_cb(XtPointer client_data, int *source, XtInputId *id) { +#define BUFSIZE 30 + char c[BUFSIZE] = {'\0'}; + if (read(*source, c, BUFSIZE) == 0) + XtRemoveInput(*id); + else + add_text_to_display_newline(c, (Widget)client_data); +} + int main(int argc, char **argv) { Widget w[4]; @@ -81,6 +97,8 @@ main(int argc, char **argv) { if (app == NULL) return 1; + XtAppAddInput(app, /* stdin */ 0, XtInputReadMask, pollin_cb, w[0]); + Widget parent, oldparent; oldparent = w[0]; do {