Changeset 132ab5d1 in mainline for kernel/generic/src/lib/str_error.c


Ignore:
Timestamp:
2018-01-30T03:20:45Z (8 years ago)
Author:
Jenda <jenda.jzqk73@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5a6cc679
Parents:
8bfb163 (diff), 6a5d05b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge commit '6a5d05bd2551e64111bea4f9332dd7448c26ce84' into forwardport

Separate return value from error code in gen_irq_code*().

File:
1 moved

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/str_error.c

    r8bfb163 r132ab5d1  
    11/*
    2  * Copyright (c) 2006 Ondrej Palkovsky
    3  * Copyright (c) 2008 Martin Decky
     2 * Copyright (c) 2010 Martin Decky
     3 * Copyright (c) 2017 CZ.NIC, z.s.p.o.
    44 * All rights reserved.
    55 *
     
    2828 */
    2929
    30 /** @file
     30#include <errno.h>
     31#include <str.h>
     32
     33/* The arrays below are automatically generated from the same file that
     34 * errno.h constants are generated from. Triple-include of the same list
     35 * with redefinitions of __errno() macro are used to ensure that the
     36 * information cannot get out of synch. This is inpired by musl libc.
    3137 */
    3238
    33 #include <stdint.h>
    34 #include <errno.h>
    35 #include <str.h>
    36 #include <sysinfo.h>
    37 #include <ddi.h>
    38 #include <align.h>
    39 #include <as.h>
    40 #include "../ctl/serial.h"
    41 #include "kchar.h"
     39#undef __errno_entry
     40#define __errno_entry(name, num, desc) name,
    4241
    43 typedef struct {
    44         uint8_t *addr;
    45 } kchar_t;
     42static const int err_num[] = {
     43#include <abi/errno.in>
     44};
    4645
    47 static kchar_t kchar;
     46#undef __errno_entry
     47#define __errno_entry(name, num, desc) #name,
    4848
    49 static void kchar_putchar(wchar_t ch)
     49static const char* err_name[] = {
     50#include <abi/errno.in>
     51};
     52
     53#undef __errno_entry
     54#define __errno_entry(name, num, desc) "[" #name "]" desc,
     55
     56static const char* err_desc[] = {
     57#include <abi/errno.in>
     58};
     59
     60/* Returns index corresponding to the given errno, or -1 if not found. */
     61static int find_errno(errno_t e)
    5062{
    51         if (ascii_check(ch))
    52                 *kchar.addr = ch;
    53         else
    54                 *kchar.addr = '?';
     63        /* Just a dumb linear search.
     64         * There too few entries to warrant anything smarter.
     65         */
     66
     67        int len = sizeof(err_num) / sizeof(errno_t);
     68
     69        for (int i = 0; i < len; i++) {
     70                if (err_num[i] == e) {
     71                        return i;
     72                }
     73        }
     74
     75        return -1;
    5576}
    5677
    57 static void kchar_control_puts(const char *str)
     78const char *str_error_name(errno_t e)
    5879{
    59         while (*str)
    60                 *kchar.addr = *(str++);
     80        int i = find_errno(e);
     81
     82        if (i >= 0) {
     83                return err_name[i];
     84        }
     85
     86        return "(unknown)";
    6187}
    6288
    63 int kchar_init(void)
     89const char *str_error(errno_t e)
    6490{
    65         sysarg_t present;
    66         int rc = sysinfo_get_value("fb", &present);
    67         if (rc != EOK)
    68                 present = false;
    69        
    70         if (!present)
    71                 return ENOENT;
    72        
    73         sysarg_t kind;
    74         rc = sysinfo_get_value("fb.kind", &kind);
    75         if (rc != EOK)
    76                 kind = (sysarg_t) -1;
    77        
    78         if (kind != 3)
    79                 return EINVAL;
    80        
    81         sysarg_t paddr;
    82         rc = sysinfo_get_value("fb.address.physical", &paddr);
    83         if (rc != EOK)
    84                 return rc;
    85        
    86         kchar.addr = AS_AREA_ANY;
    87        
    88         rc = physmem_map(paddr,
    89             ALIGN_UP(1, PAGE_SIZE) >> PAGE_WIDTH,
    90             AS_AREA_READ | AS_AREA_WRITE, (void *) &kchar.addr);
    91         if (rc != EOK)
    92                 return rc;
    93        
    94         return serial_init(kchar_putchar, kchar_control_puts);
     91        int i = find_errno(e);
     92
     93        if (i >= 0) {
     94                return err_desc[i];
     95        }
     96
     97        return "Unknown error code";
    9598}
    9699
    97 /** @}
    98  */
Note: See TracChangeset for help on using the changeset viewer.