source: mainline/uspace/app/bdsh/scli.c@ 55e35a22

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

correcting setting and updating aliases

  • Property mode set to 100644
File size: 3.4 KB
Line 
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 <stddef.h>
32#include <str.h>
33#include <adt/odict.h>
34
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.