Java String Format formatCommand(String cmd)

Here you can find the source of formatCommand(String cmd)

Description

Format the specified command line.

License

Open Source License

Parameter

Parameter Description
cmd command line

Return

a trimed command line

Declaration

public static String formatCommand(String cmd) 

Method Source Code

//package com.java2s;
/*/*  ww  w.  j a  va  2  s  .c om*/
 * Bootchart -- Boot Process Visualization
 *
 * Copyright (C) 2004  Ziga Mahkovec <ziga.mahkovec@klika.si>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.
 */

import java.util.Arrays;

import java.util.List;

public class Main {
    /** A list of processes which should include parameters in their command lines. */
    public static final List PROC_PARAM = Arrays
            .asList(new String[] { "init", "udev", "initlog", "modprobe", "rc", "ifup", "ifconfig", "cat", "sed",
                    "awk", "grep", "hotplug", "default.hotplug", "05-wait_for_sysfs.hotplug", "20-hal.hotplug" });

    /**
     * Format the specified command line.  Shell invocations, paths and
     * parameters are removed (e.g. "/bin/bash /etc/rc.d/rc.sysinit" ->
     * "rc.sysinit").  Paramaters are included for certain commands
     * (e.g. modprobe and rc).
     * 
     * @param cmd  command line
     * @return     a trimed command line
     */
    public static String formatCommand(String cmd) {
        if (cmd == null) {
            return null;
        }
        cmd = cmd.replaceFirst(" <defunct>", "");
        if (cmd.matches("\\[.+\\]")) {
            cmd = cmd.substring(1, cmd.length() - 1);
        }
        String[] tokens = cmd.trim().split("\\s+");
        for (int i = 0; i < tokens.length; i++) {
            if (i == 0 && tokens.length > 1 && (tokens[i].matches(".*/?sh") || tokens[i].matches(".*/?bash")
                    || tokens[i].matches(".*/?python") || tokens[i].matches(".*/?perl"))) {
                continue;
            } else if (tokens[i].startsWith("-") && tokens.length > 1) {
                continue;
            } else {
                String fcmd = tokens[i];
                if (fcmd.startsWith("/") || fcmd.startsWith("./") || fcmd.startsWith("../")) {
                    fcmd = fcmd.substring(fcmd.lastIndexOf('/') + 1);
                }
                StringBuffer sb = new StringBuffer();
                if (PROC_PARAM.contains(fcmd)) {
                    for (int j = i + 1; j < tokens.length; j++) {
                        if (!tokens[j].startsWith("-")) {
                            sb.append(" " + formatCommand(tokens[j]));
                            break;
                        }
                    }
                }
                fcmd += sb.toString();
                return fcmd;
            }
        }
        return null;
    }
}

Related

  1. format(String name, char separator)
  2. format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)
  3. format(String s)
  4. format(String source, String placeholder, Object... objs)
  5. format(String string)
  6. formatDescriptors(String descriptorString)
  7. formatError(String errorMsg)
  8. formatJson(String json)
  9. formatLore(String str)