#!/pkg/sbin/tclsh
# This script prints out all of the ospf trace buffers.
# It is executed by the "show ospfv3 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 ospfv3_show_trace without the "-n" 
# 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 ospfv3_show_trace 
# -n 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 ospfv3_show_trace $cmd} res]
if { $a != 0 } {
    return
} else {
    set a [eval exec ospfv3_show_trace $cmd]
}    
set a [eval exec ospfv3_show_trace $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 "|ospfv3_show_trace $cmd -n $buffer($i)"]
    while {![eof $pipe]} {
	if {[gets $pipe line] >= 0} {
            if { [catch {puts $line}] } {
                exit
            }
	}
    }
    catch {close $pipe}
}

