~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_seti.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_seti.c 1136 2010-11-28 16:07:16Z mzuther $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_seti.c $
 
3
 *
 
4
 * plugin for seti@home status reporting
 
5
 *
 
6
 * Copyright (C) 2004 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * based on the old seti client which is 
 
10
 * Copyright (C) 2001 Axel Ehnert <axel@ehnert.net>
 
11
 *
 
12
 *
 
13
 * This file is part of LCD4Linux.
 
14
 *
 
15
 * LCD4Linux is free software; you can redistribute it and/or modify
 
16
 * it under the terms of the GNU General Public License as published by
 
17
 * the Free Software Foundation; either version 2, or (at your option)
 
18
 * any later version.
 
19
 *
 
20
 * LCD4Linux is distributed in the hope that it will be useful,
 
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 * GNU General Public License for more details.
 
24
 *
 
25
 * You should have received a copy of the GNU General Public License
 
26
 * along with this program; if not, write to the Free Software
 
27
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 *
 
29
 */
 
30
 
 
31
/* 
 
32
 * exported functions:
 
33
 *
 
34
 * int plugin_init_seti (void)
 
35
 *  adds functions to access /seti/state.sah
 
36
 *
 
37
 */
 
38
 
 
39
 
 
40
#include "config.h"
 
41
 
 
42
#include <stdlib.h>
 
43
#include <stdio.h>
 
44
#include <string.h>
 
45
#include <ctype.h>
 
46
#include <errno.h>
 
47
 
 
48
#include "debug.h"
 
49
#include "plugin.h"
 
50
#include "hash.h"
 
51
#include "cfg.h"
 
52
 
 
53
#define SECTION   "Plugin:Seti"
 
54
#define DIRKEY    "Directory"
 
55
#define STATEFILE "state.sah"
 
56
 
 
57
static HASH SETI;
 
58
static int fatal = 0;
 
59
 
 
60
static int parse_seti(void)
 
61
{
 
62
    static char fn[256] = "";
 
63
    FILE *stream;
 
64
    int age;
 
65
 
 
66
    /* if a fatal error occurred, do nothing */
 
67
    if (fatal != 0)
 
68
        return -1;
 
69
 
 
70
    /* reread every 100 msec only */
 
71
    age = hash_age(&SETI, NULL);
 
72
    if (age > 0 && age <= 100)
 
73
        return 0;
 
74
 
 
75
    if (fn[0] == '\0') {
 
76
        char *dir = cfg_get(SECTION, DIRKEY, NULL);
 
77
        if (dir == NULL || *dir == '\0') {
 
78
            error("no '%s.%s' entry from %s\n", SECTION, DIRKEY, cfg_source());
 
79
            fatal = 1;
 
80
            return -1;
 
81
        }
 
82
        if (strlen(dir) > sizeof(fn) - sizeof(STATEFILE) - 2) {
 
83
            error("entry '%s.%s' too long from %s!\n", SECTION, DIRKEY, cfg_source());
 
84
            fatal = 1;
 
85
            free(dir);
 
86
            return -1;
 
87
        }
 
88
        strcpy(fn, dir);
 
89
        if (fn[strlen(fn) - 1] != '/')
 
90
            strcat(fn, "/");
 
91
        strcat(fn, STATEFILE);
 
92
        free(dir);
 
93
    }
 
94
 
 
95
    stream = fopen(fn, "r");
 
96
    if (stream == NULL) {
 
97
        error("fopen(%s) failed: %s", fn, strerror(errno));
 
98
        return -1;
 
99
    }
 
100
 
 
101
    while (!feof(stream)) {
 
102
        char buffer[256];
 
103
        char *c, *key, *val;
 
104
        fgets(buffer, sizeof(buffer), stream);
 
105
        c = strchr(buffer, '=');
 
106
        if (c == NULL)
 
107
            continue;
 
108
        key = buffer;
 
109
        val = c + 1;
 
110
        /* strip leading blanks from key */
 
111
        while (isspace(*key))
 
112
            *key++ = '\0';
 
113
        /* strip trailing blanks from key */
 
114
        do
 
115
            *c = '\0';
 
116
        while (isspace(*--c));
 
117
        /* strip leading blanks from value */
 
118
        while (isspace(*val))
 
119
            *val++ = '\0';
 
120
        /* strip trailing blanks from value */
 
121
        for (c = val; *c != '\0'; c++);
 
122
        while (isspace(*--c))
 
123
            *c = '\0';
 
124
        /* add entry to hash table */
 
125
        hash_put(&SETI, key, val);
 
126
    }
 
127
 
 
128
    fclose(stream);
 
129
 
 
130
    return 0;
 
131
}
 
132
 
 
133
 
 
134
static void my_seti(RESULT * result, RESULT * arg1)
 
135
{
 
136
    char *key, *val;
 
137
 
 
138
    if (parse_seti() < 0) {
 
139
        SetResult(&result, R_STRING, "");
 
140
        return;
 
141
    }
 
142
 
 
143
    key = R2S(arg1);
 
144
    val = hash_get(&SETI, key, NULL);
 
145
    if (val == NULL)
 
146
        val = "";
 
147
 
 
148
    SetResult(&result, R_STRING, val);
 
149
}
 
150
 
 
151
 
 
152
int plugin_init_seti(void)
 
153
{
 
154
    hash_create(&SETI);
 
155
    AddFunction("seti", 1, my_seti);
 
156
    return 0;
 
157
}
 
158
 
 
159
 
 
160
void plugin_exit_seti(void)
 
161
{
 
162
    hash_destroy(&SETI);
 
163
}