helenos.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00031 #include <assert.h>
00032 #include <errno.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <str.h>
00036 #include <task.h>
00037 #include <tinput.h>
00038 #include <str_error.h>
00039 
00040 #include "os.h"
00041 
00043 static char *ef_path;
00044 
00045 /*
00046  * Using HelenOS-specific string API.
00047  */
00048 
00049 static tinput_t *tinput = NULL;
00050 
00057 char *os_str_acat(const char *a, const char *b)
00058 {
00059         int a_size, b_size;
00060         char *str;
00061 
00062         a_size = str_size(a);
00063         b_size = str_size(b);
00064 
00065         str = malloc(a_size + b_size + 1);
00066         if (str == NULL) {
00067                 printf("Memory allocation error.\n");
00068                 exit(1);
00069         }
00070 
00071         memcpy(str, a, a_size);
00072         memcpy(str + a_size, b, b_size);
00073         str[a_size + b_size] = '\0';
00074 
00075         return str;
00076 }
00077 
00090 char *os_str_aslice(const char *str, size_t start, size_t length)
00091 {
00092         char *slice;
00093         size_t offset;
00094         size_t i;
00095         size_t size;
00096         wchar_t c;
00097 
00098         assert(start + length <= str_length(str));
00099 
00100         offset = 0;
00101         for (i = 0; i < start; ++i) {
00102                 c = str_decode(str, &offset, STR_NO_LIMIT);
00103                 assert(c != '\0');
00104                 assert(c != U_SPECIAL);
00105                 (void) c;
00106         }
00107 
00108         size = str_lsize(str, length);
00109         slice = str_ndup(str + offset, size);
00110 
00111         return slice;
00112 }
00113 
00120 int os_str_cmp(const char *a, const char *b)
00121 {
00122         return str_cmp(a, b);
00123 }
00124 
00130 size_t os_str_length(const char *str)
00131 {
00132         return str_length(str);
00133 }
00134 
00140 char *os_str_dup(const char *str)
00141 {
00142         return str_dup(str);
00143 }
00144 
00153 int os_str_get_char(const char *str, int index, int *out_char)
00154 {
00155         size_t offset;
00156         int i;
00157         wchar_t c;
00158 
00159         if (index < 0)
00160                 return EINVAL;
00161 
00162         offset = 0;
00163         for (i = 0; i <= index; ++i) {
00164                 c = str_decode(str, &offset, STR_NO_LIMIT);
00165                 if (c == '\0')
00166                         return EINVAL;
00167                 if (c == U_SPECIAL)
00168                         return EIO;
00169         }
00170 
00171         *out_char = (int) c;
00172         return EOK;
00173 }
00174 
00180 char *os_chr_to_astr(wchar_t chr)
00181 {
00182         char *str;
00183         size_t offset;
00184 
00185         str = malloc(STR_BOUNDS(1) + 1);
00186         if (str == NULL) {
00187                 printf("Memory allocation error.\n");
00188                 exit(1);
00189         }
00190 
00191         offset = 0;
00192         if (chr_encode(chr, str, &offset, STR_BOUNDS(1)) != EOK) {
00193                 /* XXX Should handle gracefully */
00194                 printf("String conversion error.\n");
00195                 exit(1);
00196         }
00197 
00198         str[offset] = '\0';
00199         return str;
00200 }
00201 
00203 void os_input_disp_help(void)
00204 {
00205         printf("Press Ctrl-Q to quit.\n");
00206 }
00207 
00212 int os_input_line(char **ptr)
00213 {
00214         char *line;
00215         int rc;
00216 
00217         if (tinput == NULL) {
00218                 tinput = tinput_new();
00219                 if (tinput == NULL)
00220                         return EIO;
00221         }
00222 
00223         rc = tinput_read(tinput, &line);
00224         if (rc == ENOENT) {
00225                 /* User-requested abort */
00226                 *ptr = os_str_dup("");
00227                 return EOK;
00228         }
00229 
00230         if (rc != EOK) {
00231                 /* Error in communication with console */
00232                 return EIO;
00233         }
00234 
00235         /* XXX Input module needs trailing newline to keep going. */
00236         *ptr = os_str_acat(line, "\n");
00237         free(line);
00238 
00239         return EOK;
00240 }
00241 
00247 int os_exec(char *const cmd[])
00248 {
00249         task_id_t tid;
00250         task_exit_t texit;
00251         int rc, retval;
00252 
00253         rc = task_spawnv(&tid, cmd[0], (char const * const *) cmd);
00254         if (rc != EOK) {
00255                 printf("Error: Failed spawning '%s' (%s).\n", cmd[0],
00256                     str_error(rc));
00257                 exit(1);
00258         }
00259 
00260         /* XXX Handle exit status and return value. */
00261         rc = task_wait(tid, &texit, &retval);
00262         (void) rc;
00263 
00264         return EOK;
00265 }
00266 
00271 void os_store_ef_path(char *path)
00272 {
00273         ef_path = path;
00274 }
00275 
00280 char *os_get_lib_path(void)
00281 {
00282         return os_str_dup("/src/sysel/lib");
00283 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7