/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.hyperic.sigar.cmd;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarPermissionDeniedException;

/**
 * Display all process information.
 */
public class ProcInfo extends SigarCommandBase {

    public ProcInfo(Shell shell) {
        super(shell);
    }

    public ProcInfo() {
        super();
    }

    protected boolean validateArgs(String[] args) {
        return true;
    }

    public String getUsageShort() {
        return "Display all process info";
    }

    public boolean isPidCompleter() {
        return true;
    }

    public void output(String[] args) throws SigarException {
        long[] pids = this.shell.findPids(args);

        for (int i=0; i<pids.length; i++) {
            try {
                output(pids[i]);
            } catch (SigarPermissionDeniedException e) {
                println(this.shell.getUserDeniedMessage(pids[i]));
            } catch (SigarException e) {
                println("(" + e.getMessage() + ")");
            }
            println("\n------------------------\n");
        }
    }

    public void output(long pid) throws SigarException {
        println("pid=" + pid);
        try {
            println("state=" + sigar.getProcState(pid));
        } catch (SigarException e) {}
        try {
            println("mem=" + sigar.getProcMem(pid));
        } catch (SigarException e) {}
        try {
            println("time=" + sigar.getProcTime(pid));
        } catch (SigarException e) {}
        try {
            println("cred=" + sigar.getProcCred(pid));
        } catch (SigarException e) {}
        try {
            println("credname=" + sigar.getProcCredName(pid));
        } catch (SigarException e) {}
    }

    public static void main(String[] args) throws Exception {
        new ProcInfo().processCommand(args);
    }
}

@@ -952,16 +952,54 @@
 Base %7B%0A%0A
+    private boolean isSingleProcess;%0A%0A
     publ
@@ -1388,32 +1388,548 @@
 igarException %7B%0A
+        this.isSingleProcess = false;%0A%0A        if ((args.length != 0) && args%5B0%5D.startsWith(%22-s%22)) %7B%0A            this.isSingleProcess = true;%0A        %7D%0A%0A        if (this.isSingleProcess) %7B%0A            for (int i=1; i%3Cargs.length; i++) %7B%0A                try %7B%0A                    output(args%5Bi%5D);%0A                %7D catch (SigarException e) %7B%0A                    println(%22(%22 + e.getMessage() + %22)%22);%0A                %7D%0A                println(%22%5Cn------------------------%5Cn%22);%0A            %7D%0A        %7D%0A        else %7B%0A    
         long%5B%5D p
@@ -1962,32 +1962,36 @@
 args);%0A%0A        
+   
+ 
 for (int i=0; i%3C
@@ -2014,38 +2014,46 @@
 ) %7B%0A            
+    
 try %7B%0A
+    
                 
@@ -2059,24 +2059,39 @@
  output(
+String.valueOf(
 pids%5Bi%5D)
 ;%0A      
@@ -2078,26 +2078,31 @@
 eOf(pids%5Bi%5D)
+)
 ;%0A
+    
             
@@ -2138,32 +2138,36 @@
 dException e) %7B%0A
+    
                 
@@ -2221,32 +2221,36 @@
 ));%0A            
+    
 %7D catch (SigarEx
@@ -2254,32 +2254,36 @@
 rException e) %7B%0A
+    
                 
@@ -2323,34 +2323,42 @@
 %22);%0A            
+   
+ 
 %7D%0A
+    
             prin
@@ -2386,32 +2386,46 @@
 ----------%5Cn%22);%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -2448,18 +2448,20 @@
  output(
-lo
+Stri
 ng pid) 
@@ -2614,32 +2614,136 @@
 arException e) %7B
+%0A            if (this.isSingleProcess) %7B%0A                println(e.getMessage());%0A            %7D%0A        
 %7D%0A        try %7B%0A

use -s to test single-process ptql queries