aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ic/src/icpreproc.erl
blob: fc936c4bf3b4906f16ac20e47e106faaccd155bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%
%%
-module(icpreproc).



-export([preproc/2]).


-import(lists, [filter/2]).


%%----------------------------------------------------------------------
%%----------------------------------------------------------------------


preproc(G, File) ->
    Cmd		= ic_options:get_opt(G, preproc_cmd),
    Flags	= ic_options:get_opt(G, preproc_flags),


    case Cmd of
	"erl" ->
	    case ic_pp:run(File,Flags) of
		{ok, [$#, $ , $1 | Rest], []} ->
		    [$#, $ , $1 | Rest];
		{ok, [$#, $ , $1 | Rest], Warning} ->
		    print_warning(G,Warning),
		    [$#, $ , $1 | Rest];
		{error,Error} ->
		    print_error(G,Error)
	    end;
	
	_ ->
	    Line	= Cmd++" "++Flags++" "++File,
	    % FIXME: Check status code of command instead of this test
	    case os:cmd(Line) of
		[$#, $ , C | Rest] when is_integer(C), C > $0, C =< $9 ->
		    [$#, $ , C | Rest];
		X ->
		    ic_error:fatal_error(G, {preproc, filter(X)})
	    end
    end.


filter(X) ->  
    X2 = divide_nl(X, []),
    filter_x_switch(X2).


divide_nl([10 | Xs], Out) ->
    [lists:reverse(Out) | divide_nl(Xs, [])];
divide_nl([X | Xs], Out) -> divide_nl(Xs, [X|Out]);
divide_nl([], Out) -> lists:reverse(Out).


filter_x_switch(L) ->
    filter(fun([$g,$c,$c,$:,$ ,$W,$a,$r,$n,$i,$n,$g,$:,$ ,$`,$-,$x,$ | _]) ->
		   false;
	      (_) -> true end, L).


print_error(_G,[]) ->
    ok;
print_error(G,[{File,Line,Text}]) ->
    ErrorText = File++":"++integer_to_list(Line)++": "++Text,
    ic_error:fatal_error(G, {ic_pp_error, ErrorText}),
    ok;
print_error(G,[{File,Line,Text}|T]) ->
    ErrorText = File++":"++integer_to_list(Line)++": "++Text,
    ic_error:error(G, {ic_pp_error, ErrorText}),
    print_error(G,T);
print_error(G,[H]) ->
    ErrorText = H++"\n",
    ic_error:fatal_error(G, {ic_pp_error, ErrorText}),
    ok;
print_error(G,[H|T]) ->
    ErrorText = H++"\n",
    ic_error:error(G, {ic_pp_error, ErrorText}),
    print_error(G,T).


print_warning(_G,[]) ->
    ok;
print_warning(G,[{File,Line,Text}|T]) ->
    WarText = File++":"++integer_to_list(Line)++": "++Text,
    ic_error:warn(G, {ic_pp_warning, WarText}),
    print_warning(G,T);
print_warning(G,[H|T]) ->
    WarText = H++"\n",
    ic_error:warn(G, {ic_pp_warning, WarText}),
    print_warning(G,T).