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