Skip to content

Commit

Permalink
initial checkin of nvram-faker and supporting inih library
Browse files Browse the repository at this point in the history
  • Loading branch information
zcutlip committed Jul 19, 2013
0 parents commit f8eb415
Show file tree
Hide file tree
Showing 32 changed files with 1,306 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
TRUNK ?=.
include $(TRUNK)/arch.mk

TEST=test-write-pid

AR?=ar

CFLAGS+=-g
OBJS=nvram-faker.o ini.o

LIB=libnvram-faker.so
#CC=mipsel-linux-gcc
all:$(LIB)

%.o:%.c
$(CC) $(CFLAGS) -fPIC -c -o $@ $<

$(LIB): $(OBJS)
$(CC) -shared -o $@ $^ -Wl,-nostdlib

clean:
-rm *.o
-rm *.so


12 changes: 12 additions & 0 deletions arch.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ifeq ($(ARCH),mips)
export HOST?="$(ARCH)-linux"
#export OLDPATH=$(PATH)
#MIPS_TOOLCHAIN_PATH=/opt/gcc/$(ARCH)/host/usr/bin
#export PATH:=$(MIPS_TOOLCHAIN_PATH):$(OLDPATH) export
#LD?=$(MIPS_TOOLCHAIN_PATH)/$(HOST)-ld
endif

ifeq ($(ARCH),mipsel)
export HOST?="$(ARCH)-linux"
endif

52 changes: 52 additions & 0 deletions buildmips.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh

export ARCH=mips
TARGET=$1

# Sets up toolchain environment variables for various mips toolchain

warn()
{
echo "$1" >&2
}

if [ ! -z $(which mips-linux-gcc) ];
then
export CC=$(which mips-linux-gcc)
else
warn "Not setting CC: can't locate mips-linux-gcc."
fi

if [ ! -z $(which mips-linux-ld) ];
then
export LD=$(which mips-linux-ld)
else
warn "Not setting LD: can't locate mips-linux-ld."
fi

if [ ! -z $(which mips-linux-ar) ];
then
export AR=$(which mips-linux-ar)
else
warn "Not setting AR: can't locate mips-linux-ar."
fi


if [ ! -z $(which mips-linux-strip) ];
then
export STRIP=$(which mips-linux-strip)
else
warn "Not setting STRIP: can't locate mips-linux-strip."
fi

if [ ! -z $(which mips-linux-nm) ];
then
export NM=$(which mips-linux-nm)
else
warn "Not setting NM: can't lcoate mips-linux-nm."
fi


make $TARGET || exit $?


27 changes: 27 additions & 0 deletions contrib/inih/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

The "inih" library is distributed under the New BSD license:

Copyright (c) 2009, Brush Technology
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Brush Technology nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5 changes: 5 additions & 0 deletions contrib/inih/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

inih is a simple .INI file parser written in C, released under the New BSD
license (see LICENSE.txt). Go to the project home page for more info:

http://code.google.com/p/inih/
67 changes: 67 additions & 0 deletions contrib/inih/cpp/INIReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Read an INI file into easy-to-access name/value pairs.

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include "../ini.h"
#include "INIReader.h"

using std::string;

INIReader::INIReader(string filename)
{
_error = ini_parse(filename.c_str(), ValueHandler, this);
}

int INIReader::ParseError()
{
return _error;
}

string INIReader::Get(string section, string name, string default_value)
{
string key = MakeKey(section, name);
return _values.count(key) ? _values[key] : default_value;
}

long INIReader::GetInteger(string section, string name, long default_value)
{
string valstr = Get(section, name, "");
const char* value = valstr.c_str();
char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
long n = strtol(value, &end, 0);
return end > value ? n : default_value;
}

bool INIReader::GetBoolean(string section, string name, bool default_value)
{
string valstr = Get(section, name, "");
// Convert to lower case to make string comparisons case-insensitive
std::transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower);
if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1")
return true;
else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0")
return false;
else
return default_value;
}

string INIReader::MakeKey(string section, string name)
{
string key = section + "." + name;
// Convert to lower case to make section/name lookups case-insensitive
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return key;
}

int INIReader::ValueHandler(void* user, const char* section, const char* name,
const char* value)
{
INIReader* reader = (INIReader*)user;
string key = MakeKey(section, name);
if (reader->_values[key].size() > 0)
reader->_values[key] += "\n";
reader->_values[key] += value;
return 1;
}
48 changes: 48 additions & 0 deletions contrib/inih/cpp/INIReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Read an INI file into easy-to-access name/value pairs.

// inih and INIReader are released under the New BSD license (see LICENSE.txt).
// Go to the project home page for more info:
//
// http://code.google.com/p/inih/

#ifndef __INIREADER_H__
#define __INIREADER_H__

#include <map>
#include <string>

// Read an INI file into easy-to-access name/value pairs. (Note that I've gone
// for simplicity here rather than speed, but it should be pretty decent.)
class INIReader
{
public:
// Construct INIReader and parse given filename. See ini.h for more info
// about the parsing.
INIReader(std::string filename);

// Return the result of ini_parse(), i.e., 0 on success, line number of
// first error on parse error, or -1 on file open error.
int ParseError();

// Get a string value from INI file, returning default_value if not found.
std::string Get(std::string section, std::string name,
std::string default_value);

// Get an integer (long) value from INI file, returning default_value if
// not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
long GetInteger(std::string section, std::string name, long default_value);

// Get a boolean value from INI file, returning default_value if not found or if
// not a valid true/false value. Valid true values are "true", "yes", "on", "1",
// and valid false values are "false", "no", "off", "0" (not case sensitive).
bool GetBoolean(std::string section, std::string name, bool default_value);

private:
int _error;
std::map<std::string, std::string> _values;
static std::string MakeKey(std::string section, std::string name);
static int ValueHandler(void* user, const char* section, const char* name,
const char* value);
};

#endif // __INIREADER_H__
20 changes: 20 additions & 0 deletions contrib/inih/cpp/INIReaderTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Example that shows simple usage of the INIReader class

#include <iostream>
#include "INIReader.h"

int main()
{
INIReader reader("../examples/test.ini");

if (reader.ParseError() < 0) {
std::cout << "Can't load 'test.ini'\n";
return 1;
}
std::cout << "Config loaded from 'test.ini': version="
<< reader.GetInteger("protocol", "version", -1) << ", name="
<< reader.Get("user", "name", "UNKNOWN") << ", email="
<< reader.Get("user", "email", "UNKNOWN") << ", active="
<< reader.GetBoolean("user", "active", true) << "\n";
return 0;
}
8 changes: 8 additions & 0 deletions contrib/inih/examples/config.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// CFG(section, name, default)

CFG(protocol, version, "0")

CFG(user, name, "Fatty Lumpkin")
CFG(user, email, "[email protected]")

#undef CFG
40 changes: 40 additions & 0 deletions contrib/inih/examples/ini_dump.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ini.h example that simply dumps an INI file without comments */

#include <stdio.h>
#include <string.h>
#include "../ini.h"

static int dumper(void* user, const char* section, const char* name,
const char* value)
{
static char prev_section[50] = "";

if (strcmp(section, prev_section)) {
printf("%s[%s]\n", (prev_section[0] ? "\n" : ""), section);
strncpy(prev_section, section, sizeof(prev_section));
prev_section[sizeof(prev_section) - 1] = '\0';
}
printf("%s = %s\n", name, value);
return 1;
}

int main(int argc, char* argv[])
{
int error;

if (argc <= 1) {
printf("Usage: ini_dump filename.ini\n");
return 1;
}

error = ini_parse(argv[1], dumper, NULL);
if (error < 0) {
printf("Can't read '%s'!\n", argv[1]);
return 2;
}
else if (error) {
printf("Bad config file (first error on line %d)!\n", error);
return 3;
}
return 0;
}
44 changes: 44 additions & 0 deletions contrib/inih/examples/ini_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Example: parse a simple configuration file */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ini.h"

typedef struct
{
int version;
const char* name;
const char* email;
} configuration;

static int handler(void* user, const char* section, const char* name,
const char* value)
{
configuration* pconfig = (configuration*)user;

#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("protocol", "version")) {
pconfig->version = atoi(value);
} else if (MATCH("user", "name")) {
pconfig->name = strdup(value);
} else if (MATCH("user", "email")) {
pconfig->email = strdup(value);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}

int main(int argc, char* argv[])
{
configuration config;

if (ini_parse("test.ini", handler, &config) < 0) {
printf("Can't load 'test.ini'\n");
return 1;
}
printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n",
config.version, config.name, config.email);
return 0;
}
Loading

0 comments on commit f8eb415

Please sign in to comment.