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