bool queryCommand_execute()

in bundles/shell/shell/src/query_command.c [171:247]


bool queryCommand_execute(void *_ptr, const char *command_line_str, FILE *sout, FILE *serr) {
    bundle_context_t* ctx = _ptr;

    char *commandLine = celix_utils_strdup(command_line_str); //note command_line_str should be treated as const.

    struct query_options opts;
    memset(&opts, 0, sizeof(opts));
    opts.bndId = -1L;
    opts.queryProvided = true;
    opts.queryRequested = true;

    opts.nameQueries = celix_arrayList_create();
    opts.filterQueries = celix_arrayList_create();
    opts.useColors = celix_bundleContext_getPropertyAsBool(ctx, CELIX_SHELL_USE_ANSI_COLORS, CELIX_SHELL_USE_ANSI_COLORS_DEFAULT_VALUE);

    bool validCommand = true;
    char *sub_str = NULL;
    char *save_ptr = NULL;

    strtok_r(commandLine, CELIX_SHELL_COMMAND_SEPARATOR, &save_ptr);
    sub_str = strtok_r(NULL, CELIX_SHELL_COMMAND_SEPARATOR, &save_ptr);
    while (sub_str != NULL) {
        if (strcmp(sub_str, "-v") == 0) {
            opts.verbose = true;
        } else if (strcmp(sub_str, "-p") == 0) {
            opts.queryProvided = true;
            opts.queryRequested = false;
        } else if (strcmp(sub_str, "-r") == 0) {
            opts.queryProvided = false;
            opts.queryRequested = true;
        } else {
            //check if its a number (bundle id)
            errno = 0;
            long bndId = strtol(sub_str, NULL, 10);
            if (bndId >= CELIX_FRAMEWORK_BUNDLE_ID && isdigit(sub_str[0]) && errno == 0 /*not EINVAL*/) {
                opts.bndId = bndId;
            } else {
                //not option and not a bundle id -> query
                if (strnlen(sub_str, 16) > 1 && sub_str[0] == '(') {
                    //assume this is a filter.
                    celix_filter_t *filter = celix_filter_create(sub_str);
                    if (filter != NULL) {
                        celix_arrayList_add(opts.filterQueries, filter);
                    } else {
                        validCommand = false;
                        fprintf(serr, "Cannot parse provided filter '%s'!\n", sub_str);
                        break;
                    }
                } else {
                    celix_arrayList_add(opts.nameQueries, celix_utils_strdup(sub_str));
                }

            }
        }
        sub_str = strtok_r(NULL, CELIX_SHELL_COMMAND_SEPARATOR, &save_ptr);
    }

    free(commandLine);

    if (validCommand) {
        queryCommand_listServices(ctx, &opts, sout, serr);
    }

    for (int i = 0; i < celix_arrayList_size(opts.nameQueries); ++i) {
        char *name = celix_arrayList_get(opts.nameQueries, i);
        free(name);
    }
    celix_arrayList_destroy(opts.nameQueries);

    for (int i = 0; i < celix_arrayList_size(opts.filterQueries); ++i) {
        celix_filter_t *filter = celix_arrayList_get(opts.filterQueries, i);
        celix_filter_destroy(filter);
    }
    celix_arrayList_destroy(opts.filterQueries);

    return validCommand;
}