add emacs indentation variables to source files in line with current vim settings
[monky] / src / tailhead.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "text_object.h"
33 #include "logging.h"
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #define MAX_HEADTAIL_LINES 30
39 #define DEFAULT_MAX_HEADTAIL_USES 2
40
41 void tailstring(char *string, int endofstring, int wantedlines) {
42         int i, linescounted = 0;
43
44         string[endofstring] = 0;
45         if(endofstring > 0) {
46                 if(string[endofstring-1] == '\n') {     //work with or without \n at end of file
47                         string[endofstring-1] = 0;
48                 }
49                 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
50                         if(string[i] == '\n') {
51                                 linescounted++;
52                         }
53                 }
54                 if(i > 0) {
55                         strfold(string, i+2);
56                 }
57         }
58 }
59
60 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
61         unsigned int args;
62
63         if(arg) {
64                 obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
65                 obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
66                 args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
67                 if(args == 2 || args == 3) {
68                         if(obj->data.headtail.max_uses < 1) {
69                                 free(obj->data.headtail.logfile);
70                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
71                         }
72                         if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
73                                 to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
74                                 obj->data.headtail.buffer = NULL;
75                                 obj->data.headtail.current_use = 0;
76                         }else{
77                                 free(obj->data.headtail.logfile);
78                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
79                         }
80                 } else {
81                         free(obj->data.headtail.logfile);
82                         CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
83                 }
84         } else {
85                 CRIT_ERR(obj, free_at_crash, "%s needs arguments", type);
86         }
87 }
88
89 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
90         int fd, i, endofstring = 0, linescounted = 0;
91         FILE *fp;
92         struct stat st;
93
94         //empty the buffer and reset the counter if we used it the max number of times
95         if(obj->data.headtail.buffer && obj->data.headtail.current_use >= obj->data.headtail.max_uses - 1) {
96                 free(obj->data.headtail.buffer);
97                 obj->data.headtail.buffer = NULL;
98                 obj->data.headtail.current_use = 0;
99         }
100         //use the buffer if possible
101         if(obj->data.headtail.buffer) {
102                 strcpy(p, obj->data.headtail.buffer);
103                 obj->data.headtail.current_use++;
104         }else{  //otherwise find the needed data
105                 if(stat(obj->data.headtail.logfile, &st) == 0) {
106                         if (S_ISFIFO(st.st_mode)) {
107                                 fd = open_fifo(obj->data.headtail.logfile, &obj->a);
108                                 if(fd != -1) {
109                                         if(strcmp(type, "head") == 0) {
110                                                 for(i = 0; linescounted < obj->data.headtail.wantedlines; i++) {
111                                                         read(fd, p + i, 1);
112                                                         if(p[i] == '\n') {
113                                                                 linescounted++;
114                                                         }
115                                                 }
116                                                 p[i] = 0;
117                                         } else if(strcmp(type, "tail") == 0) {
118                                                 i = read(fd, p, p_max_size - 1);
119                                                 tailstring(p, i, obj->data.headtail.wantedlines);
120                                         } else {
121                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
122                                         }
123                                 }
124                                 close(fd);
125                         } else {
126                                 fp = open_file(obj->data.headtail.logfile, &obj->a);
127                                 if(fp != NULL) {
128                                         if(strcmp(type, "head") == 0) {
129                                                 for(i = 0; i < obj->data.headtail.wantedlines; i++) {
130                                                         fgets(p + endofstring, p_max_size - endofstring, fp);
131                                                         endofstring = strlen(p);
132                                                 }
133                                         } else if(strcmp(type, "tail") == 0) {
134                                                 fseek(fp, - p_max_size, SEEK_END);
135                                                 i = fread(p, 1, p_max_size - 1, fp);
136                                                 tailstring(p, i, obj->data.headtail.wantedlines);
137                                         } else {
138                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
139                                         }
140                                         fclose(fp);
141                                 }
142                         }
143                         obj->data.headtail.buffer = strdup(p);
144                 } else {
145                         CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, obj->data.headtail.logfile);
146                 }
147         }
148         return;
149 }