1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-25 04:06:03 +00:00

tail, head: use OBJ_ARG to simplify code

This commit is contained in:
Phil Sutter 2009-10-04 14:44:20 +02:00
parent a529ac52de
commit b9a28c37a8
2 changed files with 18 additions and 22 deletions

View File

@ -901,9 +901,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
free(buf);
}
#endif /* __linux__ */
END OBJ(tail, 0)
END OBJ_ARG(tail, 0, "tail needs arguments")
init_tailhead("tail", arg, obj, free_at_crash);
END OBJ(head, 0)
END OBJ_ARG(head, 0, "head needs arguments")
init_tailhead("head", arg, obj, free_at_crash);
END OBJ_ARG(lines, 0, "lines needs an argument")
obj->data.s = strndup(arg, text_buffer_size);

View File

@ -59,29 +59,25 @@ void tailstring(char *string, int endofstring, int wantedlines) {
void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
unsigned int args;
if(arg) {
obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
if(args == 2 || args == 3) {
if(obj->data.headtail.max_uses < 1) {
free(obj->data.headtail.logfile);
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
}
if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
obj->data.headtail.buffer = NULL;
obj->data.headtail.current_use = 0;
}else{
free(obj->data.headtail.logfile);
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
}
} else {
obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
if(args == 2 || args == 3) {
if(obj->data.headtail.max_uses < 1) {
free(obj->data.headtail.logfile);
CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
}
if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
obj->data.headtail.buffer = NULL;
obj->data.headtail.current_use = 0;
}else{
free(obj->data.headtail.logfile);
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
}
} else {
CRIT_ERR(obj, free_at_crash, "%s needs arguments", type);
free(obj->data.headtail.logfile);
CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
}
}