aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-02-17 20:00:33 +0900
committerFreeArtMan <dos21h@gmail.com>2015-02-17 20:00:33 +0900
commit670e03997afca107abbfc61a61f89477a9f9ab82 (patch)
treedc6b31a985104ef1180859780379408e1abdb215
parent35a421a727841319d5a998806924205ec6663bc2 (diff)
downloadcode-snippets-670e03997afca107abbfc61a61f89477a9f9ab82.tar.gz
code-snippets-670e03997afca107abbfc61a61f89477a9f9ab82.zip
c_conf_gen lua script that generates getopt code for fast argc,argv parsing. for lazy people
-rw-r--r--c_conf_gen/Makefile7
-rw-r--r--c_conf_gen/cconfgen.lua70
-rw-r--r--c_conf_gen/config/cs_config1.lua9
-rw-r--r--c_conf_gen/config/cs_config2.lua33
-rw-r--r--c_conf_gen/config/cs_config3.lua0
-rw-r--r--c_conf_gen/consts.lua121
6 files changed, 240 insertions, 0 deletions
diff --git a/c_conf_gen/Makefile b/c_conf_gen/Makefile
new file mode 100644
index 0000000..09232e0
--- /dev/null
+++ b/c_conf_gen/Makefile
@@ -0,0 +1,7 @@
+LUA=/home/fam/downloads/source/lua/lua-5.3.0/src/lua
+
+make:
+ @$(LUA) cconfgen.lua
+
+test:
+ @$(LUA) cconfgen.lua -c config/cs_config2 -o config/cs_config2 \ No newline at end of file
diff --git a/c_conf_gen/cconfgen.lua b/c_conf_gen/cconfgen.lua
new file mode 100644
index 0000000..8fcd27c
--- /dev/null
+++ b/c_conf_gen/cconfgen.lua
@@ -0,0 +1,70 @@
+#!/usr/bin/lua
+
+TYPE_INT = 1
+TYPE_STR = 2
+TYPE_FILE = 3
+
+consts = require('consts')
+
+debug_mode = false
+
+--check if version is supported
+--why 5.3? utf8 and 64bit support
+if (_VERSION ~= "Lua 5.3" ) then
+ print "Wrong lua versions"
+ print "Supported 5.3 only"
+ os.exit(1)
+end
+
+config_file = nil
+output_file = nil
+for k,v in ipairs( arg ) do
+ if v == "-c" then
+ config_file = arg[k+1]
+ elseif v == "-o" then
+ output_file = arg[k+1]
+ end
+end
+if (config_file == nil) or (output_file == nil) then
+ print("I need at leas one argument")
+ os.exit(1)
+end
+
+print("Config file " .. config_file .. " loaded")
+
+cfg = require( config_file )
+of = io.open( output_file .. ".c", "w" )
+
+of:write( consts.implement_start( output_file .. ".h" ) )
+
+--generate list of params
+local getopt_p = ""
+for k,v in ipairs( cfg.params_list ) do
+ if v ~= nil then
+ getopt_p = getopt_p .. v.shortopt
+ end
+end
+of:write( consts.getopt_start( getopt_p ) )
+
+for k,v in ipairs( cfg.params_list ) do
+ if v.type == TYPE_INT then
+ of:write( consts.getopt_int( v ) )
+ elseif v.type == TYPE_STR then
+ of:write( consts.getopt_string( v ))
+ elseif v.type == TYPE_FILE then
+ of:write( consts.getopt_file( v ))
+ end
+end
+
+of:write( consts.getopt_end() )
+
+of:write( consts.implement_end() )
+
+of:close()
+
+hf = io.open( output_file .. ".h", "w")
+hf:write( consts.header_def_start( string.upper( cfg.project_name ) ) )
+hf:write( consts.header_def_end( ) )
+hf:close()
+
+
diff --git a/c_conf_gen/config/cs_config1.lua b/c_conf_gen/config/cs_config1.lua
new file mode 100644
index 0000000..3069bd3
--- /dev/null
+++ b/c_conf_gen/config/cs_config1.lua
@@ -0,0 +1,9 @@
+--config for empty config parsing file
+
+local M = {}
+
+M.project_name = "HelloWorld"
+M.params_list = nil
+
+
+return M \ No newline at end of file
diff --git a/c_conf_gen/config/cs_config2.lua b/c_conf_gen/config/cs_config2.lua
new file mode 100644
index 0000000..9646834
--- /dev/null
+++ b/c_conf_gen/config/cs_config2.lua
@@ -0,0 +1,33 @@
+--config for empty config parsing file
+
+TYPE_INT = 1
+TYPE_STR = 2
+TYPE_FILE = 3
+
+local M = {}
+
+M.project_name = "INT_STR_FILE"
+M.params_list = {}
+
+param1 = {
+ type = TYPE_INT,
+ name = "number",
+ shortopt = "i:"
+}
+M.params_list[1] = param1
+
+param2 = {
+ type = TYPE_STR,
+ name = "username",
+ shortopt = "s:"
+}
+M.params_list[2] = param2
+
+param3 = {
+ type = TYPE_FILE,
+ name = "configfile",
+ shortopt = "f:"
+}
+M.params_list[3] = param3
+
+return M \ No newline at end of file
diff --git a/c_conf_gen/config/cs_config3.lua b/c_conf_gen/config/cs_config3.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_conf_gen/config/cs_config3.lua
diff --git a/c_conf_gen/consts.lua b/c_conf_gen/consts.lua
new file mode 100644
index 0000000..87e183a
--- /dev/null
+++ b/c_conf_gen/consts.lua
@@ -0,0 +1,121 @@
+local M = {}
+
+--
+local function header_def_start( name )
+ local s = ""
+ local name = string.upper( name )
+ s = s .. "#ifndef __" .. name .. "_H\n"
+ s = s .. "#define __" .. name .. "_H\n"
+ s = s .. "#include <unistd.h>\n"
+ s = s .. "void parse_args( int argc, char **argv, cmd_config *cmd );"
+ return s
+end
+M.header_def_start = header_def_start
+
+
+--
+local function header_def_end( )
+ local s = ""
+ s = s .. [[
+
+#endif
+]]
+ return s
+end
+M.header_def_end = header_def_end
+
+local function implement_start( headername )
+ local s = ""
+ s = s .. [[//auto-generated file no meaning to edit it
+#include "]].. headername .. [["
+]]
+ return s
+end
+M.implement_start = implement_start
+
+local function implement_end()
+ local s = ""
+ return s
+end
+M.implement_end = implement_end
+
+local function config_structure( values )
+ local s = ""
+ return s
+end
+M.config_structure = config_structure
+
+--make as separete function
+--just to make it easy to generate and then embed in code
+-- to not make name coalisions
+local function getopt_start( sup_args )
+ local s = ""
+ if sup_args == nil then
+ sup_args = "\"\""
+ end
+ s = s .. [[ void parse_args( int argc, char **argv, cmd_config *cmd )
+{
+ int c;
+ char *cvalue;
+
+ while ( (c = getopt(argc, argv, "]] .. sup_args .. [[" )) != -1 )
+ switch ( c )
+ {
+]]
+ return s
+end
+M.getopt_start = getopt_start
+
+local function getopt_end()
+ local s = ""
+ s = s .. [[
+ default:
+ printf("Unknown option\n");
+ }
+}
+]]
+ return s
+end
+M.getopt_end = getopt_end
+
+--
+local function getopt_int( param_config )
+ local s = ""
+ s = s .. [[
+ case ']] .. param_config.shortopt .. [[':{
+ ]] .. param_config.name .. [[ = atoi( optarg );
+ }
+]]
+ return s
+end
+M.getopt_int = getopt_int
+
+
+--
+local function getopt_string( param_config )
+ local s = ""
+ s = s .. [[
+ case ']] .. param_config.shortopt .. [[':{
+ ]] .. param_config.name .. [[ = atoi( optarg );
+ }
+]]
+ return s
+end
+M.getopt_string = getopt_string
+
+
+--
+local function getopt_file( param_config )
+ local s = ""
+ s = s .. [[
+ case ']] .. param_config.shortopt .. [[':{
+ ]] .. param_config.name .. [[ = atoi( optarg );
+ }
+]]
+ return s
+end
+M.getopt_file = getopt_file
+
+
+print("cconsts module loaded")
+return M \ No newline at end of file