| 1 |
;;; |
| 2 |
;;; SRFI-238 - Codesets |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module srfi.238 |
| 35 |
(use gauche.record) |
| 36 |
(use gauche.threads) |
| 37 |
(export codeset? codeset-symbols |
| 38 |
codeset-symbol codeset-number codeset-message) |
| 39 |
) |
| 40 |
(select-module srfi.238) |
| 41 |
|
| 42 |
;; Global mapping from name -> codeset |
| 43 |
(define *codesets* (atom (make-hash-table 'eq?))) |
| 44 |
|
| 45 |
(define (name->codeset name) |
| 46 |
(atomic *codesets* (cut hash-table-get <> name #f))) |
| 47 |
|
| 48 |
(define (add-codeset! codeset) |
| 49 |
(atomic *codesets* (cut hash-table-put! <> (%codeset-name codeset) codeset))) |
| 50 |
|
| 51 |
;; The record is internal. We don't expose codeset object. |
| 52 |
(define-record-type %codeset #t #t |
| 53 |
name |
| 54 |
symbols |
| 55 |
symbol->number |
| 56 |
number->symbol |
| 57 |
symbol->message |
| 58 |
number->message) |
| 59 |
|
| 60 |
;; API |
| 61 |
(define (codeset? name) (and (symbol? name) (boolean (name->codeset name)))) |
| 62 |
|
| 63 |
;; API |
| 64 |
(define (codeset-symbols name) |
| 65 |
(assume-type name <symbol>) |
| 66 |
(if-let1 cs (name->codeset name) |
| 67 |
(%codeset-symbols cs) |
| 68 |
'())) |
| 69 |
|
| 70 |
;; API |
| 71 |
(define (codeset-symbol name code) |
| 72 |
(assume-type name <symbol>) |
| 73 |
(and-let1 cs (name->codeset name) |
| 74 |
(cond [(symbol? code) (and ((%codeset-symbol->number cs) code) code)] |
| 75 |
[(integer? code) ((%codeset-number->symbol cs) code)] |
| 76 |
[else #f]))) |
| 77 |
|
| 78 |
;; API |
| 79 |
(define (codeset-number name code) |
| 80 |
(assume-type name <symbol>) |
| 81 |
(and-let1 cs (name->codeset name) |
| 82 |
(cond [(symbol? code) ((%codeset-symbol->number cs) code)] |
| 83 |
[(integer? code) (and ((%codeset-number->symbol cs) code) code)] |
| 84 |
[else #f]))) |
| 85 |
|
| 86 |
;; API |
| 87 |
(define (codeset-message name code) |
| 88 |
(assume-type name <symbol>) |
| 89 |
(and-let1 cs (name->codeset name) |
| 90 |
(cond [(symbol? code) ((%codeset-symbol->message cs) code)] |
| 91 |
[(integer? code) ((%codeset-number->message cs) code)] |
| 92 |
[else #f]))) |
| 93 |
|
| 94 |
;;; |
| 95 |
;;; Actual codesets |
| 96 |
;;; |
| 97 |
(define (make-errno-codeset) |
| 98 |
(let* ([errno-alist (filter-map (^n (and-let1 s (sys-errno->symbol n) |
| 99 |
(cons s n))) |
| 100 |
(iota 500))] |
| 101 |
[sym->num (alist->hash-table errno-alist)] |
| 102 |
[num->sym (alist->hash-table (map (^p (cons (cdr p) (car p))) |
| 103 |
errno-alist) |
| 104 |
eqv-comparator)]) |
| 105 |
(make-%codeset |
| 106 |
'errno |
| 107 |
(map car errno-alist) ;symbols |
| 108 |
(cut hash-table-get sym->num <> #f) ;symbol->number |
| 109 |
(cut hash-table-get num->sym <> #f) ;number->symbol |
| 110 |
(^s (and-let1 n (hash-table-get sym->num s #f) ;symbol->message |
| 111 |
(sys-strerror n))) |
| 112 |
(^n (sys-strerror n))))) ;number->message |
| 113 |
|
| 114 |
(define (make-signal-codeset) |
| 115 |
(let* ([sig-alist (filter-map (^n (and-let1 s (sys-signal-name n) |
| 116 |
(cons (string->symbol s) n))) |
| 117 |
(iota 200))] |
| 118 |
[sym->num (alist->hash-table sig-alist)] |
| 119 |
[num->sym (alist->hash-table (map (^p (cons (cdr p) (car p))) |
| 120 |
sig-alist) |
| 121 |
eqv-comparator)]) |
| 122 |
(make-%codeset |
| 123 |
'signal |
| 124 |
(map car sig-alist) ;symbols |
| 125 |
(cut hash-table-get sym->num <> #f) ;symbol->number |
| 126 |
(cut hash-table-get num->sym <> #f) ;number->symbol |
| 127 |
(^s (and-let1 n (hash-table-get sym->num s #f) ;symbol->message |
| 128 |
(sys-strsignal n))) |
| 129 |
(^n (sys-strsignal n))))) ;number->message |
| 130 |
|
| 131 |
;; Register to global index |
| 132 |
(add-codeset! (make-errno-codeset)) |
| 133 |
(add-codeset! (make-signal-codeset)) |