[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[63087f1d] | 3 | * Copyright (c) 2018 Matthieu Riolo
|
---|
[216d6fc] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
[216d6fc] | 9 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 17 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 28 | */
|
---|
| 29 |
|
---|
| 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
[582a0b8] | 32 | #include <stddef.h>
|
---|
[19f857a] | 33 | #include <str.h>
|
---|
[60c332e] | 34 | #include <adt/odict.h>
|
---|
[216d6fc] | 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;
|
---|
[6ea9a1d] | 44 | static iostate_t *iostate;
|
---|
| 45 | static iostate_t stdiostate;
|
---|
[216d6fc] | 46 |
|
---|
[60c332e] | 47 | odict_t alias_dict;
|
---|
| 48 |
|
---|
[7c3fb9b] | 49 | /*
|
---|
| 50 | * Globals that are modified during start-up that modules/builtins
|
---|
| 51 | * should be aware of.
|
---|
| 52 | */
|
---|
[e2ea8d7e] | 53 | volatile unsigned int cli_quit = 0;
|
---|
[216d6fc] | 54 | volatile unsigned int cli_verbocity = 1;
|
---|
| 55 |
|
---|
[7c3fb9b] | 56 | /*
|
---|
| 57 | * The official name of this program
|
---|
| 58 | * (change to your liking in configure.ac and re-run autoconf)
|
---|
| 59 | */
|
---|
[216d6fc] | 60 | const char *progname = PACKAGE_NAME;
|
---|
| 61 |
|
---|
[a02aa5c] | 62 | static int alias_cmp(void *a, void *b)
|
---|
[4bf08aa5] | 63 | {
|
---|
[a02aa5c] | 64 | return str_cmp((char *)a, (char *)b);
|
---|
[60c332e] | 65 | }
|
---|
| 66 |
|
---|
[a02aa5c] | 67 | static void *alias_key(odlink_t *odlink)
|
---|
[4bf08aa5] | 68 | {
|
---|
[a02aa5c] | 69 | return (void *)odict_get_instance(odlink, alias_t, odict)->name;
|
---|
[4bf08aa5] | 70 | }
|
---|
[60c332e] | 71 |
|
---|
[e2ea8d7e] | 72 | /* These are not exposed, even to builtins */
|
---|
[901e827] | 73 | static int cli_init(cliuser_t *);
|
---|
| 74 | static void cli_finit(cliuser_t *);
|
---|
[e2ea8d7e] | 75 |
|
---|
| 76 | /* Constructor */
|
---|
| 77 | static int cli_init(cliuser_t *usr)
|
---|
[216d6fc] | 78 | {
|
---|
| 79 | usr->line = (char *) NULL;
|
---|
| 80 | usr->name = "root";
|
---|
| 81 | usr->cwd = (char *) NULL;
|
---|
| 82 | usr->prompt = (char *) NULL;
|
---|
| 83 | usr->lasterr = 0;
|
---|
[da2bd08] | 84 |
|
---|
[36a75a2] | 85 | if (input_init() != 0)
|
---|
| 86 | return 1;
|
---|
[da2bd08] | 87 |
|
---|
[216d6fc] | 88 | return (int) cli_set_prompt(usr);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /* Destructor */
|
---|
[e2ea8d7e] | 92 | static void cli_finit(cliuser_t *usr)
|
---|
[216d6fc] | 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 |
|
---|
[6ea9a1d] | 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 |
|
---|
[216d6fc] | 115 | int main(int argc, char *argv[])
|
---|
| 116 | {
|
---|
[b7fd2a0] | 117 | errno_t ret = 0;
|
---|
[a35b458] | 118 |
|
---|
[6ea9a1d] | 119 | stdiostate.stdin = stdin;
|
---|
| 120 | stdiostate.stdout = stdout;
|
---|
| 121 | stdiostate.stderr = stderr;
|
---|
| 122 | iostate = &stdiostate;
|
---|
[216d6fc] | 123 |
|
---|
[60c332e] | 124 | odict_initialize(&alias_dict, alias_key, alias_cmp);
|
---|
| 125 |
|
---|
[216d6fc] | 126 | if (cli_init(&usr))
|
---|
| 127 | exit(EXIT_FAILURE);
|
---|
| 128 |
|
---|
| 129 | while (!cli_quit) {
|
---|
| 130 | get_input(&usr);
|
---|
| 131 | if (NULL != usr.line) {
|
---|
[28ee877e] | 132 | ret = process_input(&usr);
|
---|
[0320823] | 133 | cli_set_prompt(&usr);
|
---|
[216d6fc] | 134 | usr.lasterr = ret;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[5db9084] | 138 | printf("Leaving %s.\n", progname);
|
---|
| 139 |
|
---|
[216d6fc] | 140 | cli_finit(&usr);
|
---|
| 141 | return ret;
|
---|
| 142 | }
|
---|