Example #1
0
void Command::parse_redirect(string &command, command_s &result)
{
    auto input  = command.find_last_of ("<");
    auto output = command.find_last_of (">");

    bool both = input != string::npos && output != string::npos;

    if ((both && input > output) || (!both && input != string::npos))
    {
        result.in = command.substr (input + 1, string::npos);
        trim (result.in);
        command.erase (input, string::npos);

        if (both) parse_redirect (command, result);
        return;
    }
    if ((both && input < output) || (!both && output != string::npos))
    {
        result.out = command.substr (output + 1, string::npos);
        trim (result.out);
        command.erase (output, string::npos);

        if (both) parse_redirect (command, result);
        return;
    }
}
Example #2
0
// get static net bandwidth
int32_t System::GetBandWidth(const char* interface) {
    char cmd[30] = {0};
    // cmd "ethtool eth0"
    snprintf(cmd, 30, "ethtool %s:", interface);

    FILE *stream = popen(cmd, "r");
    if (NULL == stream)
        return -1;

    int32_t bandwidth = -1;
    char buf[256] = {0};
    string s_buf = "";
    // get Speed from ethtool
    while (fgets(buf, sizeof(buf) - 1, stream)) {
        s_buf = buf;
        trim(s_buf);
         
        if (0 == strncmp(s_buf.c_str(), "Speed:", 6)) {
            if (sscanf(s_buf.c_str(), "Speed: %dMb/s", &bandwidth) < 1) {
                fclose(stream);
                return -1;
            }
            break;
        }
    }
    
    // close stream
    pclose(stream);
    return bandwidth;
}
Example #3
0
command_s Command::parse_command (string command)
{
    command_s result;

    trim (command);

    parse_redirect (command, result);

    trim (command);

    split (result.params, command, is_any_of (L" "), boost::token_compress_on);

    result.name = result.params [0];
    //result.params.erase (result.params.begin ());

    return result;
}
Example #4
0
string Worker::process_command (string command)
{
    DEBUG(cout << command << endl);

    trim (command);

    vector <string> parts;
    split (parts, command, is_any_of (L" "), boost::token_compress_on);

    const size_t command_p = 0;

    if (parts.size() < 1)
    {
        return string ("error \"bad command format\"");
    }

    if (parts [0] == "get")
    {
        const size_t key_p = 1;

        if (parts.size() != 2)
        {
            return string ("error \"bad get format\"");
        }

        return process_get (parts [key_p]);
    }

    if (parts [0] == "set")
    {
        const size_t ttl_p   = 1;
        const size_t key_p   = 2;
        const size_t value_p = 3;

        if (parts.size () != 4)
        {
            return string ("error \"bad set format\"");
        }

        ttl_t ttl = 0;
        try
        {
            ttl = boost::lexical_cast<ttl_t> (parts [ttl_p]);
        }
        catch(boost::bad_lexical_cast& e)
        {
            return string ("error \"bad set format (ttl format)\"");
        }

        return process_set (parts [key_p], ttl, parts [value_p]);
    }

    return string ("error \"bad command format\"");
}
Example #5
0
void Command::parse_parts ()
{
    string& current_part = *(parts_.begin ());

    trim (current_part);
    if (ends_with (current_part, "&"))
    {
        parallel_ = true;
        current_part.erase (current_part.length () - 1);
    }

    vector<string> separators = {"&&", "||", "|"};

    for (auto const & separator : separators)
    {
        for (auto i = parts_.begin (); i != parts_.end (); ++ i)
        {
            if (std::find (separators.begin (), separators.end (), *i) != separators.end ())
            {
                continue;
            }

            unsigned long pos;
            while ((pos = (*i).find (separator)) != string::npos)
            {
                string& part = *i;

                auto right = part.substr (pos + separator.length (), string::npos);
                part.erase (pos, string::npos);

                ++ i;

                parts_.insert (i, separator);
                parts_.insert (i, right);

                -- i;
            }
        }
    }
}