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