misc.c

References to this file elsewhere.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 
6 #include "protos.h"
7 #include "registry.h"
8 #include "data.h"
9 
10 char *
11 dimension_with_colons( char * pre , char * tmp , node_t * p , char * post )
12 {
13   int i ;
14   if ( p == NULL ) return("") ;
15   if ( p->ndims <= 0 && ! p->boundary_array ) return("") ;
16   strcpy(tmp,"") ;
17   if ( pre != NULL ) strcat(tmp,pre) ;
18   if ( p->boundary_array )
19   {
20     if ( ! sw_new_bdys ) { strcat( tmp,":,") ; }
21     if ( !strcmp( p->use , "_4d_bdy_array_" ) ) {
22       strcat( tmp, ":,:,:,:" ) ;  /* boundary array for 4d tracer array */
23     } else {
24       strcat( tmp, ":,:,:" ) ;  /* most always have four dimensions */
25     }
26   }
27   else
28   {
29     for ( i = 0 ; i < p->ndims ; i++ ) strcat(tmp,":,") ; 
30     if ( p->node_kind & FOURD ) strcat(tmp,":,") ;       /* add an extra for 4d arrays */
31     tmp[strlen(tmp)-1] = '\0' ;
32   }
33   if ( post != NULL ) strcat(tmp,post)  ;
34   return(tmp) ;
35 }
36 
37 char *
38 dimension_with_ones( char * pre , char * tmp , node_t * p , char * post )
39 {
40   int i ;
41   char r[NAMELEN],s[NAMELEN],four_d[NAMELEN] ;
42   char *pp ;
43   if ( p == NULL ) return("") ;
44   if ( p->ndims <= 0 && ! p->boundary_array ) return("") ;
45   strcpy(tmp,"") ;
46   if ( pre != NULL ) strcat(tmp,pre) ;
47 
48   if ( p->boundary_array )
49   {
50     if ( ! sw_new_bdys ) { strcpy( tmp,"(1,") ; }
51     if ( !strcmp( p->use , "_4d_bdy_array_" ) ) {   /* if a boundary array for a 4d tracer */
52       strcpy(s, p->name ) ;  /* copy the name and then remove everything after last underscore */
53       if ((pp=rindex( s, '_' )) != NULL ) *pp = '\0' ;
54       sprintf( four_d, "num_%s,", s  ) ;
55     } else {
56       strcpy( four_d, "" ) ;
57     }
58 
59     if ( !strcmp( p->use , "_4d_bdy_array_" ) ) {
60       sprintf( r, "1,1,1,%s", four_d ) ;  /* boundary array for 4d tracer array */
61       strcat( tmp, r ) ;
62     } else {
63       strcat( tmp, "1,1,1," ) ;
64     }
65     tmp[strlen(tmp)-1] = '\0' ;
66   }
67   else
68   {
69     for ( i = 0 ; i < p->ndims ; i++ ) strcat(tmp,"1,") ;
70     if ( p->node_kind & FOURD ) strcat(tmp,"1,") ;       /* add an extra for 4d arrays */
71     tmp[strlen(tmp)-1] = '\0' ;
72   }
73   if ( post != NULL ) strcat(tmp,post)  ;
74   return(tmp) ;
75 }
76 
77 char *
78 dimension_with_ranges( char * refarg , char * pre , 
79                        int bdy , /* as defined in data.h */
80                        char * tmp , node_t * p , char * post ,
81                        char * nlstructname  )   /* added 20020130;
82  						   provides name (with %) of structure in
83 						   which a namelist supplied dimension
84                                                    should be dereference from, or ""  */
85 {
86   int i ;
87   char tx[NAMELEN] ;
88   char r[NAMELEN],s[NAMELEN],four_d[NAMELEN] ;
89   int   bdex, xdex, ydex, zdex ;
90   node_t *xdim, *ydim, *zdim ;
91   char *pp ;
92   if ( p == NULL ) return("") ;
93   if ( p->ndims <= 0 && !p->boundary_array ) return("") ;
94   strcpy(tmp,"") ;
95   if ( pre != NULL ) strcat(tmp,pre) ;
96   strcpy(r,"") ;
97   if ( refarg != NULL ) strcat(r,refarg) ;
98 
99   if ( p->boundary_array )
100   {
101     if ( p->ndims > 0 )
102     {
103       xdim = get_dimnode_for_coord( p , COORD_X ) ;
104       ydim = get_dimnode_for_coord( p , COORD_Y ) ;
105       zdim = get_dimnode_for_coord( p , COORD_Z ) ;
106       if ( ydim == NULL )
107        { fprintf(stderr,"dimension_with_ranges: y dimension not specified for %s\n",p->name) ; return("") ; }
108       if ( xdim == NULL )
109        { fprintf(stderr,"dimension_with_ranges: x dimension not specified for %s\n",p->name) ; return("") ; }
110       
111       xdex = xdim->dim_order ;
112       ydex = ydim->dim_order ;
113 
114       if ( !strcmp( p->use , "_4d_bdy_array_" ) ) {   /* if a boundary array for a 4d tracer */
115         strcpy(s, p->name ) ;  /* copy the name and then remove everything after last underscore */
116         if ((pp=rindex( s, '_' )) != NULL ) *pp = '\0' ;
117         sprintf( four_d, "num_%s,", s  ) ;
118       } else {
119         strcpy( four_d, "" ) ;
120       }
121       if ( sw_new_bdys ) {
122         if      ( bdy == P_XSB || bdy == P_XEB ) { bdex = ydex ; } 
123         else if ( bdy == P_YSB || bdy == P_YEB ) { bdex = xdex ; }
124         else { fprintf(stderr,"REGISTRY WARNING: internal error %s %d, bdy=%d,%s,%d \n",__FILE__,__LINE__,bdy,p->name,p->boundary) ; }
125         if ( zdim != NULL ) {
126           zdex = zdim->dim_order ;
127           sprintf(tx,"%ssm3%d:%sem3%d,%ssm3%d:%sem3%d,%sspec_bdy_width,%s", r,bdex,r,bdex,r,zdex,r,zdex,r,four_d ) ;
128         } else {
129           sprintf(tx,"%ssm3%d:%sem3%d,1,%sspec_bdy_width,%s", r,bdex,r,bdex,r,four_d ) ;
130         }
131       } else {
132         if ( zdim != NULL ) {
133           zdex = zdim->dim_order ;
134           sprintf(tx,"max(%sed3%d,%sed3%d),%ssd3%d:%sed3%d,%sspec_bdy_width,4,%s", r,xdex,r,ydex,r,zdex,r,zdex,r,four_d ) ;
135         } else {
136           sprintf(tx,"max(%sed3%d,%sed3%d),1,%sspec_bdy_width,4,%s", r,xdex,r,ydex,r,four_d ) ;
137         }
138       }
139     }
140     else
141     {
142       sprintf(tx,"%sspec_bdy_width,",r ) ;
143     }
144     strcat(tmp,tx) ;
145   }
146   else
147   {
148     for ( i = 0 ; i < p->ndims ; i++ )
149     {
150       range_of_dimension( r, tx , i , p , nlstructname ) ;
151       strcat(tmp,tx) ;
152       strcat(tmp,",") ;
153     }
154   }
155   tmp[strlen(tmp)-1] = '\0' ;
156   if ( post != NULL ) strcat(tmp,post)  ;
157 
158   return(tmp) ;
159 }
160 
161 int
162 range_of_dimension ( char * r , char * tx , int i , node_t * p , char * nlstructname )
163 {
164    char s[NAMELEN], e[NAMELEN] ;
165 
166    get_elem( r , nlstructname , s , i , p , 0 ) ;
167    get_elem( r , nlstructname , e , i , p , 1 ) ;
168    sprintf(tx,"%s:%s", s , e ) ;
169 
170 }
171 
172 char *
173 index_with_firstelem( char * pre , char * dref , int bdy ,  /* as defined in data.h */
174                       char * tmp , node_t * p , char * post )
175 {
176   int i ;
177   char tx[NAMELEN] ;
178   char tmp2[NAMELEN] ;
179   int  bdex, xdex, ydex, zdex ;
180   node_t *xdim, *ydim, *zdim ;
181   char r[NAMELEN] ;
182 
183   if ( p == NULL ) return("") ;
184   if ( p->ndims <= 0 ) return("") ;
185   strcpy(tmp,"") ;
186   if ( pre != NULL ) strcat(tmp,pre) ;
187 
188   strcpy(r,"") ;
189   if ( dref != NULL ) strcat(r,dref) ;
190 
191   if ( p->boundary_array )
192   {
193     if ( sw_new_bdys ) {
194 
195       xdim = get_dimnode_for_coord( p , COORD_X ) ;
196       ydim = get_dimnode_for_coord( p , COORD_Y ) ;
197       zdim = get_dimnode_for_coord( p , COORD_Z ) ;
198       if ( ydim == NULL )
199        { fprintf(stderr,"dimension_with_ranges: y dimension not specified for %s\n",p->name) ; return("") ; }
200       if ( xdim == NULL )
201        { fprintf(stderr,"dimension_with_ranges: x dimension not specified for %s\n",p->name) ; return("") ; }
202 
203       xdex = xdim->dim_order ;
204       ydex = ydim->dim_order ;
205 
206       if      ( bdy == P_XSB || bdy == P_XEB ) { bdex = ydex ; } 
207       else if ( bdy == P_YSB || bdy == P_YEB ) { bdex = xdex ; }
208       else { fprintf(stderr,"REGISTRY WARNING: internal error %s %d \n",__FILE__,__LINE__) ; }
209       if ( p->ndims > 0 )
210       {
211         if ( !strcmp( p->use , "_4d_bdy_array_" ) ) {
212           sprintf(tmp,"%ssm3%d,%ssm3%d,1,1", r,bdex,r,zdex ) ;
213         } else {
214           sprintf(tmp,"%ssm3%d,%ssm3%d,1", r,bdex,r,zdex ) ;
215         }
216       }
217       else
218       {
219         sprintf(tx,"1," ) ;
220         strcat(tmp,tx) ;
221       }
222 
223     } else {
224       if ( p->ndims > 0 )
225       {
226         if ( !strcmp( p->use , "_4d_bdy_array_" ) ) {
227           strcat(tmp,"1,1,1,1,1,") ;
228         } else {
229           strcat(tmp,"1,1,1,1,") ;
230         }
231       }
232       else
233       {
234         sprintf(tx,"1," ) ;
235         strcat(tmp,tx) ;
236       }
237     }
238   }
239   else
240   {
241     for ( i = 0 ; i < p->ndims ; i++ )
242     {
243       get_elem( dref, "", tx, i, p , 0 ) ;
244       strcat( tmp, tx ) ;
245       strcat(tmp,",") ;
246     }
247   }
248   tmp[strlen(tmp)-1] = '\0' ;  /* remove trailing comma */
249   if ( post != NULL ) strcat(tmp,post)  ;
250   return(tmp) ;
251 }
252 
253 get_elem ( char * structname , char * nlstructname , char * tx , int i , node_t * p , int first_last )
254 {
255    char dref[NAMELEN], nlstruct[NAMELEN] ;
256 
257    if ( structname == NULL ) { strcpy( dref, "" ) ;}
258    else                      { strcpy( dref, structname ) ; }
259    if ( nlstructname == NULL ) { strcpy( nlstruct, "" ) ;}
260    else                        { strcpy( nlstruct, nlstructname ) ; }
261    if ( p->dims[i] != NULL )
262    {
263      switch ( p->dims[i]->len_defined_how )
264      {
265        case (DOMAIN_STANDARD) :
266          {
267          char *ornt ;
268          if      ( p->proc_orient == ALL_X_ON_PROC ) ornt = "x" ;
269          else if ( p->proc_orient == ALL_Y_ON_PROC ) ornt = "y" ;
270          else                                        ornt = "" ;
271            sprintf(tx,"%s%cm3%d%s",dref,first_last==0?'s':'e',p->dims[i]->dim_order,ornt) ;
272          }
273          break ;
274        case (NAMELIST) :
275          if ( first_last == 0 ) { if ( !strcmp( p->dims[i]->assoc_nl_var_s , "1" ) ) {
276                                     sprintf(tx,"%s",p->dims[i]->assoc_nl_var_s) ;
277                                   } else {
278                                     sprintf(tx,"%s%s%s",nlstructname,structname,p->dims[i]->assoc_nl_var_s) ; 
279                                   }
280                                 }
281          else                   { sprintf(tx,"%s%s%s",nlstructname,structname,p->dims[i]->assoc_nl_var_e) ; }
282          break ;
283        case (CONSTANT) :
284          if ( first_last == 0 ) { sprintf(tx,"%d",p->dims[i]->coord_start) ; }
285          else                   { sprintf(tx,"%d",p->dims[i]->coord_end) ; }
286          break ;
287        default : break ;
288      }
289    }
290    else
291    {
292      fprintf(stderr,"WARNING: %s %d: something wrong with internal representation for dim %d\n",__FILE__,__LINE__,i) ;
293    }
294 }
295 
296 char *
297 declare_array_as_pointer( char * tmp , node_t * p )
298 {
299   strcpy( tmp , "" ) ;
300   if ( p != NULL )
301     if ( p->ndims > 0 || p->boundary_array ) strcpy ( tmp, ",POINTER" ) ;
302   return(tmp);
303 }
304 
305 char *
306 field_type( char * tmp , node_t * p )
307 {
308   if ( p == NULL ) {
309     strcpy( tmp , "" ) ;
310   } else if ( p->type == NULL ) {
311     strcpy( tmp , "" ) ;
312   } else if ( p->type->type_type == SIMPLE ) {
313     strcpy( tmp , p->type->name ) ;
314   } else {
315     sprintf( tmp , "TYPE(%s)", p->type->name ) ;
316   }
317   return( tmp ) ;
318 }
319 
320 char *
321 field_name( char * tmp , node_t * p , int tag  )
322 {
323   if ( p == NULL ) return("") ;
324   if ( tag < 1 )
325   {
326     strcpy(tmp,p->name) ;
327     if ( p->scalar_array_member ) strcpy(tmp,p->use) ;
328   }
329   else
330   {
331     sprintf(tmp,"%s_%d",p->name,tag) ;
332     if ( p->scalar_array_member ) sprintf(tmp,"%s_%d",p->use,tag) ;
333   }
334   return( tmp ) ;
335 }
336 
337 char *
338 field_name_bdy( char * tmp , node_t * p , int tag, int bdy  )
339 {
340   if ( p == NULL ) return("") ;
341   if ( tag < 1 )
342   {
343     strcpy(tmp,p->name) ;
344     if ( p->scalar_array_member ) strcpy(tmp,p->use) ;
345     if ( p->boundary_array ) strcat(tmp,bdy_indicator(bdy)) ;
346   }
347   else
348   {
349     sprintf(tmp,"%s_%d",p->name,tag) ;
350     if ( p->scalar_array_member ) sprintf(tmp,"%s_%d",p->use,tag) ;
351     if ( p->boundary_array ) strcat(tmp,bdy_indicator(bdy)) ;
352   }
353   return( tmp ) ;
354 }
355 
356 static char *emp_str = "" ;
357 static char *xs_str = "xs" ;
358 static char *xe_str = "xe" ;
359 static char *ys_str = "ys" ;
360 static char *ye_str = "ye" ;
361 
362 char *
363 bdy_indicator( int bdy )
364 {
365   char * res ;
366   res = emp_str ;
367   if      ( bdy == P_XSB ) { res = xs_str ; }
368   else if ( bdy == P_XEB ) { res = xe_str ; }
369   else if ( bdy == P_YSB ) { res = ys_str ; }
370   else if ( bdy == P_YEB ) { res = ye_str ; }
371   return(res) ;
372 }
373 
374 int
375 print_warning( FILE * fp , char * fname )
376 {
377 fprintf(fp,"!STARTOFREGISTRYGENERATEDINCLUDE '%s'\n", fname) ;
378 fprintf(fp,"!\n") ;
379 fprintf(fp,"! WARNING This file is generated automatically by use_registry\n") ;
380 fprintf(fp,"! using the data base in the file named Registry.\n") ;
381 fprintf(fp,"! Do not edit.  Your changes to this file will be lost.\n") ;
382 fprintf(fp,"!\n") ;
383 return(0) ;
384 }
385 
386 close_the_file( FILE * fp )
387 {
388 fprintf(fp,"!ENDOFREGISTRYGENERATEDINCLUDE\n") ;
389 fclose(fp) ;
390 }
391 
392 int
393 make_entries_uniq ( char * fname )
394 {
395   char tempfile[NAMELEN] ;
396   char commline[4096] ;
397   sprintf(tempfile,"regtmp1%d",getpid()) ;
398   sprintf(commline,"%s < %s > %s ; %s %s %s ",
399           UNIQSORT,fname,tempfile,
400           MVCOMM,tempfile,fname ) ;
401   return(system(commline)) ;
402 }
403 
404 int
405 add_warning ( char * fname )
406 {
407   FILE * fp ;
408   char tempfile[NAMELEN] ;
409   char tempfile1[NAMELEN] ;
410   char commline[4096] ;
411   sprintf(tempfile,"regtmp1%d",getpid()) ;
412   sprintf(tempfile1,"regtmp2%d",getpid()) ;
413   if (( fp = fopen( tempfile, "w" )) == NULL ) return(1) ;
414   print_warning(fp,tempfile) ; 
415   close_the_file(fp) ;
416   sprintf(commline,"%s %s %s > %s ; %s %s %s ; %s %s ",
417           CATCOMM,tempfile,fname,tempfile1,
418           MVCOMM,tempfile1,fname,
419           RMCOMM,tempfile) ;
420   return(system(commline)) ;
421 }
422 
423 static int NumCores ;
424 static char dyncores[MAX_DYNCORES][NAMELEN] ;
425 
426 int
427 init_core_table()
428 {
429   NumCores = 0 ;
430   return(0) ;
431 }
432 
433 int
434 get_num_cores()
435 {
436   return( NumCores ) ;
437 }
438 
439 char *
440 get_corename_i(int i)
441 {
442   if ( i >= 0 && i < NumCores ) return( dyncores[i] ) ;
443   return(NULL) ;
444 }
445 
446 int
447 add_core_name ( char * name )
448 {
449   if ( name == NULL ) return(1) ;
450   if (get_core_name ( name ) == NULL )
451   {
452     if ( NumCores >= MAX_DYNCORES ) return(1) ;
453     strcpy( dyncores[NumCores++] , name ) ;
454   }
455   return(0) ;
456 }
457 
458 char *
459 get_core_name ( char * name )
460 {
461   int i ;
462   if ( name == NULL ) return(NULL) ;
463   for ( i = 0 ; i < NumCores ; i++ )
464   {
465     if ( !strcmp(name,dyncores[i]) ) return( dyncores[i] ) ; 
466   }
467   return(NULL) ;
468 }
469 
470 /* DESTRUCTIVE */
471 char *
472 make_upper_case ( char * str )
473 {
474   char * p ;
475   if ( str == NULL ) return (NULL) ;
476   for ( p = str ; *p ; p++ ) *p = toupper(*p) ; 
477   return(str) ;
478 }
479 
480 /* DESTRUCTIVE */
481 char *
482 make_lower_case ( char * str )
483 {
484   char * p ;
485   if ( str == NULL ) return (NULL) ;
486   for ( p = str ; *p ; p++ ) *p = tolower(*p) ; 
487   return(str) ;
488 }
489 
490 /* Routines for keeping typedef history  -ajb */
491 
492 static int NumTypeDefs ;
493 static char typedefs[MAX_TYPEDEFS][NAMELEN] ;
494 
495 int
496 init_typedef_history()
497 {
498   NumTypeDefs = 0 ;
499   return(0) ;
500 }
501 
502 int
503 get_num_typedefs()
504 {
505   return( NumTypeDefs ) ;
506 }
507 
508 char *
509 get_typename_i(int i)
510 {
511   if ( i >= 0 && i < NumTypeDefs ) return( typedefs[i] ) ;
512   return(NULL) ;
513 }
514 
515 int
516 add_typedef_name ( char * name )
517 {
518   if ( name == NULL ) return(1) ;
519   if ( get_typedef_name ( name ) == NULL )
520   {
521     if ( NumTypeDefs >= MAX_TYPEDEFS ) return(1) ;
522     strcpy( typedefs[NumTypeDefs++] , name ) ;
523   }
524   return(0) ;
525 }
526 
527 char *
528 get_typedef_name ( char * name )
529 {
530   int i ;
531   if ( name == NULL ) return(NULL) ;
532   for ( i = 0 ; i < NumTypeDefs ; i++ )
533   {
534     if ( !strcmp(name,typedefs[i]) ) return( typedefs[i] ) ; 
535   }
536   return(NULL) ;
537 }
538 
539 int
540 associated_with_4d_array( node_t * p ) 
541 {
542   int res = 0 ;
543   node_t * possble ;
544   char * last_underscore ;
545   char name_copy[128] ;
546   if ( p != NULL )
547   {
548     /* check this variable and see if it is a boundary variable that is associated with a 4d array */
549     strcpy( name_copy, p->name ) ;
550     if (( last_underscore = rindex( name_copy , '_' )) != NULL ) {
551       if ( !strcmp( last_underscore , "_b" ) || !strcmp( last_underscore , "_bt" ) ) {
552         *last_underscore = '\0' ;
553         if (( possble = get_entry( name_copy , Domain.fields )) != NULL ) {
554           res = possble->node_kind & FOURD ;
555         }
556       }
557     }
558   }
559   return(res) ;
560 }
561