1 /*
2  * generic CLI test helper functions
3  *
4  * Copyright (C) 2015 by David Lamparter,
5  *                   for Open Source Routing / NetDEF, Inc.
6  *
7  * Quagga is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * Quagga is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; see the file COPYING; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <zebra.h>
23 
24 #include "thread.h"
25 #include "vty.h"
26 #include "command.h"
27 #include "memory.h"
28 #include "lib_vty.h"
29 #include "log.h"
30 
31 #include "common_cli.h"
32 
33 struct thread_master *master;
34 
dump_args(struct vty * vty,const char * descr,int argc,struct cmd_token * argv[])35 int dump_args(struct vty *vty, const char *descr, int argc,
36 	      struct cmd_token *argv[])
37 {
38 	int i;
39 	vty_out(vty, "%s with %d args.\n", descr, argc);
40 	for (i = 0; i < argc; i++) {
41 		vty_out(vty, "[%02d] %s@%s: %s\n", i, argv[i]->text,
42 			argv[i]->varname, argv[i]->arg);
43 	}
44 
45 	return CMD_SUCCESS;
46 }
47 
vty_do_exit(int isexit)48 static void vty_do_exit(int isexit)
49 {
50 	printf("\nend.\n");
51 	cmd_terminate();
52 	vty_terminate();
53 	nb_terminate();
54 	yang_terminate();
55 	thread_master_free(master);
56 
57 	log_memstats(stderr, "testcli");
58 	if (!isexit)
59 		exit(0);
60 }
61 
62 /* main routine. */
main(int argc,char ** argv)63 int main(int argc, char **argv)
64 {
65 	struct thread thread;
66 
67 	/* Set umask before anything for security */
68 	umask(0027);
69 
70 	/* master init. */
71 	master = thread_master_create(NULL);
72 
73 	zlog_aux_init("NONE: ", ZLOG_DISABLED);
74 
75 	/* Library inits. */
76 	cmd_init(1);
77 	cmd_hostname_set("test");
78 	cmd_domainname_set("test.domain");
79 
80 	vty_init(master, false);
81 	lib_cmd_init();
82 	yang_init(true);
83 	nb_init(master, NULL, 0, false);
84 
85 	test_init(argc, argv);
86 
87 	vty_stdio(vty_do_exit);
88 
89 	/* Fetch next active thread. */
90 	while (thread_fetch(master, &thread))
91 		thread_call(&thread);
92 
93 	/* Not reached. */
94 	exit(0);
95 }
96