Motr  M0
console_it.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/memory.h" /* M0_ALLOC_ARR */
24 #include "fop/fop.h" /* m0_fop */
25 
26 #include "console/console.h" /* m0_console_verbose */
27 #include "console/console_it.h"
28 #include "console/console_yaml.h"
29 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CONSOLE
31 #include "lib/trace.h"
32 
39 
40 static void depth_print(int depth)
41 {
42  static const char ruler[] = "\t\t\t\t\t\t\t\t\t\t";
44  printf("%*.*s", depth, depth, ruler);
45 }
46 
47 static void default_show(const struct m0_xcode_type *xct,
48  const char *name, void *data)
49 {
50  printf("%s:%s\n", name, xct->xct_name);
51 }
52 
53 static void void_get(const struct m0_xcode_type *xct,
54  const char *name, void *data)
55 {
56 }
57 
58 static void void_set(const struct m0_xcode_type *xct,
59  const char *name, void *data)
60 {
61 }
62 
63 static void byte_get(const struct m0_xcode_type *xct,
64  const char *name, void *data)
65 {
67  printf("%s(%s) = %s\n", name, xct->xct_name, (char *)data);
68 }
69 
70 static void *cons_yaml_get_unsafe(const char *name)
71 {
72  void *result = m0_cons_yaml_get_value(name);
73  M0_ASSERT_INFO(result != NULL, "name=`%s'", name);
74  return result;
75 }
76 
77 static void byte_set(const struct m0_xcode_type *xct,
78  const char *name, void *data)
79 {
80  void *tmp_value;
81  char value;
82 
83  if (yaml_support) {
84  tmp_value = cons_yaml_get_unsafe(name);
85  strcpy(data, tmp_value);
87  printf("%s(%s) = %s\n", name, xct->xct_name,
88  (char *)data);
89  } else {
90  printf("%s(%s) = ", name, xct->xct_name);
91  if (scanf("\r%c", &value) != EOF)
92  *(char *)data = value;
93  }
94 }
95 
96 static void u32_get(const struct m0_xcode_type *xct,
97  const char *name, void *data)
98 {
100  printf("%s(%s) = %d\n", name, xct->xct_name, *(uint32_t *)data);
101 }
102 
103 static void u32_set(const struct m0_xcode_type *xct,
104  const char *name, void *data)
105 {
106  uint32_t value;
107  void *tmp_value;
108 
109  if (yaml_support) {
110  tmp_value = cons_yaml_get_unsafe(name);
111  *(uint32_t *)data = atoi((const char *)tmp_value);
112  if (m0_console_verbose)
113  printf("%s(%s) = %u\n", name, xct->xct_name,
114  *(uint32_t *)data);
115  } else {
116  printf("%s(%s) = ", name, xct->xct_name);
117  if (scanf("%u", &value) != EOF)
118  *(uint32_t *)data = value;
119  }
120 }
121 
122 static void u64_get(const struct m0_xcode_type *xct,
123  const char *name, void *data)
124 {
125  if (m0_console_verbose)
126  printf("%s(%s) = %" PRId64 "\n", name, xct->xct_name,
127  *(uint64_t *)data);
128 }
129 
130 static void u64_set(const struct m0_xcode_type *xct,
131  const char *name, void *data)
132 {
133  void *tmp_value;
134  uint64_t value;
135 
136  if (yaml_support) {
137  tmp_value = cons_yaml_get_unsafe(name);
138  *(uint64_t *)data = atol((const char *)tmp_value);
139  if (m0_console_verbose)
140  printf("%s(%s) = %" PRId64 "\n", name, xct->xct_name,
141  *(uint64_t *)data);
142  } else {
143  printf("%s(%s) = ", name, xct->xct_name);
144  if (scanf("%"SCNi64, &value) != EOF)
145  *(uint64_t *)data = value;
146  }
147 }
148 
157 };
158 
159 static void console_xc_atom_process(struct m0_xcode_cursor_frame *top,
161 {
162  const char *name;
163  struct m0_xcode_obj *cur = &top->s_obj;
164  const struct m0_xcode_type *xt = cur->xo_type;
165  enum m0_xode_atom_type atype = xt->xct_atype;
166  const struct m0_xcode_type *pt;
167  const struct m0_xcode_obj *par;
168  const struct m0_xcode_cursor_frame *prev;
169 
170  prev = top - 1;
171  par = &prev->s_obj;
172  pt = par->xo_type;
173  name = pt->xct_child[prev->s_fieldno].xf_name;
174 
175  switch (type) {
176  case CONS_IT_INPUT:
177  atom_ops[atype].catom_val_set(xt, name, cur->xo_ptr);
178  break;
179  case CONS_IT_OUTPUT:
180  atom_ops[atype].catom_val_get(xt, name, cur->xo_ptr);
181  break;
182  case CONS_IT_SHOW:
183  /*
184  * If it's a sequence element, set count field to 1.
185  * If count field is 0, rest of the sequence element
186  * will be skipped, and we won't be able to display it.
187  */
188  if (pt->xct_aggr == M0_XA_SEQUENCE) {
189  void **ptr;
190 
191  ptr = m0_xcode_addr(par, 1, ~0ULL);
192  *(uint32_t *)ptr = 1;
193  }
194  default:
195  atom_ops[atype].catom_val_show(xt, name, cur->xo_ptr);
196  }
197 }
198 
199 static int
201 {
202  int fop_depth = 0;
203  int result;
204  bool skip_next = false;
205  struct m0_xcode_ctx ctx;
206  struct m0_xcode_cursor *it;
207 
208  M0_PRE(fop != NULL);
209 
211  it = &ctx.xcx_it;
212 
213  printf("\n");
214 
215  while((result = m0_xcode_next(it)) > 0) {
216  int rc;
217  struct m0_xcode_cursor_frame *top;
218  struct m0_xcode_obj *cur;
219  const struct m0_xcode_type *xt;
220  enum m0_xcode_aggr gtype;
221  const struct m0_xcode_obj *par;
222  const struct m0_xcode_cursor_frame *prev;
223 
225  cur = &top->s_obj;
226  xt = cur->xo_type;
227  prev = top - 1;
228  par = &prev->s_obj;
229  gtype = xt->xct_aggr;
230 
231  switch (top->s_flag) {
232  case M0_XCODE_CURSOR_PRE:
234  if (rc != 0)
235  return M0_RC(rc);
236  ++fop_depth;
237  depth_print(fop_depth);
238 
239  if (gtype != M0_XA_ATOM && m0_console_verbose)
240  printf("%s\n", xt->xct_name);
241  else if (gtype == M0_XA_ATOM)
243  break;
244  case M0_XCODE_CURSOR_IN:
245  if (gtype == M0_XA_SEQUENCE && skip_next) {
246  m0_xcode_skip(it);
247  skip_next = false;
248  --fop_depth;
249  }
250  break;
252  if (prev->s_fieldno == 1 &&
253  par->xo_type->xct_aggr == M0_XA_SEQUENCE) {
254  if (m0_xcode_tag(par) == 1)
255  skip_next = true;
256  else if (xt->xct_atype == M0_XAT_U8)
257  skip_next = true;
258  }
259  --fop_depth;
260  default:
261  break;
262  }
263  }
264  return 0;
265 }
266 
267 M0_INTERNAL int m0_cons_fop_obj_input(struct m0_fop *fop)
268 {
270 }
271 
272 M0_INTERNAL int m0_cons_fop_obj_output(struct m0_fop *fop)
273 {
275 }
276 
277 M0_INTERNAL int m0_cons_fop_fields_show(struct m0_fop *fop)
278 {
279  bool vo;
280  int rc;
281 
282  vo = m0_console_verbose;
283  m0_console_verbose = true;
285  m0_console_verbose = vo;
286 
287  return M0_RC(rc);
288 }
289 
290 #undef M0_TRACE_SUBSYSTEM
291 
294 /*
295  * Local variables:
296  * c-indentation-style: "K&R"
297  * c-basic-offset: 8
298  * tab-width: 8
299  * fill-column: 80
300  * scroll-step: 1
301  * End:
302  */
M0_INTERNAL struct m0_xcode_cursor_frame * m0_xcode_cursor_top(struct m0_xcode_cursor *it)
Definition: cursor.c:41
static void u64_set(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:130
M0_INTERNAL void * m0_xcode_addr(const struct m0_xcode_obj *obj, int fileno, uint64_t elno)
Definition: xcode.c:612
static void ptr(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:440
#define M0_PRE(cond)
const struct m0_xcode_type * xo_type
Definition: xcode.h:351
M0_EXTERN ssize_t m0_xcode_alloc_obj(struct m0_xcode_cursor *it, void *(*alloc)(struct m0_xcode_cursor *, size_t))
Definition: xcode.c:228
#define NULL
Definition: misc.h:38
static struct buffer * cur(struct m0_addb2_mach *mach, m0_bcount_t space)
Definition: addb2.c:791
void(* catom_val_get)(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.h:48
m0_xcode_aggr
Definition: xcode.h:164
struct m0_bufvec data
Definition: di.c:40
static void u32_get(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:96
static void void_get(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:53
int const char const void * value
Definition: dir.c:325
static struct m0_be_emap_cursor it
Definition: extmap.c:46
struct m0_xcode_field xct_child[0]
Definition: xcode.h:345
Definition: xcode.c:59
void(* catom_val_show)(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.h:52
static struct m0_xcode_type ** xt[]
Definition: protocol.c:64
static void depth_print(int depth)
Definition: console_it.c:40
void(* catom_val_set)(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.h:50
m0_xode_atom_type
Definition: xcode.h:231
return M0_RC(rc)
static int top
Definition: formation2.c:86
static void byte_set(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:77
static unsigned depth
Definition: base.c:377
static int cons_fop_iterate(struct m0_fop *fop, enum m0_cons_data_process_type type)
Definition: console_it.c:200
operation to get value of ATOM type (i.e. CHAR, U64 etc).
Definition: console_it.h:47
const char * name
Definition: trace.c:110
M0_INTERNAL uint64_t m0_xcode_tag(const struct m0_xcode_obj *obj)
Definition: xcode.c:679
M0_INTERNAL int m0_cons_fop_fields_show(struct m0_fop *fop)
Iterate over FOP fields and prints the names.
Definition: console_it.c:277
enum m0_xode_atom_type xct_atype
Definition: xcode.h:326
#define SCNi64
Definition: types.h:63
M0_INTERNAL int m0_xcode_next(struct m0_xcode_cursor *it)
Definition: cursor.c:59
M0_INTERNAL int m0_cons_fop_obj_output(struct m0_fop *fop)
Helper function for FOP output.
Definition: console_it.c:272
M0_INTERNAL void * m0_xcode_alloc(struct m0_xcode_cursor *it, size_t nob)
Definition: xcode.c:444
static void console_xc_atom_process(struct m0_xcode_cursor_frame *top, enum m0_cons_data_process_type type)
Definition: console_it.c:159
#define PRId64
Definition: types.h:57
M0_INTERNAL int m0_cons_fop_obj_input(struct m0_fop *fop)
Helper function for FOP input.
Definition: console_it.c:267
enum m0_xcode_aggr xct_aggr
Definition: xcode.h:316
static struct m0_cons_atom_ops atom_ops[M0_XAT_NR]
Methods to handle U64, U32 etc.
Definition: console_it.c:152
M0_INTERNAL void * m0_cons_yaml_get_value(const char *name)
Search for specified string and set the respctive value form YAML file. (like "name : console") ...
Definition: console_yaml.c:177
static struct m0_fop * fop
Definition: item.c:57
static void byte_get(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:63
static void void_set(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:58
M0_INTERNAL void m0_xcode_skip(struct m0_xcode_cursor *it)
Definition: cursor.c:148
static void * cons_yaml_get_unsafe(const char *name)
Definition: console_it.c:70
static void default_show(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:47
M0_INTERNAL bool yaml_support
Definition: console_yaml.c:92
static void u32_set(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:103
#define M0_ASSERT_INFO(cond, fmt,...)
Definition: nucleus.c:42
M0_INTERNAL void m0_xcode_ctx_init(struct m0_xcode_ctx *ctx, const struct m0_xcode_obj *obj)
Definition: xcode.c:373
int type
Definition: dir.c:1031
const char * xf_name
Definition: xcode.h:251
#define M0_FOP_XCODE_OBJ(f)
Definition: fop.h:334
static void u64_get(const struct m0_xcode_type *xct, const char *name, void *data)
Definition: console_it.c:122
int32_t rc
Definition: trigger_fop.h:47
const char * xct_name
Definition: xcode.h:318
Definition: fop.h:79
m0_cons_data_process_type
Definition: console_it.h:36
bool m0_console_verbose
Definition: console_it.c:38