3a071994c5637abe693d533e2d6aa646762105c3
[project/luci.git] / contrib / userspace-nvram / cli.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <sys/mman.h>
6
7 #include "nvram.h"
8
9
10 static nvram_handle_t * nvram_open_rdonly(void)
11 {
12         const char *file = nvram_find_staging();
13
14         if( file == NULL )
15                 file = nvram_find_mtd();
16
17         if( file != NULL )
18                 return nvram_open(file, NVRAM_RO);
19
20         return NULL;
21 }
22
23 static nvram_handle_t * nvram_open_staging(void)
24 {
25         if( nvram_find_staging() != NULL || nvram_to_staging() == 0 )
26                 return nvram_open(NVRAM_STAGING, NVRAM_RW);
27
28         return NULL;
29 }
30
31 static int do_show(nvram_handle_t *nvram)
32 {
33         nvram_tuple_t *t;
34         int stat = 1;
35
36         if( (t = nvram_getall(nvram)) != NULL )
37         {
38                 while( t )
39                 {
40                         printf("%s=%s\n", t->name, t->value);
41                         t = t->next;
42                 }
43
44                 stat = 0;
45         }
46
47         return stat;
48 }
49
50 static int do_get(nvram_handle_t *nvram, const char *var)
51 {
52         const char *val;
53         int stat = 1;
54
55         if( (val = nvram_get(nvram, var)) != NULL )
56         {
57                 printf("%s\n", val);
58                 stat = 0;
59         }
60
61         return stat;
62 }
63
64 static int do_unset(nvram_handle_t *nvram, const char *var)
65 {
66         return nvram_unset(nvram, var);
67 }
68
69 static int do_set(nvram_handle_t *nvram, const char *pair)
70 {
71         char *val = strstr(pair, "=");
72         char var[strlen(pair)];
73         int stat = 1;
74
75         if( val != NULL )
76         {
77                 memset(var, 0, sizeof(var));
78                 strncpy(var, pair, (int)(val-pair));
79                 stat = nvram_set(nvram, var, (char *)(val + 1));
80         }
81
82         return stat;
83 }
84
85
86 int main( int argc, const char *argv[] )
87 {
88         nvram_handle_t *nvram;
89         int commit = 0;
90         int write = 0;
91         int stat = 1;
92         int i;
93
94         /* Ugly... iterate over arguments to see whether we can expect a write */
95         for( i = 1; i < argc; i++ )
96                 if( ( !strcmp(argv[i], "set")   && ++i < argc ) ||
97                         ( !strcmp(argv[i], "unset") && ++i < argc ) ||
98                         !strcmp(argv[i], "commit") )
99                 {
100                         write = 1;
101                         break;
102                 }
103
104
105         if( (nvram = write ? nvram_open_staging() : nvram_open_rdonly()) != NULL )
106         {
107                 for( i = 1; i < argc; i++ )
108                 {
109                         if( !strcmp(argv[i], "show") )
110                         {
111                                 stat = do_show(nvram);
112                         }
113                         else if( !strcmp(argv[i], "get") && ++i < argc )
114                         {
115                                 stat = do_get(nvram, argv[i]);
116                         }
117                         else if( !strcmp(argv[i], "unset") && ++i < argc )
118                         {
119                                 stat = do_unset(nvram, argv[i]);
120                         }
121                         else if( !strcmp(argv[i], "set") && ++i < argc )
122                         {
123                                 stat = do_set(nvram, argv[i]);
124                         }
125                         else if( !strcmp(argv[i], "commit") )
126                         {
127                                 commit = 1;
128                         }
129                         else
130                         {
131                                 fprintf(stderr,
132                                         "Usage:\n"
133                                         "       nvram show\n"
134                                         "       nvram get variable\n"
135                                         "       nvram set variable=value [set ...]\n"
136                                         "       nvram unset variable [unset ...]\n"
137                                         "       nvram commit\n"
138                                 );
139
140                                 return 1;
141                         }
142                 }
143
144                 if( write )
145                         stat = nvram_commit(nvram);
146
147                 nvram_close(nvram);
148
149                 if( commit )
150                         stat = staging_to_nvram();
151         }
152
153         return stat;
154 }