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