Fixed inconsistent socket io.
[tlb/tomd.git] / src / common / socketio.c
1 /* Copyright (C) 2018 Thomas Balzer */
2
3 /* This file is part of tomd. */
4
5 /* tomd is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 3 of the License, or */
8 /* (at your option) any later version. */
9
10 /* tomd is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
14
15 /* You should have received a copy of the GNU General Public License */
16 /* along with tomd. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/un.h>
23 #include <sys/socket.h>
24
25 #define MAX_LEN 1024
26
27 struct line{
28 char *buf;
29 struct line *next;
30 };
31
32 static struct{
33 char *buf;
34 int sfd;
35 struct line *lines;
36 }options;
37
38 void setup_socket(int sfd)
39 {
40 options.sfd = sfd;
41 options.buf = malloc(sizeof(char) * MAX_LEN);
42 }
43
44 static struct line *find_lines(int size)
45 {
46 /* printf("entering find_lines\n"); */
47 struct line *lines = malloc(sizeof(struct line));
48 struct line *cur_line = lines;
49 struct line *old_line = lines;
50 char *ptr = options.buf;
51 lines->buf = options.buf;
52 lines->next = NULL;
53 int c = 0;
54 /* printf("starting loop.\n"); */
55 while(1){
56 if(c >= size){
57 /* printf("killing read at end of reported read.\n"); */
58 /* printf("(3) used [%d] of [%d]\n", c, size); */
59 /* maybe put a null at end of cur_line->buf */
60 return lines;
61 }
62 if(*ptr == '\n' || *ptr == '\0'){
63 /* printf("----\n"); */
64 /* printf("found a new line.\n"); */
65 /* printf("'%c' -> \\0\n", *ptr); */
66 /* printf("'next \%c': %c\n", *(ptr+1)); */
67 /* if(*ptr == '\0'){ */
68 /* /\* end of buf *\/ */
69 /* /\* printf("found the end. (1)\n"); *\/ */
70 /* printf("(1) used [%d] of [%d]\n", c, size); */
71 /* return lines; */
72 /* } */
73
74 *ptr = '\0';
75 ptr++; c++;
76
77 while(*ptr == '\n' || *ptr == '\0'){
78 if(c >= size){
79 /* printf("killing read at end of reported read.\n"); */
80 /* printf("(3) used [%d] of [%d]\n", c, size); */
81 return lines;
82 }
83 /* empty, discard */
84 /* printf("empty\n"); */
85 *ptr = '\0';
86 ptr++; c++;
87 }
88
89 /* if(*ptr == '\0'){ */
90 /* /\* end of buf *\/ */
91 /* /\* printf("found the end. (2)\n"); *\/ */
92 /* printf("(2) used [%d] of [%d]\n", c, size); */
93 /* return lines; */
94 /* } */
95
96 /* printf("hallo there.\n"); */
97 cur_line = malloc(sizeof(struct line));
98 old_line->next = cur_line;
99 old_line = cur_line;
100 cur_line->buf = ptr;
101 cur_line->next = NULL;
102 }
103 /* printf("________|%c|\n", *ptr); */
104 ptr++; c++;
105 }
106 }
107
108 void socket_read(void (*func)(char *line))
109 {
110 if(options.lines != NULL){
111 /* printf("|||| already got some input. |||\n"); */
112 goto call_func;
113 }
114
115 int size;
116 do_read:
117 /* printf("||||| doing read. ||||\n"); */
118 memset(options.buf, 0, MAX_LEN); \
119 size = read(options.sfd, options.buf, MAX_LEN);
120 /* printf("read size: %d\n", size); */
121 /* printf("||||| got read. ||||\n"); */
122 if(size == 0){
123 /* printf("||||| reading again. ||||\n"); */
124 goto do_read;
125 }else{
126 /* printf("||||| done read. ||||\n"); */
127 options.lines = find_lines(size);
128
129 /* struct line *current_line = options.lines; */
130
131 /* int count = 0; */
132 /* while(1){ */
133 /* printf("[%d] '%s'\n", count++, current_line->buf); */
134 /* if(current_line->next == NULL){ */
135 /* printf("found the end.\n"); */
136 /* break; */
137 /* } */
138 /* current_line = current_line->next; */
139 /* } */
140 }
141
142 call_func:{
143 struct line *cur_line = options.lines;
144 if(cur_line == NULL){
145 /* printf("bad options line.\n"); */
146 }else{
147 while(1){
148 /* printf("[%s]->", cur_line->buf); */
149 if(cur_line->next == NULL){
150 /* printf("(nil)\n"); */
151 break;
152 }else{
153 cur_line = cur_line->next;
154 }
155 }
156 }
157 }
158
159 /* printf("calling function with %s\n", options.lines->buf); */
160 func(options.lines->buf);
161 options.lines = options.lines->next;
162 return;
163 }