Initial commit
[platform/core/appfw/vconf-buxton.git] / src / vconf-buxton-tool.c
1 /*
2  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
3  * Copyright (C) 2014 Intel Corporation
4  *
5  * Author: José Bollo <jose.bollo@open.eurogiciel.org>
6  * Author: Hakjoo Ko <hakjoo.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "vconf-buxton.h"
26 #include "log.h"
27
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <wordexp.h>
35 #include <glib.h>
36
37 enum
38 {
39   VCONFTOOL_TYPE_NO = 0x00,
40   VCONFTOOL_TYPE_STRING,
41   VCONFTOOL_TYPE_INT,
42   VCONFTOOL_TYPE_DOUBLE,
43   VCONFTOOL_TYPE_BOOL
44 };
45
46 static char *guid = NULL;
47 static char *uid = NULL;
48 static char *vconf_type = NULL;
49 static int is_recursive = FALSE;
50 static int is_initialization = FALSE;
51 static int is_forced = FALSE;
52 static int get_num = 0;
53
54 static GOptionEntry entries[] = {
55   {"type", 't', 0, G_OPTION_ARG_STRING, &vconf_type, "type of value",
56    "int|bool|double|string"},
57   {"recursive", 'r', 0, G_OPTION_ARG_NONE, &is_recursive,
58    "retrieve keys recursively", NULL},
59   {"guid", 'g', 0, G_OPTION_ARG_STRING, &guid, "group permission", NULL},
60   {"uid", 'u', 0, G_OPTION_ARG_STRING, &uid, "user permission", NULL},
61   {"initialization", 'i', 0, G_OPTION_ARG_NONE, &is_initialization,
62    "memory backend initialization", NULL},
63   {"force", 'f', 0, G_OPTION_ARG_NONE, &is_forced,
64    "overwrite vconf values by force", NULL},
65   {NULL}
66 };
67
68 static void get_operation (const char *input);
69 static void print_keylist (keylist_t * keylist);
70
71 static char usage[] =
72   "Usage:\n"
73   "\n"
74   "[Set vconf value]\n"
75   "       %1$s set -t <TYPE> <KEY NAME> <VALUE> <OPTIONS>\n"
76   "                 <TYPE>=int|bool|double|string\n"
77   "\n"
78   "       Ex) %1$s set -t string db/testapp/key1 \"This is test\" \n"
79   "\n"
80   "       <OPTIONS>\n"
81   "          any option is ignored! (compatibility)\n"
82   "\n"
83   "[Get vconf value]\n"
84   "       %1$s get <OPTIONS> <KEY NAME>\n"
85   "\n"
86   "       <OPTIONS>\n"
87   "          -r : retrieve all keys included in sub-directorys \n"
88   "       Ex) %1$s get db/testapp/key1\n"
89   "           %1$s get db/testapp/\n"
90   "\n"
91   "[Unset vconf value]\n"
92   "       %1$s unset <KEY NAME>\n"
93   "\n" "       Ex) %1$s unset db/testapp/key1\n" "\n"
94   "\n"
95   "[Set vconf label (Smack)]\n"
96   "       %1$s label <KEY NAME> <SMACK LABEL>\n"
97   "\n" "       Ex) %1$s label db/testapp/key1 User::Share\n" "\n";
98
99 static void
100 print_help (const char *cmd)
101 {
102   fprintf (stderr, usage, cmd);
103 }
104
105 static int
106 check_type (void)
107 {
108   if (vconf_type)
109     {
110       if (!strncasecmp (vconf_type, "int", 3))
111         return VCONFTOOL_TYPE_INT;
112       else if (!strncasecmp (vconf_type, "string", 6))
113         return VCONFTOOL_TYPE_STRING;
114       else if (!strncasecmp (vconf_type, "double", 6))
115         return VCONFTOOL_TYPE_DOUBLE;
116       else if (!strncasecmp (vconf_type, "bool", 4))
117         return VCONFTOOL_TYPE_BOOL;
118     }
119   return VCONFTOOL_TYPE_NO;
120 }
121
122 int
123 main (int argc, char **argv)
124 {
125   int set_type;
126
127   GError *error = NULL;
128   GOptionContext *context;
129
130   context = g_option_context_new ("- vconf library tool");
131   g_option_context_add_main_entries (context, entries, NULL);
132   g_option_context_set_help_enabled (context, FALSE);
133   g_option_context_set_ignore_unknown_options (context, TRUE);
134
135   if (!g_option_context_parse (context, &argc, &argv, &error))
136     {
137       g_print ("option parsing failed: %s\n", error->message);
138       exit (1);
139     }
140
141   if (argc < 2)
142     {
143       print_help (argv[0]);
144       return 1;
145     }
146
147   if (!strcmp (argv[1], "set"))
148     {
149       set_type = check_type ();
150       if (argc < 4 || !set_type)
151         {
152           print_help (argv[0]);
153           return 1;
154         }
155
156       switch (set_type)
157         {
158         case VCONFTOOL_TYPE_STRING:
159           vconf_set_str (argv[2], argv[3]);
160           break;
161         case VCONFTOOL_TYPE_INT:
162           vconf_set_int (argv[2], atoi (argv[3]));
163           break;
164         case VCONFTOOL_TYPE_DOUBLE:
165           vconf_set_dbl (argv[2], atof (argv[3]));
166           break;
167         case VCONFTOOL_TYPE_BOOL:
168           vconf_set_bool (argv[2], !!atoi (argv[3]));
169           break;
170         default:
171           fprintf (stderr, "never reach");
172           exit (1);
173         }
174
175     }
176   else if (!strcmp (argv[1], "get"))
177     {
178       if (argv[2])
179         get_operation (argv[2]);
180       else
181         print_help (argv[0]);
182     }
183   else if (!strcmp (argv[1], "unset"))
184     {
185       if (argv[2])
186         vconf_unset (argv[2]);
187       else
188         print_help (argv[0]);
189     }
190   else if (!strcmp (argv[1], "label"))
191     {
192       if (argv[2] && argv[3])
193         vconf_set_label (argv[2], argv[3]);
194       else
195         print_help (argv[0]);
196     }
197   else
198     fprintf (stderr, "%s is a invalid command\n", argv[1]);
199   return 0;
200 }
201
202 static void
203 get_operation (const char *input)
204 {
205   keylist_t *keylist;
206
207   keylist = vconf_keylist_new ();
208   if (is_recursive)
209     {
210       vconf_scan (keylist, input, VCONF_GET_KEY_REC);
211     }
212   else
213     {
214       vconf_keylist_add_null (keylist, input);
215       vconf_refresh (keylist);
216     }
217   vconf_keylist_sort (keylist);
218   print_keylist (keylist);
219   if (!get_num)
220     printf ("No data\n");
221   vconf_keylist_free (keylist);
222 }
223
224 static void
225 print_keylist (keylist_t * keylist)
226 {
227   keynode_t *node;
228
229   vconf_keylist_rewind (keylist);
230   while ((node = vconf_keylist_nextnode (keylist)))
231     {
232       switch (vconf_keynode_get_type (node))
233         {
234         case VCONF_TYPE_INT:
235           printf ("%s, value = %d (int)\n",
236                   vconf_keynode_get_name (node),
237                   vconf_keynode_get_int (node));
238           get_num++;
239           break;
240         case VCONF_TYPE_BOOL:
241           printf ("%s, value = %d (bool)\n",
242                   vconf_keynode_get_name (node),
243                   vconf_keynode_get_bool (node));
244           get_num++;
245           break;
246         case VCONF_TYPE_DOUBLE:
247           printf ("%s, value = %f (double)\n",
248                   vconf_keynode_get_name (node),
249                   vconf_keynode_get_dbl (node));
250           get_num++;
251           break;
252         case VCONF_TYPE_STRING:
253           printf ("%s, value = %s (string)\n",
254                   vconf_keynode_get_name (node),
255                   vconf_keynode_get_str (node));
256           get_num++;
257           break;
258         default:
259           break;
260         }
261     }
262 }