Skip to content

Commit f8eb415

Browse files
committed
initial checkin of nvram-faker and supporting inih library
0 parents  commit f8eb415

32 files changed

+1306
-0
lines changed

Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
TRUNK ?=.
2+
include $(TRUNK)/arch.mk
3+
4+
TEST=test-write-pid
5+
6+
AR?=ar
7+
8+
CFLAGS+=-g
9+
OBJS=nvram-faker.o ini.o
10+
11+
LIB=libnvram-faker.so
12+
#CC=mipsel-linux-gcc
13+
all:$(LIB)
14+
15+
%.o:%.c
16+
$(CC) $(CFLAGS) -fPIC -c -o $@ $<
17+
18+
$(LIB): $(OBJS)
19+
$(CC) -shared -o $@ $^ -Wl,-nostdlib
20+
21+
clean:
22+
-rm *.o
23+
-rm *.so
24+
25+

arch.mk

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ifeq ($(ARCH),mips)
2+
export HOST?="$(ARCH)-linux"
3+
#export OLDPATH=$(PATH)
4+
#MIPS_TOOLCHAIN_PATH=/opt/gcc/$(ARCH)/host/usr/bin
5+
#export PATH:=$(MIPS_TOOLCHAIN_PATH):$(OLDPATH) export
6+
#LD?=$(MIPS_TOOLCHAIN_PATH)/$(HOST)-ld
7+
endif
8+
9+
ifeq ($(ARCH),mipsel)
10+
export HOST?="$(ARCH)-linux"
11+
endif
12+

buildmips.sh

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
export ARCH=mips
4+
TARGET=$1
5+
6+
# Sets up toolchain environment variables for various mips toolchain
7+
8+
warn()
9+
{
10+
echo "$1" >&2
11+
}
12+
13+
if [ ! -z $(which mips-linux-gcc) ];
14+
then
15+
export CC=$(which mips-linux-gcc)
16+
else
17+
warn "Not setting CC: can't locate mips-linux-gcc."
18+
fi
19+
20+
if [ ! -z $(which mips-linux-ld) ];
21+
then
22+
export LD=$(which mips-linux-ld)
23+
else
24+
warn "Not setting LD: can't locate mips-linux-ld."
25+
fi
26+
27+
if [ ! -z $(which mips-linux-ar) ];
28+
then
29+
export AR=$(which mips-linux-ar)
30+
else
31+
warn "Not setting AR: can't locate mips-linux-ar."
32+
fi
33+
34+
35+
if [ ! -z $(which mips-linux-strip) ];
36+
then
37+
export STRIP=$(which mips-linux-strip)
38+
else
39+
warn "Not setting STRIP: can't locate mips-linux-strip."
40+
fi
41+
42+
if [ ! -z $(which mips-linux-nm) ];
43+
then
44+
export NM=$(which mips-linux-nm)
45+
else
46+
warn "Not setting NM: can't lcoate mips-linux-nm."
47+
fi
48+
49+
50+
make $TARGET || exit $?
51+
52+

contrib/inih/LICENSE.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
The "inih" library is distributed under the New BSD license:
3+
4+
Copyright (c) 2009, Brush Technology
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
* Neither the name of Brush Technology nor the names of its contributors
15+
may be used to endorse or promote products derived from this software
16+
without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY
19+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY
22+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

contrib/inih/README.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
inih is a simple .INI file parser written in C, released under the New BSD
3+
license (see LICENSE.txt). Go to the project home page for more info:
4+
5+
http://code.google.com/p/inih/

contrib/inih/cpp/INIReader.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Read an INI file into easy-to-access name/value pairs.
2+
3+
#include <algorithm>
4+
#include <cctype>
5+
#include <cstdlib>
6+
#include "../ini.h"
7+
#include "INIReader.h"
8+
9+
using std::string;
10+
11+
INIReader::INIReader(string filename)
12+
{
13+
_error = ini_parse(filename.c_str(), ValueHandler, this);
14+
}
15+
16+
int INIReader::ParseError()
17+
{
18+
return _error;
19+
}
20+
21+
string INIReader::Get(string section, string name, string default_value)
22+
{
23+
string key = MakeKey(section, name);
24+
return _values.count(key) ? _values[key] : default_value;
25+
}
26+
27+
long INIReader::GetInteger(string section, string name, long default_value)
28+
{
29+
string valstr = Get(section, name, "");
30+
const char* value = valstr.c_str();
31+
char* end;
32+
// This parses "1234" (decimal) and also "0x4D2" (hex)
33+
long n = strtol(value, &end, 0);
34+
return end > value ? n : default_value;
35+
}
36+
37+
bool INIReader::GetBoolean(string section, string name, bool default_value)
38+
{
39+
string valstr = Get(section, name, "");
40+
// Convert to lower case to make string comparisons case-insensitive
41+
std::transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower);
42+
if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1")
43+
return true;
44+
else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0")
45+
return false;
46+
else
47+
return default_value;
48+
}
49+
50+
string INIReader::MakeKey(string section, string name)
51+
{
52+
string key = section + "." + name;
53+
// Convert to lower case to make section/name lookups case-insensitive
54+
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
55+
return key;
56+
}
57+
58+
int INIReader::ValueHandler(void* user, const char* section, const char* name,
59+
const char* value)
60+
{
61+
INIReader* reader = (INIReader*)user;
62+
string key = MakeKey(section, name);
63+
if (reader->_values[key].size() > 0)
64+
reader->_values[key] += "\n";
65+
reader->_values[key] += value;
66+
return 1;
67+
}

contrib/inih/cpp/INIReader.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Read an INI file into easy-to-access name/value pairs.
2+
3+
// inih and INIReader are released under the New BSD license (see LICENSE.txt).
4+
// Go to the project home page for more info:
5+
//
6+
// http://code.google.com/p/inih/
7+
8+
#ifndef __INIREADER_H__
9+
#define __INIREADER_H__
10+
11+
#include <map>
12+
#include <string>
13+
14+
// Read an INI file into easy-to-access name/value pairs. (Note that I've gone
15+
// for simplicity here rather than speed, but it should be pretty decent.)
16+
class INIReader
17+
{
18+
public:
19+
// Construct INIReader and parse given filename. See ini.h for more info
20+
// about the parsing.
21+
INIReader(std::string filename);
22+
23+
// Return the result of ini_parse(), i.e., 0 on success, line number of
24+
// first error on parse error, or -1 on file open error.
25+
int ParseError();
26+
27+
// Get a string value from INI file, returning default_value if not found.
28+
std::string Get(std::string section, std::string name,
29+
std::string default_value);
30+
31+
// Get an integer (long) value from INI file, returning default_value if
32+
// not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
33+
long GetInteger(std::string section, std::string name, long default_value);
34+
35+
// Get a boolean value from INI file, returning default_value if not found or if
36+
// not a valid true/false value. Valid true values are "true", "yes", "on", "1",
37+
// and valid false values are "false", "no", "off", "0" (not case sensitive).
38+
bool GetBoolean(std::string section, std::string name, bool default_value);
39+
40+
private:
41+
int _error;
42+
std::map<std::string, std::string> _values;
43+
static std::string MakeKey(std::string section, std::string name);
44+
static int ValueHandler(void* user, const char* section, const char* name,
45+
const char* value);
46+
};
47+
48+
#endif // __INIREADER_H__

contrib/inih/cpp/INIReaderTest.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Example that shows simple usage of the INIReader class
2+
3+
#include <iostream>
4+
#include "INIReader.h"
5+
6+
int main()
7+
{
8+
INIReader reader("../examples/test.ini");
9+
10+
if (reader.ParseError() < 0) {
11+
std::cout << "Can't load 'test.ini'\n";
12+
return 1;
13+
}
14+
std::cout << "Config loaded from 'test.ini': version="
15+
<< reader.GetInteger("protocol", "version", -1) << ", name="
16+
<< reader.Get("user", "name", "UNKNOWN") << ", email="
17+
<< reader.Get("user", "email", "UNKNOWN") << ", active="
18+
<< reader.GetBoolean("user", "active", true) << "\n";
19+
return 0;
20+
}

contrib/inih/examples/config.def

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// CFG(section, name, default)
2+
3+
CFG(protocol, version, "0")
4+
5+
CFG(user, name, "Fatty Lumpkin")
6+
CFG(user, email, "[email protected]")
7+
8+
#undef CFG

contrib/inih/examples/ini_dump.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* ini.h example that simply dumps an INI file without comments */
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include "../ini.h"
6+
7+
static int dumper(void* user, const char* section, const char* name,
8+
const char* value)
9+
{
10+
static char prev_section[50] = "";
11+
12+
if (strcmp(section, prev_section)) {
13+
printf("%s[%s]\n", (prev_section[0] ? "\n" : ""), section);
14+
strncpy(prev_section, section, sizeof(prev_section));
15+
prev_section[sizeof(prev_section) - 1] = '\0';
16+
}
17+
printf("%s = %s\n", name, value);
18+
return 1;
19+
}
20+
21+
int main(int argc, char* argv[])
22+
{
23+
int error;
24+
25+
if (argc <= 1) {
26+
printf("Usage: ini_dump filename.ini\n");
27+
return 1;
28+
}
29+
30+
error = ini_parse(argv[1], dumper, NULL);
31+
if (error < 0) {
32+
printf("Can't read '%s'!\n", argv[1]);
33+
return 2;
34+
}
35+
else if (error) {
36+
printf("Bad config file (first error on line %d)!\n", error);
37+
return 3;
38+
}
39+
return 0;
40+
}

contrib/inih/examples/ini_example.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Example: parse a simple configuration file */
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include "../ini.h"
7+
8+
typedef struct
9+
{
10+
int version;
11+
const char* name;
12+
const char* email;
13+
} configuration;
14+
15+
static int handler(void* user, const char* section, const char* name,
16+
const char* value)
17+
{
18+
configuration* pconfig = (configuration*)user;
19+
20+
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
21+
if (MATCH("protocol", "version")) {
22+
pconfig->version = atoi(value);
23+
} else if (MATCH("user", "name")) {
24+
pconfig->name = strdup(value);
25+
} else if (MATCH("user", "email")) {
26+
pconfig->email = strdup(value);
27+
} else {
28+
return 0; /* unknown section/name, error */
29+
}
30+
return 1;
31+
}
32+
33+
int main(int argc, char* argv[])
34+
{
35+
configuration config;
36+
37+
if (ini_parse("test.ini", handler, &config) < 0) {
38+
printf("Can't load 'test.ini'\n");
39+
return 1;
40+
}
41+
printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n",
42+
config.version, config.name, config.email);
43+
return 0;
44+
}

0 commit comments

Comments
 (0)