<commit_before><?php

/**
 * CLI support
 *
 * @author <NAME>
 * @version $Id$
 * @copyright Genee, 2014-02-07
 **/

/**
 * Define DocBlock
 **/

namespace Gini;

class CLI
{
    const PARSE_BLANK = 0;
    const PARSE_IN_ARG = 1;
    const PARSE_IN_QUOTE = 2;

    static $built_in_commands = array(
        'help' => 'Help',
        '-' => 'List available commands',
        );

    public static function parseArguments($line)
    {
        $max = strlen($line);
        $st;     // parsing status: PARSE_BLANK, PARSE_IN_ARG, PARSE_IN_QUOTE
        $qt;    // quote char
        $esc;    // escape or not
        $args = array();    // arguments

        for ($i = 0; $i < $max; $i++) {
            $c = $line[$i];
            if ($esc) {
                if ($c == '0' || $c == 'x') {
                    $arg .= stripcslashes('\\'.substr($line, $i, 3));
                    $i += 2;
                } else {
                    $arg .= stripcslashes('\\'.$c);
                }

                $esc = false;

                if ($st == self::PARSE_BLANK) {
                    $st = self::PARSE_IN_ARG;
                    $qt = null;
                }
                continue;
            } elseif ($c == '\\') {
                $esc = true;
                continue;
            }

            switch ($st) {
            case self::PARSE_BLANK:
                if ($c == ' ' || $c == "\t") {
                    continue;
                } elseif ($c == '"' || $c == '\'') {
                    $st = self::PARSE_IN_QUOTE;
                    $qt = $c;
                } else {
                    $arg .= $c;
                    $st = self::PARSE_IN_ARG;
                    $qt = null;
                }
                break;
            case self::PARSE_IN_ARG:
                if ($c == ' ' || $c == "\t") {
                    $args[] = $arg;
                    $arg = '';
                    $st = self::PARSE_BLANK;
                } else {
                    $arg .= $c;
                }
                break;
            case self::PARSE_IN_QUOTE:
                if ($c == $qt) {
                    $st = self::PARSE_BLANK;
                    $args[] = $arg;
                    $arg = '';
                } else {
                    $arg .= $c;
                }
                break;
            }

        }

        if ($arg) {
            $args[] = $arg;
        }

        return $args;
    }

    public static function parsePrompt($prompt)
    {
        return preg_replace_callback('|%(\w+)|', function ($matches) {
            return $_SERVER[$matches[1]] ?: getenv($matches[1]) ?: $matches[0];
        }, $prompt);
    }

    public static function relaunch()
    {
        //$ph = proc_open($_SERVER['_'] . ' &', array(STDIN, STDOUT, STDERR), $pipes, null, $env);
        // fork process to avoid memory leak
        $env_path = '/tmp/gini-cli';
        $env_file = $env_path.'/'.posix_getpid().'.json';
        if (!file_exists($env_path)) @mkdir($env_path, 0777, true);
        file_put_contents($env_file, json_encode($_SERVER));
        if (isset($_SERVER['__RELAUNCH_PROCESS'])) {
            unset($_SERVER['__RELAUNCH_PROCESS']);
            exit(200);
        } else {
            do {
                // load $_SERVER from shared memo-cliry
                $_SERVER['__RELAUNCH_PROCESS'] = 1;
                $ph = proc_open($_SERVER['SCRIPT_FILENAME'], array(STDIN, STDOUT, STDERR), $pipes, null, $_SERVER);
                if (is_resource($ph)) {
                    $code = proc_close($ph);
                    $_SERVER = (array) json_decode(@file_get_contents($env_file), true);
                }
            } while ($code == 200);
            exit;
        }
    }

    private static $prompt;

    public static function main(array $argv)
    {
        if (count($argv) < 1) {
            static::command_help($argv);
            exit;
        }

        $cli = strtolower($argv[0]);
        switch ($cli) {
        case '-':
            $method = 'commandAvailable';
            break;
        default:
            $method = 'command'.$cli;
        }

        if ($cli && method_exists(__CLASS__, $method)) {
            call_user_func(array(__CLASS__, $method), $argv);
        } else {
            $GLOBALS['GINI.CURRENT_CLI'] = $cli;
            static::exec($argv);
        }

    }

    public static function commandHelp(array $argv)
    {
        echo "usage: \e[1;34mgini\e[0m <command> [<args>]\n\n";
        echo "The most commonly used git commands are:\n";
        foreach (self::$built_in_commands as $k => $v) {
            printf("   \e[1;34m%-10s\e[0m %s\n", $k, $v);<commit_msg>fix commandHelp bug<commit_after><?php

/**
 * CLI support
 *
 * @author <NAME>
 * @version $Id$
 * @copyright Genee, 2014-02-07
 **/

/**
 * Define DocBlock
 **/

namespace Gini;

class CLI
{
    const PARSE_BLANK = 0;
    const PARSE_IN_ARG = 1;
    const PARSE_IN_QUOTE = 2;

    static $built_in_commands = array(
        'help' => 'Help',
        '-' => 'List available commands',
        );

    public static function parseArguments($line)
    {
        $max = strlen($line);
        $st;     // parsing status: PARSE_BLANK, PARSE_IN_ARG, PARSE_IN_QUOTE
        $qt;    // quote char
        $esc;    // escape or not
        $args = array();    // arguments

        for ($i = 0; $i < $max; $i++) {
            $c = $line[$i];
            if ($esc) {
                if ($c == '0' || $c == 'x') {
                    $arg .= stripcslashes('\\'.substr($line, $i, 3));
                    $i += 2;
                } else {
                    $arg .= stripcslashes('\\'.$c);
                }

                $esc = false;

                if ($st == self::PARSE_BLANK) {
                    $st = self::PARSE_IN_ARG;
                    $qt = null;
                }
                continue;
            } elseif ($c == '\\') {
                $esc = true;
                continue;
            }

            switch ($st) {
            case self::PARSE_BLANK:
                if ($c == ' ' || $c == "\t") {
                    continue;
                } elseif ($c == '"' || $c == '\'') {
                    $st = self::PARSE_IN_QUOTE;
                    $qt = $c;
                } else {
                    $arg .= $c;
                    $st = self::PARSE_IN_ARG;
                    $qt = null;
                }
                break;
            case self::PARSE_IN_ARG:
                if ($c == ' ' || $c == "\t") {
                    $args[] = $arg;
                    $arg = '';
                    $st = self::PARSE_BLANK;
                } else {
                    $arg .= $c;
                }
                break;
            case self::PARSE_IN_QUOTE:
                if ($c == $qt) {
                    $st = self::PARSE_BLANK;
                    $args[] = $arg;
                    $arg = '';
                } else {
                    $arg .= $c;
                }
                break;
            }

        }

        if ($arg) {
            $args[] = $arg;
        }

        return $args;
    }

    public static function parsePrompt($prompt)
    {
        return preg_replace_callback('|%(\w+)|', function ($matches) {
            return $_SERVER[$matches[1]] ?: getenv($matches[1]) ?: $matches[0];
        }, $prompt);
    }

    public static function relaunch()
    {
        //$ph = proc_open($_SERVER['_'] . ' &', array(STDIN, STDOUT, STDERR), $pipes, null, $env);
        // fork process to avoid memory leak
        $env_path = '/tmp/gini-cli';
        $env_file = $env_path.'/'.posix_getpid().'.json';
        if (!file_exists($env_path)) @mkdir($env_path, 0777, true);
        file_put_contents($env_file, json_encode($_SERVER));
        if (isset($_SERVER['__RELAUNCH_PROCESS'])) {
            unset($_SERVER['__RELAUNCH_PROCESS']);
            exit(200);
        } else {
            do {
                // load $_SERVER from shared memo-cliry
                $_SERVER['__RELAUNCH_PROCESS'] = 1;
                $ph = proc_open($_SERVER['SCRIPT_FILENAME'], array(STDIN, STDOUT, STDERR), $pipes, null, $_SERVER);
                if (is_resource($ph)) {
                    $code = proc_close($ph);
                    $_SERVER = (array) json_decode(@file_get_contents($env_file), true);
                }
            } while ($code == 200);
            exit;
        }
    }

    private static $prompt;

    public static function main(array $argv)
    {
        if (count($argv) < 1) {
            static::commandHelp($argv);
            exit;
        }

        $cli = strtolower($argv[0]);
        switch ($cli) {
        case '-':
            $method = 'commandAvailable';
            break;
        default:
            $method = 'command'.$cli;
        }

        if ($cli && method_exists(__CLASS__, $method)) {
            call_user_func(array(__CLASS__, $method), $argv);
        } else {
            $GLOBALS['GINI.CURRENT_CLI'] = $cli;
            static::exec($argv);
        }

    }

    public static function commandHelp(array $argv)
    {
        echo "usage: \e[1;34mgini\e[0m <command> [<args>]\n\n";
        echo "The most commonly used git commands are:\n";
        foreach (self::$built_in_commands as $k => $v) {
            printf("   \e[1;34m%-10s\e[0m %s\n", $k, $v);
{'fraction_non_alphanumeric': 0.15518538420204192, 'fraction_numerical': 0.008812466415905428, 'mean_word_length': 1.2440318302387268, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5959209', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2772, 'n_words': 583}