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