MODULE Errors;

(*---------------------------------------------------------------------------
** Unix-like error codes.
**
** Copyright (c) 1982, 1986, 1989 Regents of the University of California.
**               All rights reserved.
**
** Oberonized 1993 by Lars Düning.
**---------------------------------------------------------------------------
** Oberon-2: Amiga-Oberon v3.11, F. Siebert / A+L AG
**---------------------------------------------------------------------------
** 28-Jun-93 [lars]
** 21-Nov-93 [lars] Added 'ok' value.
** 24-May-94 [lars] Rename 'acces' to 'access'
**---------------------------------------------------------------------------
*)

IMPORT
  Strings,
  Dos, Exec, SYSTEM;

(*-------------------------------------------------------------------------*)

CONST
  ok             *=  0;  (* No Error *)
  perm           *=  1;  (* Operation not permitted *)
  noEnt          *=  2;  (* No such file or directory *)
  srch           *=  3;  (* No such process *)
  intr           *=  4;  (* Interrupted system call *)
  io             *=  5;  (* Input/output error *)
  nxio           *=  6;  (* Device not configured *)
  tooBig         *=  7;  (* Argument list too long *)
  noExec         *=  8;  (* Exec format error *)
  badF           *=  9;  (* Bad file descriptor *)
  child          *= 10;  (* No child processes *)
  deadlk         *= 11;  (* Resource deadlock avoided (was again) *)
  noMem          *= 12;  (* Cannot allocate memory *)
  access         *= 13;  (* Permission denied *)
  fault          *= 14;  (* Bad address *)
  notBlk         *= 15;  (* Block device required *)
  busy           *= 16;  (* Device busy *)
  exist          *= 17;  (* File exists *)
  xDev           *= 18;  (* Cross-device link *)
  noDev          *= 19;  (* Operation not supported by device *)
  notDir         *= 20;  (* Not a directory *)
  isDir          *= 21;  (* Is a directory *)
  inval          *= 22;  (* Invalid argument *)
  nFile          *= 23;  (* Too many open files in system *)
  mFile          *= 24;  (* Too many open files *)
  noTTY          *= 25;  (* Inappropriate ioctl for device *)
  txtBsy         *= 26;  (* Text file busy *)
  fBig           *= 27;  (* File too large *)
  noSpc          *= 28;  (* No space left on device *)
  sPipe          *= 29;  (* Illegal seek *)
  rofs           *= 30;  (* Read-only file system *)
  mLink          *= 31;  (* Too many links *)
  pipe           *= 32;  (* Broken pipe *)

    (* math software *)
  dom            *= 33;  (* Numerical argument out of domain *)
  range          *= 34;  (* Result too large *)

    (* non-blocking and interrupt i/o *)
  again          *= 35;  (* Resource temporarily unavailable *)
  wouldBlock     *= again;          (* Operation would block *)
  inProgress     *= 36;  (* Operation now in progress *)
  already        *= 37;  (* Operation already in progress *)

    (* ipc/network software -- argument errors *)
  notSock        *= 38;  (* Socket operation on non-socket *)
  destAddrReq    *= 39;  (* Destination address required *)
  msgSize        *= 40;  (* Message too long *)
  protoType      *= 41;  (* Protocol wrong type for socket *)
  noProtoOpt     *= 42;  (* Protocol not available *)
  protoNoSupport *= 43;  (* Protocol not supported *)
  sockTNoSupport *= 44;  (* Socket type not supported *)
  opNotSupp      *= 45;  (* Operation not supported on socket *)
  pfNoSupport    *= 46;  (* Protocol family not supported *)
  afNoSupport    *= 47;  (* Address family not supported by protocol family *)
  addrInUse      *= 48;  (* Address already in use *)
  addrNotAvail   *= 49;  (* Can't assign requested address *)

    (* ipc/network software -- operational errors *)
  netDown        *= 50;  (* Network is down *)
  netUnreach     *= 51;  (* Network is unreachable *)
  netReset       *= 52;  (* Network dropped connection on reset *)
  connAborted    *= 53;  (* Software caused connection abort *)
  connReset      *= 54;  (* Connection reset by peer *)
  noBufs         *= 55;  (* No buffer space available *)
  isConn         *= 56;  (* Socket is already connected *)
  notConn        *= 57;  (* Socket is not connected *)
  shutdown       *= 58;  (* Can't send after socket shutdown *)
  tooManyRefs    *= 59;  (* Too many references: can't splice *)
  timedOut       *= 60;  (* Connection timed out *)
  connRefused    *= 61;  (* Connection refused *)

  loop           *= 62;  (* Too many levels of symbolic links *)
  nameTooLong    *= 63;  (* File name too long *)

    (* should be rearranged *)
  hostDown       *= 64;  (* Host is down *)
  hostUnreach    *= 65;  (* No route to host *)
  notEmpty       *= 66;  (* Directory not empty *)

    (* quotas & mush *)
  procLim        *= 67;  (* Too many processes *)
  users          *= 68;  (* Too many users *)
  dquota         *= 69;  (* Disc quota exceeded *)

    (* Network File System *)
  stale          *= 70;  (* Stale NFS file handle *)
  remote         *= 71;  (* Too many levels of remote in path *)
  badRPC         *= 72;  (* RPC struct is bad *)
  rpcMismatch    *= 73;  (* RPC version wrong *)
  progUnavail    *= 74;  (* RPC prog. not avail *)
  progMismatch   *= 75;  (* Program version wrong *)
  procUnavail    *= 76;  (* Bad procedure for program *)

  noLock         *= 77;  (* No locks available *)
  noSys          *= 78;  (* Function not implemented *)

  ftype          *= 79;  (* Inappropriate file type or format *)

  reserved1      *= 80;  (* I was told that C= will put two in here *)
  reserved2      *= 81;

  stack          *= 82;  (* Out of stack (DICE uses this *)

  NErrs          *= 83;  (* Number of codes defined *)

(*-------------------------------------------------------------------------*)

TYPE
  Message *= ARRAY 60 OF CHAR;
  MessageList *= ARRAY NErrs OF Message;

CONST
  msg *= MessageList (
        "Undefined error",                      (*  0 - noerror *)
        "Operation not permitted",              (*  1 - perm *)
        "No such file or directory",            (*  2 - noEnt *)
        "No such process",                      (*  3 - srch *)
        "Interrupted system call",              (*  4 - intr *)
        "Input/output error",                   (*  5 - io *)
        "Device not configured",                (*  6 - nxio *)
        "Argument list too long",               (*  7 - tooBig *)
        "Exec format error",                    (*  8 - noExec *)
        "Bad file descriptor",                  (*  9 - badF *)
        "No child processes",                   (* 10 - child *)
        "Resource deadlock avoided",            (* 11 - deadlk *)
        "Cannot allocate memory",               (* 12 - noMem *)
        "Permission denied",                    (* 13 - acces *)
        "Bad address",                          (* 14 - fault *)
        "Block device required",                (* 15 - notBlk *)
        "Device busy",                          (* 16 - busy *)
        "File exists",                          (* 17 - exist *)
        "Cross-device link",                    (* 18 - xDev *)
        "Operation not supported by device",    (* 19 - noDev *)
        "Not a directory",                      (* 20 - notDir *)
        "Is a directory",                       (* 21 - isDir *)
        "Invalid argument",                     (* 22 - inval *)
        "Too many open files in system",        (* 23 - nFile *)
        "Too many open files",                  (* 24 - mFile *)
        "Inappropriate ioctl for device",       (* 25 - noTTY *)
        "Text file busy",                       (* 26 - txtBsy *)
        "File too large",                       (* 27 - fBig *)
        "No space left on device",              (* 28 - noSpc *)
        "Illegal seek",                         (* 29 - sPipe *)
        "Read-only file system",                (* 30 - rofs *)
        "Too many links",                       (* 31 - mLink *)
        "Broken pipe",                          (* 32 - pipe *)
        "Numerical argument out of domain",     (* 33 - dom *)
        "Result too large",                     (* 34 - range *)
        "Resource temporarily unavailable",     (* 35 - again *)
                                                (* 35 - wouldBlock *)
        "Operation now in progress",            (* 36 - inProgress *)
        "Operation already in progress",        (* 37 - already *)
        "Socket operation on non-socket",       (* 38 - notSock *)
        "Destination address required",         (* 39 - destAddrReq *)
        "Message too long",                     (* 40 - msgSize *)
        "Protocol wrong type for socket",       (* 41 - protoType *)
        "Protocol not available",               (* 42 - noProtoOpt *)
        "Protocol not supported",               (* 43 - protoNoSupport *)
        "Socket type not supported",            (* 44 - sockTNoSupport *)
        "Operation not supported",              (* 45 - opNotSupp *)
        "Protocol family not supported",        (* 46 - pfNoSupport *)
                                                (* 47 - afnosupport *)
        "Address family not supported by protocol family",
        "Address already in use",               (* 48 - addrInUse *)
        "Can't assign requested address",       (* 49 - addrNotAvail *)
        "Network is down",                      (* 50 - netDown *)
        "Network is unreachable",               (* 51 - netUnreach *)
        "Network dropped connection on reset",  (* 52 - netReset *)
        "Software caused connection abort",     (* 53 - connAborted *)
        "Connection reset by peer",             (* 54 - connReset *)
        "No buffer space available",            (* 55 - noBufs *)
        "Socket is already connected",          (* 56 - isConn *)
        "Socket is not connected",              (* 57 - notConn *)
        "Can't send after socket shutdown",     (* 58 - shutdown *)
        "Too many references: can't splice",    (* 59 - tooManyRefs *)
        "Connection timed out",                 (* 60 - timedOut *)
        "Connection refused",                   (* 61 - connRefused *)

        "Too many levels of symbolic links",    (* 62 - loop *)
        "File name too long",                   (* 63 - nameTooLong *)
        "Host is down",                         (* 64 - hostDown *)
        "No route to host",                     (* 65 - hostUnreach *)
        "Directory not empty",                  (* 66 - notEmpty *)
        "Too many processes",                   (* 67 - procLim *)
        "Too many users",                       (* 68 - users *)
        "Disc quota exceeded",                  (* 69 - dquota *)
        "Stale NFS file handle",                (* 70 - stale *)
        "Too many levels of remote in path",    (* 71 - remote *)
        "RPC struct is bad",                    (* 72 - badRPC *)
        "RPC version wrong",                    (* 73 - rpcMismatch *)
        "RPC prog. not avail",                  (* 74 - progUnavail *)
        "Program version wrong",                (* 75 - progMismatch *)
        "Bad procedure for program",            (* 76 - procUnavail *)

        "No locks available",                   (* 77 - noLock *)
        "Function not implemented",             (* 78 - noSys *)
        "Inappropriate file type or format",    (* 79 - ftype *)
        "Reserved error: 80",                   (* 80 - reserved1 *)
        "Reserved error: 81",                   (* 81 - reserved2 *)
        "Out of stack"                          (* 82 - stack *)
                     );

(*-------------------------------------------------------------------------*)
(* $StackChk- $RangeChk- $NilChk- $ClearVars- *)

(* $CopyArrays- *)
PROCEDURE WriteFault * (code : LONGINT; banner : ARRAY OF CHAR);

(* Print "<banner> : <errmsg for code>.\n" on stderr. *)

  VAR
    me : Dos.ProcessPtr;
    stderr : Dos.FileHandlePtr;
BEGIN
  me := Exec.FindTask(NIL);
  IF (Exec.exec.libNode.version < 37) OR (me.ces = NIL) THEN stderr := me.cos;
                                                        ELSE stderr := me.ces;
  END;
  IF stderr = NIL THEN RETURN; END;

  IF Strings.Length(banner) > 0 THEN
    SYSTEM.SETREG(0, Dos.FPuts(stderr, banner));
    SYSTEM.SETREG(0, Dos.FPuts(stderr, ": "));
  END;

  IF (code < 1) OR (code >= NErrs) THEN code := 0; END;
  SYSTEM.SETREG(0, Dos.FPuts(stderr, msg[code]));
  SYSTEM.SETREG(0, Dos.FPuts(stderr, ".\n"));
  SYSTEM.SETREG(0, Dos.Flush(stderr));
END WriteFault;

(*-------------------------------------------------------------------------*)
PROCEDURE StdErr * () : Dos.FileHandlePtr;

(* Return the 'stderr' filehandleptr of the current process *)

  VAR
    me : Dos.ProcessPtr;
    stderr : Dos.FileHandlePtr;
BEGIN
  me := Exec.FindTask(NIL);
  IF Exec.exec.libNode.version < 37 THEN stderr := me.cos;
                                    ELSE stderr := me.ces;
  END;
  RETURN stderr;
END StdErr;

(*-------------------------------------------------------------------------*)

END Errors.

(*
 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *)

(***************************************************************************)
