Beispiel #1
0
int cmd_feature(const char* arg) {
	char* argv, *ptr, *state_str, *saveptr;
	int i, ret = 0;
	struct model_list* list;
	struct data_model* model;

	if (strlen(arg) < 8) {
		cmd_feature_help();
		return 1;
	}

	argv = strdupa(arg + strlen("feature "));

	ptr = strtok_r(argv, " ", &saveptr);
	if (ptr == NULL) {
		cmd_feature_help();
		return 1;
	}

	list = find_model(ptr);
	if (list == NULL) {
		nc_verb_error("No model \"%s\" found", ptr);
		return 1;
	}
	model = list->model;

	ptr = strtok_r(NULL, " ", &saveptr);

	/* we are done, no more arguments */
	if (ptr == NULL) {
		printf("Features:\n");
		if (model->features == NULL) {
			printf("\tnone\n");
			return 0;
		}
		for (i = 0; model->features[i] != NULL; ++i) {
			printf("\t%s %s\n", model->features[i]->name, (model->features[i]->enabled ? "ON" : "OFF"));
		}
		return 0;
	}

	do {
		state_str = strtok_r(NULL, " ", &saveptr);
	} while (state_str != NULL && strcmp(state_str, "on") != 0 && strcmp(state_str, "off") != 0);
	/* there was no "yes" or "no" at the end */
	if (state_str == NULL) {
		cmd_feature_help();
		return 1;
	}

	if (model->features == NULL) {
		nc_verb_error("Model does not have any features");
		return 1;
	}

	/* all features */
	if (strcmp(ptr, "*") == 0) {
		for (i = 0; model->features[i] != NULL; ++i) {
			if (strcmp(state_str, "on") == 0) {
				model->features[i]->enabled = 1;
			} else {
				model->features[i]->enabled = 0;
			}
		}
	} else {
		/* one or more features */
		ptr = argv + strlen(argv)+1;
		while (ptr != state_str) {
			for (i = 0; model->features[i] != NULL; ++i) {
				if (strcmp(model->features[i]->name, ptr) == 0) {
					if ((model->features[i]->enabled && strcmp(state_str, "on") == 0) || (!model->features[i]->enabled && strcmp(state_str, "off") == 0)) {
						nc_verb_verbose("Feature \"%s\" is already %s", ptr, state_str);
					} else if (strcmp(state_str, "on") == 0) {
						model->features[i]->enabled = 1;
					} else {
						model->features[i]->enabled = 0;
					}
					break;
				}
			}

			if (model->features[i] == NULL) {
				nc_verb_error("Model does not have the feature \"%s\"", ptr);
				ret = 1;
			}

			ptr = ptr + strlen(ptr)+1;
		}
	}

	return ret;
}
Beispiel #2
0
int
cmd_feature(const char *arg)
{
    int c, i, argc, option_index, ret = 1, task = -1;
    char **argv = NULL, *ptr, **names, **enable_state;
    const char *feat_name = NULL;
    struct ly_module *model, *parent_model;
    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"print", no_argument, 0, 'p'},
        {"enable", required_argument, 0, 'e'},
        {"disable", required_argument, 0, 'd'},
        {NULL, 0, 0, 0}
    };

    argc = 1;
    argv = malloc(2*sizeof *argv);
    *argv = strdup(arg);
    ptr = strtok(*argv, " ");
    while ((ptr = strtok(NULL, " "))) {
        argv = realloc(argv, (argc+2)*sizeof *argv);
        argv[argc++] = ptr;
    }
    argv[argc] = NULL;

    optind = 0;
    while (1) {
        option_index = 0;
        c = getopt_long(argc, argv, "hpe:d:", long_options, &option_index);
        if (c == -1) {
            break;
        }

        switch (c) {
        case 'h':
            cmd_feature_help();
            ret = 0;
            goto cleanup;
        case 'p':
            if (task != -1) {
                fprintf(stderr, "Only one of print, enable, or disable can be specified.\n");
                goto cleanup;
            }
            task = 0;
            break;
        case 'e':
            if (task != -1) {
                fprintf(stderr, "Only one of print, enable, or disable can be specified.\n");
                goto cleanup;
            }
            task = 1;
            feat_name = optarg;
            break;
        case 'd':
            if (task != -1) {
                fprintf(stderr, "Only one of print, enable, or disable can be specified.\n");
                goto cleanup;
            }
            task = 2;
            feat_name = optarg;
            break;
        case '?':
            fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
            goto cleanup;
        }
    }

    /* model name */
    if (optind == argc) {
        fprintf(stderr, "Missing the model name.\n");
        goto cleanup;
    }
    model = ly_ctx_get_module(ctx, argv[optind], NULL, 0);
    if (model == NULL) {
        names = ly_ctx_get_module_names(ctx);
        for (i = 0; names[i]; i++) {
            if (!model) {
                parent_model = ly_ctx_get_module(ctx, names[i], NULL, 0);
                model = (struct ly_module *)ly_ctx_get_submodule(parent_model, argv[optind], NULL, 0);
            }
            free(names[i]);
        }
        free(names);
    }
    if (model == NULL) {
        fprintf(stderr, "No model \"%s\" found.\n", argv[optind]);
        goto cleanup;
    }

    if (task == -1) {
        fprintf(stderr, "One of print, enable, or disable must be specified.\n");
        goto cleanup;
    }

    if (task == 0) {
        printf("%s features:\n", model->name);

        names = ly_get_features(model, &enable_state);
        for (i = 0; names[i]; ++i) {
            printf("\t%s %s\n", names[i], enable_state[i]);
            free(names[i]);
            free(enable_state[i]);
        }
        free(names);
        free(enable_state);
        if (!i) {
            printf("\t(none)\n");
        }
    } else if (task == 1) {
        if (ly_features_enable(model, feat_name)) {
            fprintf(stderr, "Feature \"%s\" not found.\n", feat_name);
            ret = 1;
        }
    } else if (task == 2) {
        if (ly_features_disable(model, feat_name)) {
            fprintf(stderr, "Feature \"%s\" not found.\n", feat_name);
            ret = 1;
        }
    }

cleanup:
    free(*argv);
    free(argv);

    return ret;
}