1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | |
---|
4 | int drop_comment( char *); |
---|
5 | int change_to_lower( char *, int ); |
---|
6 | |
---|
7 | int |
---|
8 | main( int argc , char *argv[] ) |
---|
9 | { |
---|
10 | FILE *fp ; |
---|
11 | char linei[2048] ; |
---|
12 | char lineo[8*2048] ; |
---|
13 | char wrf_error_fatal_str[256] ; |
---|
14 | char surface_driver_str[256] ; |
---|
15 | char radiation_driver_str[256] ; |
---|
16 | char cumulus_driver_str[256] ; |
---|
17 | char pbl_driver_str[256] ; |
---|
18 | char *p, *q, *r ; |
---|
19 | char firstp ; |
---|
20 | int state, ns, ns2 ; |
---|
21 | int inparen ; |
---|
22 | int inacall ; |
---|
23 | int col ; |
---|
24 | |
---|
25 | if ( argc != 2 ) { |
---|
26 | fprintf(stderr,"usage: %s file\n",argv[0]) ; |
---|
27 | } |
---|
28 | if ((fp = fopen(argv[1],"r"))==NULL) { |
---|
29 | fprintf(stderr,"cannot open %s for reading\n",argv[1]) ; |
---|
30 | exit(2) ; |
---|
31 | } |
---|
32 | while ( fgets( linei, 2048, fp ) != NULL ) { |
---|
33 | lineo[0] = '\0' ; |
---|
34 | if ( linei[0] != '#' ) drop_comment( linei ) ; |
---|
35 | inacall = 0 ; |
---|
36 | for ( p = linei, q = lineo , firstp = *p ; *p ; p++ ) { |
---|
37 | if ( !inacall && (*(p+0) == 'c' || *(p+0) == 'C' ) && |
---|
38 | (*(p+1) == 'a' || *(p+1) == 'A' ) && |
---|
39 | (*(p+2) == 'l' || *(p+2) == 'L' ) && |
---|
40 | (*(p+3) == 'l' || *(p+3) == 'L' ) && firstp != '#' ) |
---|
41 | { |
---|
42 | inacall = 1 ; |
---|
43 | strncpy(q,p,4) ; q+=4 ; |
---|
44 | ns = 1 ; while ( *(p+3+ns) && *(p+3+ns) != '\n' && |
---|
45 | (*(p+3+ns) == ' ' || |
---|
46 | *(p+3+ns) == '\t' )) { *q++ = *(p+3+ns) ; ns++ ; } |
---|
47 | |
---|
48 | strncpy(wrf_error_fatal_str, p+3+ns,15+1) ; change_to_lower(wrf_error_fatal_str,15+1 ) ; /* 15, but add one to check for '3' */ |
---|
49 | strncpy(surface_driver_str, p+3+ns,14) ; change_to_lower(surface_driver_str,14) ; |
---|
50 | strncpy(radiation_driver_str, p+3+ns,16) ; change_to_lower(radiation_driver_str,16) ; |
---|
51 | strncpy(cumulus_driver_str, p+3+ns,14) ; change_to_lower(cumulus_driver_str,14) ; |
---|
52 | strncpy(pbl_driver_str, p+3+ns,10) ; change_to_lower(pbl_driver_str,10) ; |
---|
53 | |
---|
54 | if ( !strncmp( wrf_error_fatal_str, "wrf_error_fatal", 15 ) && wrf_error_fatal_str[15] != '3' ) |
---|
55 | { |
---|
56 | ns2 = 1 ; while ( *(p+3+ns+14+ns2) && *(p+3+ns+14+ns2) != '\n' && |
---|
57 | (*(p+3+ns+14+ns2) == ' ' ) ) ns2++ ; |
---|
58 | if ( *(p+3+ns+14+ns2) == '(' ) { |
---|
59 | *q='\0'; |
---|
60 | printf("%s",lineo) ; |
---|
61 | printf("wrf_error_fatal3(__FILE__,__LINE__,&\n") ; |
---|
62 | ns2 = 1 ; while ( *(p+3+ns+14+ns2) && *(p+3+ns+14+ns2) != '\n' && |
---|
63 | (*(p+3+ns+14+ns2) == ' ' || |
---|
64 | *(p+3+ns+14+ns2) == '(' || |
---|
65 | *(p+3+ns+14+ns2) == '\t' || |
---|
66 | *(p+3+ns+14+ns2) == '&' )) ns2++ ; |
---|
67 | if( *(p+3+ns+14+ns2) != '\n') printf("%s",(p+3+ns+14+ns2)) ; |
---|
68 | goto next_line ; |
---|
69 | } else { |
---|
70 | printf("%s",linei) ; |
---|
71 | goto next_line ; |
---|
72 | } |
---|
73 | } else if ( !strncmp ( surface_driver_str, "surface_driver", 14 ) || |
---|
74 | !strncmp ( radiation_driver_str, "radiation_driver", 16) || |
---|
75 | !strncmp ( cumulus_driver_str, "cumulus_driver", 14) || |
---|
76 | !strncmp ( pbl_driver_str, "pbl_driver", 10) |
---|
77 | ) { |
---|
78 | strcpy(lineo,p+3+ns) ; |
---|
79 | inparen = 1 ; |
---|
80 | while ( fgets( linei, 2048, fp ) != NULL ) { |
---|
81 | for ( q = linei ; *q ; q++ ) { |
---|
82 | if (*q=='!') { *q = '\n' ; *(q+1) = '\0' ; break ; } |
---|
83 | } |
---|
84 | for ( q = linei ; *q ; q++ ) { |
---|
85 | if ( *q == '(' ) inparen++ ; |
---|
86 | else if ( *q == ')' ) inparen-- ; |
---|
87 | } |
---|
88 | strcat(lineo,linei) ; |
---|
89 | if ( inparen == 0 ) { |
---|
90 | break ; |
---|
91 | } |
---|
92 | } |
---|
93 | for(q=lineo,r=lineo;*q;q++) { |
---|
94 | if (*q == '#' && *(q-1) == '\n') { /* CPP def. copy as is*/ |
---|
95 | *r++ = '&' ; |
---|
96 | *r++ = '\n' ; |
---|
97 | for (; *q; q++) { |
---|
98 | *r++ = *q; |
---|
99 | if ( *q == '\n' ) break ; |
---|
100 | } |
---|
101 | } |
---|
102 | if ( *q == ' ' || *q == '\n' || *q == '&' ) continue ; |
---|
103 | *r++ = *q ; |
---|
104 | } |
---|
105 | *r = '\0' ; |
---|
106 | printf("CALL ") ; |
---|
107 | for(q=lineo,col=130-5;*q;q++) { |
---|
108 | putchar(*q) ; |
---|
109 | if ( *q == '\n' ) { if (*(q+1) != '#') { putchar('&') ; } ; col = 131 ; } |
---|
110 | col-- ; |
---|
111 | if ( col <= 0 ) { |
---|
112 | col = 130 ; |
---|
113 | if (*q!=')' || *(q+1) ) { putchar('&') ; putchar('\n') ; putchar('&') ; } |
---|
114 | } |
---|
115 | } |
---|
116 | putchar('\n') ; |
---|
117 | goto next_line ; |
---|
118 | } else { |
---|
119 | p += 3+ns ; |
---|
120 | *q++ = *p ; |
---|
121 | } |
---|
122 | } else { |
---|
123 | *q++ = *p ; |
---|
124 | } |
---|
125 | } |
---|
126 | *q='\0'; |
---|
127 | printf("%s",lineo) ; |
---|
128 | next_line: |
---|
129 | state = 0 ; |
---|
130 | } |
---|
131 | fclose(fp) ; |
---|
132 | exit(0) ; |
---|
133 | } |
---|
134 | |
---|
135 | int |
---|
136 | drop_comment( char * linei ) |
---|
137 | { |
---|
138 | char *p, *q ; |
---|
139 | char inquote = '\0' ; |
---|
140 | |
---|
141 | for ( p = linei ; *p ; p++ ) |
---|
142 | { |
---|
143 | if ( *p == '\'' ) { if ( inquote == *p ) { inquote = '\0' ; } else { inquote = *p ; } } |
---|
144 | if ( *p == '"' ) { if ( inquote == *p ) { inquote = '\0' ; } else { inquote = *p ; } } |
---|
145 | if ( !inquote && *p == '!' ) { |
---|
146 | /* let us make sure this is not an OMP directive shall we? */ |
---|
147 | for ( q = p ; *q ; q++ ) { |
---|
148 | if ((*q == '$') && |
---|
149 | (*(q+1) == 'o' || *(q+1) == 'O') && |
---|
150 | (*(q+2) == 'm' || *(q+2) == 'M') && |
---|
151 | (*(q+3) == 'p' || *(q+3) == 'P') ) return(0) ; |
---|
152 | /* nor an intel compiler directive, what? */ |
---|
153 | if ((*(q+3) == '$') && |
---|
154 | (*(q) == 'd' || *(q) == 'D') && |
---|
155 | (*(q+1) == 'e' || *(q+1) == 'E') && |
---|
156 | (*(q+2) == 'c' || *(q+2) == 'C') ) return(0) ; |
---|
157 | /* nor a pgi accelerator directive */ |
---|
158 | if ((*q == '$') && |
---|
159 | (*(q+1) == 'a' || *(q+1) == 'A') && |
---|
160 | (*(q+2) == 'c' || *(q+2) == 'C') && |
---|
161 | (*(q+3) == 'c' || *(q+3) == 'C') ) return(0) ; |
---|
162 | } |
---|
163 | *p = '\n' ; *(p+1) = '\0' ; return(0) ; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | int |
---|
169 | change_to_lower( char * s , int n ) |
---|
170 | { |
---|
171 | int i ; |
---|
172 | for ( i = 0 ; i < n ; i++ ) |
---|
173 | { |
---|
174 | if ( s[i] >= 'A' && s[i] <= 'Z' ) s[i] = s[i] - 'A' + 'a' ; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|