[94d484a] | 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>
|
---|
[1faa995] | 34 | #include <str.h>
|
---|
[94d484a] | 35 | #include <task.h>
|
---|
[1ebc1a62] | 36 | #include <tinput.h>
|
---|
[94d484a] | 37 |
|
---|
| 38 | #include "os.h"
|
---|
| 39 |
|
---|
| 40 | /*
|
---|
| 41 | * Using HelenOS-specific string API.
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[1ebc1a62] | 44 | static tinput_t *tinput = NULL;
|
---|
| 45 |
|
---|
[94d484a] | 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 |
|
---|
[d0febca] | 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 |
|
---|
[1ebc1a62] | 103 | /** Read one line of input from the user. */
|
---|
| 104 | int os_input_line(char **ptr)
|
---|
| 105 | {
|
---|
| 106 | char *line;
|
---|
| 107 |
|
---|
| 108 | if (tinput == NULL) {
|
---|
| 109 | tinput = tinput_new();
|
---|
| 110 | if (tinput == NULL)
|
---|
| 111 | return EIO;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | line = tinput_read(tinput);
|
---|
| 115 | if (line == NULL)
|
---|
| 116 | return EIO;
|
---|
| 117 |
|
---|
| 118 | /* XXX Input module needs trailing newline to keep going. */
|
---|
| 119 | *ptr = os_str_acat(line, "\n");
|
---|
| 120 | free(line);
|
---|
| 121 |
|
---|
| 122 | return EOK;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[94d484a] | 125 | /** Simple command execution. */
|
---|
| 126 | int os_exec(char *const cmd[])
|
---|
| 127 | {
|
---|
| 128 | task_id_t tid;
|
---|
| 129 | task_exit_t texit;
|
---|
| 130 | int retval;
|
---|
| 131 |
|
---|
[7715994] | 132 | tid = task_spawn(cmd[0], (char const * const *) cmd);
|
---|
[94d484a] | 133 | if (tid == 0) {
|
---|
| 134 | printf("Error: Failed spawning '%s'.\n", cmd[0]);
|
---|
| 135 | exit(1);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /* XXX Handle exit status and return value. */
|
---|
| 139 | task_wait(tid, &texit, &retval);
|
---|
| 140 |
|
---|
| 141 | return EOK;
|
---|
| 142 | }
|
---|