a698b11a2f735e977b6c5e919526f7963b18953f
[platform/core/system/tizen-platform-wrapper.git] / src / tzplatform_get.c
1 /*
2  * Copyright (C) 2013 Intel Corporation.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors:
19  *   José Bollo <jose.bollo@open.eurogiciel.org>
20  *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21  *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <tzplatform_config.h>
30
31 #define basename(x) (x)
32
33 static const char usage [] = "type '%s --help' to get help.\n";
34
35 static const char help [] = "\
36 \n\
37 usage: %s [options...] [keys...]\n\
38 \n\
39 options:\n\
40 \n\
41 -a --all        all keys (must not be followed by keys)\n\
42 -n --not        not in the keys\n\
43 -e --export     prefix with export\n\
44 -l --list       prints only the key names, not the values\n\
45 -s --space      separate with spaces instead of new-lines\n\
46 -q --query      silently check that given variables are existing\n\
47 -c --continue   continue to process if error\n\
48 \n\
49 ";
50
51 int main(int argc, char **argv)
52 {
53         char *progname = *argv++;
54         int all = 0, not = 0, query = 0, export = 0, space = 0, list = 0, cont = 0;
55         int i, n, *sel, p;
56         enum tzplatform_variable id;
57
58         /* parse args */
59         while(*argv && **argv=='-') {
60                 char c, *opt = 1+*argv++;
61                 while(opt) {
62                         if (*opt == '-') {
63                                 char *x = 0;
64                                 c = *++opt;
65                                 switch(c) {
66                                         case 'a': x = "all"; break;
67                                         case 'n': x = "not"; break;
68                                         case 'q': x = "query"; break;
69                                         case 'e': x = "export"; break;
70                                         case 's': x = "space"; break;
71                                         case 'l': x = "list"; break;
72                                         case 'c': x = "continue"; break;
73                                         case 'h': x = "help"; break;
74                                 }
75                                 if (!x || strcmp(x,opt))
76                                         c = 0;
77                                 opt = 0;
78                         }
79                         else {
80                                 c = *opt;
81                                 if (!*++opt)
82                                         opt = 0;
83                         }
84                         switch(c) {
85                         case 'a': all = 1; break;
86                         case 'n': not = 1; break;
87                         case 'q': query = 1; break;
88                         case 'e': export = 1; break;
89                         case 's': space = 1; break;
90                         case 'l': list = 1; break;
91                         case 'c': cont = 1; break;
92                         case 'h':
93                                 fprintf( stdout, help, basename(progname));
94                                 return 0;
95                         default:
96                                 fprintf( stderr, usage, basename(progname));
97                                 return 1;
98                         }
99                 }
100         }
101
102         /* some checks */
103         if (query) {
104                 if (all) {
105                         fprintf( stderr, 
106                                 "error! --all and --query aren't compatibles.\n");
107                         return 1;
108                 }
109                 if (not) {
110                         fprintf( stderr, 
111                                 "error! --not and --query aren't compatibles.\n");
112                         return 1;
113                 }
114                 if (list) {
115                         fprintf( stderr, 
116                                 "error! --list and --query aren't compatibles.\n");
117                         return 1;
118                 }
119                 if (cont) {
120                         fprintf( stderr, 
121                                 "error! --continue and --query aren't compatibles.\n");
122                         return 1;
123                 }
124                 if (space) {
125                         fprintf( stderr, 
126                                 "warning! --space option ignored for queries.\n");
127                 }
128                 if (export) {
129                         fprintf( stderr, 
130                                 "warning! --export option ignored for queries.\n");
131                 }
132         }
133         if (all) {
134                 if (*argv) {
135                         fprintf( stderr, 
136                                 "error! --all doesn't accept any key.\n");
137                         return 1;
138                 }
139                 /* all is like not nothing!! */
140                 not = 1;
141         }
142
143         /* process */
144         n = tzplatform_getcount();
145         sel = calloc( sizeof(int), n);
146         if (sel == NULL) {
147                 fprintf( stderr, "error! out of memory!\n");
148                 return 1;
149         }
150
151         /* get the variables from the list */
152         while (*argv) {
153                 id = tzplatform_getid( *argv);
154                 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
155                         if (query)
156                                 return 1;
157                         fprintf( stderr, "error! %s isn't a variable.\n", *argv);
158                         if (!cont)
159                                 return 1;
160                 }
161                 else {
162                         sel[(int)id] = 1;
163                 }
164                 argv++;
165         }
166
167         /* finished for queries */
168         if (query) 
169                 return 0;
170
171         /* emits the result */
172         for (p = i = 0 ; i < n ; i++) {
173                 if (sel[i] != not) {
174                         if (p++) 
175                                 printf( space ? " " : export ? "\nexport " : "\n");
176                         else if (export)
177                                 printf( "export ");
178                         id = (enum tzplatform_variable) i;
179                         printf( "%s", tzplatform_getname(id));
180                         if (!list)      
181                                 printf( "=%s", tzplatform_getenv(id));
182                 }
183         }
184         if (p)
185                 printf("\n");
186         return 0;
187 }
188