1 | /*
|
---|
2 | * Copyright (c) 2008 Tim Post
|
---|
3 | * Copyright (c) 2018 Matthieu Riolo
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <stddef.h>
|
---|
33 | #include <str.h>
|
---|
34 | #include <adt/odict.h>
|
---|
35 | #include "config.h"
|
---|
36 | #include "scli.h"
|
---|
37 | #include "input.h"
|
---|
38 | #include "util.h"
|
---|
39 | #include "errors.h"
|
---|
40 | #include "cmds/cmds.h"
|
---|
41 |
|
---|
42 | /* See scli.h */
|
---|
43 | static cliuser_t usr;
|
---|
44 | static iostate_t *iostate;
|
---|
45 | static iostate_t stdiostate;
|
---|
46 |
|
---|
47 | odict_t alias_dict;
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Globals that are modified during start-up that modules/builtins
|
---|
51 | * should be aware of.
|
---|
52 | */
|
---|
53 | volatile unsigned int cli_quit = 0;
|
---|
54 | volatile unsigned int cli_verbocity = 1;
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * The official name of this program
|
---|
58 | * (change to your liking in configure.ac and re-run autoconf)
|
---|
59 | */
|
---|
60 | const char *progname = PACKAGE_NAME;
|
---|
61 |
|
---|
62 | static int alias_cmp(void *a, void *b)
|
---|
63 | {
|
---|
64 | return str_cmp((char *)a, (char *)b);
|
---|
65 | }
|
---|
66 |
|
---|
67 | static void *alias_key(odlink_t *odlink)
|
---|
68 | {
|
---|
69 | return (void *)odict_get_instance(odlink, alias_t, odict)->name;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* These are not exposed, even to builtins */
|
---|
73 | static int cli_init(cliuser_t *);
|
---|
74 | static void cli_finit(cliuser_t *);
|
---|
75 |
|
---|
76 | /* Constructor */
|
---|
77 | static int cli_init(cliuser_t *usr)
|
---|
78 | {
|
---|
79 | usr->line = (char *) NULL;
|
---|
80 | usr->name = "root";
|
---|
81 | usr->cwd = (char *) NULL;
|
---|
82 | usr->prompt = (char *) NULL;
|
---|
83 | usr->lasterr = 0;
|
---|
84 |
|
---|
85 | if (input_init() != 0)
|
---|
86 | return 1;
|
---|
87 |
|
---|
88 | return (int) cli_set_prompt(usr);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* Destructor */
|
---|
92 | static void cli_finit(cliuser_t *usr)
|
---|
93 | {
|
---|
94 | if (NULL != usr->line)
|
---|
95 | free(usr->line);
|
---|
96 | if (NULL != usr->prompt)
|
---|
97 | free(usr->prompt);
|
---|
98 | if (NULL != usr->cwd)
|
---|
99 | free(usr->cwd);
|
---|
100 | }
|
---|
101 |
|
---|
102 | iostate_t *get_iostate(void)
|
---|
103 | {
|
---|
104 | return iostate;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void set_iostate(iostate_t *ios)
|
---|
108 | {
|
---|
109 | iostate = ios;
|
---|
110 | stdin = ios->stdin;
|
---|
111 | stdout = ios->stdout;
|
---|
112 | stderr = ios->stderr;
|
---|
113 | }
|
---|
114 |
|
---|
115 | int main(int argc, char *argv[])
|
---|
116 | {
|
---|
117 | errno_t ret = 0;
|
---|
118 |
|
---|
119 | stdiostate.stdin = stdin;
|
---|
120 | stdiostate.stdout = stdout;
|
---|
121 | stdiostate.stderr = stderr;
|
---|
122 | iostate = &stdiostate;
|
---|
123 |
|
---|
124 | odict_initialize(&alias_dict, alias_key, alias_cmp);
|
---|
125 |
|
---|
126 | if (cli_init(&usr))
|
---|
127 | exit(EXIT_FAILURE);
|
---|
128 |
|
---|
129 | while (!cli_quit) {
|
---|
130 | get_input(&usr);
|
---|
131 | if (NULL != usr.line) {
|
---|
132 | ret = process_input(&usr);
|
---|
133 | cli_set_prompt(&usr);
|
---|
134 | usr.lasterr = ret;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | printf("Leaving %s.\n", progname);
|
---|
139 |
|
---|
140 | cli_finit(&usr);
|
---|
141 | return ret;
|
---|
142 | }
|
---|