[7541] | 1 | # codealgo.awk - filter of ORCHIDEE f90 files |
---|
| 2 | # command : gawk -f codealgo.awk [-d] file.f90 |
---|
| 3 | # create a file.f90_preproc_codealgo filtered with algo comments inside each routine all group |
---|
| 4 | # in the Doxygen comment just before its beginning. |
---|
| 5 | |
---|
| 6 | #************************************************************** |
---|
| 7 | # Author: Martial.Mancip |
---|
| 8 | # Contact: Martial.Mancip__at__ipsl.jussieu.fr |
---|
| 9 | # $Revision:: $ Revision of last commit |
---|
| 10 | # $Author:: $ Author of last commit |
---|
| 11 | # $Date:: |
---|
| 12 | # IPSL (2006) |
---|
| 13 | # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC |
---|
| 14 | # Modification: |
---|
| 15 | # |
---|
| 16 | #************************************************************** |
---|
| 17 | #========================== |
---|
| 18 | function debugprint(str) { |
---|
| 19 | if (debug) { |
---|
| 20 | print str | "cat 1>&2" |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | function myprint(str) { |
---|
| 24 | print str >> output_file |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | function flush_line() { |
---|
| 29 | if ( nblc > 0 ) { |
---|
| 30 | if ( inside_algo == 1 ) { |
---|
| 31 | myprint(" !> </ul>") |
---|
| 32 | } |
---|
| 33 | for ( i=1; i < nblc+1; i++ ) { |
---|
| 34 | myprint(line[i]) |
---|
| 35 | } |
---|
| 36 | nblc=0 |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | #========================== |
---|
| 42 | BEGIN { |
---|
| 43 | |
---|
| 44 | nbarg=ARGC |
---|
| 45 | if (ARGV[1] == "-d") { |
---|
| 46 | debug=1 |
---|
| 47 | file=ARGV[2] |
---|
| 48 | delete ARGV[1] |
---|
| 49 | nbarg-- |
---|
| 50 | } else { |
---|
| 51 | debug=0 |
---|
| 52 | file=ARGV[1] |
---|
| 53 | } |
---|
| 54 | output_file=file "_preproc_codealgo" |
---|
| 55 | if (debug) { |
---|
| 56 | lscommand=("ls " output_file) |
---|
| 57 | outsys=system(lscommand) |
---|
| 58 | debugprint("output of ls : " outsys) |
---|
| 59 | if ( outsys == 0 ) { |
---|
| 60 | debugprint("Already find " output_file) |
---|
| 61 | outsys=system("rm " output_file) |
---|
| 62 | debugprint("output of rm : " outsys) |
---|
| 63 | system("touch " output_file) |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | # When exit statement, 'END' rule is always executed, so defined a exit_value to manage this |
---|
| 68 | exit_value=0 |
---|
| 69 | if (nbarg != 2) { |
---|
| 70 | debugprint("Usage: codealgo.awk [-d] file.f90") |
---|
| 71 | debugprint("") |
---|
| 72 | debugprint("Args:") |
---|
| 73 | debugprint(" file.f90 = source code to be parsed") |
---|
| 74 | debugprint("") |
---|
| 75 | debugprint("Options:" ) |
---|
| 76 | debugprint(" -d = debug mode") |
---|
| 77 | debugprint("") |
---|
| 78 | exit_value=1 |
---|
| 79 | exit 2 |
---|
| 80 | } |
---|
| 81 | debugprint("filter " file " in " output_file) |
---|
| 82 | |
---|
| 83 | # Number of previous lines commented |
---|
| 84 | nblc=0 |
---|
| 85 | # Flag indicates inside a routine |
---|
| 86 | routine = 0 |
---|
| 87 | routine_name = "" |
---|
| 88 | # detect begin of algo liste |
---|
| 89 | inside_algo=0 |
---|
| 90 | |
---|
| 91 | IGNORECASE=1 |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | #========================== |
---|
| 95 | { |
---|
| 96 | debugprint($0) |
---|
| 97 | |
---|
| 98 | if ( match($0, "^[^!('\"]*subroutine") || match($0, "^[^!('\"]*function") ) { |
---|
| 99 | |
---|
| 100 | debugprint("routine line.") |
---|
| 101 | |
---|
| 102 | nblc++ |
---|
| 103 | line[nblc]=$0 |
---|
| 104 | |
---|
| 105 | # Begin of algo search |
---|
| 106 | if ( match(line[nblc],"^[[:space:]]*end") ) { |
---|
| 107 | debugprint("End of the routine.") |
---|
| 108 | if ( routine == 0 ) { |
---|
| 109 | # error ! |
---|
| 110 | debugprint("error ! End of routine has been detected but no routine was opened.") |
---|
| 111 | exit 1 |
---|
| 112 | } else { |
---|
| 113 | if ( match(line[nblc],routine_name) ) { |
---|
| 114 | # here we flush the whole routine |
---|
| 115 | debugprint("Normal End of a routine : flush previous lines.") |
---|
| 116 | flush_line() |
---|
| 117 | routine = 0 |
---|
| 118 | inside_algo = 0 |
---|
| 119 | nblc = 0 |
---|
| 120 | } else { |
---|
| 121 | debugprint("Error in a routine : routine_name " routine_name " doesn't match.") |
---|
| 122 | exit 3 |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } else { |
---|
| 126 | debugprint("Begining of a new routine.") |
---|
| 127 | |
---|
| 128 | if ( routine == 1 ) { |
---|
| 129 | if ( match(line[nblc],routine_name) ) { |
---|
| 130 | debugprint("routine : " routine_name " detect two times. Precompilation ? OK.") |
---|
| 131 | } else { |
---|
| 132 | # error ! |
---|
| 133 | debugprint("error ! Already find a routine not closed.") |
---|
| 134 | exit 1 |
---|
| 135 | } |
---|
| 136 | } else { |
---|
| 137 | routine_name=$2 |
---|
| 138 | sub(/\(.*/,"",routine_name) |
---|
| 139 | debugprint("routine : " routine_name) |
---|
| 140 | routine = 1 |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | } else if ( routine == 1 ) { |
---|
| 145 | |
---|
| 146 | nblc++ |
---|
| 147 | line[nblc]=$0 |
---|
| 148 | |
---|
| 149 | if (match(line[nblc], "^[[:space:]]+\\!+>*[[:space:]]+[[:digit:]]+\\.*")) |
---|
| 150 | { |
---|
| 151 | debugprint("New algo line.") |
---|
| 152 | if ( inside_algo == 0 ) { |
---|
| 153 | myprint(" !> ") |
---|
| 154 | myprint(" !> ALGORITHM : ") |
---|
| 155 | myprint(" !> <ul>") |
---|
| 156 | debugprint("ALGORITHM.") |
---|
| 157 | inside_algo = 1 |
---|
| 158 | } |
---|
| 159 | sub(/\!+>*/,"!> <li> ") |
---|
| 160 | myprint($0) |
---|
| 161 | } |
---|
| 162 | } else if ( routine == 0 ) { |
---|
| 163 | myprint($0) |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | END { |
---|
| 168 | close(output_file) |
---|
| 169 | } |
---|