Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!watnot!ccplumb From: ccplumb@watnot.UUCP Newsgroups: comp.lang.forth Subject: Re: Unix system calls from Forth Message-ID: <12261@watnot.UUCP> Date: Thu, 4-Dec-86 16:56:32 EST Article-I.D.: watnot.12261 Posted: Thu Dec 4 16:56:32 1986 Date-Received: Fri, 5-Dec-86 04:14:58 EST References: <12234@watnot.UUCP> <182@haddock.UUCP> Reply-To: ccplumb@watnot.UUCP (Colin Plumb) Distribution: net Organization: U of Waterloo, Ontario Lines: 66 In article <182@haddock.UUCP> karl@haddock.ISC.COM.UUCP (Karl Heuer) writes: >In article <12234@watnot.UUCP> ccplumb@watnot.UUCP (I) write: >>I'm working on a 32-bit Forth for a VAX under BSD 4.2 Unix. >>I'm trying to figure out how to implement the system call interface. > >Well, I've already done it on SysV, and I think the same idea should >work on BSD. On success, the return values (0, 1, or 2 of them) and >a success indicator are placed on the stack; on failure, only the >failure indicator. (I used 1 for ok vs. 0 for error, but 0 for ok >would've made it easier to test.) The error code can be retrieved >via "errno" (which is not a variable because I didn't think it needed >to be user-writable); I didn't leave it on the stack because most of >the time it isn't needed. For consistency, even calls that never >fail (e.g. getpid) have the flag on top. (I also defined a word >which drops the top of stack and bombs with an appropriate message if >it was a failure.) Thank you very much for the ideas... I think I'll use -1 (F-83 true) for "error", and 0 for "O.K.", since that lets me use ABORT" to bomb out, and is in agreement with C convention. I'd like to ask people with more Unix experience than my 4 months whether it's desirable to put the error in "errno", or leave it on the stack. My perception is that, while in most cases you simply bomb out (via some sort of ABORT word), which clears the stack, and thus never use the error number, if you try to handle it more gracefully, you almost always use the error number in some sort of case statement. (That is, you use it right away, and just in this one place.) So why stash it away somewhere? >I used a leading "$" (so "$dup" is a system call, while "dup" is a >Forth word), or "$_" for the "real" system call if the C interface >twiddles it (so $_getpid returns two values, while $getpid and >$getppid select the one of interest). I didn't do this with $pipe >and $wait; they just return two values (too bad C can't do that! >Returning a struct by value doesn't count). I was thinking of using a leading "_" for the system call naming convention, since that's the convention used by the innards C library, and I wanted to use $ for string extensions. Still, your idea of supporting both the C library syntax and the actual system call syntax is a good idea, and adding (in my case, another) "_" is in agreement with the way it's handled in C (exit() is a library routine that calls _exit(), the system call proper). I was worrying about what to do in some cases (like getpid and getuid), where the interface is messed up by the fact that C can't handle multiple return values. >I used one defining word which takes three arguments (number of >arguments, number of results, and chmk number), and had one chunk of >common code in assembly language. The asm code was the only part I >needed to rewrite when I ported my TIL to a 3b2. A defining word is definitely the way to go - although I'd replace "number of results" by flags telling the common code which registers to place on the stack. Of course, really badly behaved things like wait3 (which is a fancier version of wait, with options for nonblocking operation, etc.), which are really other system calls in disguise (the extra args are put in r0 and r1, and the flags in the PSW are set to indicate the fancy version, before chmk $SYS_WAIT. Can you say *ugly*?), are going to require special attention. >Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint -Colin plumb (ccplumb@watnot.UUCP) Zippy says: I'm rated PG-34!!