| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
|---|
| 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 | /** @file HelenOS-specific code. */
|
|---|
| 30 |
|
|---|
| 31 | #include <errno.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #include <str.h>
|
|---|
| 35 | #include <task.h>
|
|---|
| 36 | #include <tinput.h>
|
|---|
| 37 |
|
|---|
| 38 | #include "os.h"
|
|---|
| 39 |
|
|---|
| 40 | /*
|
|---|
| 41 | * Using HelenOS-specific string API.
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | static tinput_t *tinput = NULL;
|
|---|
| 45 |
|
|---|
| 46 | /** Concatenate two strings. */
|
|---|
| 47 | char *os_str_acat(const char *a, const char *b)
|
|---|
| 48 | {
|
|---|
| 49 | int a_size, b_size;
|
|---|
| 50 | char *str;
|
|---|
| 51 |
|
|---|
| 52 | a_size = str_size(a);
|
|---|
| 53 | b_size = str_size(b);
|
|---|
| 54 |
|
|---|
| 55 | str = malloc(a_size + b_size + 1);
|
|---|
| 56 | if (str == NULL) {
|
|---|
| 57 | printf("Memory allocation error.\n");
|
|---|
| 58 | exit(1);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | memcpy(str, a, a_size);
|
|---|
| 62 | memcpy(str + a_size, b, b_size);
|
|---|
| 63 | str[a_size + b_size] = '\0';
|
|---|
| 64 |
|
|---|
| 65 | return str;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** Compare two strings. */
|
|---|
| 69 | int os_str_cmp(const char *a, const char *b)
|
|---|
| 70 | {
|
|---|
| 71 | return str_cmp(a, b);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /** Duplicate string. */
|
|---|
| 75 | char *os_str_dup(const char *str)
|
|---|
| 76 | {
|
|---|
| 77 | return str_dup(str);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Get character from string at the given index. */
|
|---|
| 81 | int os_str_get_char(const char *str, int index, int *out_char)
|
|---|
| 82 | {
|
|---|
| 83 | size_t offset;
|
|---|
| 84 | int i;
|
|---|
| 85 | wchar_t c;
|
|---|
| 86 |
|
|---|
| 87 | if (index < 0)
|
|---|
| 88 | return EINVAL;
|
|---|
| 89 |
|
|---|
| 90 | offset = 0;
|
|---|
| 91 | for (i = 0; i <= index; ++i) {
|
|---|
| 92 | c = str_decode(str, &offset, STR_NO_LIMIT);
|
|---|
| 93 | if (c == '\0')
|
|---|
| 94 | return EINVAL;
|
|---|
| 95 | if (c == U_SPECIAL)
|
|---|
| 96 | return EIO;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | *out_char = (int) c;
|
|---|
| 100 | return EOK;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Display survival help message. */
|
|---|
| 104 | void os_input_disp_help(void)
|
|---|
| 105 | {
|
|---|
| 106 | printf("Press Ctrl-Q to quit.\n");
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Read one line of input from the user. */
|
|---|
| 110 | int os_input_line(char **ptr)
|
|---|
| 111 | {
|
|---|
| 112 | char *line;
|
|---|
| 113 | int rc;
|
|---|
| 114 |
|
|---|
| 115 | if (tinput == NULL) {
|
|---|
| 116 | tinput = tinput_new();
|
|---|
| 117 | if (tinput == NULL)
|
|---|
| 118 | return EIO;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | rc = tinput_read(tinput, &line);
|
|---|
| 122 | if (rc == ENOENT) {
|
|---|
| 123 | /* User-requested abort */
|
|---|
| 124 | *ptr = os_str_dup("");
|
|---|
| 125 | return EOK;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | if (rc != EOK) {
|
|---|
| 129 | /* Error in communication with console */
|
|---|
| 130 | return EIO;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /* XXX Input module needs trailing newline to keep going. */
|
|---|
| 134 | *ptr = os_str_acat(line, "\n");
|
|---|
| 135 | free(line);
|
|---|
| 136 |
|
|---|
| 137 | return EOK;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** Simple command execution. */
|
|---|
| 141 | int os_exec(char *const cmd[])
|
|---|
| 142 | {
|
|---|
| 143 | task_id_t tid;
|
|---|
| 144 | task_exit_t texit;
|
|---|
| 145 | int retval;
|
|---|
| 146 |
|
|---|
| 147 | tid = task_spawn(cmd[0], (char const * const *) cmd);
|
|---|
| 148 | if (tid == 0) {
|
|---|
| 149 | printf("Error: Failed spawning '%s'.\n", cmd[0]);
|
|---|
| 150 | exit(1);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /* XXX Handle exit status and return value. */
|
|---|
| 154 | task_wait(tid, &texit, &retval);
|
|---|
| 155 |
|
|---|
| 156 | return EOK;
|
|---|
| 157 | }
|
|---|