[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[216d6fc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
[216d6fc] | 8 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 16 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <stdio.h>
|
---|
| 30 | #include <stdlib.h>
|
---|
[582a0b8] | 31 | #include <stddef.h>
|
---|
[19f857a] | 32 | #include <str.h>
|
---|
[216d6fc] | 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;
|
---|
[6ea9a1d] | 42 | static iostate_t *iostate;
|
---|
| 43 | static iostate_t stdiostate;
|
---|
[216d6fc] | 44 |
|
---|
[7c3fb9b] | 45 | /*
|
---|
| 46 | * Globals that are modified during start-up that modules/builtins
|
---|
| 47 | * should be aware of.
|
---|
| 48 | */
|
---|
[e2ea8d7e] | 49 | volatile unsigned int cli_quit = 0;
|
---|
[216d6fc] | 50 | volatile unsigned int cli_verbocity = 1;
|
---|
| 51 |
|
---|
[7c3fb9b] | 52 | /*
|
---|
| 53 | * The official name of this program
|
---|
| 54 | * (change to your liking in configure.ac and re-run autoconf)
|
---|
| 55 | */
|
---|
[216d6fc] | 56 | const char *progname = PACKAGE_NAME;
|
---|
| 57 |
|
---|
[e2ea8d7e] | 58 | /* These are not exposed, even to builtins */
|
---|
[901e827] | 59 | static int cli_init(cliuser_t *);
|
---|
| 60 | static void cli_finit(cliuser_t *);
|
---|
[e2ea8d7e] | 61 |
|
---|
| 62 | /* Constructor */
|
---|
| 63 | static int cli_init(cliuser_t *usr)
|
---|
[216d6fc] | 64 | {
|
---|
| 65 | usr->line = (char *) NULL;
|
---|
| 66 | usr->name = "root";
|
---|
| 67 | usr->cwd = (char *) NULL;
|
---|
| 68 | usr->prompt = (char *) NULL;
|
---|
| 69 | usr->lasterr = 0;
|
---|
[da2bd08] | 70 |
|
---|
[36a75a2] | 71 | if (input_init() != 0)
|
---|
| 72 | return 1;
|
---|
[da2bd08] | 73 |
|
---|
[216d6fc] | 74 | return (int) cli_set_prompt(usr);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /* Destructor */
|
---|
[e2ea8d7e] | 78 | static void cli_finit(cliuser_t *usr)
|
---|
[216d6fc] | 79 | {
|
---|
| 80 | if (NULL != usr->line)
|
---|
| 81 | free(usr->line);
|
---|
| 82 | if (NULL != usr->prompt)
|
---|
| 83 | free(usr->prompt);
|
---|
| 84 | if (NULL != usr->cwd)
|
---|
| 85 | free(usr->cwd);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[6ea9a1d] | 88 | iostate_t *get_iostate(void)
|
---|
| 89 | {
|
---|
| 90 | return iostate;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | void set_iostate(iostate_t *ios)
|
---|
| 94 | {
|
---|
| 95 | iostate = ios;
|
---|
| 96 | stdin = ios->stdin;
|
---|
| 97 | stdout = ios->stdout;
|
---|
| 98 | stderr = ios->stderr;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[216d6fc] | 101 | int main(int argc, char *argv[])
|
---|
| 102 | {
|
---|
[b7fd2a0] | 103 | errno_t ret = 0;
|
---|
[a35b458] | 104 |
|
---|
[6ea9a1d] | 105 | stdiostate.stdin = stdin;
|
---|
| 106 | stdiostate.stdout = stdout;
|
---|
| 107 | stdiostate.stderr = stderr;
|
---|
| 108 | iostate = &stdiostate;
|
---|
[216d6fc] | 109 |
|
---|
| 110 | if (cli_init(&usr))
|
---|
| 111 | exit(EXIT_FAILURE);
|
---|
| 112 |
|
---|
| 113 | while (!cli_quit) {
|
---|
| 114 | get_input(&usr);
|
---|
| 115 | if (NULL != usr.line) {
|
---|
[28ee877e] | 116 | ret = process_input(&usr);
|
---|
[0320823] | 117 | cli_set_prompt(&usr);
|
---|
[216d6fc] | 118 | usr.lasterr = ret;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[5db9084] | 122 | printf("Leaving %s.\n", progname);
|
---|
| 123 |
|
---|
[216d6fc] | 124 | cli_finit(&usr);
|
---|
| 125 | return ret;
|
---|
| 126 | }
|
---|