aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_uname.c
blob: ad7fa063c253f52a64c752311f6a319e2938ec72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* $Id: plugin_uname.c,v 1.6 2005/05/08 04:32:45 reinelt Exp $
 *
 * plugin for uname() syscall
 *
 * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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 2, or (at your option)
 * any later version.
 *
 * LCD4Linux 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: plugin_uname.c,v $
 * Revision 1.6  2005/05/08 04:32:45  reinelt
 * CodingStyle added and applied
 *
 * Revision 1.5  2005/01/18 06:30:23  reinelt
 * added (C) to all copyright statements
 *
 * Revision 1.4  2005/01/09 10:53:24  reinelt
 * small type in plugin_uname fixed
 * new homepage lcd4linux.bulix.org
 *
 * Revision 1.3  2004/03/03 03:47:04  reinelt
 * big patch from Martin Hejl:
 * - use qprintf() where appropriate
 * - save CPU cycles on gettimeofday()
 * - add quit() functions to free allocated memory
 * - fixed lots of memory leaks
 *
 * Revision 1.2  2004/01/29 04:40:02  reinelt
 * every .c file includes "config.h" now
 *
 * Revision 1.1  2004/01/14 11:33:00  reinelt
 * new plugin 'uname' which does what it's called
 * text widget nearly finished
 * first results displayed on MatrixOrbital
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_uname (void)
 *  adds uname() functions
 *
 */


#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/utsname.h>

#include "debug.h"
#include "plugin.h"



static void my_uname(RESULT * result, RESULT * arg1)
{
    struct utsname utsbuf;
    char *key, *value;

    key = R2S(arg1);

    if (uname(&utsbuf) != 0) {
	error("uname() failed: %s", strerror(errno));
	SetResult(&result, R_STRING, "");
	return;
    }

    if (strcasecmp(key, "sysname") == 0) {
	value = utsbuf.sysname;
    } else if (strcasecmp(key, "nodename") == 0) {
	value = utsbuf.nodename;
    } else if (strcasecmp(key, "release") == 0) {
	value = utsbuf.release;
    } else if (strcasecmp(key, "version") == 0) {
	value = utsbuf.version;
    } else if (strcasecmp(key, "machine") == 0) {
	value = utsbuf.machine;
#ifdef _GNU_SOURCE
    } else if (strcasecmp(key, "domainname") == 0) {
	value = utsbuf.domainname;
#endif
    } else {
	error("uname: unknown field '%s'", key);
	value = "";
    }

    SetResult(&result, R_STRING, value);
}


int plugin_init_uname(void)
{
    AddFunction("uname", 1, my_uname);
    return 0;
}

void plugin_exit_uname(void)
{
}
n class="kt">void *data; int fd; int read; int write; int active; int fds_id; } event_t; //our set of FDs static event_t *events = NULL; static int event_count = 0; static void free_events(void); int event_add(void (*callback) (event_flags_t flags, void *data), void *data, const int fd, const int read, const int write, const int active) { event_count++; events = realloc(events, sizeof(event_t) * event_count); int i = event_count - 1; events[i].callback = callback; events[i].data = data; events[i].fd = fd; events[i].read = read; events[i].write = write; events[i].active = active; events[i].fds_id = -1; return 0; } int event_process(const struct timespec *timeout) { int i, j; struct pollfd *fds = malloc(sizeof(struct pollfd) * event_count); for (i = 0, j = 0; i < event_count; i++) { events[i].fds_id = -1; if (events[i].active) { events[i].fds_id = j; fds[j].events = 0; fds[j].fd = events[i].fd; if (events[i].read) { fds[j].events |= POLLIN; } if (events[i].write) { fds[j].events |= POLLOUT; } j++; } } #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4) int ready = ppoll(fds, j, timeout, NULL); #else int ready = poll(fds, j, timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000); #endif if (ready > 0) { //search the file descriptors, call all relavant callbacks for (i = 0, j = 0; i < event_count; i++) { if (events[i].fds_id != j) { continue; } if (fds[j].revents) { int flags = 0; if (fds[j].revents & POLLIN) { flags |= EVENT_READ; } if (fds[j].revents & POLLOUT) { flags |= EVENT_WRITE; } if (fds[j].revents & POLLHUP) { flags |= EVENT_HUP; } if (fds[j].revents & POLLERR) { flags |= EVENT_ERR; } events[i].callback(flags, events[i].data); } j++; } } free(fds); return 0; } int event_del(const int fd) { int i; for (i = 0; i < event_count; i++) { if (events[i].fd == fd) { events[i] = events[event_count - 1]; break; } } event_count--; events = realloc(events, sizeof(event_t) * event_count); return 0; } int event_modify(const int fd, const int read, const int write, const int active) { int i; for (i = 0; i < event_count; i++) { if (events[i].fd == fd) { events[i].read = read; events[i].write = write; events[i].active = active; break; } } event_count--; events = realloc(events, sizeof(event_t) * event_count); return 0; } static void free_events(void) { if (events != NULL) { free(events); } event_count = 0; events = NULL; } void event_exit(void) { free_events(); } /* * Named events are the user facing side of the event subsystem * */ //we rely on "=" working for copying these structs (no pointers except the callers) typedef struct { void (*callback) (void *data); void *data; } event_callback_t; typedef struct { char *name; event_callback_t *c; int callback_count; } named_event_list_t; static named_event_list_t *ev_names = NULL; static int ev_count = 0; int named_event_add(char *event, void (*callback) (void *data), void *data) { if (event == NULL || strlen(event) == 0) { return 1; } if (callback == NULL) { return 2; } int i; for (i = 0; i < ev_count; i++) { if (0 == strcmp(event, ev_names[i].name)) { break; } } if (i >= ev_count) { //create the entry ev_count++; ev_names = realloc(ev_names, sizeof(named_event_list_t) * ev_count); ev_names[i].name = strdup(event); ev_names[i].callback_count = 0; ev_names[i].c = NULL; } int j = ev_names[i].callback_count; ev_names[i].callback_count++; ev_names[i].c = realloc(ev_names[i].c, sizeof(event_callback_t) * ev_names[i].callback_count); ev_names[i].c[j].callback = callback; ev_names[i].c[j].data = data; return 0; } int named_event_del(char *event, void (*callback) (void *data), void *data) { int i, j; for (i = 0; i < ev_count; i++) { if (0 == strcmp(event, ev_names[i].name)) { break; } } if (i >= ev_count) { return 1; //nothing removed } for (j = 0; j < ev_names[i].callback_count; j++) { if (ev_names[i].c[j].callback == callback && ev_names[i].c[j].data == data) { ev_names[i].callback_count--; ev_names[i].c[j] = ev_names[i].c[ev_names[i].callback_count]; ev_names[i].c = realloc(ev_names[i].c, sizeof(event_callback_t) * ev_names[i].callback_count); if (ev_names[i].callback_count == 0) { //drop this event free(ev_names[i].name); ev_count--; ev_names[i] = ev_names[ev_count]; ev_names = realloc(ev_names, sizeof(named_event_list_t) * ev_count); } return 0; } } return 2; } int named_event_trigger(char *event) { int i, j; for (i = 0; i < ev_count; i++) { if (0 == strcmp(event, ev_names[i].name)) { for (j = 0; j < ev_names[i].callback_count; j++) { ev_names[i].c[j].callback(ev_names[i].c[j].data); } return 0; } } return 1; }