last commit i forgot to save changes in x11.c ; this fixes that file
[monky] / src / tailhead.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #define _GNU_SOURCE
32 #include "common.h"
33 #include "text_object.h"
34 #include "logging.h"
35 #include <sys/stat.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #define MAX_HEADTAIL_LINES 30
42 #define DEFAULT_MAX_HEADTAIL_USES 2
43
44 struct headtail {
45         int wantedlines;
46         char *logfile;
47         char *buffer;
48         int current_use;
49         int max_uses;
50         int reported;
51 };
52
53 static void tailstring(char *string, int endofstring, int wantedlines) {
54         int i, linescounted = 0;
55
56         string[endofstring] = 0;
57         if(endofstring > 0) {
58                 if(string[endofstring-1] == '\n') {     //work with or without \n at end of file
59                         string[endofstring-1] = 0;
60                 }
61                 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
62                         if(string[i] == '\n') {
63                                 linescounted++;
64                         }
65                 }
66                 if(i > 0) {
67                         strfold(string, i+2);
68                 }
69         }
70 }
71
72 void free_tailhead(struct text_object *obj)
73 {
74         struct headtail *ht = obj->data.opaque;
75         if (!ht)
76                 return;
77         if (ht->logfile)
78                 free(ht->logfile);
79         if (ht->buffer)
80                 free(ht->buffer);
81         free(obj->data.opaque);
82         obj->data.opaque = NULL;
83 }
84
85 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
86         unsigned int args;
87         struct headtail *ht;
88
89         ht = malloc(sizeof(struct headtail));
90         memset(ht, 0, sizeof(struct headtail));
91
92         ht->logfile = malloc(DEFAULT_TEXT_BUFFER_SIZE);
93         memset(ht->logfile, 0, DEFAULT_TEXT_BUFFER_SIZE);
94
95         ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
96
97         args = sscanf(arg, "%s %d %d", ht->logfile, &ht->wantedlines, &ht->max_uses);
98         if (args < 2 || args > 3) {
99                 free_tailhead(obj);
100                 CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
101         }
102         if (ht->max_uses < 1) {
103                 free_tailhead(obj);
104                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
105         }
106         if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
107                 to_real_path(ht->logfile, ht->logfile);
108                 ht->buffer = NULL;
109                 ht->current_use = 0;
110         } else {
111                 free_tailhead(obj);
112                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
113         }
114         obj->data.opaque = ht;
115 }
116
117 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
118         int fd, i, endofstring = 0, linescounted = 0;
119         FILE *fp;
120         struct stat st;
121         struct headtail *ht = obj->data.opaque;
122
123         if (!ht)
124                 return;
125
126         //empty the buffer and reset the counter if we used it the max number of times
127         if(ht->buffer && ht->current_use >= ht->max_uses - 1) {
128                 free(ht->buffer);
129                 ht->buffer = NULL;
130                 ht->current_use = 0;
131         }
132         //use the buffer if possible
133         if(ht->buffer) {
134                 strcpy(p, ht->buffer);
135                 ht->current_use++;
136         }else{  //otherwise find the needed data
137                 if(stat(ht->logfile, &st) == 0) {
138                         if (S_ISFIFO(st.st_mode)) {
139                                 fd = open_fifo(ht->logfile, &ht->reported);
140                                 if(fd != -1) {
141                                         if(strcmp(type, "head") == 0) {
142                                                 for(i = 0; linescounted < ht->wantedlines; i++) {
143                                                         read(fd, p + i, 1);
144                                                         if(p[i] == '\n') {
145                                                                 linescounted++;
146                                                         }
147                                                 }
148                                                 p[i] = 0;
149                                         } else if(strcmp(type, "tail") == 0) {
150                                                 i = read(fd, p, p_max_size - 1);
151                                                 tailstring(p, i, ht->wantedlines);
152                                         } else {
153                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
154                                         }
155                                 }
156                                 close(fd);
157                         } else {
158                                 fp = open_file(ht->logfile, &ht->reported);
159                                 if(fp != NULL) {
160                                         if(strcmp(type, "head") == 0) {
161                                                 for(i = 0; i < ht->wantedlines; i++) {
162                                                         fgets(p + endofstring, p_max_size - endofstring, fp);
163                                                         endofstring = strlen(p);
164                                                 }
165                                         } else if(strcmp(type, "tail") == 0) {
166                                                 fseek(fp, - p_max_size, SEEK_END);
167                                                 i = fread(p, 1, p_max_size - 1, fp);
168                                                 tailstring(p, i, ht->wantedlines);
169                                         } else {
170                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
171                                         }
172                                         fclose(fp);
173                                 }
174                         }
175                         ht->buffer = strdup(p);
176                 } else {
177                         CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, ht->logfile);
178                 }
179         }
180         return;
181 }
182
183 /* FIXME: use something more general (see also tail.c, head.c */
184 #define BUFSZ 0x1000
185
186 void print_lines(struct text_object *obj, char *p, int p_max_size)
187 {
188         static int rep = 0;
189         FILE *fp = open_file(obj->data.s, &rep);
190         char buf[BUFSZ];
191         int j, lines;
192
193         if (!fp) {
194                 snprintf(p, p_max_size, "File Unreadable");
195                 return;
196         }
197
198         lines = 0;
199         while(fgets(buf, BUFSZ, fp) != NULL){
200                 for(j = 0; buf[j] != 0; j++) {
201                         if(buf[j] == '\n') {
202                                 lines++;
203                         }
204                 }
205         }
206         snprintf(p, p_max_size, "%d", lines);
207         fclose(fp);
208 }
209
210 void print_words(struct text_object *obj, char *p, int p_max_size)
211 {
212         static int rep = 0;
213         FILE *fp = open_file(obj->data.s, &rep);
214         char buf[BUFSZ];
215         int j, words;
216         char inword = 0;
217
218         if(!fp) {
219                 snprintf(p, p_max_size, "File Unreadable");
220                 return;
221         }
222
223         words = 0;
224         while(fgets(buf, BUFSZ, fp) != NULL){
225                 for(j = 0; buf[j] != 0; j++) {
226                         if(!isspace(buf[j])) {
227                                 if(!inword) {
228                                         words++;
229                                         inword = 1;
230                                 }
231                         } else {
232                                 inword = 0;
233                         }
234                 }
235         }
236         snprintf(p, p_max_size, "%d", words);
237         fclose(fp);
238 }