~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to clients/upsc.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "upsclient.h"
27
27
 
28
 
static void help(char *prog)
 
28
static void help(const char *prog)
29
29
{
30
30
        printf("Network UPS Tools upsc %s\n\n", UPS_VERSION);
31
31
 
37
37
        printf("  <variable> - optional, display this variable only.\n");
38
38
        printf("               Default: list all variables for <host>\n");
39
39
 
40
 
        exit(0);
 
40
        exit(EXIT_SUCCESS);
41
41
}
42
42
 
43
43
static void clean_exit(UPSCONN *ups, char *upsname, char *hostname, int code)
53
53
        exit(code);
54
54
}
55
55
 
56
 
static int old_printvar(UPSCONN *ups, const char *upsname, const char *var)
57
 
{
58
 
        char    value[SMALLBUF];
59
 
 
60
 
        if (upscli_getvar(ups, upsname, var, value, sizeof(value)) < 0) {
61
 
                fprintf(stderr, "Error: %s\n", upscli_strerror(ups));
62
 
                return 1;
63
 
        }
64
 
 
65
 
        printf("%s\n", value);
66
 
 
67
 
        return 0;
68
 
}
69
 
 
70
 
static int new_printvar(UPSCONN *ups, const char *upsname, const char *var)
71
 
{
72
 
        int     ret, numq, numa;
 
56
static int printvar(UPSCONN *ups, const char *upsname, const char *var)
 
57
{
 
58
        int     ret;
 
59
        unsigned int    numq, numa;
73
60
        const   char    *query[4];
74
61
        char    **answer;
75
62
 
76
63
        /* old-style variable name? */
77
 
        if (!strchr(var, '.'))
78
 
                return old_printvar(ups, upsname, var);
 
64
        if (!strchr(var, '.')) {
 
65
                fprintf(stderr, "Error: old-style variable names are not supported\n");
 
66
                return EXIT_FAILURE;
 
67
        }
79
68
 
80
69
        if (!upsname) {
81
70
                fprintf(stderr, "Error: ups name must be defined\n");
82
 
                return 1;
 
71
                return EXIT_FAILURE;
83
72
        }
84
73
 
85
74
        query[0] = "VAR";
95
84
                /* new var and old upsd?  try to explain the situation */
96
85
                if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
97
86
                        fprintf(stderr, "Error: variable unknown (old upsd detected)\n");
98
 
                        return 1;
 
87
                        return EXIT_FAILURE;
99
88
                }
100
89
 
101
90
                fprintf(stderr, "Error: %s\n", upscli_strerror(ups));
102
 
                return 1;
 
91
                return EXIT_FAILURE;
103
92
        }
104
93
 
105
94
        if (numa < numq) {
106
95
                fprintf(stderr, "Error: insufficient data "
107
96
                        "(got %d args, need at least %d)\n", numa, numq);
108
 
                return 1;
 
97
                return EXIT_FAILURE;
109
98
        }
110
99
 
111
100
        printf("%s\n", answer[3]);
112
 
        return 0;
113
 
}
114
 
 
115
 
static int old_list(UPSCONN *ups, const char *upsname)
116
 
{
117
 
        char    vars[LARGEBUF], *v, *ptr;
118
 
 
119
 
        if (upscli_getlist(ups, upsname, UPSCLI_LIST_VARS, vars, 
120
 
                sizeof(vars)) < 0) {
121
 
                fprintf(stderr, "Unable to get variable list - %s\n", 
122
 
                        upscli_strerror(ups));
123
 
 
124
 
                return 1;
125
 
        }
126
 
 
127
 
        if (upsname)
128
 
                printf("UPS: %s\n", upsname);
129
 
        else
130
 
                printf("UPS: (default)\n");
131
 
 
132
 
        if (strlen(vars) == 0) {
133
 
                fprintf(stderr, "No data available - check your "
134
 
                        "configuration (ups.conf)\n");
135
 
 
136
 
                return 1;
137
 
        }
138
 
 
139
 
        v = vars;
140
 
        while (v != NULL) {
141
 
                ptr = strchr(v, ' ');
142
 
                if (ptr)
143
 
                        *ptr++ = '\0';
144
 
 
145
 
                printf("%s: ", v);
146
 
                old_printvar(ups, upsname, v);
147
 
 
148
 
                v = ptr;
149
 
        }               
150
 
 
151
 
        return 0;
152
 
}
153
 
 
154
 
static int new_list(UPSCONN *ups, const char *upsname)
155
 
{
156
 
        int     ret, numq, numa;
 
101
        return EXIT_SUCCESS;
 
102
}
 
103
 
 
104
static int list_vars(UPSCONN *ups, const char *upsname)
 
105
{
 
106
        int     ret;
 
107
        unsigned int    numq, numa;
157
108
        const   char    *query[4];
158
109
        char    **answer;
159
110
 
160
 
        if (!upsname)
161
 
                return old_list(ups, upsname);
 
111
        if (!upsname) {
 
112
                fprintf(stderr, "Error: a UPS name must be specified (upsname@hostname)\n");
 
113
                return EXIT_FAILURE;
 
114
        }
162
115
 
163
116
        query[0] = "VAR";
164
117
        query[1] = upsname;
168
121
 
169
122
        if (ret < 0) {
170
123
 
171
 
                /* old upsd --> fall back on old LISTVARS technique */
172
 
                if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND)
173
 
                        return old_list(ups, upsname);
 
124
                /* check for an old upsd */
 
125
                if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
 
126
                        fprintf(stderr, "Error: upsd is too old to support this query\n");
 
127
                        return EXIT_FAILURE;
 
128
                }
174
129
 
175
130
                fprintf(stderr, "Error: %s\n", upscli_strerror(ups));
176
 
                return 1;
 
131
                return EXIT_FAILURE;
177
132
        }
178
133
 
179
134
        ret = upscli_list_next(ups, numq, query, &numa, &answer);
184
139
                if (numa < 4) {
185
140
                        fprintf(stderr, "Error: insufficient data "
186
141
                                "(got %d args, need at least 4)\n", numa);
187
 
                        return 1;
 
142
                        return EXIT_FAILURE;
188
143
                }
189
144
 
190
145
                printf("%s: %s\n", answer[2], answer[3]);
192
147
                ret = upscli_list_next(ups, numq, query, &numa, &answer);
193
148
        }
194
149
 
195
 
        return 0;
 
150
        return EXIT_SUCCESS;
 
151
}
 
152
 
 
153
static void check_upsdef(const char *ups)
 
154
{
 
155
        char    *ptr;
 
156
 
 
157
        ptr = strchr(ups, '@');
 
158
 
 
159
        if (ptr)
 
160
                return;
 
161
 
 
162
        fprintf(stderr, "Error: invalid UPS definition.  Required format: upsname@hostname[:port]\n");
 
163
 
 
164
        exit(EXIT_FAILURE);
196
165
}
197
166
 
198
167
int main(int argc, char **argv)
207
176
        /* special cases since we're not using getopt */
208
177
        if (!strcmp(argv[1], "-V")) {
209
178
                printf("Network UPS Tools upsc %s\n", UPS_VERSION);
210
 
                exit(0);
 
179
                exit(EXIT_SUCCESS);
211
180
        }
212
181
 
213
182
        if (!strcmp(argv[1], "-h"))
214
183
                help(argv[0]);
215
184
 
216
 
        upscli_splitname(argv[1], &upsname, &hostname, &port);
 
185
        check_upsdef(argv[1]);
 
186
 
 
187
        upsname = hostname = NULL;
 
188
 
 
189
        if (upscli_splitname(argv[1], &upsname, &hostname, &port) != 0)
 
190
                clean_exit(&ups, upsname, hostname, EXIT_FAILURE);
217
191
 
218
192
        if (upscli_connect(&ups, hostname, port, UPSCLI_CONN_TRYSSL) < 0) {
219
193
                fprintf(stderr, "Error: %s\n", upscli_strerror(&ups));
220
194
 
221
 
                clean_exit(&ups, upsname, hostname, 1);
 
195
                clean_exit(&ups, upsname, hostname, EXIT_FAILURE);
222
196
        }
223
197
 
224
198
        if (argc >= 3) {
225
 
                ret = new_printvar(&ups, upsname, argv[2]);
 
199
                ret = printvar(&ups, upsname, argv[2]);
226
200
                clean_exit(&ups, upsname, hostname, ret);
227
201
        }
228
202
 
229
 
        ret = new_list(&ups, upsname);
 
203
        ret = list_vars(&ups, upsname);
230
204
        clean_exit(&ups, upsname, hostname, ret);
231
205
 
232
206
        /* NOTREACHED */
233
 
        return 0;
 
207
        exit(EXIT_FAILURE);
234
208
}