free(line - LWS_PRE);
}
-list *list_init(char *line)
+node *node_init(char *line)
{
- list *res = (list *)malloc(sizeof(list));
+ node *res = (node *)malloc(sizeof(node));
if (!res) {
perror("malloc");
return res;
return res;
}
-int list_add(list **in, char *line)
+list *list_init()
{
- list *new = list_init(line);
+ list *res = (list *)calloc(1, sizeof(list));
+ if (!res) {
+ perror("malloc");
+ }
+ return res;
+}
+
+int list_add(list *in, char *line)
+{
+ node *new = node_init(line);
if (!new) {
return -1;
}
- (*in)->next = new;
- *in = new;
+ if (!in->first) {
+ in->first = new;
+ } else {
+ in->last->next = new;
+ }
+ in->last = new;
return 0;
}
-void list_remove(list **in)
+void list_remove(list *in)
{
- list *tmp = *in;
- *in = (*in)->next;
- free_line(tmp->line);
- free(tmp);
+ node *tmp = in->first;
+ if (tmp) {
+ in->first = tmp->next;
+ if (!tmp->next) {
+ in->last = NULL;
+ }
+ free_line(tmp->line);
+ free(tmp);
+ }
}
void list_deinit(list *in)
{
- while (in) {
- list_remove(&in);
+ while (in->first) {
+ list_remove(in);
}
}
int n, m;
- list **lines = (list **)&(prot->user);
+ list *lines = (list *)prot->user;
char *line;
switch (reason) {
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
- line = (*lines)->line;
+ line = lines->first->line;
n = strlen(line);
m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
- if ((*lines)->next != NULL) {
+ if (lines->first->next != NULL) {
list_remove(lines);
lws_callback_on_writable_all_protocol(lws_get_context(wsi),
- &protocols[PROTOCOL_MERICA_TERMINAL]);
+ &protocols[PROTOCOL_MERICA_TERMINAL]);
}
if (m < n) {
fprintf(stderr, "ERROR %d writing to di socket\n", n);
case LWS_CALLBACK_RECEIVE:
if (strcmp((const char *)in, "reset") == 0) {
- strcpy((*lines)->line, JSON_EMPTY);
- lws_callback_on_writable_all_protocol(lws_get_context(wsi),
- &protocols[PROTOCOL_MERICA_TERMINAL]);
+ line = copy_line(JSON_EMPTY);
+ if (line) {
+ if (list_add(lines, line) == 0) {
+ lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+ &protocols[PROTOCOL_MERICA_TERMINAL]);
+ }
+ }
} else if (strcmp((const char *)in, "close") == 0) {
fprintf(stderr, "closing websocket\n");
lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
*pos = 0;
- if (list_add(&(w->lines), w->text) == 0) {
+ if (list_add(w->lines, w->text) == 0) {
char *line = new_line();
if (line) {
w->text = line;
if (!line) {
return -1;
}
- w->lines = list_init(line);
+ w->lines = list_init();
if (!w->lines) {
free_line(line);
return -1;
}
+ if (list_add(w->lines, line) != 0) {
+ list_deinit(w->lines);
+ return -1;
+ }
protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines;
line = new_line();
if (!line) {