Commit | Line | Data |
---|---|---|
7bd7fa83 TB |
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(void) | |
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 | ||
54 | /* printf("starting loop.\n"); */ | |
55 | while(1){ | |
56 | if(*ptr == '\n' || *ptr == '\0'){ | |
57 | /* printf("----\n"); */ | |
58 | /* printf("found a new line.\n"); */ | |
59 | /* printf("'%c' -> \\0\n", *ptr); */ | |
60 | /* printf("'next \%c': %c\n", *(ptr+1)); */ | |
61 | if(*ptr == '\0'){ | |
62 | /* end of buf */ | |
63 | /* printf("found the end. (1)\n"); */ | |
64 | return lines; | |
65 | } | |
66 | ||
67 | *ptr = '\0'; | |
68 | ptr++; | |
69 | ||
70 | while(*ptr == '\n'){ | |
71 | /* empty, discard */ | |
72 | /* printf("empty\n"); */ | |
73 | *ptr = '\0'; | |
74 | ptr++; | |
75 | } | |
76 | ||
77 | if(*ptr == '\0'){ | |
78 | /* end of buf */ | |
79 | /* printf("found the end. (2)\n"); */ | |
80 | return lines; | |
81 | } | |
82 | ||
83 | /* printf("hallo there.\n"); */ | |
84 | cur_line = malloc(sizeof(struct line)); | |
85 | old_line->next = cur_line; | |
86 | old_line = cur_line; | |
87 | cur_line->buf = ptr; | |
88 | } | |
89 | printf("________|%c|\n", *ptr); | |
90 | ptr++; | |
91 | } | |
92 | } | |
93 | ||
94 | void socket_read(void (*func)(char *line)) | |
95 | { | |
96 | if(options.lines != NULL){ | |
97 | printf("|||| already got some input. |||\n"); | |
98 | goto call_func; | |
99 | } | |
100 | ||
101 | int size; | |
102 | do_read: | |
103 | printf("||||| doing read. ||||\n"); | |
104 | size = read(options.sfd, options.buf, MAX_LEN); | |
105 | printf("||||| got read. ||||\n"); | |
106 | if(size == 0){ | |
107 | printf("||||| reading again. ||||\n"); | |
108 | goto do_read; | |
109 | }else{ | |
110 | printf("||||| done read. ||||\n"); | |
111 | options.lines = find_lines(); | |
112 | ||
113 | struct line *cur_line = options.lines; | |
114 | ||
115 | int count = 0; | |
116 | while(1){ | |
117 | printf("[%d] '%s'\n", count++, cur_line->buf); | |
118 | if(cur_line->next == NULL){ | |
119 | printf("found the end.\n"); | |
120 | break; | |
121 | } | |
122 | cur_line = cur_line->next; | |
123 | } | |
124 | } | |
125 | ||
126 | call_func: | |
127 | printf("calling function with %s\n", options.lines->buf); | |
128 | func(options.lines->buf); | |
129 | options.lines = options.lines->next; | |
130 | return; | |
131 | } | |
132 |