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_t profile)23 do_batchmode(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 int
main(int argc,char ** argv)112 main(int argc, char **argv)
113 {
114 profile_t profile;
115 long retval;
116 char **values, *value, **cpp;
117 const char **names;
118 char *cmd;
119 int print_value = 0;
120
121 if (argc < 2) {
122 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
123 exit(1);
124 }
125
126 initialize_prof_error_table();
127
128 retval = profile_init_path(argv[1], &profile);
129 if (retval) {
130 com_err(program_name, retval, "while initializing profile");
131 exit(1);
132 }
133 cmd = *(argv+2);
134 names = (const char **) argv+3;
135 if (!cmd || !strcmp(cmd, "batch"))
136 do_batchmode(profile);
137 if (!strcmp(cmd, "query")) {
138 retval = profile_get_values(profile, names, &values);
139 } else if (!strcmp(cmd, "query1")) {
140 retval = profile_get_value(profile, names, &value);
141 print_value++;
142 } else if (!strcmp(cmd, "list_sections")) {
143 retval = profile_get_subsection_names(profile, names, &values);
144 } else if (!strcmp(cmd, "list_relations")) {
145 retval = profile_get_relation_names(profile, names, &values);
146 } else {
147 fprintf(stderr, "Invalid command.\n");
148 exit(1);
149 }
150 if (retval) {
151 com_err(argv[0], retval, "while getting values");
152 profile_release(profile);
153 exit(1);
154 }
155 if (print_value) {
156 printf("%s\n", value);
157 } else {
158 for (cpp = values; *cpp; cpp++)
159 printf("%s\n", *cpp);
160 profile_free_list(values);
161 }
162 profile_release(profile);
163
164 return 0;
165 }
166