Files
mercury/tools/generate_errno_name
Peter Wang aaa6ac5fe1 Introduce io.system_error to io.m public interface.
Implement the error handling proposals from February 2022 on the
mercury-users list, and August 2022 on the mercury-reviews list.

We add io.system_error to the public interface of io.m
and document what its foreign representation is for each backend.

We allow io.error to optionally contain an io.system_error value,
and provide predicates to retrieve the io.system_error from an io.error.
The user may then inspect the system error via foreign code.

We also provide a predicate that takes an io.error and returns a name
for the system error it contains (if any). This makes it relatively easy
for Mercury programs to check for specific error conditions.

By returning platform-specific (actually, implementation-dependent)
error names, we are pushing the responsibility of mapping strings to
error conditions onto the application programmer. On the other hand, it
is not practical for us to map all possible system-specific error codes
to some common set of values. We could do it for a small set of common
error codes/exceptions, perhaps.

The standard library will construct io.error values containing
io.system_errors. However, we do not yet provide a facility for user
code to do the same.

library/io.m:
    Move io.system_error to the public interface.

    Change the internal representation of io.error to support containing
    a io.system_error. An io.system_error may originate from an errno
    value or a Windows system error code; the constructor distinguishes
    those cases.

    Add predicates to retrieve a system_error from io.error.

    Add predicate to return the name of the system error in an io.error.

    Replace make_err_msg with make_io_error_from_system_error.

    Replace make_maybe_win32_err_msg with
    make_io_error_from_maybe_win32_error.

    Delete ML_make_err_msg and ML_make_win32_err_msg macros.

browser/listing.m:
library/bitmap.m:
library/dir.m:
library/io.call_system.m:
library/io.environment.m:
library/io.file.m:
library/io.text_read.m:
mdbcomp/program_representation.m:
    Conform to changes.

    Leave comments for followup work.

tools/generate_errno_name:
tools/generate_windows_error_name:
    Add scripts to generate mercury_errno_name.c and
    mercury_windows_error_name.c.

runtime/Mmakefile:
runtime/mercury_errno_name.c:
runtime/mercury_errno_name.h:
runtime/mercury_windows_error_name.c:
runtime/mercury_windows_error_name.h:
    Add MR_errno_name() and MR_win32_error_name() functions,
    used by io.m to convert error codes to string names.

tests/hard_coded/null_char.exp:
    Update expected output.
2022-08-23 16:39:48 +10:00

206 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
#
# This generates runtime/mercury_errno_name.c
#
set -eu
# This list is taken from:
# - Cosmopolitan libc
# https://github.com/jart/cosmopolitan/blob/master/libc/sysv/consts.sh
# - moreutils `errno -l' output on Linux
#
names="
ENOSYS
EPERM
ENOENT
ESRCH
EINTR
EIO
ENXIO
E2BIG
ENOEXEC
EBADF
ECHILD
EAGAIN
ENOMEM
EACCES
EFAULT
ENOTBLK
EBUSY
EEXIST
EXDEV
ENODEV
ENOTDIR
EISDIR
EINVAL
ENFILE
EMFILE
ENOTTY
ETXTBSY
EFBIG
ENOSPC
EDQUOT
ESPIPE
EROFS
EMLINK
EPIPE
EDOM
ERANGE
EDEADLK
ENAMETOOLONG
ENOLCK
ENOTEMPTY
ELOOP
ENOMSG
EIDRM
ETIME
EPROTO
EOVERFLOW
EILSEQ
EUSERS
ENOTSOCK
EDESTADDRREQ
EMSGSIZE
EPROTOTYPE
ENOPROTOOPT
EPROTONOSUPPORT
ESOCKTNOSUPPORT
EOPNOTSUPP
EPFNOSUPPORT
EAFNOSUPPORT
EADDRINUSE
EADDRNOTAVAIL
ENETDOWN
ENETUNREACH
ENETRESET
ECONNABORTED
ECONNRESET
ENOBUFS
EISCONN
ENOTCONN
ESHUTDOWN
ETOOMANYREFS
ETIMEDOUT
ECONNREFUSED
EHOSTDOWN
EHOSTUNREACH
EALREADY
EINPROGRESS
ESTALE
EREMOTE
EBADRPC
ERPCMISMATCH
EPROGUNAVAIL
EPROGMISMATCH
EPROCUNAVAIL
EFTYPE
EAUTH
ENEEDAUTH
EPROCLIM
ENOATTR
EPWROFF
EDEVERR
EBADEXEC
EBADARCH
ESHLIBVERS
EBADMACHO
ENOPOLICY
EBADMSG
ECANCELED
EOWNERDEAD
ENOTRECOVERABLE
ENONET
ERESTART
ENODATA
ENOSR
ENOSTR
EMULTIHOP
ENOLINK
ENOMEDIUM
EMEDIUMTYPE
EBADFD
ECHRNG
EL2NSYNC
EL3HLT
EL3RST
ELNRNG
EUNATCH
ENOCSI
EL2HLT
EBADE
EBADR
EXFULL
ENOANO
EBADRQC
EBADSLT
ENOPKG
EADV
ESRMNT
ECOMM
EDOTDOT
ENOTUNIQ
EREMCHG
ELIBACC
ELIBBAD
ELIBSCN
ELIBMAX
ELIBEXEC
ESTRPIPE
EUCLEAN
ENOTNAM
ENAVAIL
EISNAM
EREMOTEIO
ENOKEY
EKEYEXPIRED
EKEYREVOKED
EKEYREJECTED
ERFKILL
EHWPOISON
EBFONT
"
cat <<EOF
// vim: ts=4 sw=4 expandtab ft=c
// Copyright (C) 2022 The Mercury team.
// This file is distributed under the terms specified in COPYING.LIB.
// Generated by generate_errno_name.
#include <errno.h>
#include <stdlib.h>
#include "mercury_errno_name.h"
const char *
MR_errno_name(int errnum)
{
switch (errnum) {
EOF
for name in $names ; do
echo "#ifdef $name"
echo " case $name: return \"$name\";"
echo "#endif"
done
# Handle potential aliases:
# - EWOULDBLOCK should be the same as EAGAIN
# - EDEADLOCK should be the same as EDEADLK
# - ENOTSUP may be the same as EOPNOTSUPP on some platforms
handle_alias() {
echo "#if defined($1) && !defined($2)"
echo " case $1: return \"$1\";"
echo "#endif"
}
handle_alias EWOULDBLOCK EAGAIN
handle_alias EDEADLOCK EDEADLK
handle_alias ENOTSUP EOPNOTSUPP
cat <<EOF
default: return NULL;
}
}
EOF