2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
2009-09-12 10:50:51 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2008-12-15 18:09:57 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2010-01-01 23:46:17 +00:00
|
|
|
* Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
|
2008-12-15 18:09:57 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2009-12-09 22:46:21 +00:00
|
|
|
#include "config.h"
|
2008-12-15 18:09:57 +00:00
|
|
|
#include "text_object.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "logging.h"
|
2009-10-24 23:59:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2009-11-29 19:18:37 +00:00
|
|
|
#include <string.h>
|
2008-12-15 18:09:57 +00:00
|
|
|
|
2009-11-20 00:43:00 +00:00
|
|
|
void gen_free_opaque(struct text_object *obj)
|
|
|
|
{
|
|
|
|
if (obj->data.opaque)
|
|
|
|
free(obj->data.opaque);
|
|
|
|
}
|
|
|
|
|
2009-11-28 21:53:20 +00:00
|
|
|
int gen_false_iftest(struct text_object *obj)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-28 22:02:58 +00:00
|
|
|
void gen_print_nothing(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
(void)p;
|
|
|
|
(void)p_max_size;
|
|
|
|
}
|
|
|
|
|
2009-11-29 19:04:55 +00:00
|
|
|
void gen_print_obj_data_s(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
if (!obj->data.s)
|
|
|
|
return;
|
|
|
|
snprintf(p, p_max_size, "%s", obj->data.s);
|
|
|
|
}
|
|
|
|
|
2008-12-15 18:09:57 +00:00
|
|
|
/* text_object_list
|
|
|
|
*
|
|
|
|
* this list is special. it looks like this:
|
|
|
|
* NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
|
|
|
|
* ^-------root_object----------^
|
|
|
|
* directions are reversed here
|
|
|
|
*
|
|
|
|
* why this is cool:
|
|
|
|
* - root_object points both to the start and end of the list
|
|
|
|
* - while traversing, the end of the list is always a NULL pointer
|
|
|
|
* (this works in BOTH directions)
|
|
|
|
*/
|
|
|
|
|
2009-11-08 13:42:04 +00:00
|
|
|
/* append an object or list of objects to the given root object's list */
|
2008-12-15 18:09:57 +00:00
|
|
|
int append_object(struct text_object *root, struct text_object *obj)
|
|
|
|
{
|
|
|
|
struct text_object *end;
|
|
|
|
|
2009-11-08 13:42:04 +00:00
|
|
|
/* hook in start of list to append */
|
2008-12-15 18:09:57 +00:00
|
|
|
end = root->prev;
|
|
|
|
obj->prev = end;
|
|
|
|
|
2009-11-08 13:42:04 +00:00
|
|
|
/* update pointers of the list to append to */
|
2008-12-15 18:09:57 +00:00
|
|
|
if (end) {
|
|
|
|
if (end->next)
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "huston, we have a lift-off");
|
2008-12-15 18:09:57 +00:00
|
|
|
end->next = obj;
|
|
|
|
} else {
|
|
|
|
root->next = obj;
|
|
|
|
}
|
2009-11-08 13:42:04 +00:00
|
|
|
|
|
|
|
/* find end of appended list to point root->prev there */
|
|
|
|
while (obj->next)
|
|
|
|
obj = obj->next;
|
2008-12-15 18:09:57 +00:00
|
|
|
root->prev = obj;
|
2009-11-08 13:42:04 +00:00
|
|
|
|
2008-12-15 18:09:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ifblock handlers for the object list
|
|
|
|
*
|
|
|
|
* - each if points to it's else or endif
|
|
|
|
* - each else points to it's endif
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* possible ifblock types
|
|
|
|
* only used internally, so no need to make this public
|
|
|
|
*/
|
|
|
|
enum ifblock_type {
|
|
|
|
IFBLOCK_IF = 1,
|
|
|
|
IFBLOCK_ELSE,
|
|
|
|
IFBLOCK_ENDIF,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* linked list of ifblock objects, building a stack
|
|
|
|
* only used internally, so no need to make this public
|
|
|
|
*/
|
|
|
|
struct ifblock_stack_obj {
|
|
|
|
enum ifblock_type type;
|
|
|
|
struct text_object *obj;
|
|
|
|
struct ifblock_stack_obj *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* push an ifblock object onto the stack
|
|
|
|
* in fact, this does a lot more:
|
|
|
|
* - IFBLOCK_IF is just pushed onto the stack
|
|
|
|
* - IFBLOCK_ELSE updates the "next" pointer of the upmost
|
|
|
|
* object in the stack and is then pushed onto the stack
|
|
|
|
* - IFBLOCK_ENDIF updates the "next" pointer of the upmost
|
|
|
|
* object in the stack and then triggers stack popping of
|
|
|
|
* any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
|
|
|
|
*/
|
2008-12-22 16:45:08 +00:00
|
|
|
static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
|
|
|
|
struct text_object *obj, enum ifblock_type type)
|
2008-12-15 18:09:57 +00:00
|
|
|
{
|
|
|
|
struct ifblock_stack_obj *stackobj;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case IFBLOCK_ENDIF:
|
2008-12-22 16:45:08 +00:00
|
|
|
if (!(*ifblock_stack_top))
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "got an endif without matching if");
|
2009-11-16 18:52:12 +00:00
|
|
|
(*ifblock_stack_top)->obj->ifblock_next = obj;
|
2008-12-15 18:09:57 +00:00
|
|
|
/* if there's some else in between, remove and free it */
|
2008-12-22 16:45:08 +00:00
|
|
|
if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) {
|
|
|
|
stackobj = *ifblock_stack_top;
|
|
|
|
*ifblock_stack_top = stackobj->next;
|
2008-12-15 18:09:57 +00:00
|
|
|
free(stackobj);
|
|
|
|
}
|
|
|
|
/* finally remove and free the if object */
|
2008-12-22 16:45:08 +00:00
|
|
|
stackobj = *ifblock_stack_top;
|
|
|
|
*ifblock_stack_top = stackobj->next;
|
2008-12-15 18:09:57 +00:00
|
|
|
free(stackobj);
|
|
|
|
break;
|
|
|
|
case IFBLOCK_ELSE:
|
2008-12-22 16:45:08 +00:00
|
|
|
if (!(*ifblock_stack_top))
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "got an else without matching if");
|
2009-11-16 18:52:12 +00:00
|
|
|
(*ifblock_stack_top)->obj->ifblock_next = obj;
|
2008-12-15 18:09:57 +00:00
|
|
|
/* fall through */
|
|
|
|
case IFBLOCK_IF:
|
|
|
|
stackobj = malloc(sizeof(struct ifblock_stack_obj));
|
|
|
|
stackobj->type = type;
|
|
|
|
stackobj->obj = obj;
|
2008-12-22 16:45:08 +00:00
|
|
|
stackobj->next = *ifblock_stack_top;
|
|
|
|
*ifblock_stack_top = stackobj;
|
2008-12-15 18:09:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-16 18:28:23 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "push_ifblock() missuse detected!");
|
2008-12-15 18:09:57 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public functions for client use */
|
|
|
|
|
2008-12-22 16:45:08 +00:00
|
|
|
int obj_be_ifblock_if(void **opaque, struct text_object *obj)
|
2008-12-15 18:09:57 +00:00
|
|
|
{
|
2008-12-22 16:45:08 +00:00
|
|
|
return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
|
2008-12-15 18:09:57 +00:00
|
|
|
}
|
2008-12-22 16:45:08 +00:00
|
|
|
int obj_be_ifblock_else(void **opaque, struct text_object *obj)
|
2008-12-15 18:09:57 +00:00
|
|
|
{
|
2008-12-22 16:45:08 +00:00
|
|
|
return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
|
2008-12-15 18:09:57 +00:00
|
|
|
}
|
2008-12-22 16:45:08 +00:00
|
|
|
int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
|
2008-12-15 18:09:57 +00:00
|
|
|
{
|
2008-12-22 16:45:08 +00:00
|
|
|
return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
|
2008-12-15 18:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if ifblock stack is empty
|
|
|
|
* if so, return true (!= 0)
|
|
|
|
*/
|
2008-12-22 16:45:08 +00:00
|
|
|
int ifblock_stack_empty(void **opaque)
|
2008-12-15 18:09:57 +00:00
|
|
|
{
|
2008-12-22 16:45:08 +00:00
|
|
|
return *opaque == NULL;
|
2008-12-15 18:09:57 +00:00
|
|
|
}
|
2009-11-29 19:04:55 +00:00
|
|
|
|
|
|
|
void obj_be_plain_text(struct text_object *obj, const char *text)
|
|
|
|
{
|
|
|
|
obj->data.s = strdup(text);
|
2009-11-29 19:18:37 +00:00
|
|
|
obj->verbatim_output = 1;
|
2009-11-29 19:04:55 +00:00
|
|
|
|
|
|
|
memset(&obj->callbacks, 0, sizeof(obj->callbacks));
|
|
|
|
obj->callbacks.print = &gen_print_obj_data_s;
|
|
|
|
obj->callbacks.free = &gen_free_opaque;
|
|
|
|
}
|