1 /* Copyright (C) 2018 Thomas Balzer */
3 /* This file is part of tomd. */
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. */
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. */
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/>. */
23 #include <sys/socket.h>
38 void setup_socket(int sfd
)
41 options
.buf
= malloc(sizeof(char) * MAX_LEN
);
44 static struct line
*find_lines(int size
)
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
;
54 /* printf("starting loop.\n"); */
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 */
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); */
77 while(*ptr
== '\n' || *ptr
== '\0'){
79 /* printf("killing read at end of reported read.\n"); */
80 /* printf("(3) used [%d] of [%d]\n", c, size); */
84 /* printf("empty\n"); */
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); */
96 /* printf("hallo there.\n"); */
97 cur_line
= malloc(sizeof(struct line
));
98 old_line
->next
= cur_line
;
101 cur_line
->next
= NULL
;
103 /* printf("________|%c|\n", *ptr); */
108 void socket_read(void (*func
)(char *line
))
110 if(options
.lines
!= NULL
){
111 /* printf("|||| already got some input. |||\n"); */
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"); */
123 /* printf("||||| reading again. ||||\n"); */
126 /* printf("||||| done read. ||||\n"); */
127 options
.lines
= find_lines(size
);
129 /* struct line *current_line = options.lines; */
133 /* printf("[%d] '%s'\n", count++, current_line->buf); */
134 /* if(current_line->next == NULL){ */
135 /* printf("found the end.\n"); */
138 /* current_line = current_line->next; */
143 struct line
*cur_line
= options
.lines
;
144 if(cur_line
== NULL
){
145 /* printf("bad options line.\n"); */
148 /* printf("[%s]->", cur_line->buf); */
149 if(cur_line
->next
== NULL
){
150 /* printf("(nil)\n"); */
153 cur_line
= cur_line
->next
;
159 /* printf("calling function with %s\n", options.lines->buf); */
160 func(options
.lines
->buf
);
161 options
.lines
= options
.lines
->next
;