#!/bin/ksh #************************************************************** # Author: Patrick Brockmann # Contact: Patrick.Brockmann@cea.fr # $Date$ # $Author$ # $Revision$ # IPSL (2006) # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC # History: # Modification: # #************************************************************** #================================================== # The documentation of this file can be automatically generated # if you use the prefix #D- for comments to be extracted. # Extract with command: cat lib* | grep "^#D-" | cut -c "4-" #================================================== #D-#================================================================== #D-libIGCM_card #D-This ksh library handles extraction of information from configuration file #D-called "card" file (en français fichier "carte"). #D-All function described bellow must be prefixed by IGCM_card. #D-A card file is organized as follows : #D- --------------------- #D-[Messages] #D-Option1= "Hello Earth" #D-Option2= "Hello Mars" #D- #D-# My comments #D-[Recipes] #D-Cake= "file1.doc" #D-Starter= "file2.doc" #D- #D-[ColorValues] #D-Red= 120 #D-Blue= 230 #D-Green= 178 #D- #D-[Couples] #D-List1= (up, down), \ #D- (humid, dry), \ #D- (hot, cold), \ #D- (far, close) #D-List2= (ice, fire, air, water) #D- --------------------- #D- #D-#================================================================== #D-function IGCM_card_PrintOption #D-* Purpose: Print an option from a given file.card and section #D-* Usage: IGCM_card_PrintOption file.card section option #D- function IGCM_card_PrintOption { IGCM_debug_PushStack "IGCM_card_PrintOption" $@ if [ -r "$1" ] ; then awk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk "$@" else echo "--Error--> IGCM_card_PrintOption $@" echo " $1 is not readable" IGCM_debug_Exit "IGCM_card_PrintOption" fi IGCM_debug_PopStack "IGCM_card_PrintOption" } #D-#================================================================== #D-function IGCM_card_PrintSection #D-* Purpose: Print all options from a given file.card and section #D-* Usage: IGCM_card_PrintSection file.card section #D- function IGCM_card_PrintSection { IGCM_debug_PushStack "IGCM_card_PrintSection" $@ if [ -r "$1" ] ; then awk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintSection.awk -- "$@" else echo "--Error--> IGCM_card_PrintSection $@" echo " $1 is not readable" IGCM_debug_Exit "IGCM_card_PrintSection" fi IGCM_debug_PopStack "IGCM_card_PrintSection" } #D-#================================================================== #D-function IGCM_card_DefineVariableFromOption #D-* Purpose: Define a variable from a given file.card, section and option #D-* Variable name is automatically defined as file_section_option #D-* Usage: IGCM_card_DefineVariableFromOption file.card section option #D- function IGCM_card_DefineVariableFromOption { IGCM_debug_PushStack "IGCM_card_DefineVariableFromOption" $@ if [ -r "$1" ] ; then # Get basename of card file ($1) typeset name1=${1##*/} # Build name of variable as $1_$2_$3 (cardname_Section_Option) typeset name=${name1%%.*}_${2}_${3} typeset value=$( awk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" ) if [ "${value}" = "Error: Option not found" ] ; then echo "Error with readding of ${name} variable in ${1} ." echo "Error: Option not found." IGCM_debug_Exit IGCM_debug_Verif_Exit fi eval ${name}=${value} else echo "--Error--> IGCM_card_DefineVariableFromOption $@" echo " $1 is not readable" IGCM_debug_Exit "IGCM_card_DefineVariableFromOption" fi IGCM_debug_PopStack "IGCM_card_DefineVariableFromOption" } #D-#================================================================== #D-function IGCM_card_DefineArrayFromOption #D-* Purpose: Define an array variable from a given file.card, section and option #D-* Array variable is automatically defined as file_section_option #D-* Usage: IGCM_card_DefineArrayFromOption file.card section option #D- function IGCM_card_DefineArrayFromOption { IGCM_debug_PushStack "IGCM_card_DefineArrayFromOption" $@ if [ -r "$1" ] ; then # Get basename of card file ($1) typeset name1=${1##*/} # Build name of array as $1_$2_$3 (cardname_Section_Option) typeset name=${name1%%.*}_${2}_${3} set -A ${name} -- ${NULL_STR} set +A ${name} -- $( awk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" | awk -- 'BEGIN {FS="[() ,]+"} {for (i=2; i <= NF-1; i++) printf("%s ",$i)}' ) else echo "--Error--> IGCM_card_DefineArrayFromOption $@" echo " $1 is not readable" IGCM_debug_Exit "IGCM_card_DefineArrayFromOption" fi IGCM_debug_PopStack "IGCM_card_DefineArrayFromOption" } #D-#================================================================== #D-function IGCM_card_DefineArrayFromSection #D-* Purpose: Define an array variable from a given file.card and section #D-* Array variable is automatically defined as file_section #D-* Usage: IGCM_card_DefineArrayFromSection file.card section #D- function IGCM_card_DefineArrayFromSection { IGCM_debug_PushStack "IGCM_card_DefineArrayFromSection" $@ if [ -r "$1" ] ; then # Get basename of card file ($1) typeset name1=${1##*/} # Build name of array as $1_$2 (cardname_Section) typeset name=${name1%%.*}_${2} set -A ${name} -- ${NULL_STR} set +A ${name} -- $( awk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintSection.awk -- "$@" ) else echo "--Error--> IGCM_card_DefineArrayFromSection $@" echo " $1 is not readable" IGCM_debug_Exit "IGCM_card_DefineArrayFromSection" fi IGCM_debug_PopStack "IGCM_card_DefineArrayFromSection" } #D-#================================================================== #D-function IGCM_card_WriteOption #D-* Purpose: Write an option from a given file.card and section #D-* Usage: IGCM_card_WriteOption file.card section newvalue #D-* Examples: IGCM_card_WriteOption file.card Recipes Red 150 #D- IGCM_card_WriteOption file.card Messages Option2 '"Hello Mercure"' #D- IGCM_card_WriteOption file.card Messages ListVal1 '( 1, 2, 3 )' #D- listname="(Sebastien, Martial, Patrick)" #D- IGCM_card_WriteOption NewTestFile.card Messages ListVal2 "${listname}" #D- function IGCM_card_WriteOption { IGCM_debug_PushStack "IGCM_card_WriteOption" $@ if [ -r "$1" ] && [ -w "$1" ] ; then typeset tmpfile=tmpfile_$$ ( awk -f ${libIGCM}/libIGCM_card/IGCM_card_WriteOption.awk -- "$@" 2> /dev/null ) > ${tmpfile} mv ${tmpfile} $1 else echo "--Error--> IGCM_card_WriteOption $@" echo " $1 is not readable or not writable" IGCM_debug_Exit "IGCM_card_WriteOption" fi IGCM_debug_PopStack "IGCM_card_WriteOption" } #D-#================================================================== #D-function IGCM_card_Check #D-* Purpose: Check the present file by comparison with a reference file #D-* Usage: IGCM_card_Check #D- function IGCM_card_Check { IGCM_debug_PushStack "IGCM_card_Check" #--------------------- if [ ! -n "${libIGCM}" ] ; then echo "Check libIGCM_card ...........................................[ FAILED ]" echo "--Error--> libIGCM variable is not defined" IGCM_debug_Exit "IGCM_card_Check" fi #--------------------- whence -v awk > /dev/null 2>&1 if [ ! $? -eq 0 ] ; then echo "Check libIGCM_card ...........................................[ FAILED ]" echo "--Error--> awk command is not defined" IGCM_debug_Exit "IGCM_card_Check" fi #--------------------- ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh > IGCM_card_Test.ref.failed 2>&1 sleep 2 if diff IGCM_card_Test.ref.failed ${libIGCM}/libIGCM_card/IGCM_card_Test.ref > /dev/null 2>&1 ; then echo "Check libIGCM_card ...............................................[ OK ]" rm -f IGCM_card_Test.ref.failed else echo "Check libIGCM_card ...........................................[ FAILED ]" echo "--Error--> Execution of ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh" echo " has produced the file IGCM_card_Test.ref.failed" echo " Please analyse differences with the reference file by typing:" echo " diff IGCM_card_Test.ref.failed ${libIGCM}/libIGCM_card/IGCM_card_Test.ref" echo " Report errors to the author: Patrick.Brockmann@cea.fr" IGCM_debug_Exit "IGCM_card_Check" fi #--------------------- IGCM_debug_PopStack "IGCM_card_Check" }