| 1 |
#!/usr/local/bin/tcsh -f |
| 2 |
# |
| 3 |
# Generate component context switching routines |
| 4 |
# These routines pop and push the current directory to a sub-directory |
| 5 |
# called "compname"."number" e.g. push => cd ocn.1/ ; pop => cd .. |
| 6 |
# |
| 7 |
# This script generates the c code for this with several embellished finction |
| 8 |
# names to allow different Fortran bindings. |
| 9 |
# |
| 10 |
# |
| 11 |
if ( $# == 1 ) then |
| 12 |
set comp_nam = ( $1 ) |
| 13 |
else |
| 14 |
set comp_nam = ( ocn ) |
| 15 |
endif |
| 16 |
# |
| 17 |
cat > component_${comp_nam}_context.c <<EOFA |
| 18 |
#include "unistd.h" |
| 19 |
#include "dirent.h" |
| 20 |
#include "stdio.h" |
| 21 |
#include "errno.h" |
| 22 |
|
| 23 |
#ifndef NAME_MAX |
| 24 |
#define NAME_MAX 4096 |
| 25 |
#endif |
| 26 |
|
| 27 |
#define COMP_NAME "${comp_nam}" |
| 28 |
#define DB_LEN NAME_MAX+1 |
| 29 |
#define MAX_COMP_COUNT 32 |
| 30 |
|
| 31 |
char return_dir[MAX_COMP_COUNT][DB_LEN]; |
| 32 |
char run_dir[MAX_COMP_COUNT][DB_LEN]; |
| 33 |
|
| 34 |
void Comp_ocn_push_context(int *cId); |
| 35 |
void Comp_ocn_pop_context( int *cId); |
| 36 |
|
| 37 |
void comp_${comp_nam}_push_context(int *cId) |
| 38 |
{ Comp_${comp_nam}_push_context(cId); } |
| 39 |
void comp_${comp_nam}_push_context_(int *cId) |
| 40 |
{ Comp_${comp_nam}_push_context(cId); } |
| 41 |
void comp_${comp_nam}_push_context__(int *cId) |
| 42 |
{ Comp_${comp_nam}_push_context(cId); } |
| 43 |
void _comp_${comp_nam}_push_context__(int *cId) |
| 44 |
{ Comp_${comp_nam}_push_context(cId); } |
| 45 |
|
| 46 |
void Comp_${comp_nam}_push_context(int *cId) |
| 47 |
{ |
| 48 |
int rc; |
| 49 |
if ( *cId > MAX_COMP_COUNT ) { |
| 50 |
printf("Too many components, need to recompiler \"context\" routines with larger value of MAX_COMP_COUNT\n"); |
| 51 |
exit(1); |
| 52 |
} |
| 53 |
if ( *cId < 1 ) { |
| 54 |
printf("ERROR: Negative component id passed to \"context\" routines\n"); |
| 55 |
exit(1); |
| 56 |
} |
| 57 |
/* Save return directory */ |
| 58 |
getcwd(return_dir[*cId-1], (size_t)DB_LEN); |
| 59 |
snprintf(run_dir[*cId-1],(size_t)DB_LEN,"%s.%d",COMP_NAME,*cId); |
| 60 |
/* Switch to run directory */ |
| 61 |
rc=chdir(run_dir[*cId-1]); |
| 62 |
if ( rc != 0 ) { |
| 63 |
fprintf(stderr,"ERROR: Unable to change to component run directory \"%s\", ",run_dir[*cId-1]); |
| 64 |
fprintf(stderr,"\"%s\"\n",strerror(errno)); |
| 65 |
exit(1); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
void comp_${comp_nam}_pop_context(int *cId) |
| 70 |
{ Comp_${comp_nam}_pop_context(cId); } |
| 71 |
void comp_${comp_nam}_pop_context_(int *cId) |
| 72 |
{ Comp_${comp_nam}_pop_context(cId); } |
| 73 |
void comp_${comp_nam}_pop_context__(int *cId) |
| 74 |
{ Comp_${comp_nam}_pop_context(cId); } |
| 75 |
void _comp_${comp_nam}_pop_context__(int *cId) |
| 76 |
{ Comp_${comp_nam}_pop_context(cId); } |
| 77 |
|
| 78 |
void Comp_${comp_nam}_pop_context(int *cId) |
| 79 |
{ |
| 80 |
int rc; |
| 81 |
|
| 82 |
if ( *cId > MAX_COMP_COUNT ) { |
| 83 |
printf("ERROR: Too many components, need to recompile \"context\" routines with larger value of MAX_COMP_COUNT\n"); |
| 84 |
exit(1); |
| 85 |
} |
| 86 |
if ( *cId < 1 ) { |
| 87 |
printf("ERROR: Negative component id passed to \"context\" routines\n"); |
| 88 |
exit(1); |
| 89 |
} |
| 90 |
/* Switch to return directory */ |
| 91 |
rc=chdir(return_dir[*cId-1]); |
| 92 |
if ( rc != 0 ) { |
| 93 |
fprintf(stderr,"ERROR: Unable to change to component run directory \"%s\", ",run_dir[*cId-1]); |
| 94 |
fprintf(stderr,"\"%s\"\n",strerror(errno)); |
| 95 |
exit(1); |
| 96 |
} |
| 97 |
} |
| 98 |
EOFA |