[2] | 1 | #************************************************************** |
---|
| 2 | # Author: Patrick Brockmann |
---|
[373] | 3 | # Contact: Patrick.Brockmann__at__cea.fr |
---|
| 4 | # $Revision:: $ Revision of last commit |
---|
| 5 | # $Author:: $ Author of last commit |
---|
| 6 | # $Date:: $ Date of last commit |
---|
[2] | 7 | # IPSL (2006) |
---|
| 8 | # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC |
---|
| 9 | # |
---|
| 10 | #************************************************************** |
---|
| 11 | |
---|
| 12 | #========================== |
---|
| 13 | function myprint(str) { |
---|
| 14 | if (debug) { |
---|
| 15 | print str |
---|
| 16 | } |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | #========================== |
---|
| 20 | BEGIN { |
---|
| 21 | |
---|
| 22 | nbarg=ARGC |
---|
[219] | 23 | if (ARGV[1] == "-d") { |
---|
[2] | 24 | debug=1 |
---|
| 25 | file=ARGV[2] |
---|
| 26 | section=ARGV[3] |
---|
| 27 | option=ARGV[4] |
---|
| 28 | delete ARGV[1] ; delete ARGV[3] ; delete ARGV[4] |
---|
| 29 | nbarg-- |
---|
| 30 | } else { |
---|
| 31 | debug=0 |
---|
| 32 | file=ARGV[1] |
---|
| 33 | section=ARGV[2] |
---|
| 34 | option=ARGV[3] |
---|
| 35 | delete ARGV[2] ; delete ARGV[3] |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | # When exit statement, 'END' rule is always executed, so defined a exit_value to manage this |
---|
| 39 | exit_value=0 |
---|
| 40 | if (nbarg != 4) { |
---|
[7] | 41 | print "Usage: IGCM_card_PrintOption [-d] file section option" |
---|
[2] | 42 | print |
---|
| 43 | print "Args:" |
---|
| 44 | print " file = file at CARD format" |
---|
| 45 | print " section = section to find" |
---|
| 46 | print " option = option to find" |
---|
| 47 | print |
---|
| 48 | print "Options:" |
---|
| 49 | print " -d = debug mode" |
---|
| 50 | print |
---|
| 51 | exit_value=1 |
---|
| 52 | exit |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | section_found=0 |
---|
| 56 | option_found=0 |
---|
| 57 | |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | #========================== |
---|
| 61 | { |
---|
| 62 | |
---|
| 63 | myprint($0) |
---|
| 64 | |
---|
| 65 | # Do not consider commented lines (#...) |
---|
| 66 | if (! match($0,/^#/)) { |
---|
| 67 | |
---|
| 68 | # Find section with delimiters '[' and ']' |
---|
| 69 | if (match($0, "\\[" section "\\]")) { |
---|
| 70 | |
---|
| 71 | myprint("---->section found") |
---|
| 72 | section_found=1 |
---|
| 73 | |
---|
[425] | 74 | } else if (section_found == 1 && (match($0, "^[ ]*" option "[ ]*="))) { |
---|
[2] | 75 | |
---|
| 76 | myprint("---->option found") |
---|
| 77 | option_found=1 |
---|
| 78 | |
---|
| 79 | # Extract string after '=' |
---|
| 80 | option_value=substr($0, index($0, "=")+1) |
---|
| 81 | |
---|
| 82 | # Remove space after '=' |
---|
| 83 | gsub(/^[ ]*/, "", option_value) |
---|
| 84 | |
---|
| 85 | # If continuation character '\' get next lines |
---|
| 86 | if (match($0,/\\$/)) { |
---|
| 87 | tmp=$0 |
---|
| 88 | tmp=substr(tmp, 1, length(tmp)-1) |
---|
| 89 | gsub(/[ \t]*/, "", tmp) |
---|
| 90 | option_value=tmp |
---|
| 91 | getline tmp |
---|
| 92 | while (match(tmp,/\\$/)) { |
---|
| 93 | tmp=substr(tmp, 1, length(tmp)-1) |
---|
| 94 | gsub(/[ \t]*/, "", tmp) |
---|
| 95 | option_value=option_value " " tmp |
---|
| 96 | getline tmp |
---|
| 97 | } |
---|
| 98 | gsub(/[ \t]*/, "", tmp) |
---|
| 99 | option_value=option_value " " tmp |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | exit |
---|
| 103 | |
---|
| 104 | } else if (section_found == 1 && match($0, /^[\[*\]]/)) { |
---|
| 105 | |
---|
| 106 | myprint("---->end section") |
---|
| 107 | exit |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | #========================== |
---|
| 116 | END { |
---|
| 117 | |
---|
| 118 | if (! exit_value ) { |
---|
| 119 | |
---|
| 120 | myprint("###############################") |
---|
| 121 | myprint("section ====> " section) |
---|
| 122 | myprint("option ====> " option) |
---|
| 123 | myprint("value ====> " option_value) |
---|
| 124 | |
---|
| 125 | if (section_found == 0) { |
---|
[475] | 126 | print "Error: Section not found" |
---|
[2] | 127 | } else if (option_found == 0) { |
---|
[475] | 128 | print "Error: Option not found" |
---|
[2] | 129 | } else { |
---|
| 130 | print option_value |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | #========================== |
---|