source: mainline/uspace/app/bdsh/scli.c@ 94619b9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 94619b9 was 94619b9, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

removing unnecessary newlines

  • Property mode set to 100644
File size: 3.5 KB
Line 
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 */
43static cliuser_t usr;
44static iostate_t *iostate;
45static iostate_t stdiostate;
46
47
48odict_t alias_dict;
49
50/*
51 * Globals that are modified during start-up that modules/builtins
52 * should be aware of.
53 */
54volatile unsigned int cli_quit = 0;
55volatile unsigned int cli_verbocity = 1;
56
57/*
58 * The official name of this program
59 * (change to your liking in configure.ac and re-run autoconf)
60 */
61const char *progname = PACKAGE_NAME;
62
63static int alias_cmp(void* a, void* b)
64{
65 return str_cmp((char*)a, (char*)b);
66}
67
68static void* alias_key(odlink_t *odlink)
69{
70 return (void*)odict_get_instance(odlink, alias_t, odict)->name;
71}
72
73/* These are not exposed, even to builtins */
74static int cli_init(cliuser_t *);
75static void cli_finit(cliuser_t *);
76
77/* Constructor */
78static int cli_init(cliuser_t *usr)
79{
80 usr->line = (char *) NULL;
81 usr->name = "root";
82 usr->cwd = (char *) NULL;
83 usr->prompt = (char *) NULL;
84 usr->lasterr = 0;
85
86 if (input_init() != 0)
87 return 1;
88
89 return (int) cli_set_prompt(usr);
90}
91
92/* Destructor */
93static void cli_finit(cliuser_t *usr)
94{
95 if (NULL != usr->line)
96 free(usr->line);
97 if (NULL != usr->prompt)
98 free(usr->prompt);
99 if (NULL != usr->cwd)
100 free(usr->cwd);
101}
102
103iostate_t *get_iostate(void)
104{
105 return iostate;
106}
107
108void set_iostate(iostate_t *ios)
109{
110 iostate = ios;
111 stdin = ios->stdin;
112 stdout = ios->stdout;
113 stderr = ios->stderr;
114}
115
116int main(int argc, char *argv[])
117{
118 errno_t ret = 0;
119
120 stdiostate.stdin = stdin;
121 stdiostate.stdout = stdout;
122 stdiostate.stderr = stderr;
123 iostate = &stdiostate;
124
125
126 odict_initialize(&alias_dict, alias_key, alias_cmp);
127
128 if (cli_init(&usr))
129 exit(EXIT_FAILURE);
130
131 while (!cli_quit) {
132 get_input(&usr);
133 if (NULL != usr.line) {
134 ret = process_input(&usr);
135 cli_set_prompt(&usr);
136 usr.lasterr = ret;
137 }
138 }
139
140 printf("Leaving %s.\n", progname);
141
142 cli_finit(&usr);
143 return ret;
144}
Note: See TracBrowser for help on using the repository browser.