| 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 | #include <str_error.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "os.h"
|
|---|
| 40 |
|
|---|
| 41 | /** Path to executable file via which we have been invoked. */
|
|---|
| 42 | static char *ef_path;
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | * Using HelenOS-specific string API.
|
|---|
| 46 | */
|
|---|
| 47 |
|
|---|
| 48 | static tinput_t *tinput = NULL;
|
|---|
| 49 |
|
|---|
| 50 | /** Concatenate two strings.
|
|---|
| 51 | *
|
|---|
| 52 | * @param a First string
|
|---|
| 53 | * @param b Second string
|
|---|
| 54 | * @return New string, concatenation of @a a and @a b.
|
|---|
| 55 | */
|
|---|
| 56 | char *os_str_acat(const char *a, const char *b)
|
|---|
| 57 | {
|
|---|
| 58 | int a_size, b_size;
|
|---|
| 59 | char *str;
|
|---|
| 60 |
|
|---|
| 61 | a_size = str_size(a);
|
|---|
| 62 | b_size = str_size(b);
|
|---|
| 63 |
|
|---|
| 64 | str = malloc(a_size + b_size + 1);
|
|---|
| 65 | if (str == NULL) {
|
|---|
| 66 | printf("Memory allocation error.\n");
|
|---|
| 67 | exit(1);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | memcpy(str, a, a_size);
|
|---|
| 71 | memcpy(str + a_size, b, b_size);
|
|---|
| 72 | str[a_size + b_size] = '\0';
|
|---|
| 73 |
|
|---|
| 74 | return str;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Compare two strings.
|
|---|
| 78 | *
|
|---|
| 79 | * @param a First string
|
|---|
| 80 | * @param b Second string
|
|---|
| 81 | * @return Zero if equal, nonzero if not equal
|
|---|
| 82 | */
|
|---|
| 83 | int os_str_cmp(const char *a, const char *b)
|
|---|
| 84 | {
|
|---|
| 85 | return str_cmp(a, b);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /** Return number of characters in string.
|
|---|
| 89 | *
|
|---|
| 90 | * @param str String
|
|---|
| 91 | * @return Number of characters in @a str.
|
|---|
| 92 | */
|
|---|
| 93 | size_t os_str_length(const char *str)
|
|---|
| 94 | {
|
|---|
| 95 | return str_length(str);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /** Duplicate string.
|
|---|
| 99 | *
|
|---|
| 100 | * @param str String
|
|---|
| 101 | * @return New string, duplicate of @a str.
|
|---|
| 102 | */
|
|---|
| 103 | char *os_str_dup(const char *str)
|
|---|
| 104 | {
|
|---|
| 105 | return str_dup(str);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Get character from string at the given index.
|
|---|
| 109 | *
|
|---|
| 110 | * @param str String
|
|---|
| 111 | * @param index Character index (starting from zero).
|
|---|
| 112 | * @param out_char Place to store character.
|
|---|
| 113 | * @return EOK on success, EINVAL if index is out of bounds,
|
|---|
| 114 | * EIO on decoding error.
|
|---|
| 115 | */
|
|---|
| 116 | int os_str_get_char(const char *str, int index, int *out_char)
|
|---|
| 117 | {
|
|---|
| 118 | size_t offset;
|
|---|
| 119 | int i;
|
|---|
| 120 | wchar_t c;
|
|---|
| 121 |
|
|---|
| 122 | if (index < 0)
|
|---|
| 123 | return EINVAL;
|
|---|
| 124 |
|
|---|
| 125 | offset = 0;
|
|---|
| 126 | for (i = 0; i <= index; ++i) {
|
|---|
| 127 | c = str_decode(str, &offset, STR_NO_LIMIT);
|
|---|
| 128 | if (c == '\0')
|
|---|
| 129 | return EINVAL;
|
|---|
| 130 | if (c == U_SPECIAL)
|
|---|
| 131 | return EIO;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | *out_char = (int) c;
|
|---|
| 135 | return EOK;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Display survival help message. */
|
|---|
| 139 | void os_input_disp_help(void)
|
|---|
| 140 | {
|
|---|
| 141 | printf("Press Ctrl-Q to quit.\n");
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /** Read one line of input from the user.
|
|---|
| 145 | *
|
|---|
| 146 | * @param ptr Place to store pointer to new string.
|
|---|
| 147 | */
|
|---|
| 148 | int os_input_line(char **ptr)
|
|---|
| 149 | {
|
|---|
| 150 | char *line;
|
|---|
| 151 | int rc;
|
|---|
| 152 |
|
|---|
| 153 | if (tinput == NULL) {
|
|---|
| 154 | tinput = tinput_new();
|
|---|
| 155 | if (tinput == NULL)
|
|---|
| 156 | return EIO;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | rc = tinput_read(tinput, &line);
|
|---|
| 160 | if (rc == ENOENT) {
|
|---|
| 161 | /* User-requested abort */
|
|---|
| 162 | *ptr = os_str_dup("");
|
|---|
| 163 | return EOK;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | if (rc != EOK) {
|
|---|
| 167 | /* Error in communication with console */
|
|---|
| 168 | return EIO;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /* XXX Input module needs trailing newline to keep going. */
|
|---|
| 172 | *ptr = os_str_acat(line, "\n");
|
|---|
| 173 | free(line);
|
|---|
| 174 |
|
|---|
| 175 | return EOK;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /** Simple command execution.
|
|---|
| 179 | *
|
|---|
| 180 | * @param cmd Command and arguments (NULL-terminated list of strings.)
|
|---|
| 181 | * Command is present just one, not duplicated.
|
|---|
| 182 | */
|
|---|
| 183 | int os_exec(char *const cmd[])
|
|---|
| 184 | {
|
|---|
| 185 | task_id_t tid;
|
|---|
| 186 | task_exit_t texit;
|
|---|
| 187 | int retval;
|
|---|
| 188 |
|
|---|
| 189 | tid = task_spawn(cmd[0], (char const * const *) cmd, &retval);
|
|---|
| 190 | if (tid == 0) {
|
|---|
| 191 | printf("Error: Failed spawning '%s' (%s).\n", cmd[0],
|
|---|
| 192 | str_error(retval));
|
|---|
| 193 | exit(1);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /* XXX Handle exit status and return value. */
|
|---|
| 197 | task_wait(tid, &texit, &retval);
|
|---|
| 198 |
|
|---|
| 199 | return EOK;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /** Store the executable file path via which we were executed.
|
|---|
| 203 | *
|
|---|
| 204 | * @param path Executable path via which we were executed.
|
|---|
| 205 | */
|
|---|
| 206 | void os_store_ef_path(char *path)
|
|---|
| 207 | {
|
|---|
| 208 | ef_path = path;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /** Return path to the Sysel library
|
|---|
| 212 | *
|
|---|
| 213 | * @return New string. Caller should deallocate it using @c free().
|
|---|
| 214 | */
|
|---|
| 215 | char *os_get_lib_path(void)
|
|---|
| 216 | {
|
|---|
| 217 | return os_str_dup("/src/sysel/lib");
|
|---|
| 218 | }
|
|---|