int handled_keystate_list_cmd(int sockfd, engine_type* engine, const char *cmd,
                              ssize_t n)
{
    char buf[ODS_SE_MAXLINE];
    const char *argv[8];
    const int NARGV = sizeof(argv)/sizeof(char*);
    int argc;
    const char *scmd = "key list";

    cmd = ods_check_command(cmd,n,scmd);
    if (!cmd)
        return 0; // not handled
    
    ods_log_debug("[%s] %s command", module_str, scmd);
    
    // Use buf as an intermediate buffer for the command.
    strncpy(buf,cmd,sizeof(buf));
    buf[sizeof(buf)-1] = '\0';
    
    // separate the arguments
    argc = ods_str_explode(buf,NARGV,argv);
    if (argc > NARGV) {
        ods_log_warning("[%s] too many arguments for %s command",
                        module_str,scmd);
        ods_printf(sockfd,"too many arguments\n");
		help_keystate_list_cmd(sockfd);
        return 1; // errors, but handled
    }
    
    bool bVerbose = ods_find_arg(&argc,argv,"verbose","v") != -1;
    bool bDebug = ods_find_arg(&argc,argv,"debug","d") != -1;
    if (argc) {
        ods_log_warning("[%s] unknown arguments for %s command",
                        module_str,scmd);
        ods_printf(sockfd,"unknown arguments\n");
		help_keystate_list_cmd(sockfd);
        return 1; // errors, but handled
    }
    
    time_t tstart = time(NULL);

    perform_keystate_list(sockfd, engine->config, bVerbose, bDebug);
	
	ods_printf(sockfd,"%s completed in %ld seconds.\n",scmd,time(NULL)-tstart);
    
    return 1;
}
Exemplo n.º 2
0
static int
run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
	db_connection_t *dbconn) {
    char buf[ODS_SE_MAXLINE];
#define NARGV 12
    const char *argv[NARGV];
    int success, argIndex;
    int argc, bVerbose, bDebug, bParsable, bAll;
    char* keytypeParam;
    char* keystateParam;
    const char* filterZone; /* NULL if no filtering on zone, otherwise zone to match */
    char** filterKeytype; /* NULL if no filtering on key type, NULL terminated list of key types to filter */
    char** filterKeystate; /* NULL if no filtering on key state, NULL terminated list of key states to filter */
    (void) engine;

    ods_log_debug("[%s] %s command", module_str, key_list_funcblock()->cmdname);

    cmd = ods_check_command(cmd, n, key_list_funcblock()->cmdname);
    /* Use buf as an intermediate buffer for the command. */
    strncpy(buf, cmd, sizeof (buf));
    buf[sizeof (buf) - 1] = '\0';

    /* separate the arguments */
    argc = ods_str_explode(buf, NARGV, argv);
    if (argc > NARGV) {
        ods_log_warning("[%s] too many arguments for %s command",
                module_str, key_list_funcblock()->cmdname);
        client_printf(sockfd, "too many arguments\n");
        return -1;
    }

    bVerbose = ods_find_arg(&argc, argv, "verbose", "v") != -1;
    bDebug = ods_find_arg(&argc, argv, "debug", "d") != -1;
    bParsable = ods_find_arg(&argc, argv, "parsable", "p") != -1;
    if ((argIndex = ods_find_arg_and_param(&argc, argv, "zone", "z", &filterZone)) == -1) {
        filterZone = NULL;
    }
    if (ods_find_arg_and_param(&argc, argv, "keytype", "k", (const char **)&keytypeParam) == -1) {
        keytypeParam = NULL;
    }
    if (ods_find_arg_and_param(&argc, argv, "keystate", "e", (const char **)&keystateParam) == -1) {
        keystateParam = NULL;
    }

    bAll = (ods_find_arg(&argc, argv, "all", "a") != -1);

    if (keystateParam != NULL && bAll) {
        client_printf(sockfd, "Error: --keystate and --all option cannot be given together\n");
        return -1;
    }

    if (argc) {
        ods_log_warning("[%s] unknown arguments for %s command", module_str, key_list_funcblock()->cmdname);
        client_printf(sockfd, "unknown arguments\n");
        return -1;
    }

    if (keytypeParam)
        filterKeytype = tokenizeparam(keytypeParam);
    else
        filterKeytype = NULL;
    if (keystateParam) {
        filterKeystate = tokenizeparam(keystateParam);
    } else
        filterKeystate = NULL;
    if (bAll) {
        if (filterKeystate != NULL) {
            free(filterKeystate);
        }
        filterKeystate = NULL;
    } else if(filterKeystate == NULL) {
        if ((filterKeystate = malloc(sizeof (char*) * 6))) {
            filterKeystate[0] = (char *)"publish";
            filterKeystate[1] = (char *)"ready";
            filterKeystate[2] = (char *)"active";
            filterKeystate[3] = (char *)"retire";
            filterKeystate[4] = (char *)"mixed";
            filterKeystate[5] = NULL;
        } /* else emit error */
    }

    if (bDebug) {
        if (bParsable) {
            success = perform_keystate_list(sockfd, dbconn, filterZone, filterKeytype, filterKeystate, NULL, &printdebugparsablekey);
        } else {
            success = perform_keystate_list(sockfd, dbconn, filterZone, filterKeytype, filterKeystate, &printdebugheader, &printdebugkey);
        }
    } else if (bVerbose) {
        if (bParsable) {
            success = perform_keystate_list(sockfd, dbconn, filterZone, filterKeytype, filterKeystate, NULL, &printverboseparsablekey);
        } else {
            success = perform_keystate_list(sockfd, dbconn, filterZone, filterKeytype, filterKeystate, &printverboseheader, &printverbosekey);
        }
    } else {
        success = perform_keystate_list(sockfd, dbconn, filterZone, filterKeytype, filterKeystate, &printcompatheader, &printcompatkey);
    }

    if (filterKeytype)
        free(filterKeytype);
    if (filterKeystate)
        free(filterKeystate);
    return success;
}