Ejemplo n.º 1
0
Mode *script_switcher_parse_setup ( const char *str )
{
    Mode              *sw    = g_malloc0 ( sizeof ( *sw ) );
    char              *endp  = NULL;
    char              *parse = g_strdup ( str );
    unsigned int      index  = 0;
    const char *const sep    = ":";
    for ( char *token = strtok_r ( parse, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
        if ( index == 0 ) {
            sw->name = g_strdup ( token );
        }
        else if ( index == 1 ) {
            sw->ed = (void *) rofi_expand_path ( token );
        }
        index++;
    }
    g_free ( parse );
    if ( index == 2 ) {
        sw->free               = script_switcher_free;
        sw->_init              = script_mode_init;
        sw->_get_num_entries   = script_mode_get_num_entries;
        sw->_result            = script_mode_result;
        sw->_destroy           = script_mode_destroy;
        sw->_token_match       = script_token_match;
        sw->_get_completion    = NULL,
        sw->_preprocess_input  = NULL,
        sw->_get_display_value = _get_display_value;

        return sw;
    }
    fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.", str, index );
    script_switcher_free ( sw );
    return NULL;
}
Ejemplo n.º 2
0
Switcher *script_switcher_parse_setup ( const char *str )
{
    Switcher     *sw    = g_malloc0 ( sizeof ( *sw ) );
    ScriptModeConfig *conf = g_malloc0 ( sizeof ( *conf ) );
    sw->ed = conf;
    char         *endp  = NULL;
    char         *parse = g_strdup ( str );
    unsigned int index  = 0;
    char         *opts  = NULL;
    for ( char *token = strtok_r ( parse, ":", &endp ); token != NULL; token = strtok_r ( NULL, ":", &endp ) ) {
        if ( index == 0 ) {
            g_strlcpy ( sw->name, token, 32 );
        }
        else if ( index == 1 ) {
            conf->scriptname = (void *) g_strdup ( token );
        }
        else if ( index == 2 ) {
            opts = token ;
        }
        index++;
    }
    char continous = 0;
    if (index == 3) {
        if (opts == NULL) {
            goto fail;
        }

        int opt_count = strlen(opts);

        for (int i = 0; i < opt_count; i++) {
            if (opts[i] == 'c') {
                continous = 1;
            } else if (opts[i] == 'l') {
                conf->labled = TRUE;
            } else {
                opt_count = -1;
                break;
            }
        }

        if (opt_count == -1) {
            goto fail;
        }
        index --;
    }
    g_free ( parse );
    if ( index == 2 ) {
        sw->free        = script_switcher_free;
        sw->keysym      = None;
        sw->modmask     = AnyModifier;
        sw->init        = script_mode_init;
        sw->get_data    = script_mode_get_data;
        sw->result      = script_mode_result;
        sw->destroy     = script_mode_destroy;
        // if we are in continous mode, we call the script for every
        // iteration
        if (!continous) {
            sw->token_match = token_match;
        } else {
            sw->token_match = NULL;
        }
        sw->mgrv        = mgrv;

        return sw;
    }
fail:
    fprintf ( stderr,
              "The script command takes only one option <name>:<script>:<opts>\n"
              " c for _c_ontinous \n"
              " l for _l_abeled   (\\t as separator)\n"
        );
    script_switcher_free ( sw );
    return NULL;
}