Annotation of embedtools/src/cfger.c, revision 1.1.2.2
1.1.2.1 misho 1: /*************************************************************************
2: * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
1.1.2.2 ! misho 6: * $Id: cfger.c,v 1.1.2.1 2014/01/24 15:51:32 misho Exp $
1.1.2.1 misho 7: *
8: *************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004 - 2014
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: int Verbose;
50: extern char compiled[], compiledby[], compilehost[];
51:
52: cfg_root_t cfg;
53:
54:
55: static void
56: Usage()
57: {
58:
59: printf( "CFGer is tool for managment R/W operation with CFGs\n"
60: "=== %s === %s@%s ===\n\n"
61: " Syntax: cfger [options] <file_config> [data]\n\n"
62: "\t-v\t\tVerbose ...\n"
1.1.2.2 ! misho 63: "\t-q\t\tQuiet mode\n"
! 64: "\t-o <output>\tOutput result to file\n"
1.1.2.1 misho 65: "\t-l <attr>\tList A/V pair\n"
66: "\t-s <attr>\tSet A/V pair\n"
67: "\t-g <attr>\tGet value from A/V pair\n"
68: "*\"attr\" format: [section/]attribute\n"
69: "\n", compiled, compiledby, compilehost);
70: }
71:
72: int
73: main(int argc, char **argv)
74: {
1.1.2.2 ! misho 75: char ch, *str, m = 0, q = 0, szAttr[STRSIZ], szName[PATH_MAX] = { 0 };
1.1.2.1 misho 76: int ret = 0;
77: ait_val_t data = AIT_VAL_INITIALIZER(data);
1.1.2.2 ! misho 78: FILE *out = stdout;
1.1.2.1 misho 79:
1.1.2.2 ! misho 80: while ((ch = getopt(argc, argv, "hvql:s:g:o:")) != -1)
1.1.2.1 misho 81: switch (ch) {
82: case 'v':
83: Verbose++;
84: break;
1.1.2.2 ! misho 85: case 'q':
! 86: q = 42;
! 87: break;
! 88: case 'o':
! 89: strlcpy(szName, optarg, sizeof szName);
! 90: break;
1.1.2.1 misho 91: case 'l':
92: if (m) {
93: Usage();
94: return 1;
95: } else
96: m = 'l';
1.1.2.2 ! misho 97: strlcpy(szAttr, optarg, sizeof szAttr);
1.1.2.1 misho 98: break;
99: case 's':
100: if (m) {
101: Usage();
102: return 1;
103: } else
104: m = 's';
1.1.2.2 ! misho 105: strlcpy(szAttr, optarg, sizeof szAttr);
1.1.2.1 misho 106: break;
107: case 'g':
108: if (m) {
109: Usage();
110: return 1;
111: } else
112: m = 'g';
1.1.2.2 ! misho 113: strlcpy(szAttr, optarg, sizeof szAttr);
1.1.2.1 misho 114: break;
115: case 'h':
116: default:
117: Usage();
118: return 1;
119: }
120: argc -= optind;
121: argv += optind;
122: if (!argc) {
123: Usage();
124: return 1;
125: }
126: if (argc > 1 && argv[1])
127: AIT_SET_STR(&data, argv[1]);
128:
129: if (cfgLoadConfig(*argv, &cfg)) {
130: ELIBERR(cfg);
131: ret = 2;
132: goto end;
133: }
134:
1.1.2.2 ! misho 135: if (*szName) {
! 136: out = fopen(szName, "w");
! 137: if (!out) {
! 138: printf("Error:: Can't create file %s\n", szName);
! 139: ret = 2;
! 140: out = stdout;
! 141: goto end;
! 142: }
! 143: }
! 144:
1.1.2.1 misho 145: switch (m) {
146: case 'g':
1.1.2.2 ! misho 147: if (!q)
! 148: fprintf(out, "%s = ", szAttr);
! 149: str = strchr(szAttr, '/');
! 150: if (str) {
! 151: *str++ = 0;
! 152: str = (char*) cfg_getAttribute(&cfg, szAttr, str);
! 153: } else
! 154: str = (char*) cfg_getAttribute(&cfg, str, szAttr);
! 155: fprintf(out, "%s\n", str ? str : "\rError:: Variable not found!");
1.1.2.1 misho 156: break;
157: case 's':
1.1.2.2 ! misho 158: str = strchr(szAttr, '/');
! 159: if (str) {
! 160: *str++ = 0;
! 161: if (AIT_ISEMPTY(&data))
! 162: cfg_unsetAttribute(&cfg, szAttr, str);
! 163: else
! 164: cfg_setAttribute(&cfg, szAttr, str,
! 165: AIT_GET_STRZ(&data));
1.1.2.1 misho 166: } else {
1.1.2.2 ! misho 167: if (AIT_ISEMPTY(&data))
! 168: cfg_unsetAttribute(&cfg, str, szAttr);
! 169: else
! 170: cfg_setAttribute(&cfg, str, szAttr,
! 171: AIT_GET_STRZ(&data));
1.1.2.1 misho 172: }
173: case 'l':
174: default:
1.1.2.2 ! misho 175: cfgWriteConfig(out, &cfg, 42);
! 176: break;
1.1.2.1 misho 177: }
178: end:
1.1.2.2 ! misho 179: if (out != stdout)
! 180: fclose(out);
1.1.2.1 misho 181: AIT_FREE_VAL(&data);
182: cfgUnloadConfig(&cfg);
183: return ret;
184: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>