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