]> arthur.barton.de Git - netdata.git/blob - src/procfile.c
splitted netdata to multiple source files
[netdata.git] / src / procfile.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <malloc.h>
10 #include <inttypes.h>
11 #include <ctype.h>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <sys/wait.h>
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19
20 #include "procfile.h"
21
22 #define PF_PREFIX "PROCFILE"
23 int pfdebug = 0;
24
25 // ----------------------------------------------------------------------------
26 // An array of words
27
28 #define PFWORDS_INCREASE_STEP 200
29
30 pfwords *pfwords_add(pfwords *fw, char *str) {
31         if(pfdebug) fprintf(stderr, PF_PREFIX ":        adding word No %d: '%s'\n", fw->len, str);
32
33         if(fw->len == fw->size) {
34                 if(pfdebug) fprintf(stderr, PF_PREFIX ":        expanding words\n");
35
36                 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
37                 if(!new) {
38                         fprintf(stderr, PF_PREFIX ":    failed to expand words\n");
39                         free(fw);
40                         return NULL;
41                 }
42                 fw = new;
43                 fw->size += PFWORDS_INCREASE_STEP;
44         }
45
46         fw->words[fw->len++] = str;
47
48         return fw;
49 }
50
51 pfwords *pfwords_new(void) {
52         if(pfdebug) fprintf(stderr, PF_PREFIX ":        initializing words\n");
53
54         pfwords *new = malloc(sizeof(pfwords) + PFWORDS_INCREASE_STEP * sizeof(char *));
55         if(!new) return NULL;
56
57         new->len = 0;
58         new->size = PFWORDS_INCREASE_STEP;
59         return new;
60 }
61
62 void pfwords_reset(pfwords *fw) {
63         if(pfdebug) fprintf(stderr, PF_PREFIX ":        reseting words\n");
64         fw->len = 0;
65 }
66
67 void pfwords_free(pfwords *fw) {
68         if(pfdebug) fprintf(stderr, PF_PREFIX ":        freeing words\n");
69
70         free(fw);
71 }
72
73
74 // ----------------------------------------------------------------------------
75 // An array of lines
76
77 #define PFLINES_INCREASE_STEP 10
78
79 pflines *pflines_add(pflines *fl, uint32_t first_word) {
80         if(pfdebug) fprintf(stderr, PF_PREFIX ":        adding line %d at word %d\n", fl->len, first_word);
81
82         if(fl->len == fl->size) {
83                 if(pfdebug) fprintf(stderr, PF_PREFIX ":        expanding lines\n");
84
85                 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
86                 if(!new) {
87                         fprintf(stderr, PF_PREFIX ":    failed to expand lines\n");
88                         free(fl);
89                         return NULL;
90                 }
91                 fl = new;
92                 fl->size += PFLINES_INCREASE_STEP;
93         }
94
95         fl->lines[fl->len].words = 0;
96         fl->lines[fl->len++].first = first_word;
97
98         return fl;
99 }
100
101 pflines *pflines_new(void) {
102         if(pfdebug) fprintf(stderr, PF_PREFIX ":        initializing lines\n");
103
104         pflines *new = malloc(sizeof(pflines) + PFLINES_INCREASE_STEP * sizeof(ffline));
105         if(!new) return NULL;
106
107         new->len = 0;
108         new->size = PFLINES_INCREASE_STEP;
109         return new;
110 }
111
112 void pflines_reset(pflines *fl) {
113         if(pfdebug) fprintf(stderr, PF_PREFIX ":        reseting lines\n");
114
115         fl->len = 0;
116 }
117
118 void pflines_free(pflines *fl) {
119         if(pfdebug) fprintf(stderr, PF_PREFIX ":        freeing lines\n");
120
121         free(fl);
122 }
123
124
125 // ----------------------------------------------------------------------------
126 // The procfile
127
128 #define PROCFILE_INITIAL_BUFFER 256
129 #define PROCFILE_INCREMENT_BUFFER 512
130
131 #define PF_CHAR_IS_SEPARATOR    0
132 #define PF_CHAR_IS_NEWLINE      1
133 #define PF_CHAR_IS_WORD         2
134
135 void procfile_close(procfile *ff) {
136         if(pfdebug) fprintf(stderr, PF_PREFIX ": Closing file '%s'. Reason: %s\n", ff->filename, strerror(errno));
137
138         if(ff->lines) pflines_free(ff->lines);
139         if(ff->words) pfwords_free(ff->words);
140
141         if(ff->fd != -1) close(ff->fd);
142         free(ff);
143 }
144
145 procfile *procfile_parser(procfile *ff) {
146         if(pfdebug) fprintf(stderr, PF_PREFIX ": Parsing file '%s'\n", ff->filename);
147
148         char *s = ff->data, *e = ff->data, *t = ff->data;
149         uint32_t l = 0, w = 0;
150         e += ff->len;
151         
152         ff->lines = pflines_add(ff->lines, w);
153         if(!ff->lines) goto cleanup;
154
155         while(s < e) {
156                 switch(ff->separators[(int)(*s)]) {
157                         case PF_CHAR_IS_SEPARATOR:
158                                 if(s == t) {
159                                         // skip all leading white spaces
160                                         t = ++s;
161                                         continue;
162                                 }
163
164                                 // end of word
165                                 *s = '\0';
166
167                                 ff->words = pfwords_add(ff->words, t);
168                                 if(!ff->words) goto cleanup;
169
170                                 ff->lines->lines[l].words++;
171                                 w++;
172
173                                 t = ++s;
174                                 continue;
175
176                         case PF_CHAR_IS_NEWLINE:
177                                 // end of line
178                                 *s = '\0';
179
180                                 ff->words = pfwords_add(ff->words, t);
181                                 if(!ff->words) goto cleanup;
182
183                                 ff->lines->lines[l].words++;
184                                 w++;
185
186                                 if(pfdebug) fprintf(stderr, PF_PREFIX ":        ended line %d with %d words\n", l, ff->lines->lines[l].words);
187
188                                 ff->lines = pflines_add(ff->lines, w);
189                                 if(!ff->lines) goto cleanup;
190                                 l++;
191
192                                 t = ++s;
193                                 continue;
194
195                         default:
196                                 s++;
197                                 continue;
198                 }
199         }
200
201         if(s != t) {
202                 // the last word
203                 if(ff->len < ff->size) *s = '\0';
204                 else {
205                         // we are going to loose the last byte
206                         ff->data[ff->size - 1] = '\0';
207                 }
208
209                 ff->words = pfwords_add(ff->words, t);
210                 if(!ff->words) goto cleanup;
211
212                 ff->lines->lines[l].words++;
213                 w++;
214         }
215
216         return ff;
217
218 cleanup:
219         fprintf(stderr, PF_PREFIX ": Failed to parse file '%s'. Reason: %s\n", ff->filename, strerror(errno));
220         procfile_close(ff);
221         return NULL;
222 }
223
224 procfile *procfile_readall(procfile *ff) {
225         if(pfdebug) fprintf(stderr, PF_PREFIX ": Reading file '%s'.\n", ff->filename);
226
227         ssize_t s = 0, r = ff->size, x = ff->size;
228         ff->len = 0;
229
230         while(r == x) {
231                 if(s) {
232                         if(pfdebug) fprintf(stderr, PF_PREFIX ": Expanding data buffer for file '%s'.\n", ff->filename);
233
234                         procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
235                         if(!new) {
236                                 fprintf(stderr, PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
237                                 procfile_close(ff);
238                                 return NULL;
239                         }
240                         ff = new;
241                         ff->size += PROCFILE_INCREMENT_BUFFER;
242                         x = PROCFILE_INCREMENT_BUFFER;
243                 }
244
245                 r = read(ff->fd, &ff->data[s], ff->size - s);
246                 if(r == -1) {
247                         fprintf(stderr, PF_PREFIX ": Cannot read from file '%s'. Reason: %s\n", ff->filename, strerror(errno));
248                         procfile_close(ff);
249                         return NULL;
250                 }
251
252                 ff->len += r;
253                 s = ff->len;
254         }
255
256         if(lseek(ff->fd, 0, SEEK_SET) == -1) {
257                 fprintf(stderr, PF_PREFIX ": Cannot rewind on file '%s'.\n", ff->filename);
258                 procfile_close(ff);
259                 return NULL;
260         }
261
262         pflines_reset(ff->lines);
263         pfwords_reset(ff->words);
264
265         ff = procfile_parser(ff);
266
267         return ff;
268 }
269
270 procfile *procfile_open(const char *filename, const char *separators) {
271         if(pfdebug) fprintf(stderr, PF_PREFIX ": Opening file '%s'\n", filename);
272
273         int fd = open(filename, O_RDONLY|O_NOATIME, 0666);
274         if(fd == -1) {
275                 fprintf(stderr, PF_PREFIX ": Cannot open file '%s'. Reason: %s\n", filename, strerror(errno));
276                 return NULL;
277         }
278
279         procfile *ff = malloc(sizeof(procfile) + PROCFILE_INITIAL_BUFFER);
280         if(!ff) {
281                 fprintf(stderr, PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
282                 close(fd);
283                 return NULL;
284         }
285
286         strncpy(ff->filename, filename, FILENAME_MAX);
287         ff->filename[FILENAME_MAX] = '\0';
288
289         ff->fd = fd;
290         ff->size = PROCFILE_INITIAL_BUFFER;
291         ff->len = 0;
292
293         ff->lines = pflines_new();
294         ff->words = pfwords_new();
295
296         if(!ff->lines || !ff->words) {
297                 fprintf(stderr, PF_PREFIX ": Cannot initialize parser for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
298                 procfile_close(ff);
299                 return NULL;
300         }
301
302         int i;
303         for(i = 0; i < 256 ;i++) {
304                 if(i == '\n' || i == '\r') ff->separators[i] = PF_CHAR_IS_NEWLINE;
305                 else if(isspace(i) || !isprint(i)) ff->separators[i] = PF_CHAR_IS_SEPARATOR;
306                 else ff->separators[i] = PF_CHAR_IS_WORD;
307         }
308
309         if(!separators) separators = " \t=|";
310         const char *s = separators;
311         while(*s) ff->separators[(int)*s++] = PF_CHAR_IS_SEPARATOR;
312
313         return ff;
314 }
315
316
317 // ----------------------------------------------------------------------------
318 // example parsing of procfile data
319
320 void procfile_print(procfile *ff) {
321         uint32_t lines = procfile_lines(ff), l;
322         uint32_t words, w;
323         char *s;
324
325         fprintf(stderr, "File '%s' with %d lines and %d words\n", ff->filename, ff->lines->len, ff->words->len);
326
327         for(l = 0; l < lines ;l++) {
328                 words = procfile_linewords(ff, l);
329
330                 fprintf(stderr, "       line %d starts at word %d and has %d words\n", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
331
332                 for(w = 0; w < words ;w++) {
333                         s = procfile_lineword(ff, l, w);
334                         fprintf(stderr, "               [%d.%d] '%s'\n", l, w, s);
335                 }
336         }
337 }