[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 |
|
---|
[e2ea8d7e] | 45 | /* Globals that are modified during start-up that modules/builtins
|
---|
| 46 | * should be aware of. */
|
---|
| 47 | volatile unsigned int cli_quit = 0;
|
---|
[216d6fc] | 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 |
|
---|
[e2ea8d7e] | 54 | /* These are not exposed, even to builtins */
|
---|
[901e827] | 55 | static int cli_init(cliuser_t *);
|
---|
| 56 | static void cli_finit(cliuser_t *);
|
---|
[e2ea8d7e] | 57 |
|
---|
| 58 | /* Constructor */
|
---|
| 59 | static int cli_init(cliuser_t *usr)
|
---|
[216d6fc] | 60 | {
|
---|
| 61 | usr->line = (char *) NULL;
|
---|
| 62 | usr->name = "root";
|
---|
| 63 | usr->cwd = (char *) NULL;
|
---|
| 64 | usr->prompt = (char *) NULL;
|
---|
| 65 | usr->lasterr = 0;
|
---|
[da2bd08] | 66 |
|
---|
[36a75a2] | 67 | if (input_init() != 0)
|
---|
| 68 | return 1;
|
---|
[da2bd08] | 69 |
|
---|
[216d6fc] | 70 | return (int) cli_set_prompt(usr);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /* Destructor */
|
---|
[e2ea8d7e] | 74 | static void cli_finit(cliuser_t *usr)
|
---|
[216d6fc] | 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 |
|
---|
[6ea9a1d] | 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 |
|
---|
[216d6fc] | 98 | int main(int argc, char *argv[])
|
---|
| 99 | {
|
---|
[b7fd2a0] | 100 | errno_t ret = 0;
|
---|
[6ea9a1d] | 101 |
|
---|
| 102 | stdiostate.stdin = stdin;
|
---|
| 103 | stdiostate.stdout = stdout;
|
---|
| 104 | stdiostate.stderr = stderr;
|
---|
| 105 | iostate = &stdiostate;
|
---|
[216d6fc] | 106 |
|
---|
| 107 | if (cli_init(&usr))
|
---|
| 108 | exit(EXIT_FAILURE);
|
---|
| 109 |
|
---|
| 110 | while (!cli_quit) {
|
---|
| 111 | get_input(&usr);
|
---|
| 112 | if (NULL != usr.line) {
|
---|
[28ee877e] | 113 | ret = process_input(&usr);
|
---|
[0320823] | 114 | cli_set_prompt(&usr);
|
---|
[216d6fc] | 115 | usr.lasterr = ret;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[5db9084] | 119 | printf("Leaving %s.\n", progname);
|
---|
| 120 |
|
---|
[216d6fc] | 121 | cli_finit(&usr);
|
---|
| 122 | return ret;
|
---|
| 123 | }
|
---|