source: mainline/uspace/app/bdsh/input.c@ 631ee0c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 631ee0c was 5db9084, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Ctrl-Q to quit CLUI application.

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[216d6fc]1/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the original program's authors nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
[19f857a]33#include <str.h>
[73878c1]34#include <io/console.h>
35#include <io/keycode.h>
36#include <io/style.h>
[7e0cb78]37#include <io/color.h>
[3bf907a]38#include <vfs/vfs.h>
[371a012]39#include <clipboard.h>
[f1b37d6]40#include <macros.h>
[6071a8f]41#include <errno.h>
[da2bd08]42#include <assert.h>
[6071a8f]43#include <bool.h>
[36a75a2]44#include <tinput.h>
[216d6fc]45
46#include "config.h"
47#include "util.h"
48#include "scli.h"
49#include "input.h"
50#include "errors.h"
51#include "exec.h"
52
[5db9084]53extern volatile unsigned int cli_quit;
54
[ed372da]55/** Text input field. */
[36a75a2]56static tinput_t *tinput;
[88944695]57
[216d6fc]58/* Tokenizes input from console, sees if the first word is a built-in, if so
59 * invokes the built-in entry point (a[0]) passing all arguments in a[] to
60 * the handler */
61int tok_input(cliuser_t *usr)
62{
63 char *cmd[WORD_MAX];
64 int n = 0, i = 0;
65 int rc = 0;
66 char *tmp;
67
68 if (NULL == usr->line)
69 return CL_EFAIL;
70
[095003a8]71 tmp = str_dup(usr->line);
[216d6fc]72
[4cc0c9ee]73 cmd[n] = strtok(tmp, " ");
[216d6fc]74 while (cmd[n] && n < WORD_MAX) {
[4cc0c9ee]75 cmd[++n] = strtok(NULL, " ");
[216d6fc]76 }
77
78 /* We have rubbish */
79 if (NULL == cmd[0]) {
80 rc = CL_ENOENT;
81 goto finit;
82 }
83
[d752cf4]84 /* Its a builtin command ? */
[216d6fc]85 if ((i = (is_builtin(cmd[0]))) > -1) {
86 rc = run_builtin(i, cmd, usr);
87 goto finit;
[d752cf4]88 /* Its a module ? */
[216d6fc]89 } else if ((i = (is_module(cmd[0]))) > -1) {
90 rc = run_module(i, cmd);
91 goto finit;
92 }
93
[d752cf4]94 /* See what try_exec thinks of it */
95 rc = try_exec(cmd[0], cmd);
96
[216d6fc]97finit:
98 if (NULL != usr->line) {
99 free(usr->line);
100 usr->line = (char *) NULL;
101 }
102 if (NULL != tmp)
103 free(tmp);
104
105 return rc;
106}
107
108void get_input(cliuser_t *usr)
109{
[19528516]110 char *str;
[5db9084]111 int rc;
[216d6fc]112
[ef8bcc6]113 fflush(stdout);
[73878c1]114 console_set_style(fphone(stdout), STYLE_EMPHASIS);
[216d6fc]115 printf("%s", usr->prompt);
[ef8bcc6]116 fflush(stdout);
[73878c1]117 console_set_style(fphone(stdout), STYLE_NORMAL);
[9805cde]118
[5db9084]119 rc = tinput_read(tinput, &str);
120 if (rc == ENOENT) {
121 /* User requested exit */
122 cli_quit = 1;
123 putchar('\n');
124 return;
125 }
126
127 if (rc != EOK) {
128 /* Error in communication with console */
129 return;
130 }
[19528516]131
132 /* Check for empty input. */
133 if (str_cmp(str, "") == 0) {
134 free(str);
[216d6fc]135 return;
[19528516]136 }
[216d6fc]137
[19528516]138 usr->line = str;
[216d6fc]139 return;
140}
[da2bd08]141
[36a75a2]142int input_init(void)
[da2bd08]143{
[36a75a2]144 tinput = tinput_new();
145 if (tinput == NULL) {
146 printf("Failed to initialize input.\n");
147 return 1;
148 }
149
150 return 0;
[da2bd08]151}
Note: See TracBrowser for help on using the repository browser.