From e4faae6550be391faf6294992980b8f28ab69656 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Sun, 19 Dec 2010 01:25:28 +0100 Subject: Added the first skeleton of a MIB compiler frontend escript. --- lib/snmp/src/compile/Makefile | 14 ++++- lib/snmp/src/compile/depend.mk | 3 + lib/snmp/src/compile/modules.mk | 3 + lib/snmp/src/compile/snmpc.src | 129 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 lib/snmp/src/compile/snmpc.src (limited to 'lib/snmp/src/compile') diff --git a/lib/snmp/src/compile/Makefile b/lib/snmp/src/compile/Makefile index 4be60e1835..1f1086eae1 100644 --- a/lib/snmp/src/compile/Makefile +++ b/lib/snmp/src/compile/Makefile @@ -20,6 +20,7 @@ include $(ERL_TOP)/make/target.mk EBIN = ../../ebin +BIN = ../../bin include $(ERL_TOP)/make/$(TARGET)/otp.mk @@ -44,9 +45,11 @@ RELSYSDIR = $(RELEASE_PATH)/lib/snmp-$(VSN) include modules.mk +ESCRIPT_BIN = $(ESCRIPT_SRC:%.src=$(BIN)/%) + ERL_FILES = $(MODULES:%=%.erl) -TARGET_FILES = $(MODULES:%=$(EBIN)/%.$(EMULATOR)) +TARGET_FILES = $(MODULES:%=$(EBIN)/%.$(EMULATOR)) $(ESCRIPT_BIN) GENERATED_PARSER = $(PARSER_MODULE:%=%.erl) @@ -97,8 +100,12 @@ info: @echo "" @echo "EBIN: $(EBIN)" @echo "" + @echo "ESCRIPT_SRC: $(ESCRIPT_SRC)" + @echo "ESCRIPT_BIN: $(ESCRIPT_BIN)" + @echo "" @echo "" + # ---------------------------------------------------- # Special Build Targets # ---------------------------------------------------- @@ -107,6 +114,7 @@ parser: $(PARSER_TARGET) $(GENERATED_PARSER): $(PARSER_SRC) + # ---------------------------------------------------- # Release Target # ---------------------------------------------------- @@ -115,9 +123,11 @@ include $(ERL_TOP)/make/otp_release_targets.mk release_spec: opt $(INSTALL_DIR) $(RELSYSDIR)/src $(INSTALL_DIR) $(RELSYSDIR)/src/compiler - $(INSTALL_DATA) $(PARSER_SRC) $(ERL_FILES) $(INTERNAL_HRL_FILES) $(RELSYSDIR)/src/compiler + $(INSTALL_DATA) $(ESCRIPT_SRC) $(PARSER_SRC) $(ERL_FILES) $(INTERNAL_HRL_FILES) $(RELSYSDIR)/src/compiler $(INSTALL_DIR) $(RELSYSDIR)/ebin $(INSTALL_DATA) $(TARGET_FILES) $(RELSYSDIR)/ebin + $(INSTALL_DIR) $(RELSYSDIR)/bin + $(INSTALL_SCRIPT) $(ESCRIPT_BIN) $(RELSYSDIR)/bin release_docs_spec: diff --git a/lib/snmp/src/compile/depend.mk b/lib/snmp/src/compile/depend.mk index 75af1bf293..74eb6e0864 100644 --- a/lib/snmp/src/compile/depend.mk +++ b/lib/snmp/src/compile/depend.mk @@ -44,3 +44,6 @@ $(EBIN)/snmpc_mib_gram.$(EMULATOR): \ ../../include/snmp_types.hrl \ snmpc_mib_gram.erl +$(BIN)/snmpc: snmpc.src + $(PERL) -p -e 's?%VSN%?$(VSN)? ' < $< > $@ + chmod 755 $@ diff --git a/lib/snmp/src/compile/modules.mk b/lib/snmp/src/compile/modules.mk index 6365b0e694..ca78e2e6a9 100644 --- a/lib/snmp/src/compile/modules.mk +++ b/lib/snmp/src/compile/modules.mk @@ -21,6 +21,9 @@ PARSER_SRC = snmpc_mib_gram.yrl PARSER_MODULE = $(PARSER_SRC:%.yrl=%) +ESCRIPT_SRC = \ + snmpc.src + MODULES = \ $(PARSER_MODULE) \ snmpc \ diff --git a/lib/snmp/src/compile/snmpc.src b/lib/snmp/src/compile/snmpc.src new file mode 100644 index 0000000000..bafcc79150 --- /dev/null +++ b/lib/snmp/src/compile/snmpc.src @@ -0,0 +1,129 @@ +#!/usr/bin/env escript +%% -*- erlang -*- +%% + +-record(state, + { + version = %VSN%, + mib_file, + outdir = "./", + db = volatile, + include_dirs = ["./"], + include_lib_dirs = [], + deprecated = true, + group_check = true, + description = false, + reference = false, + imports = false, + module_identity = false, + module, + no_defs = false, + relaxed_row_name_assigne_check, + verbosity + }). + +%% --o Dir [defaults to "./"] +%% --i Dir [defaults to "./"] +%% --il Dir +%% --gc +%% --db DB [defaults to volatile] +%% --dep +%% --desc +%% --ref +%% --imp +%% --mi +%% --mod Mod +%% --nd +%% --rrnac +%% --version +%% --verbosity V +main(Args) when is_list(Args) -> + case (catch process_args(Args)) of + ok -> + usage(); + {ok, State} when is_record(State, state) -> + io:format("snmpc: ~p~n", [State]); + {ok, Str} when is_list(Str) -> + io:format("~s~n~n", [Str]), + halt(1); + {error, ReasonStr} -> + usage(ReasonStr) + end; +main(_) -> + usage(). + +process_args([]) -> + {error, lists:flatten(io_lib:format("No MIB-file", []))}; +process_args(Args) -> + %% CWD = "./", + process_args(Args, #state{}). + +process_args([], State) -> + {ok, State}; +process_args(["--help"|_Args], _State) -> + ok; +process_args(["--version"|_Args], State) -> + {ok, lists:flatten(io_lib:format("snmpc ~s", [State#state.version]))}; +process_args(["--verbosity", Verbosity0|Args], #state{verbosity = V} = State) + when (V =:= undefined) -> + Verbosity = list_to_atom(Verbosity0), + case lists:member(Verbosity, [trace,debug,log,info,silence]) of + true -> + process_args(Args, State#state{verbosity = Verbosity}); + false -> + e(lists:flatten(io_lib:format("Unknown verbosity: ~s", [Verbosity0]))) + end; +process_args(["--verbosity"|_Args], #state{verbosity = V}) + when (V =/= undefined) -> + e(lists:flatten(io_lib:format("Verbosity already set to ~w", [V]))); +process_args([Arg|Args], State) -> + io:format("Arg: ~p~n", [Arg]), + process_args(Args, State). + +usage(ReasonStr) -> + io:format("ERROR: ~s~n", [ReasonStr]), + usage(). + +usage() -> + io:format("Usage: snmpc [options] MIB.mib" + "~nOptions:" + "~n --help - Prints this info." + "~n --version - Prints compiler version." + "~n --verbosity - Print debug info." + "~n verbosity = trace | debug | log | info | silence" + "~n Defaults to silence." + "~n --warnings - Print warning messages." + "~n --i - Add this dir to the list of dirs that will be" + "~n searched for imported (compiled) MIB files." + "~n The current workin dir will always be included. " + "~n --il - Add this dir to the list of dirs that will be" + "~n searched for imported (compiled) MIB files." + "~n It assumes that the first element in the dir name" + "~n correspond to an OTP application. For example snmp/mibs/" + "~n The current workin dir and the /priv/mibs " + "~n are always listed last the includ path. " + "~n --db - Database ro used for the defaul instrumentation." + "~n Defaults to volatile." + "~n --deprecated - Keep deprecated definition(s)." + "~n If not specified the compiler will ignore" + "~n deprecated definitions." + "~n --description - The DESCRIPTION field will be included." + "~n --reference - The REFERENCE field will be included." + "~n --imports - The IMPORTS field will be included." + "~n --module_id - The MODULE-IDENTITY field will be included." + "~n --module + throw({error, Reason}). -- cgit v1.2.3 From 031613986bf597c879cefbd64581b5fbd7cfb2ea Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Sat, 19 Feb 2011 20:59:41 +0100 Subject: Fixed spelling and such things... Please enter the commit message for your changes. Lines starting --- lib/snmp/src/compile/snmpc.src | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/snmp/src/compile') diff --git a/lib/snmp/src/compile/snmpc.src b/lib/snmp/src/compile/snmpc.src index bafcc79150..5ad88eacc8 100644 --- a/lib/snmp/src/compile/snmpc.src +++ b/lib/snmp/src/compile/snmpc.src @@ -102,7 +102,7 @@ usage() -> "~n correspond to an OTP application. For example snmp/mibs/" "~n The current workin dir and the /priv/mibs " "~n are always listed last the includ path. " - "~n --db - Database ro used for the defaul instrumentation." + "~n --db - Database to used for the default instrumentation." "~n Defaults to volatile." "~n --deprecated - Keep deprecated definition(s)." "~n If not specified the compiler will ignore" @@ -111,7 +111,7 @@ usage() -> "~n --reference - The REFERENCE field will be included." "~n --imports - The IMPORTS field will be included." "~n --module_id - The MODULE-IDENTITY field will be included." - "~n --module - The module which implements all the instrumentation" "~n functions. " "~n The name of the of all instrumentation functions" "~n must be the same as the corresponding managed object" -- cgit v1.2.3 From 9b824d59d33094b013058090729c1c4b98200716 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Tue, 22 Feb 2011 19:11:26 +0100 Subject: Backup commit (time to go home)... --- lib/snmp/src/compile/snmpc.src | 230 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 203 insertions(+), 27 deletions(-) (limited to 'lib/snmp/src/compile') diff --git a/lib/snmp/src/compile/snmpc.src b/lib/snmp/src/compile/snmpc.src index 5ad88eacc8..61d7a0b04f 100644 --- a/lib/snmp/src/compile/snmpc.src +++ b/lib/snmp/src/compile/snmpc.src @@ -2,47 +2,57 @@ %% -*- erlang -*- %% +-include_lib("kernel/include/file.hrl"). + -record(state, { - version = %VSN%, + version = "%VSN%", mib_file, outdir = "./", db = volatile, include_dirs = ["./"], include_lib_dirs = [], - deprecated = true, - group_check = true, - description = false, - reference = false, - imports = false, - module_identity = false, + deprecated = false, + group_check = true, + description = false, + reference = false, + imports = false, + module_identity = false, + module_compliance = false, + agent_capabilities = false, module, - no_defs = false, - relaxed_row_name_assigne_check, - verbosity + no_defaults = false, + relaxed_row_name_assigne_check = false, + %% The default verbosity (silence) will be filled in + %% during argument processing. + verbosity, + warnings = false }). %% --o Dir [defaults to "./"] %% --i Dir [defaults to "./"] %% --il Dir -%% --gc +%% --sgc %% --db DB [defaults to volatile] %% --dep %% --desc %% --ref %% --imp %% --mi +%% --mc +%% --ac %% --mod Mod %% --nd %% --rrnac %% --version %% --verbosity V +%% --warnings main(Args) when is_list(Args) -> case (catch process_args(Args)) of ok -> usage(); {ok, State} when is_record(State, state) -> - io:format("snmpc: ~p~n", [State]); + compile(State); {ok, Str} when is_list(Str) -> io:format("~s~n~n", [Str]), halt(1); @@ -52,14 +62,101 @@ main(Args) when is_list(Args) -> main(_) -> usage(). +compile(State) -> + io:format("snmpc: ~p~n", [State]), + File = mk_file(State), + Options = mk_options(State), + case snmpc:compile(File, Options) of + {ok, BinFileName} -> + io:format("BinFileName: ~p~n", [BinFileName]), + ok; + {error, Reason} -> + io:format("Reason: ~p~n", [Reason]), + ok + end. + +mk_file(#state{mib_file = MIB}) -> + MIB -- ".mib". + +mk_options(#state{outdir = OutDir, + db = DB, + include_dirs = IDs, + include_lib_dirs = ILDs, + deprecated = Dep, + group_check = GC, + description = Desc, + reference = Ref, + imports = Imp, + module_identity = MI, + module_compliance = MC, + agent_capabilities = AC, + module = Mod, + no_defaults = ND, + relaxed_row_name_assigne_check = RRNAC, + %% The default verbosity (silence) will be filled in + %% during argument processing. + verbosity = V, + warnings = W}) -> + [{outdir, OutDir}, + {db, DB}, + {i, IDs}, + {il, ILDs}, + {group_check, GC}, + {verbosity, V}, + {warnings, W}, + {deprecated, Dep}] ++ + if + (Mod =/= undefined) -> + [{module, Mod}]; + true -> + [] + end ++ + maybe_option(ND, no_defs) ++ + maybe_option(RRNAC, relaxed_row_name_assign_check) ++ + maybe_option(Desc, description) ++ + maybe_option(Ref, reference) ++ + maybe_option(Imp, imports) ++ + maybe_option(MI, module_identity) ++ + maybe_option(MC, module_compliance) ++ + maybe_option(AC, agent_capabilities). + +maybe_option(true, Opt) -> [Opt]; +maybe_option(_, _) -> []. + process_args([]) -> - {error, lists:flatten(io_lib:format("No MIB-file", []))}; + e("No input file"); process_args(Args) -> - %% CWD = "./", process_args(Args, #state{}). -process_args([], State) -> - {ok, State}; +process_args([], #state{verbosity = Verbosity0, mib_file = MIB} = State) -> + if + (MIB =:= undefined) -> + e("No input file"); + true -> + Verbosity = + case Verbosity0 of + undefined -> + silence; + _ -> + Verbosity0 + end, + IPath = lists:reverse(State#state.include_dirs), + IlPath = lists:reverse(State#state.include_lib_dirs), + {ok, State#state{verbosity = Verbosity, + include_dirs = IPath, + include_lib_dirs = IlPath}} + end; +process_args([MIB], State) -> + case (catch file:read_file_info(MIB)) of + {ok, #file_info{type = regular}} -> + {ok, State#state{mib_file = MIB}}; + {ok, #file_info{type = BadType}} -> + e(lists:flatten(io_lib:format("~s not a file: ~w", [MIB, BadType]))); + {error, enoent} -> + e(lists:flatten(io_lib:format("No such file: ~s", [MIB]))); + _ -> + e(lists:flatten(io_lib:format("Bad file: ~s", [MIB]))) + end; process_args(["--help"|_Args], _State) -> ok; process_args(["--version"|_Args], State) -> @@ -76,9 +173,70 @@ process_args(["--verbosity", Verbosity0|Args], #state{verbosity = V} = State) process_args(["--verbosity"|_Args], #state{verbosity = V}) when (V =/= undefined) -> e(lists:flatten(io_lib:format("Verbosity already set to ~w", [V]))); -process_args([Arg|Args], State) -> - io:format("Arg: ~p~n", [Arg]), - process_args(Args, State). +process_args(["--warnings"|Args], State) -> + process_args(Args, State#state{warnings = true}); +process_args(["--o", Dir|Args], State) -> + case (catch file:read_file_info(Dir)) of + {ok, #file_info{type = directory}} -> + process_args(Args, State#state{outdir = Dir}); + {ok, #file_info{type = BadType}} -> + e(lists:flatten(io_lib:format("Not a directory: ~p (~w)", [Dir, BadType]))); + _ -> + e(lists:flatten(io_lib:format("Bad directory: ~p", [Dir]))) + end; +process_args(["--i", Dir|Args], State) -> + case (catch file:read_file_info(Dir)) of + {ok, #file_info{type = directory}} -> + IPath = [Dir | State#state.include_dirs], + process_args(Args, State#state{include_dirs = IPath}); + {ok, #file_info{type = BadType}} -> + e(lists:flatten(io_lib:format("Not a directory: ~p (~w)", [Dir, BadType]))); + _ -> + e(lists:flatten(io_lib:format("Bad directory: ~p", [Dir]))) + end; +process_args(["--il", Dir|Args], State) -> + case (catch file:read_file_info(Dir)) of + {ok, #file_info{type = directory}} -> + IlPath = [Dir | State#state.include_lib_dirs], + process_args(Args, State#state{include_lib_dirs = IlPath}); + {ok, #file_info{type = BadType}} -> + e(lists:flatten(io_lib:format("Not a directory: ~p (~w)", [Dir, BadType]))); + _ -> + e(lists:flatten(io_lib:format("Bad directory: ~p", [Dir]))) + end; +process_args(["--db", DB0|Args], State) -> + DB = list_to_atom(DB0), + case lists:member(DB, [volatile,persistent,mnesia]) of + true -> + process_args(Args, State#state{db = DB}); + false -> + e(lists:flatten(io_lib:format("Invalid db: ~s", [DB0]))) + end; +process_args(["--dep"|Args], State) -> + process_args(Args, State#state{deprecated = true}); +process_args(["--sgc"|Args], State) -> + process_args(Args, State#state{group_check = false}); +process_args(["--desc"|Args], State) -> + process_args(Args, State#state{description = true}); +process_args(["--ref"|Args], State) -> + process_args(Args, State#state{reference = true}); +process_args(["--imp"|Args], State) -> + process_args(Args, State#state{imports = true}); +process_args(["--mi"|Args], State) -> + process_args(Args, State#state{module_identity = true}); +process_args(["--mod", Module0|Args], #state{module = M} = State) + when (M =:= undefined) -> + Module = list_to_atom(Module0), + process_args(Args, State#state{module = Module}); +process_args(["--mod"|_Args], #state{module = M}) + when (M =/= undefined) -> + e(lists:flatten(io_lib:format("Module already set to ~w", [M]))); +process_args(["--nd"|Args], State) -> + process_args(Args, State#state{no_defaults = true}); +process_args(["--rrnac"|Args], State) -> + process_args(Args, State#state{relaxed_row_name_assigne_check = true}); +process_args([Arg|Args], _State) when Args =/= [] -> + e(lists:flatten(io_lib:format("Unknown option: ~s", [Arg]))). usage(ReasonStr) -> io:format("ERROR: ~s~n", [ReasonStr]), @@ -93,10 +251,12 @@ usage() -> "~n verbosity = trace | debug | log | info | silence" "~n Defaults to silence." "~n --warnings - Print warning messages." + "~n --o - The output dir." + "~n Defaults to current working dir." "~n --i - Add this dir to the list of dirs that will be" "~n searched for imported (compiled) MIB files." "~n The current workin dir will always be included. " - "~n --il - Add this dir to the list of dirs that will be" + "~n --il - Add this dir to the list of dirs that will be" "~n searched for imported (compiled) MIB files." "~n It assumes that the first element in the dir name" "~n correspond to an OTP application. For example snmp/mibs/" @@ -104,22 +264,37 @@ usage() -> "~n are always listed last the includ path. " "~n --db - Database to used for the default instrumentation." "~n Defaults to volatile." - "~n --deprecated - Keep deprecated definition(s)." + "~n --sgc - This option (skip group check), if present, disables " + "~n the \"group check\" of the mib compiler. " + "~n That is, should the OBJECT-GROUP and the NOTIFICATION-GROUP " + "~n macro(s) be checked for correctness or not. " + "~n By default the check is done. " + "~n --dep - Keep deprecated definition(s)." "~n If not specified the compiler will ignore" "~n deprecated definitions." - "~n --description - The DESCRIPTION field will be included." - "~n --reference - The REFERENCE field will be included." - "~n --imports - The IMPORTS field will be included." - "~n --module_id - The MODULE-IDENTITY field will be included." - "~n --module - The module which implements all the instrumentation" + "~n --desc - The DESCRIPTION field will be included." + "~n --ref - The REFERENCE field will be included." + "~n --imp - The IMPORTS field will be included." + "~n --mi - The MODULE-IDENTITY field will be included." + "~n --mc - The MODULE-COMPLIANCE field will be included." + "~n --ac - The AGENT-CAPABILITIES field will be included." + "~n --mod - The module which implements all the instrumentation" "~n functions. " "~n The name of the of all instrumentation functions" "~n must be the same as the corresponding managed object" "~n it implements." - "~n --no_defs - The default instrumentation functions will *not* be used" + "~n --nd - The default instrumentation functions will *not* be used" "~n if a managed object have no instrumentation function. " "~n Instead this will be reported as an error, and the " "~n compilation aborts. " + "~n --rrnac - This option, if present, specifies that the row name " + "~n assign check shall not be done strictly according to" + "~n the SMI (which allows only the value 1). " + "~n With this option, all values gŕeater than zero is allowed" + "~n (>= 1). This means that the error will be converted to " + "~n a warning. " + "~n By default it is not included, but if this option is " + "~n is present it will be. " "~n " "~n", []), halt(1). @@ -127,3 +302,4 @@ usage() -> e(Reason) -> throw({error, Reason}). + -- cgit v1.2.3 From 1ffc9f43fe3e80428389636a2e9ba5bf101338f2 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Wed, 23 Feb 2011 09:51:29 +0100 Subject: Fixed MIB file name making... --- lib/snmp/src/compile/snmpc.src | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/snmp/src/compile') diff --git a/lib/snmp/src/compile/snmpc.src b/lib/snmp/src/compile/snmpc.src index 61d7a0b04f..f4336a3264 100644 --- a/lib/snmp/src/compile/snmpc.src +++ b/lib/snmp/src/compile/snmpc.src @@ -67,8 +67,7 @@ compile(State) -> File = mk_file(State), Options = mk_options(State), case snmpc:compile(File, Options) of - {ok, BinFileName} -> - io:format("BinFileName: ~p~n", [BinFileName]), + {ok, _BinFileName} -> ok; {error, Reason} -> io:format("Reason: ~p~n", [Reason]), @@ -76,7 +75,9 @@ compile(State) -> end. mk_file(#state{mib_file = MIB}) -> - MIB -- ".mib". + DirName = filename:dirname(MIB), + BaseName = filename:basename(MIB, ".mib"), + filename:join(DirName, BaseName). mk_options(#state{outdir = OutDir, db = DB, -- cgit v1.2.3 From ec1eba2f8daff87ca84a17e738fc3b9480c42517 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Wed, 23 Feb 2011 10:12:35 +0100 Subject: Fixed verbosity. --- lib/snmp/src/compile/snmpc.src | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'lib/snmp/src/compile') diff --git a/lib/snmp/src/compile/snmpc.src b/lib/snmp/src/compile/snmpc.src index f4336a3264..4581339015 100644 --- a/lib/snmp/src/compile/snmpc.src +++ b/lib/snmp/src/compile/snmpc.src @@ -1,6 +1,27 @@ #!/usr/bin/env escript %% -*- erlang -*- %% +%% %CopyrightBegin% +%% +%% Copyright Ericsson AB 2008-2009. All Rights Reserved. +%% +%% The contents of this file are subject to the Erlang Public License, +%% Version 1.1, (the "License"); you may not use this file except in +%% compliance with the License. You should have received a copy of the +%% Erlang Public License along with this software. If not, it can be +%% retrieved online at http://www.erlang.org/. +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and limitations +%% under the License. +%% +%% %CopyrightEnd% +%% +%% SNMP MIB compiler frontend +%% + +-mode(compile). -include_lib("kernel/include/file.hrl"). @@ -25,10 +46,13 @@ relaxed_row_name_assigne_check = false, %% The default verbosity (silence) will be filled in %% during argument processing. - verbosity, - warnings = false + verbosity, + warnings = false }). + +%% ------------------------------------------------------------------------ +%% Valid arguments: %% --o Dir [defaults to "./"] %% --i Dir [defaults to "./"] %% --il Dir @@ -75,7 +99,7 @@ compile(State) -> end. mk_file(#state{mib_file = MIB}) -> - DirName = filename:dirname(MIB), + DirName = filename:dirname(MIB), BaseName = filename:basename(MIB, ".mib"), filename:join(DirName, BaseName). @@ -150,7 +174,7 @@ process_args([], #state{verbosity = Verbosity0, mib_file = MIB} = State) -> process_args([MIB], State) -> case (catch file:read_file_info(MIB)) of {ok, #file_info{type = regular}} -> - {ok, State#state{mib_file = MIB}}; + process_args([], State#state{mib_file = MIB}); {ok, #file_info{type = BadType}} -> e(lists:flatten(io_lib:format("~s not a file: ~w", [MIB, BadType]))); {error, enoent} -> -- cgit v1.2.3 From 984e0b99fb38d4b4a4bd18e8e1d1abfc7859d80d Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Wed, 23 Feb 2011 11:44:47 +0100 Subject: Now also handling generation of header files. --- lib/snmp/src/compile/snmpc.src | 108 ++++++++++++++++++++---------- lib/snmp/src/compile/snmpc_mib_to_hrl.erl | 3 +- 2 files changed, 76 insertions(+), 35 deletions(-) (limited to 'lib/snmp/src/compile') diff --git a/lib/snmp/src/compile/snmpc.src b/lib/snmp/src/compile/snmpc.src index 4581339015..03044c6454 100644 --- a/lib/snmp/src/compile/snmpc.src +++ b/lib/snmp/src/compile/snmpc.src @@ -28,7 +28,7 @@ -record(state, { version = "%VSN%", - mib_file, + file, % .mib or .bin depending on which are compiled outdir = "./", db = volatile, include_dirs = ["./"], @@ -88,40 +88,72 @@ main(_) -> compile(State) -> io:format("snmpc: ~p~n", [State]), - File = mk_file(State), - Options = mk_options(State), - case snmpc:compile(File, Options) of - {ok, _BinFileName} -> - ok; - {error, Reason} -> - io:format("Reason: ~p~n", [Reason]), - ok + case mk_file(State) of + {mib, File} -> + Options = mk_mib_options(State), + case mib2bin(File, Options) of + {ok, _BinFileName} -> + ok; + {error, Reason} -> + io:format("ERROR: Failed compiling mib: " + "~n ~p~n", [Reason]), + halt(1) + end; + {bin, File} -> + Options = mk_hrl_options(State), + case bin2hrl(File, Options) of + ok -> + ok; + {error, Reason} -> + io:format("ERROR: Failed generating hrl from mib: " + "~n ~p~n", [Reason]), + halt(1) + end end. -mk_file(#state{mib_file = MIB}) -> +mib2bin(MibFileName, Options) -> + snmpc:compile(MibFileName, Options). + +bin2hrl(BinFileName, {OutDir, Verbosity}) -> + MibName = filename:basename(BinFileName), + BinFile = BinFileName ++ ".bin", + HrlFile = filename:join(OutDir, MibName) ++ ".hrl", + put(verbosity, Verbosity), + snmpc_mib_to_hrl:convert(BinFile, HrlFile, MibName). + + +mk_file(#state{file = MIB}) -> DirName = filename:dirname(MIB), - BaseName = filename:basename(MIB, ".mib"), - filename:join(DirName, BaseName). + case filename:extension(MIB) of + ".mib" -> + BaseName = filename:basename(MIB, ".mib"), + {mib, filename:join(DirName, BaseName)}; + ".bin" -> + BaseName = filename:basename(MIB, ".bin"), + {bin, filename:join(DirName, BaseName)}; + BadExt -> + e(lists:flatten(io_lib:format("Unsupported file type: ~s", [BadExt]))) + end. -mk_options(#state{outdir = OutDir, - db = DB, - include_dirs = IDs, - include_lib_dirs = ILDs, - deprecated = Dep, - group_check = GC, - description = Desc, - reference = Ref, - imports = Imp, - module_identity = MI, - module_compliance = MC, - agent_capabilities = AC, - module = Mod, - no_defaults = ND, - relaxed_row_name_assigne_check = RRNAC, - %% The default verbosity (silence) will be filled in - %% during argument processing. - verbosity = V, - warnings = W}) -> +mk_mib_options(#state{outdir = OutDir, + db = DB, + include_dirs = IDs, + include_lib_dirs = ILDs, + deprecated = Dep, + group_check = GC, + description = Desc, + reference = Ref, + imports = Imp, + module_identity = MI, + module_compliance = MC, + agent_capabilities = AC, + module = Mod, + no_defaults = ND, + relaxed_row_name_assigne_check = RRNAC, + %% The default verbosity (silence) will be filled in + %% during argument processing. + verbosity = V, + warnings = W}) -> [{outdir, OutDir}, {db, DB}, {i, IDs}, @@ -148,12 +180,18 @@ mk_options(#state{outdir = OutDir, maybe_option(true, Opt) -> [Opt]; maybe_option(_, _) -> []. + +mk_hrl_options(#state{outdir = OutDir, + verbosity = Verbosity}) -> + {OutDir, Verbosity}. + + process_args([]) -> e("No input file"); process_args(Args) -> process_args(Args, #state{}). -process_args([], #state{verbosity = Verbosity0, mib_file = MIB} = State) -> +process_args([], #state{verbosity = Verbosity0, file = MIB} = State) -> if (MIB =:= undefined) -> e("No input file"); @@ -174,7 +212,7 @@ process_args([], #state{verbosity = Verbosity0, mib_file = MIB} = State) -> process_args([MIB], State) -> case (catch file:read_file_info(MIB)) of {ok, #file_info{type = regular}} -> - process_args([], State#state{mib_file = MIB}); + process_args([], State#state{file = MIB}); {ok, #file_info{type = BadType}} -> e(lists:flatten(io_lib:format("~s not a file: ~w", [MIB, BadType]))); {error, enoent} -> @@ -268,7 +306,9 @@ usage(ReasonStr) -> usage(). usage() -> - io:format("Usage: snmpc [options] MIB.mib" + io:format("Usage: snmpc [options] MIB.mib|MIB.bin" + "~nCompile a MIB (.mib -> .bin) or generate an erlang header " + "~nfile from a compiled MIB file (.bin -> .hrl)" "~nOptions:" "~n --help - Prints this info." "~n --version - Prints compiler version." diff --git a/lib/snmp/src/compile/snmpc_mib_to_hrl.erl b/lib/snmp/src/compile/snmpc_mib_to_hrl.erl index 07bd29231b..decc1ce557 100644 --- a/lib/snmp/src/compile/snmpc_mib_to_hrl.erl +++ b/lib/snmp/src/compile/snmpc_mib_to_hrl.erl @@ -24,7 +24,8 @@ -include("snmpc_lib.hrl"). %% External exports --export([convert/1, compile/3]). +-export([convert/1, convert/3, compile/3]). + %%----------------------------------------------------------------- %% Func: convert/1 -- cgit v1.2.3