2 * Copyright (c) 2000 Daniel Capo Sobral
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 /*******************************************************************
31 ** Additional FICL words designed for FreeBSD's loader
33 *******************************************************************/
36 #include <sys/types.h>
46 #include "bootstrap.h"
51 /* FreeBSD's loader interaction words and extras
53 * setenv ( value n name n' -- )
54 * setenv? ( value n name n' flag -- )
55 * getenv ( addr n -- addr' n' | -1 )
56 * unsetenv ( addr n -- )
57 * copyin ( addr addr' len -- )
58 * copyout ( addr addr' len -- )
59 * findfile ( name len type len' -- addr )
60 * pnpdevices ( -- addr )
61 * pnphandlers ( -- addr )
62 * ccall ( [[...[p10] p9] ... p1] n addr -- result )
63 * uuid-from-string ( addr n -- addr' )
64 * uuid-to-string ( addr' -- addr n )
69 ficlSetenv(FICL_VM *pVM)
78 vmCheckStack(pVM, 4, 0);
80 names = stackPopINT(pVM->pStack);
81 namep = (char*) stackPopPtr(pVM->pStack);
82 values = stackPopINT(pVM->pStack);
83 valuep = (char*) stackPopPtr(pVM->pStack);
86 name = (char*) ficlMalloc(names+1);
88 vmThrowErr(pVM, "Error: out of memory");
89 strncpy(name, namep, names);
91 value = (char*) ficlMalloc(values+1);
93 vmThrowErr(pVM, "Error: out of memory");
94 strncpy(value, valuep, values);
97 setenv(name, value, 1);
106 ficlSetenvq(FICL_VM *pVM)
111 char *namep, *valuep;
112 int names, values, overwrite;
115 vmCheckStack(pVM, 5, 0);
117 overwrite = stackPopINT(pVM->pStack);
118 names = stackPopINT(pVM->pStack);
119 namep = (char*) stackPopPtr(pVM->pStack);
120 values = stackPopINT(pVM->pStack);
121 valuep = (char*) stackPopPtr(pVM->pStack);
124 name = (char*) ficlMalloc(names+1);
126 vmThrowErr(pVM, "Error: out of memory");
127 strncpy(name, namep, names);
129 value = (char*) ficlMalloc(values+1);
131 vmThrowErr(pVM, "Error: out of memory");
132 strncpy(value, valuep, values);
133 value[values] = '\0';
135 setenv(name, value, overwrite);
144 ficlGetenv(FICL_VM *pVM)
153 vmCheckStack(pVM, 2, 2);
155 names = stackPopINT(pVM->pStack);
156 namep = (char*) stackPopPtr(pVM->pStack);
159 name = (char*) ficlMalloc(names+1);
161 vmThrowErr(pVM, "Error: out of memory");
162 strncpy(name, namep, names);
165 value = getenv(name);
169 stackPushPtr(pVM->pStack, value);
170 stackPushINT(pVM->pStack, strlen(value));
173 stackPushINT(pVM->pStack, -1);
179 ficlUnsetenv(FICL_VM *pVM)
188 vmCheckStack(pVM, 2, 0);
190 names = stackPopINT(pVM->pStack);
191 namep = (char*) stackPopPtr(pVM->pStack);
194 name = (char*) ficlMalloc(names+1);
196 vmThrowErr(pVM, "Error: out of memory");
197 strncpy(name, namep, names);
208 ficlCopyin(FICL_VM *pVM)
215 vmCheckStack(pVM, 3, 0);
218 len = stackPopINT(pVM->pStack);
219 dest = stackPopINT(pVM->pStack);
220 src = stackPopPtr(pVM->pStack);
223 archsw.arch_copyin(src, dest, len);
230 ficlCopyout(FICL_VM *pVM)
237 vmCheckStack(pVM, 3, 0);
240 len = stackPopINT(pVM->pStack);
241 dest = stackPopPtr(pVM->pStack);
242 src = stackPopINT(pVM->pStack);
245 archsw.arch_copyout(src, dest, len);
252 ficlFindfile(FICL_VM *pVM)
258 struct preloaded_file* fp;
262 vmCheckStack(pVM, 4, 1);
265 types = stackPopINT(pVM->pStack);
266 typep = (char*) stackPopPtr(pVM->pStack);
267 names = stackPopINT(pVM->pStack);
268 namep = (char*) stackPopPtr(pVM->pStack);
270 name = (char*) ficlMalloc(names+1);
272 vmThrowErr(pVM, "Error: out of memory");
273 strncpy(name, namep, names);
275 type = (char*) ficlMalloc(types+1);
277 vmThrowErr(pVM, "Error: out of memory");
278 strncpy(type, typep, types);
281 fp = file_findfile(name, type);
285 stackPushPtr(pVM->pStack, fp);
291 ficlCcall(FICL_VM *pVM)
293 int (*func)(int, ...);
298 vmCheckStack(pVM, 2, 0);
301 func = stackPopPtr(pVM->pStack);
302 nparam = stackPopINT(pVM->pStack);
305 vmCheckStack(pVM, nparam, 1);
308 for (i = 0; i < nparam; i++)
309 p[i] = stackPopINT(pVM->pStack);
311 result = func(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8],
314 stackPushINT(pVM->pStack, result);
320 ficlUuidFromString(FICL_VM *pVM)
331 vmCheckStack(pVM, 2, 0);
334 uuids = stackPopINT(pVM->pStack);
335 uuidp = (char *) stackPopPtr(pVM->pStack);
338 uuid = (char *)ficlMalloc(uuids + 1);
340 vmThrowErr(pVM, "Error: out of memory");
341 strncpy(uuid, uuidp, uuids);
344 u = (uuid_t *)ficlMalloc(sizeof (*u));
346 uuid_from_string(uuid, u, &status);
348 if (status != uuid_s_ok) {
355 stackPushPtr(pVM->pStack, u);
362 ficlUuidToString(FICL_VM *pVM)
371 vmCheckStack(pVM, 1, 0);
374 u = (uuid_t *)stackPopPtr(pVM->pStack);
377 uuid_to_string(u, &uuid, &status);
378 if (status != uuid_s_ok) {
379 stackPushPtr(pVM->pStack, uuid);
380 stackPushINT(pVM->pStack, strlen(uuid));
383 stackPushINT(pVM->pStack, -1);
388 /**************************************************************************
390 ** reads in text from file fd and passes it to ficlExec()
391 * returns VM_OUTOFTEXT on success or the ficlExec() error code on
395 int ficlExecFD(FICL_VM *pVM, int fd)
398 int nLine = 0, rval = VM_OUTOFTEXT;
403 pVM->sourceID.i = fd;
405 /* feed each line to ficlExec */
410 while ((status = read(fd, &ch, 1)) > 0 && ch != '\n')
418 rval = ficlExecC(pVM, cp, i);
419 if(rval != VM_QUIT && rval != VM_USEREXIT && rval != VM_OUTOFTEXT)
426 ** Pass an empty line with SOURCE-ID == -1 to flush
427 ** any pending REFILLs (as required by FILE wordset)
429 pVM->sourceID.i = -1;
436 static void displayCellNoPad(FICL_VM *pVM)
440 vmCheckStack(pVM, 1, 0);
442 c = stackPop(pVM->pStack);
443 ltoa((c).i, pVM->pad, pVM->base);
444 vmTextOut(pVM, pVM->pad, 0);
448 /* isdir? - Return whether an fd corresponds to a directory.
450 * isdir? ( fd -- bool )
452 static void isdirQuestion(FICL_VM *pVM)
459 vmCheckStack(pVM, 1, 1);
462 fd = stackPopINT(pVM->pStack);
467 if (fstat(fd, &sb) < 0)
469 if (!S_ISDIR(sb.st_mode))
473 stackPushINT(pVM->pStack, flag);
476 /* fopen - open a file and return new fd on stack.
478 * fopen ( ptr count mode -- fd )
480 static void pfopen(FICL_VM *pVM)
486 vmCheckStack(pVM, 3, 1);
489 mode = stackPopINT(pVM->pStack); /* get mode */
490 count = stackPopINT(pVM->pStack); /* get count */
491 ptr = stackPopPtr(pVM->pStack); /* get ptr */
493 if ((count < 0) || (ptr == NULL)) {
494 stackPushINT(pVM->pStack, -1);
498 /* ensure that the string is null terminated */
499 name = (char *)malloc(count+1);
500 bcopy(ptr,name,count);
504 fd = open(name, mode);
505 #ifdef LOADER_VERIEXEC
507 if (verify_file(fd, name, 0, VE_GUESS) < 0) {
508 /* not verified writing ok but reading is not */
509 if ((mode & O_ACCMODE) != O_WRONLY) {
514 /* verified reading ok but writing is not */
515 if ((mode & O_ACCMODE) != O_RDONLY) {
523 stackPushINT(pVM->pStack, fd);
527 /* fclose - close a file who's fd is on stack.
531 static void pfclose(FICL_VM *pVM)
536 vmCheckStack(pVM, 1, 0);
538 fd = stackPopINT(pVM->pStack); /* get fd */
544 /* fread - read file contents
546 * fread ( fd buf nbytes -- nread )
548 static void pfread(FICL_VM *pVM)
554 vmCheckStack(pVM, 3, 1);
556 len = stackPopINT(pVM->pStack); /* get number of bytes to read */
557 buf = stackPopPtr(pVM->pStack); /* get buffer */
558 fd = stackPopINT(pVM->pStack); /* get fd */
559 if (len > 0 && buf && fd != -1)
560 stackPushINT(pVM->pStack, read(fd, buf, len));
562 stackPushINT(pVM->pStack, -1);
566 /* freaddir - read directory contents
568 * freaddir ( fd -- ptr len TRUE | FALSE )
570 static void pfreaddir(FICL_VM *pVM)
573 static struct dirent dirent;
584 vmCheckStack(pVM, 1, 3);
587 fd = stackPopINT(pVM->pStack);
590 * The readdirfd() function is specific to the loader environment.
591 * We do the best we can to make freaddir work, but it's not at
599 if (fstat(fd, &sb) == -1)
601 blksz = (sb.st_blksize) ? sb.st_blksize : getpagesize();
602 if ((blksz & (blksz - 1)) != 0)
607 off = lseek(fd, 0LL, SEEK_CUR);
611 if (lseek(fd, 0, SEEK_SET) == -1)
613 bufsz = getdents(fd, buf, blksz);
614 while (bufsz > 0 && bufsz <= ptr) {
616 bufsz = getdents(fd, buf, blksz);
620 d = (void *)(buf + ptr);
623 d = (lseek(fd, off, SEEK_SET) != off) ? NULL : &dirent;
631 stackPushPtr(pVM->pStack, d->d_name);
632 stackPushINT(pVM->pStack, strlen(d->d_name));
633 stackPushINT(pVM->pStack, FICL_TRUE);
635 stackPushINT(pVM->pStack, FICL_FALSE);
639 /* fload - interpret file contents
643 static void pfload(FICL_VM *pVM)
648 vmCheckStack(pVM, 1, 0);
650 fd = stackPopINT(pVM->pStack); /* get fd */
656 /* fwrite - write file contents
658 * fwrite ( fd buf nbytes -- nwritten )
660 static void pfwrite(FICL_VM *pVM)
666 vmCheckStack(pVM, 3, 1);
668 len = stackPopINT(pVM->pStack); /* get number of bytes to read */
669 buf = stackPopPtr(pVM->pStack); /* get buffer */
670 fd = stackPopINT(pVM->pStack); /* get fd */
671 if (len > 0 && buf && fd != -1)
672 stackPushINT(pVM->pStack, write(fd, buf, len));
674 stackPushINT(pVM->pStack, -1);
678 /* fseek - seek to a new position in a file
680 * fseek ( fd ofs whence -- pos )
682 static void pfseek(FICL_VM *pVM)
687 vmCheckStack(pVM, 3, 1);
689 whence = stackPopINT(pVM->pStack);
690 pos = stackPopINT(pVM->pStack);
691 fd = stackPopINT(pVM->pStack);
692 stackPushINT(pVM->pStack, lseek(fd, pos, whence));
696 /* key - get a character from stdin
700 static void key(FICL_VM *pVM)
703 vmCheckStack(pVM, 0, 1);
705 stackPushINT(pVM->pStack, getchar());
709 /* key? - check for a character from stdin (FACILITY)
713 static void keyQuestion(FICL_VM *pVM)
716 vmCheckStack(pVM, 0, 1);
719 /* XXX Since we don't fiddle with termios, let it always succeed... */
720 stackPushINT(pVM->pStack, FICL_TRUE);
722 /* But here do the right thing. */
723 stackPushINT(pVM->pStack, ischar()? FICL_TRUE : FICL_FALSE);
728 /* seconds - gives number of seconds since beginning of time
730 * beginning of time is defined as:
732 * BTX - number of seconds since midnight
733 * FreeBSD - number of seconds since Jan 1 1970
737 static void pseconds(FICL_VM *pVM)
740 vmCheckStack(pVM,0,1);
742 stackPushUNS(pVM->pStack, (FICL_UNS) time(NULL));
746 /* ms - wait at least that many milliseconds (FACILITY)
751 static void ms(FICL_VM *pVM)
754 vmCheckStack(pVM,1,0);
757 usleep(stackPopUNS(pVM->pStack)*1000);
759 delay(stackPopUNS(pVM->pStack)*1000);
764 /* fkey - get a character from a file
766 * fkey ( file -- char )
768 static void fkey(FICL_VM *pVM)
774 vmCheckStack(pVM, 1, 1);
776 fd = stackPopINT(pVM->pStack);
777 i = read(fd, &ch, 1);
778 stackPushINT(pVM->pStack, i > 0 ? ch : -1);
784 ** Retrieves free space remaining on the dictionary
787 static void freeHeap(FICL_VM *pVM)
789 stackPushINT(pVM->pStack, dictCellsAvail(ficlGetDict(pVM->pSys)));
793 /******************* Increase dictionary size on-demand ******************/
795 static void ficlDictThreshold(FICL_VM *pVM)
797 stackPushPtr(pVM->pStack, &dictThreshold);
800 static void ficlDictIncrease(FICL_VM *pVM)
802 stackPushPtr(pVM->pStack, &dictIncrease);
805 /**************************************************************************
806 f i c l C o m p i l e P l a t f o r m
807 ** Build FreeBSD platform extensions into the system dictionary
808 **************************************************************************/
809 void ficlCompilePlatform(FICL_SYSTEM *pSys)
811 ficlCompileFcn **fnpp;
812 FICL_DICT *dp = pSys->dp;
815 dictAppendWord(dp, ".#", displayCellNoPad, FW_DEFAULT);
816 dictAppendWord(dp, "isdir?", isdirQuestion, FW_DEFAULT);
817 dictAppendWord(dp, "fopen", pfopen, FW_DEFAULT);
818 dictAppendWord(dp, "fclose", pfclose, FW_DEFAULT);
819 dictAppendWord(dp, "fread", pfread, FW_DEFAULT);
820 dictAppendWord(dp, "freaddir", pfreaddir, FW_DEFAULT);
821 dictAppendWord(dp, "fload", pfload, FW_DEFAULT);
822 dictAppendWord(dp, "fkey", fkey, FW_DEFAULT);
823 dictAppendWord(dp, "fseek", pfseek, FW_DEFAULT);
824 dictAppendWord(dp, "fwrite", pfwrite, FW_DEFAULT);
825 dictAppendWord(dp, "key", key, FW_DEFAULT);
826 dictAppendWord(dp, "key?", keyQuestion, FW_DEFAULT);
827 dictAppendWord(dp, "ms", ms, FW_DEFAULT);
828 dictAppendWord(dp, "seconds", pseconds, FW_DEFAULT);
829 dictAppendWord(dp, "heap?", freeHeap, FW_DEFAULT);
830 dictAppendWord(dp, "dictthreshold", ficlDictThreshold, FW_DEFAULT);
831 dictAppendWord(dp, "dictincrease", ficlDictIncrease, FW_DEFAULT);
833 dictAppendWord(dp, "setenv", ficlSetenv, FW_DEFAULT);
834 dictAppendWord(dp, "setenv?", ficlSetenvq, FW_DEFAULT);
835 dictAppendWord(dp, "getenv", ficlGetenv, FW_DEFAULT);
836 dictAppendWord(dp, "unsetenv", ficlUnsetenv, FW_DEFAULT);
837 dictAppendWord(dp, "copyin", ficlCopyin, FW_DEFAULT);
838 dictAppendWord(dp, "copyout", ficlCopyout, FW_DEFAULT);
839 dictAppendWord(dp, "findfile", ficlFindfile, FW_DEFAULT);
840 dictAppendWord(dp, "ccall", ficlCcall, FW_DEFAULT);
841 dictAppendWord(dp, "uuid-from-string", ficlUuidFromString, FW_DEFAULT);
842 dictAppendWord(dp, "uuid-to-string", ficlUuidToString, FW_DEFAULT);
844 SET_FOREACH(fnpp, Xficl_compile_set)
847 #if defined(__i386__)
848 ficlSetEnv(pSys, "arch-i386", FICL_TRUE);
849 ficlSetEnv(pSys, "arch-powerpc", FICL_FALSE);
850 #elif defined(__powerpc__)
851 ficlSetEnv(pSys, "arch-i386", FICL_FALSE);
852 ficlSetEnv(pSys, "arch-powerpc", FICL_TRUE);