Skip to content

Latest commit

 

History

History
382 lines (327 loc) · 8.94 KB

profileclient.c

File metadata and controls

382 lines (327 loc) · 8.94 KB
 
Jan 26, 2011
Jan 26, 2011
1
Jan 28, 2011
Jan 28, 2011
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
/******************************************************************************
** This file is part of profile-qt
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer. Redistributions in
** binary form must reproduce the above copyright notice, this list of
** conditions and the following disclaimer in the documentation and/or
** other materials provided with the distribution.
**
** Neither the name of Nokia Corporation nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
Jan 26, 2011
Jan 26, 2011
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "profiled_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <time.h>
#include <sys/time.h>
#include "libprofile-internal.h"
#include "xutil.h"
static char *slice(char **p, int c)
{
char *b = *p;
char *e = strchr(b, c);
if( e != 0 )
{
if( *e ) *e++ = 0;
}
else
{
e = strchr(b, 0);
}
*p = e;
return b;
}
static void parse(char *s, char **p, char **k, char **v)
{
*p = 0;
if( strchr(s, ':') )
{
*p = slice(&s, ':');
}
*k = slice(&s, '=');
*v = slice(&s, 0);
}
static void getmonotime(struct timeval *tv)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
TIMESPEC_TO_TIMEVAL(tv, &ts);
}
static void message(const char *profile, const char *fmt, ...)
__attribute__((format(printf,2,3)));
static void message(const char *profile, const char *fmt, ...)
{
static char *previous = 0;
static struct timeval zen;
static const struct timeval tmo = { 0, 100 * 1000 };
struct timeval now, dif;
int gap = 0;
getmonotime(&now);
timersub(&now, &zen, &dif);
if( timercmp(&tmo, &dif, <) )
{
zen = now;
gap = 1;
}
if( !xstrsame(previous, profile) )
{
xstrset(&previous, profile);
gap = 1;
}
if( gap ) printf("\n");
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
va_end(va);
}
static
void
track_profile(const char *profile)
{
message(profile, "PROFILE -> '%s'\n", profile);
}
static
void
track_active(const char *profile, const char *key, const char *val, const char *type)
{
message(profile, "%s*: %s = %s (%s)\n", profile, key, val, type);
}
static
void
track_change(const char *profile, const char *key, const char *val, const char *type)
{
message(profile, "%s: %s = %s (%s)\n", profile, key, val, type);
}
static int track(void)
{
int exit_code = EXIT_FAILURE;
GMainLoop *mainloop = g_main_loop_new(NULL, FALSE);
profile_track_set_profile_cb(track_profile);
profile_track_set_active_cb(track_active);
profile_track_set_change_cb(track_change);
profile_tracker_init();
g_main_loop_run(mainloop);
exit_code = EXIT_SUCCESS;
profile_tracker_quit();
if( mainloop != 0 )
{
g_main_loop_unref(mainloop);
mainloop = 0;
}
return exit_code;
}
static const char usage[] =
"NAME\n"
" profileclient -- command line utility for profile daemon access\n"
"\n"
"SYNOPSIS\n"
" profileclient <options>\n"
"\n"
"DESCRIPTION\n"
" This tools can be used to set and query profile data from profile\n"
" daemon over dbus interface.\n"
" interface.\n"
"\n"
"OPTIONS\n"
" -h\n"
" This help text\n"
" -p <profile>\n"
" Set currently active profile.\n"
" -l\n"
" List available profiles, active marked with asterisk (*).\n"
" -k\n"
" List available profile keys.\n"
" -v\n"
" List values for current profile.\n"
" -V <profile>\n"
" List values for named profile.\n"
" -T\n"
" Start tracking profile changes.\n"
" -s [profile:]<key>=<value>\n"
" Set profile value in named/current profile.\n"
" -g [profile:]<key>\n"
" Get profile value in named/current profile.\n"
" -G [profile:]<key>\n"
" Get profile value in named/current profile.\n"
" Print results also as parsed bool/int/double.\n"
" -d [profile:]<key>\n"
" Reset profile value in named/current profile.\n"
" -t <key>\n"
" Get type of given key. \n"
" -r\n"
" Reset current profile to default values\n"
" -R <profile>\n"
" Reset named profile to default values\n"
"\n"
"EXAMPLES\n"
" % profileclient -psilent\n"
" \n"
" Activate 'silent' profile.\n"
" \n"
" % profileclient -T\n"
" \n"
" Any changes to profile data are emitted to stdout.\n"
"\n"
"\n"
"COPYRIGHT\n"
" Copyright (C) 2008 Nokia Corporation.\n"
" \n"
"SEE ALSO\n"
" profiled\n";
int main(int argc, char **argv)
{
int opt;
char *p,*k,*v,**w;
while( (opt = getopt(argc, argv, "hp:s:d:g:t:TlkvV:B:I:D:G:rR:")) != -1 )
{
switch (opt)
{
case 'h':
Oct 4, 2013
Oct 4, 2013
231
232
233
234
if( write(STDOUT_FILENO, usage, sizeof usage - 1) == -1 )
{
// we do not really care, but keep static analyzers happy
}
Jan 26, 2011
Jan 26, 2011
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
exit(0);
case 'p':
profile_set_profile(optarg);
break;
case 'l':
p = profile_get_profile();
w = profile_get_profiles();
for( size_t i = 0; w && w[i]; ++i )
{
printf("%s%s\n", w[i], strcmp(w[i],p) ? "" : "*");
}
profile_free_profiles(w);
free(p);
break;
case 'k':
w = profile_get_keys();
for( size_t i = 0; w && w[i]; ++i )
{
printf("%s\n", w[i]);
}
profile_free_keys(w);
break;
case 'r':
w = profile_get_keys();
for( size_t i = 0; w && w[i]; ++i )
{
profile_set_value(0, w[i], "");
}
profile_free_keys(w);
break;
case 'R':
w = profile_get_keys();
for( size_t i = 0; w && w[i]; ++i )
{
profile_set_value(optarg, w[i], "");
}
profile_free_keys(w);
break;
case 'v':
case 'V':
{
p = (opt == 'v') ? 0 : optarg;
profileval_t *t = profile_get_values(p);
for( size_t i = 0; t && t[i].pv_key; ++i )
{
printf("%s = %s (%s)\n", t[i].pv_key, t[i].pv_val, t[i].pv_type);
}
profile_free_values(t);
}
break;
case 'T':
exit(track());
case 's':
parse(optarg, &p,&k,&v);
profile_set_value(p, k, v);
break;
case 'd':
parse(optarg, &p,&k,&v);
profile_set_value(p, k, "");
break;
case 'g':
parse(optarg, &p,&k,&v);
v = profile_get_value(p,k);
printf("%s\n", v);
free(v);
break;
case 'G':
{
parse(optarg, &p,&k,&v);
char *s = profile_get_value(p,k);
printf("STRING: '%s'\n", s);
free(s);
int b = profile_get_value_as_bool(p,k);
printf("BOOL: %s (%d)\n", b ? "On" : "Off", b);
int i = profile_get_value_as_int(p,k);
printf("INT: %d\n", i);
double d = profile_get_value_as_double(p,k);
printf("DOUBLE:%g\n", d);
}
break;
case 'B':
{
parse(optarg, &p,&k,&v);
int v = profile_get_value_as_bool(p,k);
printf("BOOL:%s (%d)\n", v ? "On" : "Off", v);
}
break;
case 'I':
{
parse(optarg, &p,&k,&v);
int v = profile_get_value_as_int(p,k);
printf("INT:%d\n", v);
}
break;
case 'D':
{
parse(optarg, &p,&k,&v);
double v = profile_get_value_as_double(p,k);
printf("DOUBLE:%g\n", v);
}
break;
case 't':
v = profile_get_type(optarg);
printf("%s\n", v);
free(v);
break;
default: /* '?' */
fprintf(stderr, "(use -h for usage info)\n");
exit(EXIT_FAILURE);
}
}
return 0;
}
#if 0
int profile_has_profile (const char *profile);
int profile_has_value (const char *key);
int profile_get_value_as_int (const char *profile, const char *key);
int profile_set_value_as_int (const char *profile, const char *key, int val);
int profile_get_value_as_double(const char *profile, const char *key);
int profile_set_value_as_double(const char *profile, const char *key, double val);
#endif