1%%-------------------------------------------------------------------- 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2010-2018. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%%---------------------------------------------------------------------- 20%% File : ct_config_plain.erl 21%% Description : CT callback module for reading configs from text files 22%% 23%% Created : 15 February 2010 24%%---------------------------------------------------------------------- 25-module(ct_config_plain). 26-export([read_config/1, check_parameter/1]). 27 28read_config(ConfigFile) -> 29 case file:consult(ConfigFile) of 30 {ok,Config} -> 31 {ok, Config}; 32 {error,enoent} -> 33 {error,{config_file_error,file:format_error(enoent)}}; 34 {error,Reason} -> 35 Key = 36 case application:get_env(common_test, decrypt) of 37 {ok,KeyOrFile} -> 38 case KeyOrFile of 39 {key,K} -> 40 K; 41 {file,F} -> 42 ct_config:get_crypt_key_from_file(F) 43 end; 44 _ -> 45 ct_config:get_crypt_key_from_file() 46 end, 47 case Key of 48 {error,no_crypt_file} -> 49 {error,{config_file_error, 50 lists:flatten( 51 io_lib:format("~ts",[file:format_error(Reason)]))}}; 52 {error,CryptError} -> 53 {error,{decrypt_file_error,CryptError}}; 54 _ when is_list(Key) -> 55 case ct_config:decrypt_config_file(ConfigFile, 56 undefined, 57 {key,Key}) of 58 {ok,CfgBin} -> 59 case read_config_terms(CfgBin) of 60 {error,ReadFail} -> 61 {error,{config_file_error,ReadFail}}; 62 Config -> 63 {ok,Config} 64 end; 65 {error,DecryptFail} -> 66 {error,{decrypt_config_error,DecryptFail}} 67 end; 68 _ -> 69 {error,{bad_decrypt_key,Key}} 70 end 71 end. 72 73% check if config file exists 74check_parameter(File)-> 75 case filelib:is_file(File) of 76 true-> 77 {ok,{file,File}}; 78 false-> 79 {error,{nofile,File}} 80 end. 81 82read_config_terms(Bin) when is_binary(Bin) -> 83 case catch binary_to_list(Bin) of 84 {'EXIT',_} -> 85 {error,invalid_textfile}; 86 Lines -> 87 read_config_terms(Lines) 88 end; 89read_config_terms(Lines) when is_list(Lines) -> 90 read_config_terms1(erl_scan:tokens([], Lines, 0), 1, [], []). 91 92read_config_terms1({done,{ok,Ts,EL},Rest}, L, Terms, _) -> 93 case erl_parse:parse_term(Ts) of 94 {ok,Term} when Rest == [] -> 95 lists:reverse([Term|Terms]); 96 {ok,Term} -> 97 read_config_terms1(erl_scan:tokens([], Rest, 0), 98 EL+1, [Term|Terms], Rest); 99 _ -> 100 {error,{bad_term,{L,EL}}} 101 end; 102read_config_terms1({done,{eof,_},_}, _, Terms, Rest) when Rest == [] -> 103 lists:reverse(Terms); 104read_config_terms1({done,{eof,EL},_}, L, _, _) -> 105 {error,{bad_term,{L,EL}}}; 106read_config_terms1({done,{error,Info,EL},_}, L, _, _) -> 107 {error,{Info,{L,EL}}}; 108read_config_terms1({more,_}, L, Terms, Rest) -> 109 case string:lexemes(Rest, [$\n,[$\r,$\n],$\t]) of 110 [] -> 111 lists:reverse(Terms); 112 _ -> 113 {error,{bad_term,L}} 114 end. 115