Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / tools / standard.c
blob2b34beb807762b79700f954d174959ca2fecb0e9
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #define LINELEN 8192
6 #define STRINGLEN 1024
7 int drop_comment( char *);
8 int change_to_lower( char *, int );
10 int
11 main( int argc , char *argv[] )
13 FILE *fp ;
14 char linei[LINELEN] ;
15 char lineo[8*LINELEN] ;
16 char wrf_error_fatal_str[STRINGLEN] ;
17 char surface_driver_str[STRINGLEN] ;
18 char radiation_driver_str[STRINGLEN] ;
19 char cumulus_driver_str[STRINGLEN] ;
20 char pbl_driver_str[STRINGLEN] ;
21 char *p, *q, *r ;
22 char firstp ;
23 int state, ns, ns2 ;
24 int inparen ;
25 int inacall ;
26 int col ;
28 if ( argc != 2 ) {
29 fprintf(stderr,"usage: %s file\n",argv[0]) ;
31 if ((fp = fopen(argv[1],"r"))==NULL) {
32 fprintf(stderr,"cannot open %s for reading\n",argv[1]) ;
33 exit(2) ;
35 while ( fgets( linei, LINELEN, fp ) != NULL ) {
36 lineo[0] = '\0' ;
37 if ( linei[0] != '#' ) drop_comment( linei ) ;
38 inacall = 0 ;
39 for ( p = linei, q = lineo , firstp = *p ; *p ; p++ ) {
40 if ( !inacall && (*(p+0) == 'c' || *(p+0) == 'C' ) &&
41 (*(p+1) == 'a' || *(p+1) == 'A' ) &&
42 (*(p+2) == 'l' || *(p+2) == 'L' ) &&
43 (*(p+3) == 'l' || *(p+3) == 'L' ) && firstp != '#' )
45 inacall = 1 ;
46 strncpy(q,p,4) ; q+=4 ;
47 ns = 1 ; while ( *(p+3+ns) && *(p+3+ns) != '\n' &&
48 (*(p+3+ns) == ' ' ||
49 *(p+3+ns) == '\t' )) { *q++ = *(p+3+ns) ; ns++ ; }
51 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' */
52 strncpy(surface_driver_str, p+3+ns,14) ; change_to_lower(surface_driver_str,14) ;
53 strncpy(radiation_driver_str, p+3+ns,16) ; change_to_lower(radiation_driver_str,16) ;
54 strncpy(cumulus_driver_str, p+3+ns,14) ; change_to_lower(cumulus_driver_str,14) ;
55 strncpy(pbl_driver_str, p+3+ns,10) ; change_to_lower(pbl_driver_str,10) ;
57 if ( !strncmp( wrf_error_fatal_str, "wrf_error_fatal", 15 ) && wrf_error_fatal_str[15] != '3' )
59 ns2 = 1 ; while ( *(p+3+ns+14+ns2) && *(p+3+ns+14+ns2) != '\n' &&
60 (*(p+3+ns+14+ns2) == ' ' ) ) ns2++ ;
61 if ( *(p+3+ns+14+ns2) == '(' ) {
62 *q='\0';
63 printf("%s",lineo) ;
64 printf("wrf_error_fatal3(__FILE__,__LINE__,&\n") ;
65 ns2 = 1 ; while ( *(p+3+ns+14+ns2) && *(p+3+ns+14+ns2) != '\n' &&
66 (*(p+3+ns+14+ns2) == ' ' ||
67 *(p+3+ns+14+ns2) == '(' ||
68 *(p+3+ns+14+ns2) == '\t' ||
69 *(p+3+ns+14+ns2) == '&' )) ns2++ ;
70 if( *(p+3+ns+14+ns2) != '\n') printf("%s",(p+3+ns+14+ns2)) ;
71 goto next_line ;
72 } else {
73 printf("%s",linei) ;
74 goto next_line ;
76 } else if ( !strncmp ( surface_driver_str, "surface_driver", 14 ) ||
77 !strncmp ( radiation_driver_str, "radiation_driver", 16) ||
78 !strncmp ( cumulus_driver_str, "cumulus_driver", 14) ||
79 !strncmp ( pbl_driver_str, "pbl_driver", 10)
80 ) {
81 strcpy(lineo,p+3+ns) ;
82 inparen = 1 ;
83 while ( fgets( linei, LINELEN, fp ) != NULL ) {
84 for ( q = linei ; *q ; q++ ) {
85 if (*q=='!') { *q = '\n' ; *(q+1) = '\0' ; break ; }
87 for ( q = linei ; *q ; q++ ) {
88 if ( *q == '(' ) inparen++ ;
89 else if ( *q == ')' ) inparen-- ;
91 strcat(lineo,linei) ;
92 if ( inparen == 0 ) {
93 break ;
96 for(q=lineo,r=lineo;*q;q++) {
97 if (*q == '#' && *(q-1) == '\n') { /* CPP def. copy as is*/
98 *r++ = '&' ;
99 *r++ = '\n' ;
100 for (; *q; q++) {
101 *r++ = *q;
102 if ( *q == '\n' ) break ;
105 if ( *q == ' ' || *q == '\n' || *q == '&' ) continue ;
106 *r++ = *q ;
108 *r = '\0' ;
109 printf("CALL ") ;
110 for(q=lineo,col=130-5;*q;q++) {
111 putchar(*q) ;
112 if ( *q == '\n' ) { if (*(q+1) != '#') { putchar('&') ; } ; col = 131 ; }
113 col-- ;
114 if ( col <= 0 ) {
115 col = 130 ;
116 if (*q!=')' || *(q+1) ) { putchar('&') ; putchar('\n') ; putchar('&') ; }
119 putchar('\n') ;
120 goto next_line ;
121 } else {
122 p += 3+ns ;
123 *q++ = *p ;
125 } else {
126 *q++ = *p ;
129 *q='\0';
130 printf("%s",lineo) ;
131 next_line:
132 state = 0 ;
134 fclose(fp) ;
135 exit(0) ;
139 drop_comment( char * linei )
141 char *p, *q ;
142 char inquote = '\0' ;
144 for ( p = linei ; *p ; p++ )
146 if ( *p == '\'' ) { if ( inquote == *p ) { inquote = '\0' ; } else { inquote = *p ; } }
147 if ( *p == '"' ) { if ( inquote == *p ) { inquote = '\0' ; } else { inquote = *p ; } }
148 if ( !inquote && *p == '!' ) {
149 /* let us make sure this is not an OMP directive shall we? */
150 for ( q = p ; *q ; q++ ) {
151 if ((*q == '$') &&
152 (*(q+1) == 'o' || *(q+1) == 'O') &&
153 (*(q+2) == 'm' || *(q+2) == 'M') &&
154 (*(q+3) == 'p' || *(q+3) == 'P') ) return(0) ;
155 /* nor an intel compiler directive, what? */
156 if ((*(q+3) == '$') &&
157 (*(q) == 'd' || *(q) == 'D') &&
158 (*(q+1) == 'e' || *(q+1) == 'E') &&
159 (*(q+2) == 'c' || *(q+2) == 'C') ) return(0) ;
160 /* nor an intel compiler directive, just so. quite. */
161 if ((*(q+3) == '$') &&
162 (*(q) == 'd' || *(q) == 'D') &&
163 (*(q+1) == 'i' || *(q+1) == 'I') &&
164 (*(q+2) == 'r' || *(q+2) == 'R') ) return(0) ;
165 /* nor a pgi accelerator directive */
166 if ((*q == '$') &&
167 (*(q+1) == 'a' || *(q+1) == 'A') &&
168 (*(q+2) == 'c' || *(q+2) == 'C') &&
169 (*(q+3) == 'c' || *(q+3) == 'C') ) return(0) ;
170 /* nor a fujitsu compiler directive */
171 if ((*(q+1) == 'o' || *(q+1) == 'O') &&
172 (*(q+2) == 'c' || *(q+2) == 'C') &&
173 (*(q+3) == 'l' || *(q+3) == 'L') ) return(0) ;
175 *p = '\n' ; *(p+1) = '\0' ; return(0) ;
178 return 0; /* SamT: bug fix: return a value */
181 int
182 change_to_lower( char * s , int n )
184 int i ;
185 for ( i = 0 ; i < n ; i++ )
187 if ( s[i] >= 'A' && s[i] <= 'Z' ) s[i] = s[i] - 'A' + 'a' ;
189 return 0; /* SamT: bug fix: return a value */