e3cd826d772324df7932859b61da820fec9a1d48
[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 done = 0;
93         int i;
94
95         /* Ugly... iterate over arguments to see whether we can expect a write */
96         for( i = 1; i < argc; i++ )
97                 if( ( !strcmp(argv[i], "set")   && ++i < argc ) ||
98                         ( !strcmp(argv[i], "unset") && ++i < argc ) ||
99                         !strcmp(argv[i], "commit") )
100                 {
101                         write = 1;
102                         break;
103                 }
104
105
106         if( (nvram = write ? nvram_open_staging() : nvram_open_rdonly()) != NULL && argc > 1 )
107         {
108                 for( i = 1; i < argc; i++ )
109                 {
110                         if( !strcmp(argv[i], "show") )
111                         {
112                                 stat = do_show(nvram);
113                                 done++;
114                         }
115                         else if( !strcmp(argv[i], "get") && ++i < argc )
116                         {
117                                 stat = do_get(nvram, argv[i]);
118                                 done++;
119                         }
120                         else if( !strcmp(argv[i], "unset") && ++i < argc )
121                         {
122                                 stat = do_unset(nvram, argv[i]);
123                                 done++;
124                         }
125                         else if( !strcmp(argv[i], "set") && ++i < argc )
126                         {
127                                 stat = do_set(nvram, argv[i]);
128                                 done++;
129                         }
130                         else if( !strcmp(argv[i], "commit") )
131                         {
132                                 commit = 1;
133                                 done++;
134                         }
135                         else
136                         {
137                                 done = 0;
138                                 break;
139                         }
140                 }
141
142                 if( write )
143                         stat = nvram_commit(nvram);
144
145                 nvram_close(nvram);
146
147                 if( commit )
148                         stat = staging_to_nvram();
149         }
150
151         if( !nvram )
152         {
153                 fprintf(stderr,
154                         "Could not open nvram! Possible reasons are:\n"
155                         "       - No device found (/proc not mounted or no nvram present)\n"
156                         "       - Insufficient permissions to open mtd device\n"
157                         "       - Insufficient memory to complete operation\n"
158                         "       - Memory mapping failed or not supported\n"
159                 );
160
161                 stat = 1;
162         }
163         else if( !done )
164         {
165                 fprintf(stderr,
166                         "Usage:\n"
167                         "       nvram show\n"
168                         "       nvram get variable\n"
169                         "       nvram set variable=value [set ...]\n"
170                         "       nvram unset variable [unset ...]\n"
171                         "       nvram commit\n"
172                 );
173
174                 stat = 1;
175         }
176
177         return stat;
178 }