Path: blob/main/crypto/krb5/src/util/profile/test_profile.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* test_profile.c --- testing program for the profile routine3*/45#include "prof_int.h"67#include <stdio.h>8#include <string.h>9#ifdef HAVE_STDLIB_H10#include <stdlib.h>11#endif1213#include "argv_parse.h"14#include "com_err.h"1516const char *program_name = "test_profile";1718#define PRINT_VALUE 119#define PRINT_VALUES 22021static void22do_batchmode(profile_t profile)23{24errcode_t retval;25int argc, ret;26char **argv, **values, *value, **cpp;27char buf[256];28const char **names, *name;29char *cmd;30int print_status;3132while (!feof(stdin)) {33if (fgets(buf, sizeof(buf), stdin) == NULL)34break;35printf(">%s", buf);36ret = argv_parse(buf, &argc, &argv);37if (ret != 0) {38printf("Argv_parse returned %d!\n", ret);39continue;40}41cmd = *(argv);42names = (const char **) argv + 1;43print_status = 0;44retval = 0;45if (cmd == 0) {46argv_free(argv);47continue;48}49if (!strcmp(cmd, "query")) {50retval = profile_get_values(profile, names, &values);51print_status = PRINT_VALUES;52} else if (!strcmp(cmd, "query1")) {53retval = profile_get_value(profile, names, &value);54print_status = PRINT_VALUE;55} else if (!strcmp(cmd, "list_sections")) {56retval = profile_get_subsection_names(profile, names,57&values);58print_status = PRINT_VALUES;59} else if (!strcmp(cmd, "list_relations")) {60retval = profile_get_relation_names(profile, names,61&values);62print_status = PRINT_VALUES;63} else if (!strcmp(cmd, "dump")) {64retval = profile_write_tree_file65(profile->first_file->data->root, stdout);66} else if (!strcmp(cmd, "clear")) {67retval = profile_clear_relation(profile, names);68} else if (!strcmp(cmd, "update")) {69retval = profile_update_relation(profile, names+2,70*names, *(names+1));71} else if (!strcmp(cmd, "verify")) {72retval = profile_verify_node73(profile->first_file->data->root);74} else if (!strcmp(cmd, "rename_section")) {75retval = profile_rename_section(profile, names+1,76*names);77} else if (!strcmp(cmd, "add")) {78name = *names;79if (strcmp(name, "NULL") == 0)80name = NULL;81retval = profile_add_relation(profile, names+1, name);82} else if (!strcmp(cmd, "flush")) {83retval = profile_flush(profile);84} else {85printf("Invalid command.\n");86}87if (retval) {88com_err(cmd, retval, "");89print_status = 0;90}91switch (print_status) {92case PRINT_VALUE:93printf("%s\n", value);94profile_release_string(value);95break;96case PRINT_VALUES:97for (cpp = values; *cpp; cpp++)98printf("%s\n", *cpp);99profile_free_list(values);100break;101}102printf("\n");103argv_free(argv);104}105profile_release(profile);106exit(0);107108}109110int111main(int argc, char **argv)112{113profile_t profile;114long retval;115char **values, *value, **cpp;116const char **names;117char *cmd;118int print_value = 0;119120if (argc < 2) {121fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);122exit(1);123}124125initialize_prof_error_table();126127retval = profile_init_path(argv[1], &profile);128if (retval) {129com_err(program_name, retval, "while initializing profile");130exit(1);131}132cmd = *(argv+2);133names = (const char **) argv+3;134if (!cmd || !strcmp(cmd, "batch"))135do_batchmode(profile);136if (!strcmp(cmd, "query")) {137retval = profile_get_values(profile, names, &values);138} else if (!strcmp(cmd, "query1")) {139retval = profile_get_value(profile, names, &value);140print_value++;141} else if (!strcmp(cmd, "list_sections")) {142retval = profile_get_subsection_names(profile, names, &values);143} else if (!strcmp(cmd, "list_relations")) {144retval = profile_get_relation_names(profile, names, &values);145} else {146fprintf(stderr, "Invalid command.\n");147exit(1);148}149if (retval) {150com_err(argv[0], retval, "while getting values");151profile_release(profile);152exit(1);153}154if (print_value) {155printf("%s\n", value);156} else {157for (cpp = values; *cpp; cpp++)158printf("%s\n", *cpp);159profile_free_list(values);160}161profile_release(profile);162163return 0;164}165166167