1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 1997-2016. 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%% 21-module(icpreproc). 22 23 24 25-export([preproc/2]). 26 27 28-import(lists, [filter/2]). 29 30 31%%---------------------------------------------------------------------- 32%%---------------------------------------------------------------------- 33 34 35preproc(G, File) -> 36 Cmd = ic_options:get_opt(G, preproc_cmd), 37 Flags = ic_options:get_opt(G, preproc_flags), 38 39 40 case Cmd of 41 "erl" -> 42 case ic_pp:run(File,Flags) of 43 {ok, [$#, $ , $1 | Rest], []} -> 44 [$#, $ , $1 | Rest]; 45 {ok, [$#, $ , $1 | Rest], Warning} -> 46 print_warning(G,Warning), 47 [$#, $ , $1 | Rest]; 48 {error,Error} -> 49 print_error(G,Error) 50 end; 51 52 _ -> 53 Line = Cmd++" "++Flags++" "++File, 54 % FIXME: Check status code of command instead of this test 55 case os:cmd(Line) of 56 [$#, $ , C | Rest] when is_integer(C), C > $0, C =< $9 -> 57 [$#, $ , C | Rest]; 58 X -> 59 ic_error:fatal_error(G, {preproc, filter(X)}) 60 end 61 end. 62 63 64filter(X) -> 65 X2 = divide_nl(X, []), 66 filter_x_switch(X2). 67 68 69divide_nl([10 | Xs], Out) -> 70 [lists:reverse(Out) | divide_nl(Xs, [])]; 71divide_nl([X | Xs], Out) -> divide_nl(Xs, [X|Out]); 72divide_nl([], Out) -> lists:reverse(Out). 73 74 75filter_x_switch(L) -> 76 filter(fun([$g,$c,$c,$:,$ ,$W,$a,$r,$n,$i,$n,$g,$:,$ ,$`,$-,$x,$ | _]) -> 77 false; 78 (_) -> true end, L). 79 80 81print_error(_G,[]) -> 82 ok; 83print_error(G,[{File,Line,Text}]) -> 84 ErrorText = File++":"++integer_to_list(Line)++": "++Text, 85 ic_error:fatal_error(G, {ic_pp_error, ErrorText}), 86 ok; 87print_error(G,[{File,Line,Text}|T]) -> 88 ErrorText = File++":"++integer_to_list(Line)++": "++Text, 89 ic_error:error(G, {ic_pp_error, ErrorText}), 90 print_error(G,T); 91print_error(G,[H]) -> 92 ErrorText = H++"\n", 93 ic_error:fatal_error(G, {ic_pp_error, ErrorText}), 94 ok; 95print_error(G,[H|T]) -> 96 ErrorText = H++"\n", 97 ic_error:error(G, {ic_pp_error, ErrorText}), 98 print_error(G,T). 99 100 101print_warning(_G,[]) -> 102 ok; 103print_warning(G,[{File,Line,Text}|T]) -> 104 WarText = File++":"++integer_to_list(Line)++": "++Text, 105 ic_error:warn(G, {ic_pp_warning, WarText}), 106 print_warning(G,T); 107print_warning(G,[H|T]) -> 108 WarText = H++"\n", 109 ic_error:warn(G, {ic_pp_warning, WarText}), 110 print_warning(G,T). 111 112 113