home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / sbin / kp < prev    next >
Encoding:
Tcl/Tk script  |  2004-11-02  |  5.7 KB  |  307 lines

  1. #!/usr/bin/wish -f
  2. #    Process Lister -- list processes, signal them
  3. #
  4. #Hacked by GuestToo and BK, for Puppy Linux, www.puppylinux.org
  5.  
  6.  
  7. # initial values for menu options 
  8. set user    $env(LOGNAME)
  9. set signal    -9
  10. # ...have changed this from -2
  11.  
  12. option add *borderWidth    1
  13.  
  14. #
  15. #    Get the selected line(s) from the list-box,
  16. #    extract the pid, signal the process
  17. #
  18. proc kill_proc {} {
  19.     global signal
  20.  
  21.     #
  22.     # Get the list of selected line numbers.
  23.     # If the list is empty, nothing is selected, so return
  24.     #
  25.     set sellist [.ps.list curselection]
  26.     if {[llength $sellist] == 0} {
  27.         return
  28.     }
  29.  
  30.     #
  31.     # For each entry in the selected-lines-list,
  32.     # get the line, extract the pid, append to a list of PID's 
  33.     # (optional: filter out critical processes that shouldn't be touched)
  34.     #
  35.     foreach index $sellist {
  36.         set line [.ps.list get $index]
  37.         set pid "" 
  38.         scan $line "%s %d" uid pid
  39.         if {$pid == ""} {
  40.             scan $line "%d" pid
  41.             if {$pid == ""} {
  42.                 continue
  43.             }
  44.         }
  45.  
  46.         # Use this to skip over protected processes:
  47.         # if {[string match "*precious*" $line]} {
  48.         #     err "$line\n\nMust not kill this process!"
  49.         #     continue
  50.         # }
  51.  
  52.         lappend pid_list $pid
  53.     }
  54.  
  55.     #
  56.     # Construct a "kill" command line,
  57.     # execute it and get the error return
  58.     #
  59.     set cmd [list exec kill $signal $pid_list]
  60.     set ret [catch $cmd err]
  61.     
  62.     #
  63.     # If there was a problem signalling a process, 
  64.     # let the user know
  65.     #
  66.     if {$ret == 1} {
  67.         msg "Could not kill a process;\nit's already gone?"
  68.         return
  69.     }
  70.  
  71.     #
  72.     # Refresh the process list
  73.     #
  74.     scan_proc
  75. }
  76.  
  77. #
  78. #    Do a "ps", collect the output, put into a list-box
  79. #
  80. proc scan_proc {} {
  81.     set cmd "|ps"
  82.     #
  83.     # Sort the output (by names or PIDs)
  84.     #
  85.     append cmd " | sort -n "
  86.  
  87.     #
  88.     # Evaluate the command line
  89.     # Get the resulting list of lines
  90.     # Fill the list-box with the lines
  91.     # Be careful to retain our position in the listbox!
  92.     #
  93.     set fid [open $cmd r]
  94.     if {$fid != 0} {
  95.         # save original position
  96.         set sel [lindex [.ps.list curselection] 0]
  97.         set top [.ps.list nearest 1]
  98.  
  99.         # clear contents, refill it
  100.         .ps.list delete 0 end
  101.         while {[gets $fid line] > 0} {
  102.             .ps.list insert end $line
  103.         }
  104.         catch {close $fid}
  105.  
  106.         # restore listbox position
  107.         if {$sel != ""} {
  108.             .ps.list selection set $sel
  109.             .ps.list activate $sel
  110.         }
  111.         .ps.list yview $top
  112.     } {
  113.         err "Cannot run command\n$cmd"
  114.         return
  115.     }
  116. }
  117.  
  118. #
  119.  
  120. #
  121.  
  122. proc err { msg } {
  123.     tk_dialog .err "Error!" $msg error 0 OK
  124. }
  125.  
  126. proc msg { msg } {
  127.     tk_dialog .msg "Message:" $msg info 0 OK
  128. }
  129.  
  130. proc ask_yn { msg } {
  131.     return [tk_dialog .ask "Question:" "$msg" question 0 Yes No]
  132. }
  133.  
  134. #
  135. #    Make a listbox and scrollbar, glue them together,
  136. #    add some keyboard controls
  137. #
  138. proc list_box {w {action ""}} {
  139.     if {[winfo exists $w] == 0} {
  140.         frame $w
  141.     }
  142.     scrollbar $w.sb -command "$w.list yview"
  143.     listbox $w.list -yscroll "$w.sb set" \
  144.         -font fixed -setgrid 1 -width 81 -height 10
  145.     pack $w.sb   -side right -fill y
  146.     pack $w.list -side left  -fill both -expand yes
  147.     if {$action != ""} {
  148.         bind $w.list    <Double-1>    $action
  149.         bind $w.list    <Return>    $action
  150.     }
  151.     bind $w.list    <Control-p>    {tkListboxUpDown %W -1}
  152.     bind $w.list    <Control-n>    {tkListboxUpDown %W  1}
  153.     bind $w.list    <k>        {tkListboxUpDown %W -1}
  154.     bind $w.list    <j>        {tkListboxUpDown %W  1}
  155.     bind $w        <Any-Enter>    "focus $w.list"
  156. }
  157.  
  158. #
  159. #    Make a menubar with menus
  160. #
  161. proc menu_bar {w args} {
  162.     if {[winfo exists $w] == 0} {
  163.         frame $w -relief raised
  164.     }
  165.     set mnum 0        ;# menu counter
  166.     set inum 0        ;# item counter
  167.  
  168.     foreach arg $args {
  169.         foreach {type name var val v2} $arg { }
  170.     
  171.         if {$inum == 0 && [string match "menu*" $type] == 0} {
  172.             err "menu_bar: need menu to hold $type/$name menu-item"
  173.             return
  174.         }
  175.  
  176.         switch $type {
  177.         menu {
  178.             set mbase $w.m[incr mnum]
  179.             set new $mbase.menu
  180.             menubutton $mbase -text $name -menu $new
  181.             menu $new -disabledforeground DarkBlue
  182.             set inum 1
  183.         }
  184.  
  185.         sep {
  186.             $new add sep
  187.         }
  188.  
  189.         label {
  190.             $new add command -label $name -state disabled
  191.             incr inum    
  192.         }
  193.  
  194.         cmd {
  195.             $new add command -label $name -command $var
  196.             incr inum
  197.         }
  198.  
  199.         radio {
  200.             global $var
  201.             $new add radiobutton -label $name \
  202.                 -variable $var -value $val
  203.             incr inum
  204.         }
  205.         
  206.         check {
  207.             global $var
  208.             $new add checkbutton -label $name \
  209.                 -variable $var -offvalue $val -onvalue $v2
  210.             incr inum
  211.         }
  212.  
  213.         end {
  214.             # pack this menu on the given side
  215.             pack $mbase -side $name -padx 1m
  216.             set inum 0
  217.         }
  218.  
  219.         default {
  220.             err "menu_bar: unknown type '$type' for '$name'"
  221.             return
  222.         }
  223.  
  224.         }
  225.     }
  226. }
  227.  
  228.  
  229. #
  230. #    Make a button row from some button names and commands
  231. #
  232. proc buttons {w args} {
  233.     if {[winfo exists $w] == 0} {
  234.         frame $w
  235.     }
  236.     set i 0
  237.     set btns ""
  238.     foreach arg $args {
  239.         foreach {name cmd} $arg { }
  240.         set btn $w.b[incr i]
  241.         append btns " $btn"
  242.         button $btn -text $name -command $cmd
  243.         
  244.     }
  245.     eval pack $btns -side left -fill x -expand yes
  246. }
  247.  
  248.  
  249. #
  250. #    Make the main control panel -- fill it with some controls 
  251. #
  252. proc make_control_panel { } {
  253.     global user
  254.     wm title    . "Process Lister"
  255.     wm iconname    . "PL"
  256.     wm command    . "PL"
  257.  
  258.     menu_bar .mbar                        \
  259.         {menu    File}                    \
  260.         {cmd    Quit        exit}            \
  261.         {end    left}                     \
  262.         \
  263.         {menu    Signals}                \
  264.         {radio    " 1 Hangup"    signal     -1}        \
  265.         {radio    " 2 Interrupt"    signal     -2}        \
  266.         {radio    " 3 Quit"    signal     -3}        \
  267.         {radio    " 9 Kill"    signal     -9}        \
  268.         {radio    "14 Alarm"    signal    -14}        \
  269.         {radio    "15 Terminate"    signal    -15}        \
  270.         {radio    "23 Stop"    signal    -23}        \
  271.         {radio    "24 Term. Stop"    signal    -24}        \
  272.         {radio    "25 Continue"    signal    -25}        \
  273.         {end    left}
  274.  
  275.     list_box .ps kill_proc
  276.  
  277.     buttons    .cmd            \
  278.         {Scan    scan_proc}    \
  279.         {Kill    kill_proc}    \
  280.         {Quit    exit}
  281.  
  282.     # pack menu         -> top
  283.     # pack btns         -> on the bottom
  284.     # pack scroll-box    -> in the middle
  285.  
  286.     pack .mbar     -side top    -fill x 
  287.     pack .cmd    -side bottom    -fill x 
  288.     pack .ps     -side top    -fill both    -expand yes
  289. }
  290.  
  291.  
  292. #
  293. #    Read user's settings, make the interface
  294. #
  295. make_control_panel
  296. update
  297. scan_proc
  298.  
  299. #
  300. #    Stub routine to get a new process listing 
  301. #    (called from Tcl 'trace', setup below)
  302. #
  303. proc do_new_scan {n1 n2 op} {
  304.     scan_proc
  305. }
  306.