#!/pkg/sbin/tclsh
# This script prints out all of the ospf trace buffers.
# It is executed by the "show ospf trace all" command.
# If the user gives an optional number, only the last N
# entries of each trace buffer are printed out.
#
# For portability, this script obtains the list of trace 
# buffers by calling ospf_show trace without the "-y" 
# argument. This will return the header which is then 
# parsed to obtain the list of buffer names. The 
# script then loops through the list invoking ospf_show -y
# for each buffer.

# March 2012, Balaji Venkatraman
#
#  Copyright (c) 2000-2016 by cisco Systems, Inc.
#  All rights reserved.

set cmd [join $argv]

set a [catch  {eval exec ospf_show $cmd} res]
if { $a != 0 } {
    return
} else {
    set a [eval exec ospf_show $cmd]
}    

set i 0
foreach line [split $a \n] {
    set a_str [join $line]
    set a_list [regexp -all -inline {\S+} $a_str]
    set buf_index [lindex $a_list 0]
    set buffer_size [lindex $a_list 3]
    if {([regexp {\d+.} $buf_index] == 1) && ($buffer_size != 0)} {                    
        set buffer($i) [lindex $a_list 1]
        incr i
    }
}

set count $i
for {set i 0} {$i < $count} {incr i} {
    set  pipe  [open "|ospf_show $cmd -y $buffer($i)"]
    while {![eof $pipe]} {
        if {[gets $pipe line] >= 0} {
            if { [catch {puts $line}] } {
                exit
            }
        }
    }
    catch {close $pipe}
}

