├── 1mgm.sf2 ├── README.md ├── brotexplorer ├── README.md ├── bexturbo.bas ├── bexturbo.c ├── bexturbo_imp.bas ├── border.pcx ├── brotexplorer.bas ├── xcursor.pcx └── ycursor.pcx ├── collage ├── README.md └── collage.bas ├── famicom ├── README.md ├── autoplay.bas └── play.bas ├── fosdem2019 ├── README.md ├── be_proto1.pcx ├── be_proto1p.pcx ├── be_shuttle.pcx ├── be_shuttlep.pcx ├── esp8266.pcx ├── esp8266p.pcx ├── fosdem.bas ├── rev1.pcx ├── rev1_small.pcx ├── rev1_smallp.pcx ├── rev1p.pcx ├── scene.bg ├── scene_wide.pcx ├── ship.pcx ├── toyoshiki.pcx ├── toyoshiki_small.pcx ├── toyoshiki_smallp.pcx ├── toyoshikip.pcx ├── vs23s010.pcx └── vs23s010p.pcx ├── mg ├── .gitignore ├── .travis.yml ├── CVS │ ├── Entries │ ├── Repository │ └── Root ├── NOTES.md ├── README-Mg ├── README.md ├── apple.h ├── autoexec.c ├── basename.c ├── basic.c ├── bell.c ├── buffer.c ├── cclass.h ├── cdbr.c ├── cdbr.h ├── cdbw.c ├── cdbw.h ├── chrdef.h ├── cinfo.c ├── cmode.c ├── cname.h ├── common.h ├── compile.c ├── compiled_terms.c ├── config.h ├── configure ├── cscope.c ├── curterm.c ├── def.h ├── dir.c ├── dired.c ├── display.c ├── dragonfly.h ├── echo.c ├── engine.c ├── enginebasic.c ├── extend.c ├── extensions.c ├── file.c ├── fileio.c ├── fparseln.c ├── freebsd.h ├── fstatat.c ├── funmap.c ├── funmap.h ├── futimens.c ├── getline.c ├── graph2.c ├── graph2.h ├── graph3.c ├── graph3.h ├── grep.c ├── hash.c ├── help.c ├── interpreter.c ├── kbd.c ├── kbd.h ├── key.h ├── keymap.c ├── line.c ├── linux.h ├── log.c ├── log.h ├── macro.c ├── macro.h ├── main.c ├── match.c ├── mg.1 ├── mg.bas ├── mi_vector_hash.c ├── modes.c ├── nbperf-bdz.c ├── nbperf-chm.c ├── nbperf-chm3.c ├── nbperf.c ├── nbperf.h ├── netbsd.h ├── paragraph.c ├── pathnames.h ├── re_search.c ├── reallocarray.c ├── regcomp.c ├── regerror.c ├── regex2.h ├── regexec.c ├── regfree.c ├── region.c ├── search.c ├── setupterm.c ├── spawn.c ├── strlcat.c ├── strlcpy.c ├── strndup.c ├── strtonum.c ├── tags.c ├── term.c ├── term_private.h ├── termcap.c ├── termcap_hash.c ├── termcap_map.c ├── terminfo_term.h ├── terminfo_termcap.h ├── ti.c ├── tic.c ├── tparm.c ├── tputs.c ├── tree.h ├── tty.c ├── ttyio.c ├── ttykbd.c ├── tutorial ├── undo.c ├── util.c ├── util.h ├── utils.h ├── version.c ├── window.c ├── word.c └── yank.c ├── playmod ├── README.md ├── loader.bas ├── playmod.c ├── pocketmod.h └── space_debris.mod ├── shmup ├── README.md ├── bullet.pcx ├── charwalk.pcx ├── explosion.pcx ├── scene.bg ├── scene.pcx ├── ship.pcx ├── shmup.bas └── shmup_ng.bas ├── sokoban ├── README.md ├── sokoban.bas ├── sokoban.pcx ├── sokoban_simplegfx.bas └── sokoban_text.bas ├── sys └── modules │ ├── famicom │ ├── Makefile │ ├── README.md │ ├── agnes.h │ ├── fami.c │ ├── famicom_arm.o │ ├── famicom_i386.o │ └── famicom_x86_64.o │ ├── lua │ ├── Makefile │ ├── lapi.c │ ├── lapi.h │ ├── lauxlib.c │ ├── lauxlib.h │ ├── lbaselib.c │ ├── lcode.c │ ├── lcode.h │ ├── lcorolib.c │ ├── lctype.c │ ├── lctype.h │ ├── ldblib.c │ ├── ldebug.c │ ├── ldebug.h │ ├── ldo.c │ ├── ldo.h │ ├── ldump.c │ ├── lfunc.c │ ├── lfunc.h │ ├── lgc.c │ ├── lgc.h │ ├── linit.c │ ├── liolib.c │ ├── ljumptab.h │ ├── llex.c │ ├── llex.h │ ├── llimits.h │ ├── lmathlib.c │ ├── lmem.c │ ├── lmem.h │ ├── loadlib.c │ ├── lobject.c │ ├── lobject.h │ ├── lopcodes.c │ ├── lopcodes.h │ ├── lopnames.h │ ├── loslib.c │ ├── lparser.c │ ├── lparser.h │ ├── lprefix.h │ ├── lstate.c │ ├── lstate.h │ ├── lstring.c │ ├── lstring.h │ ├── lstrlib.c │ ├── ltable.c │ ├── ltable.h │ ├── ltablib.c │ ├── ltests.c │ ├── ltests.h │ ├── ltm.c │ ├── ltm.h │ ├── lua.h │ ├── lua_arm.o │ ├── lua_be.c │ ├── lua_bg.c │ ├── lua_defs.h │ ├── lua_i386.o │ ├── lua_img.c │ ├── lua_input.c │ ├── lua_io.c │ ├── lua_video.c │ ├── lua_x86_64.o │ ├── luaconf.h │ ├── lualib.h │ ├── lundump.c │ ├── lundump.h │ ├── lutf8lib.c │ ├── lvm.c │ ├── lvm.h │ ├── lzio.c │ ├── lzio.h │ └── sys.c │ └── sam │ ├── ESP8266SAM.cpp │ ├── ESP8266SAM.h │ ├── Makefile │ ├── README.md │ ├── ReciterTabs.h │ ├── RenderTabs.h │ ├── SamTabs.h │ ├── build.ninja │ ├── debug.cpp │ ├── debug.h │ ├── module.cpp │ ├── reciter.cpp │ ├── reciter.h │ ├── render.cpp │ ├── render.h │ ├── sam_arm.o │ ├── sam_core.cpp │ ├── sam_i386.o │ └── sam_x86_64.o └── tetris ├── README.md └── tetris.bas /1mgm.sf2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/1mgm.sf2 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | See [BASIC Engine firmware](https://github.com/uli/basicengine-firmware) and 2 | [BASIC Engine hardware](https://github.com/uli/basicengine-pcb) for details. 3 | 4 | -------------------------------------------------------------------------------- /brotexplorer/README.md: -------------------------------------------------------------------------------- 1 | A program for exploring the Madelbrot set. Originally posted by SanguineBrah 2 | on the BASIC Engine forums: 3 | https://betest.freeflarum.com/d/65-first-kit-assembly-mandelbrot 4 | 5 | This version has been modified to do incremental rendering. Run 6 | `brotexplorer.bas` for the 100% interpreted version. For a high-performance 7 | version that uses machine code for the innermost loop, run `bexturbo.bas`. 8 | -------------------------------------------------------------------------------- /brotexplorer/bexturbo.bas: -------------------------------------------------------------------------------- 1 | TCC "bexturbo.c" 2 | TCCLINK "bexturbo" 3 | CHAIN "bexturbo_imp.bas" 4 | -------------------------------------------------------------------------------- /brotexplorer/bexturbo.c: -------------------------------------------------------------------------------- 1 | int iterbrot(double x0, double y0, double x, double y, int maxiteration) 2 | { 3 | int iteration = 0; 4 | double xtemp; 5 | while (x*x + y*y <= 4 && iteration < maxiteration) { 6 | xtemp = x*x - y*y + x0; 7 | y = 2 * x * y + y0; 8 | x = xtemp; 9 | ++iteration; 10 | } 11 | if (iteration >= maxiteration) 12 | return 0; 13 | else 14 | return iteration; 15 | } 16 | -------------------------------------------------------------------------------- /brotexplorer/bexturbo_imp.bas: -------------------------------------------------------------------------------- 1 | REM Brotexplorer by SanguineBrah 2 | REM modified by Ulrich Hecht 3 | 4 | screen 1:palette 0 5 | 6 | REM set initial conditions 7 | capnum=0 8 | maxiteration=100 9 | ioffsetx=-0.5 10 | ioffsety=0 11 | iscale=3 12 | offsetx=ioffsetx 13 | offsety=ioffsety 14 | renderscale=iscale 15 | CPOSX=0 16 | CPOSY=0 17 | cxspeed=PSIZE(0)*0.005 18 | cyspeed=PSIZE(1)*0.005 19 | zoomfactor=3 20 | colsalt=RND(1)*255 21 | 22 | REM load graphics 23 | LOAD PCX "border.pcx" AS BG 0:LOAD PCX "border.pcx" AS BG 1 24 | BG 0 TILES 1,CSIZE(1)WINDOW PSIZE(0)-9,0,9,PSIZE(1)-8 ON 25 | BG 1 TILES CSIZE(0)-1,1 WINDOW 0,PSIZE(1)-9,PSIZE(0),9 ON 26 | LOAD PCX "xcursor.pcx" AS SPRITE 0 KEY 0 27 | SPRITE 0 ON 28 | LOAD PCX "ycursor.pcx" AS SPRITE 1 KEY 0 29 | SPRITE 1 ON 30 | CALL wipeit 31 | 32 | REM main loop 33 | DO 34 | REM cursor and input handling 35 | CALL updatepad 36 | IF pad1<>0 THEN 37 | renderscale=renderscale*zoomfactor 38 | CALL wipeit 39 | ENDIF 40 | IF pad2<>0 THEN 41 | coffsetx=(CPOSX/(PSIZE(0)-9))-0.5 42 | coffsety=(CPOSY/(PSIZE(1)-9))-0.5 43 | offsetx=offsetx+(coffsetx*renderscale) 44 | offsety=offsety+(coffsety*renderscale) 45 | renderscale=renderscale/zoomfactor 46 | CALL wipeit 47 | ENDIF 48 | IF pad3<>0 THEN colsalt=RND(0)*255:CALL wipeit 49 | IF pad4<>0 THEN 50 | SAVE PCX STR$(capnum)+".pcx"SIZE PSIZE(0)-8,PSIZE(1)-8 51 | GPRINT 0,0,"Captured render as "+STR$(capnum)+".pcx"; 52 | capnum=capnum+1 53 | ENDIF 54 | IF padup<>0 THEN CPOSY=CPOSY-cyspeed 55 | IF paddown<>0 THEN CPOSY=CPOSY+cyspeed 56 | IF padleft<>0 THEN CPOSX=CPOSX-cxspeed 57 | IF padright<>0 THEN CPOSX=CPOSX+cxspeed 58 | IF CPOSX<0 THEN CPOSX=0 59 | IF CPOSX>PSIZE(0)-9 THEN CPOSX=PSIZE(0)-9 60 | IF CPOSY<0 THEN CPOSY=0 61 | IF CPOSY>PSIZE(1)-9 THEN CPOSY=PSIZE(1)-9 62 | MOVE SPRITE 0 TO CPOSX-4,PSIZE(1)-9 63 | MOVE SPRITE 1 TO PSIZE(0)-9,CPOSY-4 64 | 65 | REM render as many pixels as possible this frame 66 | DO 67 | IF ypPSIZE(0)-10 THEN yp=yp+1:xp=(yp*3+xoff) and 7 80 | if yp>PSIZE(1)-10 then yp=0:xoff=xoff+3 81 | ENDIF 82 | LOOP UNTIL f0 THEN 37 | renderscale=renderscale*zoomfactor 38 | CALL wipeit 39 | ENDIF 40 | IF pad2<>0 THEN 41 | coffsetx=(CPOSX/(PSIZE(0)-9))-0.5 42 | coffsety=(CPOSY/(PSIZE(1)-9))-0.5 43 | offsetx=offsetx+(coffsetx*renderscale) 44 | offsety=offsety+(coffsety*renderscale) 45 | renderscale=renderscale/zoomfactor 46 | CALL wipeit 47 | ENDIF 48 | IF pad3<>0 THEN colsalt=RND(0)*255:CALL wipeit 49 | IF pad4<>0 THEN 50 | SAVE PCX STR$(capnum)+".pcx"SIZE PSIZE(0)-8,PSIZE(1)-8 51 | GPRINT 0,0,"Captured render as "+STR$(capnum)+".pcx"; 52 | capnum=capnum+1 53 | ENDIF 54 | IF padup<>0 THEN CPOSY=CPOSY-cyspeed 55 | IF paddown<>0 THEN CPOSY=CPOSY+cyspeed 56 | IF padleft<>0 THEN CPOSX=CPOSX-cxspeed 57 | IF padright<>0 THEN CPOSX=CPOSX+cxspeed 58 | IF CPOSX<0 THEN CPOSX=0 59 | IF CPOSX>PSIZE(0)-9 THEN CPOSX=PSIZE(0)-9 60 | IF CPOSY<0 THEN CPOSY=0 61 | IF CPOSY>PSIZE(1)-9 THEN CPOSY=PSIZE(1)-9 62 | MOVE SPRITE 0 TO CPOSX-4,PSIZE(1)-9 63 | MOVE SPRITE 1 TO PSIZE(0)-9,CPOSY-4 64 | 65 | REM render as many pixels as possible this frame 66 | DO 67 | IF ypmaxiteration THEN c=iteration ELSE c=0 84 | PSET xp,yp,c+colsalt MOD 255 85 | xp=xp+8 86 | IF xp>PSIZE(0)-10 THEN yp=yp+1:xp=(yp*3+xoff) and 7 87 | if yp>PSIZE(1)-10 then yp=0:xoff=xoff+3 88 | ENDIF 89 | LOOP UNTIL fmin_scal THEN min_scal=min_scal_h 39 | 40 | max_scal=PSIZE(0)*max_img_scale/img_w 41 | max_scal_h=PSIZE(1)*max_img_scale/img_h 42 | IF max_scal_h>24);" "; 79 | NEXT 80 | ? 81 | NEXT 82 | RETURN 83 | 84 | PROC scandir(max) 85 | @count=0 86 | DO 87 | @f$=DIR$(1) 88 | IF RIGHT$(@f$,4)=".jpg" OR RIGHT$(@f$,5)=".jpeg" THEN 89 | APPEND ~imgs$,dr$+"/"+@f$ 90 | 'LOCATE 0,POS(1)-1:?LEN(~imgs$) 91 | @count=@count+1 92 | ENDIF 93 | LOOP UNTIL @f$="" or @count>@max 94 | RETURN 95 | -------------------------------------------------------------------------------- /famicom/README.md: -------------------------------------------------------------------------------- 1 | Example showing how to use the scriptable Famicom emulator module. 2 | 3 | - `play.bas` relays input from the (emulated or real) gamepad to the 4 | emulator. 5 | - `autoplay.bas` contains a simple algorithm that plays Super Mario Bros. 6 | 7 | (ROM images are not included and have to be obtained separately. Only 8 | mappers 0, 1, 2 and 4 are supported.) 9 | 10 | Shows how to use `#REQUIRE` to ensure specific modules are loaded. 11 | 12 | The source code for the emulator module can be found in the 13 | `sys/modules/famicom` directory. 14 | -------------------------------------------------------------------------------- /famicom/autoplay.bas: -------------------------------------------------------------------------------- 1 | #REQUIRE "famicom" 2 | INPUT "ROM file? ";rom$ 3 | 4 | FAMIMOVE PSIZE(0)-256, 0 5 | FAMILOAD rom$ 6 | 7 | DO 8 | 'start screen? 9 | IF FAMIREAD($0770)=0 THEN 10 | inp=16 'start 11 | ELSE 12 | inp=RIGHT 13 | ENDIF 14 | 15 | 'run? 16 | IF RND(0)>0.2 THEN inp=inp OR 256 17 | 18 | FAMINPUT inp 19 | CALL fam(RND(30)+20) 20 | 21 | inp=inp AND NOT 16 'release start (if set) 22 | IF RND(0)>0.2 THEN inp=inp OR 256 23 | 24 | FAMINPUT inp OR 512 'jump 25 | CALL fam(RND(30)+20) 26 | LOOP 27 | 28 | PROC fam(count) 29 | FOR @i=1 TO @count 30 | FAMULATE 31 | 'VSYNC 32 | NEXT 33 | RETURN 34 | -------------------------------------------------------------------------------- /famicom/play.bas: -------------------------------------------------------------------------------- 1 | #REQUIRE "famicom" 2 | INPUT "ROM file? ";rom$ 3 | 4 | FAMIMOVE PSIZE(0)-256, 0 5 | FAMILOAD rom$ 6 | 7 | DO 8 | f=FRAME() 9 | FAMULATE 10 | FAMINPUT PAD(0) 11 | VSYNC f+1 12 | LOOP 13 | -------------------------------------------------------------------------------- /fosdem2019/README.md: -------------------------------------------------------------------------------- 1 | Presentation on the BASIC Engine given at FOSDEM 2019. 2 | 3 | Does not include the "Zork I" binary because I'm not sure about its 4 | legal status. Add the file "zork1.z3" if you want the XYZZY slide to work. 5 | -------------------------------------------------------------------------------- /fosdem2019/be_proto1.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/be_proto1.pcx -------------------------------------------------------------------------------- /fosdem2019/be_proto1p.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/be_proto1p.pcx -------------------------------------------------------------------------------- /fosdem2019/be_shuttle.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/be_shuttle.pcx -------------------------------------------------------------------------------- /fosdem2019/be_shuttlep.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/be_shuttlep.pcx -------------------------------------------------------------------------------- /fosdem2019/esp8266.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/esp8266.pcx -------------------------------------------------------------------------------- /fosdem2019/esp8266p.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/esp8266p.pcx -------------------------------------------------------------------------------- /fosdem2019/rev1.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/rev1.pcx -------------------------------------------------------------------------------- /fosdem2019/rev1_small.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/rev1_small.pcx -------------------------------------------------------------------------------- /fosdem2019/rev1_smallp.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/rev1_smallp.pcx -------------------------------------------------------------------------------- /fosdem2019/rev1p.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/rev1p.pcx -------------------------------------------------------------------------------- /fosdem2019/scene.bg: -------------------------------------------------------------------------------- 1 |  2 |   !"#$!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()*)*)*)*)*)*)*)*+,+,+,+,+,+,+,+,-.-.-.-.-.-.-.-. -------------------------------------------------------------------------------- /fosdem2019/scene_wide.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/scene_wide.pcx -------------------------------------------------------------------------------- /fosdem2019/ship.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/ship.pcx -------------------------------------------------------------------------------- /fosdem2019/toyoshiki.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/toyoshiki.pcx -------------------------------------------------------------------------------- /fosdem2019/toyoshiki_small.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/toyoshiki_small.pcx -------------------------------------------------------------------------------- /fosdem2019/toyoshiki_smallp.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/toyoshiki_smallp.pcx -------------------------------------------------------------------------------- /fosdem2019/toyoshikip.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/toyoshikip.pcx -------------------------------------------------------------------------------- /fosdem2019/vs23s010.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/vs23s010.pcx -------------------------------------------------------------------------------- /fosdem2019/vs23s010p.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/fosdem2019/vs23s010p.pcx -------------------------------------------------------------------------------- /mg/.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.o 3 | *~ 4 | mg 5 | -------------------------------------------------------------------------------- /mg/.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | os: 3 | - linux 4 | - osx 5 | compiler: 6 | - clang 7 | - gcc 8 | -------------------------------------------------------------------------------- /mg/CVS/Entries: -------------------------------------------------------------------------------- 1 | /version.c/1.10/Thu Mar 19 21:22:15 2015// 2 | /word.c/1.19/Sun Jan 3 04:14:49 2016// 3 | /tags.c/1.16/Result of merge// 4 | /extend.c/1.71/Result of merge// 5 | /funmap.c/1.59/Result of merge+Thu Oct 3 02:58:58 2019// 6 | /Makefile/1.35/Sun Feb 28 00:19:23 2021// 7 | /README/1.14/Sun Feb 28 00:19:23 2021// 8 | /autoexec.c/1.17/Sun Feb 28 00:19:14 2021// 9 | /basic.c/1.50/Sun Feb 28 00:19:23 2021// 10 | /bell.c/1.5/Sun Feb 28 00:19:14 2021// 11 | /buffer.c/1.107/Sun Feb 28 00:19:14 2021// 12 | /chrdef.h/1.10/Sun Feb 28 00:19:14 2021// 13 | /cinfo.c/1.18/Sun Feb 28 00:19:14 2021// 14 | /cmode.c/1.17/Sun Feb 28 00:19:14 2021// 15 | /cscope.c/1.18/Sun Feb 28 00:19:14 2021// 16 | /def.h/1.167/Result of merge// 17 | /dir.c/1.31/Sun Feb 28 00:19:14 2021// 18 | /dired.c/1.96/Sun Feb 28 00:19:25 2021// 19 | /display.c/1.48/Sun Feb 28 00:19:14 2021// 20 | /echo.c/1.66/Sun Feb 28 00:19:14 2021// 21 | /file.c/1.102/Sun Feb 28 00:19:14 2021// 22 | /fileio.c/1.107/Result of merge// 23 | /funmap.h/1.8/Sun Feb 28 00:19:14 2021// 24 | /grep.c/1.48/Sun Feb 28 00:19:14 2021// 25 | /help.c/1.35/Sun Feb 28 00:19:14 2021// 26 | /interpreter.c/1.6/Sun Feb 28 00:19:25 2021// 27 | /kbd.c/1.34/Sun Feb 28 00:19:14 2021// 28 | /kbd.h/1.19/Sun Feb 28 00:19:14 2021// 29 | /key.h/1.6/Sun Feb 28 00:19:14 2021// 30 | /keymap.c/1.58/Sun Feb 28 00:19:14 2021// 31 | /line.c/1.62/Sun Feb 28 00:19:14 2021// 32 | /log.c/1.11/Sun Feb 28 00:19:14 2021// 33 | /log.h/1.5/Sun Feb 28 00:19:14 2021// 34 | /macro.c/1.16/Sun Feb 28 00:19:14 2021// 35 | /macro.h/1.7/Sun Feb 28 00:19:14 2021// 36 | /main.c/1.88/Result of merge// 37 | /match.c/1.21/Sun Feb 28 00:19:14 2021// 38 | /mg.1/1.120/Sun Feb 28 00:19:25 2021// 39 | /modes.c/1.21/Sun Feb 28 00:19:14 2021// 40 | /paragraph.c/1.46/Sun Feb 28 00:19:14 2021// 41 | /pathnames.h/1.1/Sun Feb 28 00:19:14 2021// 42 | /re_search.c/1.35/Sun Feb 28 00:19:14 2021// 43 | /region.c/1.38/Sun Feb 28 00:19:14 2021// 44 | /search.c/1.47/Sun Feb 28 00:19:14 2021// 45 | /spawn.c/1.12/Sun Feb 28 00:19:14 2021// 46 | /tty.c/1.37/Sun Feb 28 00:19:14 2021// 47 | /ttyio.c/1.38/Sun Feb 28 00:19:14 2021// 48 | /ttykbd.c/1.20/Sun Feb 28 00:19:25 2021// 49 | /tutorial/1.18/Sun Feb 28 00:19:14 2021// 50 | /undo.c/1.58/Sun Feb 28 00:19:14 2021// 51 | /util.c/1.42/Sun Feb 28 00:19:14 2021// 52 | /window.c/1.36/Sun Feb 28 00:19:14 2021// 53 | /yank.c/1.14/Sun Feb 28 00:19:14 2021// 54 | D 55 | -------------------------------------------------------------------------------- /mg/CVS/Repository: -------------------------------------------------------------------------------- 1 | src/usr.bin/mg 2 | -------------------------------------------------------------------------------- /mg/CVS/Root: -------------------------------------------------------------------------------- 1 | anoncvs@ftp4.usa.openbsd.org:/cvs 2 | -------------------------------------------------------------------------------- /mg/NOTES.md: -------------------------------------------------------------------------------- 1 | NOTES 2 | 3 | * On some Linux distributions, the backspace key doesn't work 4 | correctly. This can be fixed with `bsmap-mode`; this should probably 5 | be placed in the user's `$HOME/.mg` file. 6 | -------------------------------------------------------------------------------- /mg/README-Mg: -------------------------------------------------------------------------------- 1 | [This is an edited version of the original mg README, updated slightly to 2 | reflect changes in the last 20 years.] 3 | 4 | 5 | Mg (mg) is a Public Domain EMACS style editor. It is "broadly" 6 | compatible with GNU Emacs, the latest creation of Richard M. 7 | Stallman, Chief GNUisance and inventor of Emacs. GNU Emacs (and other 8 | portions of GNU as they are released) are essentially free, (there are 9 | handling charges for obtaining it) and so is Mg. You may never have 10 | to learn another editor. (But probably will, at least long enough to 11 | port Mg...) Mg was formerly named MicroGnuEmacs, the name change was 12 | done at the request of Richard Stallman. 13 | 14 | Mg is not associated with the GNU project, and it does not have the 15 | copyright restrictions present in GNU Emacs. (However, some modules 16 | do have copyright notices.) The Mg authors individually may or may 17 | not agree with the opinions expressed by Richard Stallman in "The GNU 18 | Manifesto". 19 | 20 | This program is intended to be a small, fast, and portable editor for 21 | people who can't (or don't want to) run real Emacs for one reason 22 | or another. It is compatible with GNU because there shouldn't be 23 | any reason to learn more than one Emacs flavor. 24 | 25 | 26 | Beyond the work of Dave Conroy, author of the original public domain 27 | v30, the current version contains the work of: 28 | 29 | blarson@ecla.usc.edu Bob Larson 30 | mic@emx.utexas.edu Mic Kaczmarczik 31 | mwm@violet.berkeley.edu Mike Meyer 32 | sandra@cs.utah.edu Sandra Loosemore 33 | mp1u+@andrew.cmu.edu Michael Portuesi 34 | RCKG01M@CALSTATE.BITNET Stephen Walton 35 | hakanson@mist.cs.orst.edu Marion Hakanson 36 | 37 | People who have worked on previous versions of Mg: 38 | 39 | rtech!daveb@sun.com Dave Brower 40 | 41 | Early release history: 42 | 43 | * Nov 16, 1986: First release to mod.sources 44 | * Mar 3, 1987: First Release (mg1a) via comp.sources.unix 45 | * May 26, 1988: Second release: (mg2a) via comp.sources.misc 46 | * Jan 26, 1992: Linux port released by Charles Hedrick. This version 47 | later makes its way onto tsx-11, Infomagic, and various other Linux 48 | repositories. 49 | * Feb 25, 2000: First import into the OpenBSD tree, where it is 50 | currently maintained with contributions from many others. 51 | 52 | ---------------------------------------------------------------------- 53 | 54 | Known limitations: 55 | 56 | Recursive bindings may cause help and key rebinding code to go into 57 | an infinite loop, aborting with a stack overflow. 58 | 59 | Overwrite mode does not work in macros. (Characters are inserted 60 | rather than overwriting.) 61 | 62 | Dired mode has some problems: Rename does not update the buffer. 63 | Doing a dired again will update the buffer (whether it needs it or 64 | not) and will lose any marks for deletion. .. and . are not 65 | recognized as special cases. 66 | 67 | On systems with 16 bit integers, the kill buffer cannot exceed 32767 68 | bytes. 69 | 70 | Unlike GNU Emacs, Mg's minibuffer isn't multi-line aware and hence 71 | some commands like "shell-command-on-region" always pop up a buffer to 72 | display output irrespective of output's size. 73 | 74 | While navigating source code using Mg's cscope commands, the cursor 75 | is always at the match location rather than in *cscope* buffer. Mg uses 76 | the same keybindings of GNU Emacs's xcscope package for it's cscope commands. 77 | As Mg's keybindings are case-insensitive some of the commands don't have a 78 | default keybinding. 79 | 80 | New implementation oddities: 81 | 82 | insert and define-key are new commands corresponding to the mocklisp 83 | functions in GNU Emacs. (Mg does not have non-command functions.) 84 | (Mg's insert will only insert one string.) 85 | 86 | The display wrap code does not work at all like that of GNU emacs. 87 | 88 | Some commands that do not mimic emacs exactly don't have a "standard" 89 | emacs name. For example 'backup-to-home-directory' is only a partial 90 | implementation of emacs' range of commands that allow a user to 91 | customise the backup file location. If a more complete implementation 92 | were coded of these commands the non standard commands would probably 93 | be removed. 94 | -------------------------------------------------------------------------------- /mg/README.md: -------------------------------------------------------------------------------- 1 | This is a port of text editor MG to "next-generation" BASIC Engine platforms 2 | (H3 and SDL). 3 | 4 | It is a tech demo and not very well tested. Its primary purpose is to 5 | demonstrate the changes required to port a text-mode program with a 6 | large interface footprint and many dependencies to Engine BASIC. 7 | 8 | To compile the program, run `mg.bas`. (Make sure you have the required 9 | header files in the root directory in the sub-folder "include".) 10 | 11 | After compilation you can call the editor with `|mg` and the file(s) you 12 | want to edit as parameters. 13 | 14 | The required changes to the original code were numerous and can be found by 15 | searching the source code for `ENGINEBASIC`. 16 | 17 | Several features that rely on external commands have been disabled, namely 18 | dired, cscope, ctags, diff and grep. 19 | 20 | 21 | 22 | Original README: 23 | 24 | 25 | mg 26 | == 27 | This is a portable version of the Mg editor from OpenBSD. 28 | 29 | Mg is intended to be a small, fast, and portable editor for people who 30 | can't (or don't want to) run emacs for one reason or another, or are not 31 | familiar with the vi editor. It is compatible with emacs because there 32 | shouldn't be any reason to learn more editor types than emacs or vi. 33 | 34 | This repository aggressively tracks upstream. 35 | 36 | Compiling 37 | --------- 38 | `mg` has a simple configure script that generates a `POSIX` `Makefile`. 39 | ``` 40 | $ ./configure 41 | $ make 42 | $ sudo make install 43 | ``` 44 | 45 | Dependencies 46 | ------------ 47 | By default, you need the ncurses library. 48 | 49 | NetBSD users will use the in-base NetBSD curses library. 50 | 51 | If you do not have the ncurses library, you can call `configure` with the 52 | `--with-builtin-curses` flag to compile with a simplified version of the 53 | NetBSD curses library. In this setup, there are no dependencies other than 54 | the system's libc. 55 | 56 | Testing 57 | ------- 58 | Tested on recent versions of Arch, Alpine, Cygwin, Debian, DragonFly BSD, 59 | FreeBSD, Mac OS X (10.4 or later), NetBSD, Slackware, and Ubuntu. 60 | 61 | Licensing 62 | --------- 63 | Files originating from `mg` are Public Domain. Files needed for portability 64 | have their own individual license headers. 65 | All licenses are ISC or BSD. 66 | 67 | Commonly asked questions 68 | ------------------------ 69 | `mg` does not yet support UTF-8. If you would like to work on this, please 70 | reach out to the tech@ mailing list on OpenBSD. 71 | 72 | Get a tarball 73 | ------------- 74 | See the Releases tab on GitHub. 75 | The latest version is mg-6.8.1. 76 | -------------------------------------------------------------------------------- /mg/apple.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Mac OS X-specific support. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | /* 12 | * fparseln() specific operation flags. 13 | */ 14 | #define FPARSELN_UNESCESC 0x01 15 | #define FPARSELN_UNESCCONT 0x02 16 | #define FPARSELN_UNESCCOMM 0x04 17 | #define FPARSELN_UNESCREST 0x08 18 | #define FPARSELN_UNESCALL 0x0f 19 | 20 | /* struct stat compatibilty */ 21 | #define st_atim st_atimespec 22 | #define st_mtim st_mtimespec 23 | #define st_ctim st_ctimespec 24 | -------------------------------------------------------------------------------- /mg/autoexec.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: autoexec.c,v 1.17 2015/09/26 21:51:58 jasper Exp $ */ 2 | /* this file is in the public domain */ 3 | /* Author: Vincent Labrecque April 2002 */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "def.h" 13 | #include "funmap.h" 14 | 15 | struct autoexec { 16 | SLIST_ENTRY(autoexec) next; /* link in the linked list */ 17 | const char *pattern; /* Pattern to match to filenames */ 18 | PF fp; 19 | }; 20 | 21 | static SLIST_HEAD(, autoexec) autos; 22 | static int ready; 23 | 24 | 25 | #define AUTO_GROW 8 26 | /* 27 | * Return a NULL terminated array of function pointers to be called 28 | * when we open a file that matches . The list must be free(ed) 29 | * after use. 30 | */ 31 | PF * 32 | find_autoexec(const char *fname) 33 | { 34 | #ifndef ENGINEBASIC 35 | PF *pfl, *npfl; 36 | int have, used; 37 | struct autoexec *ae; 38 | 39 | if (!ready) 40 | return (NULL); 41 | 42 | pfl = NULL; 43 | have = 0; 44 | used = 0; 45 | SLIST_FOREACH(ae, &autos, next) { 46 | if (fnmatch(ae->pattern, fname, 0) == 0) { 47 | if (used >= have) { 48 | npfl = reallocarray(pfl, have + AUTO_GROW + 1, 49 | sizeof(PF)); 50 | if (npfl == NULL) 51 | panic("out of memory"); 52 | pfl = npfl; 53 | have += AUTO_GROW; 54 | } 55 | pfl[used++] = ae->fp; 56 | } 57 | } 58 | if (used) 59 | pfl[used] = NULL; 60 | 61 | return (pfl); 62 | #else 63 | return NULL; 64 | #endif 65 | } 66 | 67 | int 68 | add_autoexec(const char *pattern, const char *func) 69 | { 70 | PF fp; 71 | struct autoexec *ae; 72 | 73 | if (!ready) { 74 | SLIST_INIT(&autos); 75 | ready = 1; 76 | } 77 | fp = name_function(func); 78 | if (fp == NULL) 79 | return (FALSE); 80 | ae = malloc(sizeof(*ae)); 81 | if (ae == NULL) 82 | return (FALSE); 83 | ae->fp = fp; 84 | ae->pattern = strdup(pattern); 85 | if (ae->pattern == NULL) { 86 | free(ae); 87 | return (FALSE); 88 | } 89 | SLIST_INSERT_HEAD(&autos, ae, next); 90 | 91 | return (TRUE); 92 | } 93 | 94 | /* 95 | * Register an auto-execute hook; that is, specify a filename pattern 96 | * (conforming to the shell's filename globbing rules) and an associated 97 | * function to execute when a file matching the specified pattern 98 | * is read into a buffer. 99 | */ 100 | /* ARGSUSED */ 101 | int 102 | auto_execute(int f, int n) 103 | { 104 | char patbuf[128], funcbuf[128], *patp, *funcp; 105 | int s; 106 | 107 | if ((patp = eread("Filename pattern: ", patbuf, sizeof(patbuf), 108 | EFNEW | EFCR)) == NULL) 109 | return (ABORT); 110 | else if (patp[0] == '\0') 111 | return (FALSE); 112 | if ((funcp = eread("Execute: ", funcbuf, sizeof(funcbuf), 113 | EFNEW | EFCR | EFFUNC)) == NULL) 114 | return (ABORT); 115 | else if (funcp[0] == '\0') 116 | return (FALSE); 117 | if ((s = add_autoexec(patp, funcp)) != TRUE) 118 | return (s); 119 | return (TRUE); 120 | } 121 | -------------------------------------------------------------------------------- /mg/basename.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *dirname (char *path) 4 | { 5 | static const char dot[] = "."; 6 | char *last_slash; 7 | 8 | /* Find last '/'. */ 9 | last_slash = path != NULL ? strrchr (path, '/') : NULL; 10 | 11 | if (last_slash != NULL && last_slash != path && last_slash[1] == '\0') 12 | { 13 | /* Determine whether all remaining characters are slashes. */ 14 | char *runp; 15 | 16 | for (runp = last_slash; runp != path; --runp) 17 | if (runp[-1] != '/') 18 | break; 19 | 20 | /* The '/' is the last character, we have to look further. */ 21 | if (runp != path) 22 | last_slash = memrchr (path, '/', runp - path); 23 | } 24 | 25 | if (last_slash != NULL) 26 | { 27 | /* Determine whether all remaining characters are slashes. */ 28 | char *runp; 29 | 30 | for (runp = last_slash; runp != path; --runp) 31 | if (runp[-1] != '/') 32 | break; 33 | 34 | /* Terminate the path. */ 35 | if (runp == path) 36 | { 37 | /* The last slash is the first character in the string. We have to 38 | return "/". As a special case we have to return "//" if there 39 | are exactly two slashes at the beginning of the string. See 40 | XBD 4.10 Path Name Resolution for more information. */ 41 | if (last_slash == path + 1) 42 | ++last_slash; 43 | else 44 | last_slash = path + 1; 45 | } 46 | else 47 | last_slash = runp; 48 | 49 | last_slash[0] = '\0'; 50 | } 51 | else 52 | /* This assignment is ill-designed but the XPG specs require to 53 | return a string containing "." in any case no directory part is 54 | found and so a static and constant string is required. */ 55 | path = (char *) dot; 56 | 57 | return path; 58 | } 59 | 60 | 61 | char *basename (const char *filename) 62 | { 63 | char *p = strrchr (filename, '/'); 64 | return p ? p + 1 : (char *) filename; 65 | } 66 | -------------------------------------------------------------------------------- /mg/bell.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: bell.c,v 1.5 2019/07/17 18:18:37 lum Exp $ */ 2 | 3 | /* 4 | * This file is in the public domain. 5 | * 6 | * Author: Mark Lumsden 7 | * 8 | */ 9 | 10 | /* 11 | * Control how mg communicates with the user. 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "def.h" 20 | #include "macro.h" 21 | 22 | void 23 | bellinit(void) 24 | { 25 | doaudiblebell = 1; 26 | dovisiblebell = 0; 27 | } 28 | 29 | int 30 | dobeep_msgs(const char *msg, const char *s) 31 | { 32 | ewprintf("%s %s", msg, s); 33 | dobeep(); 34 | return (FALSE); 35 | } 36 | 37 | int 38 | dobeep_msg(const char *msg) 39 | { 40 | ewprintf("%s", msg); 41 | dobeep(); 42 | return (FALSE); 43 | } 44 | 45 | void 46 | dobeep(void) 47 | { 48 | if (doaudiblebell) { 49 | ttbeep(); 50 | } 51 | if (dovisiblebell) { 52 | sgarbf = TRUE; 53 | update(CNONE); 54 | if (inmacro) /* avoid delaying macro execution. */ 55 | return; 56 | usleep(50000); 57 | } 58 | } 59 | 60 | /* ARGSUSED */ 61 | int 62 | toggleaudiblebell(int f, int n) 63 | { 64 | if (f & FFARG) 65 | doaudiblebell = n > 0; 66 | else 67 | doaudiblebell = !doaudiblebell; 68 | 69 | return (TRUE); 70 | } 71 | 72 | /* ARGSUSED */ 73 | int 74 | togglevisiblebell(int f, int n) 75 | { 76 | if (f & FFARG) 77 | dovisiblebell = n > 0; 78 | else 79 | dovisiblebell = !dovisiblebell; 80 | 81 | return (TRUE); 82 | } 83 | -------------------------------------------------------------------------------- /mg/cclass.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1992, 1993, 1994 Henry Spencer. 3 | * Copyright (c) 1992, 1993, 1994 4 | * The Regents of the University of California. All rights reserved. 5 | * 6 | * This code is derived from software contributed to Berkeley by 7 | * Henry Spencer. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 3. All advertising materials mentioning features or use of this software 18 | * must display the following acknowledgement: 19 | * This product includes software developed by the University of 20 | * California, Berkeley and its contributors. 21 | * 4. Neither the name of the University nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software 23 | * without specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 | * SUCH DAMAGE. 36 | * 37 | * @(#)cclass.h 8.3 (Berkeley) 3/20/94 38 | * $FreeBSD: src/lib/libc/regex/cclass.h,v 1.4 2002/03/22 23:41:56 obrien Exp $ 39 | */ 40 | 41 | 42 | typedef enum {CALNUM, CALPHA, CBLANK, CCNTRL, CDIGIT, CGRAPH, 43 | CLOWER, CPRINT, CPUNCT, CSPACE, CUPPER, CXDIGIT} citype; 44 | 45 | /* character-class table */ 46 | static struct cclass { 47 | char *name; 48 | citype fidx; 49 | } cclasses[] = { 50 | {"alnum", CALNUM}, 51 | {"alpha", CALPHA}, 52 | {"blank", CBLANK}, 53 | {"cntrl", CCNTRL}, 54 | {"digit", CDIGIT}, 55 | {"graph", CGRAPH}, 56 | {"lower", CLOWER}, 57 | {"print", CPRINT}, 58 | {"punct", CPUNCT}, 59 | {"space", CSPACE}, 60 | {"upper", CUPPER}, 61 | {"xdigit", CXDIGIT}, 62 | {NULL, } 63 | }; 64 | -------------------------------------------------------------------------------- /mg/cdbr.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: cdbr.h,v 1.1 2013/12/11 01:24:08 joerg Exp $ */ 2 | /*- 3 | * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 | * All rights reserved. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Joerg Sonnenberger. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in 17 | * the documentation and/or other materials provided with the 18 | * distribution. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef _CDBR_H 35 | #define _CDBR_H 36 | 37 | #include 38 | #if defined(_KERNEL) || defined(_STANDALONE) 39 | #include 40 | #else 41 | #include 42 | #include 43 | #endif 44 | 45 | #define CDBR_DEFAULT 0 46 | 47 | struct cdbr; 48 | 49 | __BEGIN_DECLS 50 | 51 | #if !defined(_KERNEL) && !defined(_STANDALONE) 52 | struct cdbr *cdbr_open(const char *, int); 53 | #endif 54 | struct cdbr *cdbr_open_mem(void *, size_t, int, 55 | void (*)(void *, void *, size_t), void *); 56 | uint32_t cdbr_entries(struct cdbr *); 57 | int cdbr_get(struct cdbr *, uint32_t, const void **, size_t *); 58 | int cdbr_find(struct cdbr *, const void *, size_t, 59 | const void **, size_t *); 60 | void cdbr_close(struct cdbr *); 61 | 62 | __END_DECLS 63 | 64 | #endif /* _CDBR_H */ 65 | -------------------------------------------------------------------------------- /mg/cdbw.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: cdbw.h,v 1.2 2012/06/03 21:21:45 joerg Exp $ */ 2 | /*- 3 | * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 | * All rights reserved. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Joerg Sonnenberger. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in 17 | * the documentation and/or other materials provided with the 18 | * distribution. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef _CDBW_H 35 | #define _CDBW_H 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | struct cdbw; 42 | 43 | struct cdbw *cdbw_open(void); 44 | int cdbw_put(struct cdbw *, const void *, size_t, 45 | const void *, size_t); 46 | int cdbw_put_data(struct cdbw *, const void *, size_t, 47 | uint32_t *); 48 | int cdbw_put_key(struct cdbw *, const void *, size_t, 49 | uint32_t); 50 | uint32_t cdbw_stable_seeder(void); 51 | int cdbw_output(struct cdbw *, int, const char[16], 52 | uint32_t (*)(void)); 53 | void cdbw_close(struct cdbw *); 54 | 55 | #endif /* _CDBW_H */ 56 | -------------------------------------------------------------------------------- /mg/chrdef.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: chrdef.h,v 1.10 2015/03/25 12:29:03 bcallah Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * sys/default/chardef.h: character set specific #defines for Mg 2a 7 | * Warning: System specific ones exist 8 | */ 9 | 10 | /* 11 | * Casting should be at least as efficient as anding with 0xff, 12 | * and won't have the size problems. 13 | */ 14 | #define CHARMASK(c) ((unsigned char) (c)) 15 | 16 | /* 17 | * These flags, and the macros below them, 18 | * make up a do-it-yourself set of "ctype" macros that 19 | * understand the DEC multinational set, and let me ask 20 | * a slightly different set of questions. 21 | */ 22 | #define _MG_W 0x01 /* Word. */ 23 | #define _MG_U 0x02 /* Upper case letter. */ 24 | #define _MG_L 0x04 /* Lower case letter. */ 25 | #define _MG_C 0x08 /* Control. */ 26 | #define _MG_P 0x10 /* end of sentence punctuation */ 27 | #define _MG_D 0x20 /* is decimal digit */ 28 | 29 | #define ISWORD(c) ((cinfo[CHARMASK(c)]&_MG_W)!=0) 30 | #define ISCTRL(c) ((cinfo[CHARMASK(c)]&_MG_C)!=0) 31 | #define ISUPPER(c) ((cinfo[CHARMASK(c)]&_MG_U)!=0) 32 | #define ISLOWER(c) ((cinfo[CHARMASK(c)]&_MG_L)!=0) 33 | #define ISEOSP(c) ((cinfo[CHARMASK(c)]&_MG_P)!=0) 34 | #define ISDIGIT(c) ((cinfo[CHARMASK(c)]&_MG_D)!=0) 35 | #define TOUPPER(c) ((c)-0x20) 36 | #define TOLOWER(c) ((c)+0x20) 37 | 38 | /* 39 | * Generally useful thing for chars 40 | */ 41 | #define CCHR(x) ((x) ^ 0x40) /* CCHR('?') == DEL */ 42 | 43 | #define K00 256 44 | #define K01 257 45 | #define K02 258 46 | #define K03 259 47 | #define K04 260 48 | #define K05 261 49 | #define K06 262 50 | #define K07 263 51 | #define K08 264 52 | #define K09 265 53 | #define K0A 266 54 | #define K0B 267 55 | #define K0C 268 56 | #define K0D 269 57 | #define K0E 270 58 | #define K0F 271 59 | #define K10 272 60 | #define K11 273 61 | #define K12 274 62 | #define K13 275 63 | #define K14 276 64 | #define K15 277 65 | #define K16 278 66 | #define K17 279 67 | #define K18 280 68 | #define K19 281 69 | #define K1A 282 70 | #define K1B 283 71 | #define K1C 284 72 | #define K1D 285 73 | #define K1E 286 74 | #define K1F 287 75 | -------------------------------------------------------------------------------- /mg/cname.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1992, 1993, 1994 Henry Spencer. 3 | * Copyright (c) 1992, 1993, 1994 4 | * The Regents of the University of California. All rights reserved. 5 | * 6 | * This code is derived from software contributed to Berkeley by 7 | * Henry Spencer. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 3. All advertising materials mentioning features or use of this software 18 | * must display the following acknowledgement: 19 | * This product includes software developed by the University of 20 | * California, Berkeley and its contributors. 21 | * 4. Neither the name of the University nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software 23 | * without specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 | * SUCH DAMAGE. 36 | * 37 | * @(#)cname.h 8.3 (Berkeley) 3/20/94 38 | * $FreeBSD: src/lib/libc/regex/cname.h,v 1.3 2002/03/22 23:41:56 obrien Exp $ 39 | */ 40 | 41 | /* character-name table */ 42 | static struct cname { 43 | char *name; 44 | char code; 45 | } cnames[] = { 46 | {"NUL", '\0'}, 47 | {"SOH", '\001'}, 48 | {"STX", '\002'}, 49 | {"ETX", '\003'}, 50 | {"EOT", '\004'}, 51 | {"ENQ", '\005'}, 52 | {"ACK", '\006'}, 53 | {"BEL", '\007'}, 54 | {"alert", '\007'}, 55 | {"BS", '\010'}, 56 | {"backspace", '\b'}, 57 | {"HT", '\011'}, 58 | {"tab", '\t'}, 59 | {"LF", '\012'}, 60 | {"newline", '\n'}, 61 | {"VT", '\013'}, 62 | {"vertical-tab", '\v'}, 63 | {"FF", '\014'}, 64 | {"form-feed", '\f'}, 65 | {"CR", '\015'}, 66 | {"carriage-return", '\r'}, 67 | {"SO", '\016'}, 68 | {"SI", '\017'}, 69 | {"DLE", '\020'}, 70 | {"DC1", '\021'}, 71 | {"DC2", '\022'}, 72 | {"DC3", '\023'}, 73 | {"DC4", '\024'}, 74 | {"NAK", '\025'}, 75 | {"SYN", '\026'}, 76 | {"ETB", '\027'}, 77 | {"CAN", '\030'}, 78 | {"EM", '\031'}, 79 | {"SUB", '\032'}, 80 | {"ESC", '\033'}, 81 | {"IS4", '\034'}, 82 | {"FS", '\034'}, 83 | {"IS3", '\035'}, 84 | {"GS", '\035'}, 85 | {"IS2", '\036'}, 86 | {"RS", '\036'}, 87 | {"IS1", '\037'}, 88 | {"US", '\037'}, 89 | {"space", ' '}, 90 | {"exclamation-mark", '!'}, 91 | {"quotation-mark", '"'}, 92 | {"number-sign", '#'}, 93 | {"dollar-sign", '$'}, 94 | {"percent-sign", '%'}, 95 | {"ampersand", '&'}, 96 | {"apostrophe", '\''}, 97 | {"left-parenthesis", '('}, 98 | {"right-parenthesis", ')'}, 99 | {"asterisk", '*'}, 100 | {"plus-sign", '+'}, 101 | {"comma", ','}, 102 | {"hyphen", '-'}, 103 | {"hyphen-minus", '-'}, 104 | {"period", '.'}, 105 | {"full-stop", '.'}, 106 | {"slash", '/'}, 107 | {"solidus", '/'}, 108 | {"zero", '0'}, 109 | {"one", '1'}, 110 | {"two", '2'}, 111 | {"three", '3'}, 112 | {"four", '4'}, 113 | {"five", '5'}, 114 | {"six", '6'}, 115 | {"seven", '7'}, 116 | {"eight", '8'}, 117 | {"nine", '9'}, 118 | {"colon", ':'}, 119 | {"semicolon", ';'}, 120 | {"less-than-sign", '<'}, 121 | {"equals-sign", '='}, 122 | {"greater-than-sign", '>'}, 123 | {"question-mark", '?'}, 124 | {"commercial-at", '@'}, 125 | {"left-square-bracket", '['}, 126 | {"backslash", '\\'}, 127 | {"reverse-solidus", '\\'}, 128 | {"right-square-bracket",']'}, 129 | {"circumflex", '^'}, 130 | {"circumflex-accent", '^'}, 131 | {"underscore", '_'}, 132 | {"low-line", '_'}, 133 | {"grave-accent", '`'}, 134 | {"left-brace", '{'}, 135 | {"left-curly-bracket", '{'}, 136 | {"vertical-line", '|'}, 137 | {"right-brace", '}'}, 138 | {"right-curly-bracket", '}'}, 139 | {"tilde", '~'}, 140 | {"DEL", '\177'}, 141 | {NULL, 0} 142 | }; 143 | -------------------------------------------------------------------------------- /mg/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Support for non-OpenBSD systems. 3 | */ 4 | 5 | /* From OpenBSD sys/stat.h; glibc io/sys/stat.h agrees */ 6 | #ifndef DEFFILEMODE 7 | #define DEFFILEMODE 0000666 8 | #endif 9 | 10 | /* From OpenBSD regex.h */ 11 | #ifndef REG_STARTEND 12 | #define REG_STARTEND 00004 13 | #endif 14 | 15 | /* From OpenBSD sys/queue.h */ 16 | #ifndef SLIST_FOREACH_SAFE 17 | #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 18 | for ((var) = SLIST_FIRST(head); \ 19 | (var) && ((tvar) = SLIST_NEXT(var, field), 1); \ 20 | (var) = (tvar)) 21 | #endif 22 | 23 | /* From OpenBSD sys/queue.h */ 24 | #ifndef TAILQ_END 25 | #define TAILQ_END(head) NULL 26 | #endif 27 | 28 | /* From OpenBSD sys/queue.h */ 29 | #ifndef TAILQ_FOREACH_SAFE 30 | #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 31 | for ((var) = TAILQ_FIRST(head); \ 32 | (var) != TAILQ_END(head) && \ 33 | ((tvar) = TAILQ_NEXT(var, field), 1); \ 34 | (var) = (tvar)) 35 | #endif 36 | -------------------------------------------------------------------------------- /mg/config.h: -------------------------------------------------------------------------------- 1 | /* This file generated automatically by configure. */ 2 | 3 | #include "common.h" 4 | #if defined(__linux__) || defined(__CYGWIN__) 5 | #include "linux.h" 6 | #elif defined(__APPLE__) 7 | #include "apple.h" 8 | #elif defined(__FreeBSD__) 9 | #include "freebsd.h" 10 | #elif defined(__NetBSD__) 11 | #include "netbsd.h" 12 | #elif defined(__DragonFly__) 13 | #include "dragonfly.h" 14 | #endif 15 | 16 | //extern char *fparseln(FILE *, size_t *, size_t *, const char[3], int); 17 | //#define HAVE_FSTATAT 18 | //#define HAVE_FUTIMENS 19 | #define HAVE_GETLINE 20 | //#define HAVE_REALLOCARRAY 21 | extern size_t strlcat(char *, const char *, size_t); 22 | extern size_t strlcpy(char *, const char *, size_t); 23 | #define HAVE_STRNDUP 24 | extern long long strtonum(const char *, long long, long long, const char **); 25 | #define REGEX 26 | 27 | #define __unused 28 | -------------------------------------------------------------------------------- /mg/curterm.c: -------------------------------------------------------------------------------- 1 | /* $NetBSD: curterm.c,v 1.13 2017/05/04 09:42:23 roy Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Roy Marples. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "term_private.h" 38 | #include "terminfo_term.h" 39 | 40 | #ifndef __arraycount 41 | #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) 42 | #endif 43 | 44 | #ifndef __UNCONST 45 | #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) 46 | #endif 47 | 48 | TERMINAL *cur_term; 49 | 50 | /* 51 | * There is no standard way of getting a list of aliases for the 52 | * terminal. However, some applications such as telnet want to know this. 53 | * ncurses dumps the terminfo header into an undefined variable ttytype 54 | * and these applications then parse it to work out the aliases. 55 | * We should do the same for now, until a standard mechanism for getting 56 | * the information is available or the need for it goes away. 57 | */ 58 | #define NAMESIZE 256 59 | char ttytype[NAMESIZE]; 60 | 61 | static const speed_t bauds[] = { 62 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 2400, 4800, 9600, 63 | 19200, 38400, 57600, 115200, 230400, 460800, 921600 64 | }; 65 | 66 | void 67 | _ti_setospeed(TERMINAL *term) 68 | { 69 | struct termios termios; 70 | speed_t os; 71 | size_t i; 72 | 73 | term->_ospeed = 0; 74 | if (tcgetattr(term->fildes, &termios) == 0) { 75 | os = cfgetospeed(&termios); 76 | for (i = 0; i < __arraycount(bauds); i++) 77 | if (bauds[i] == os) { 78 | term->_ospeed = (short)i; 79 | break; 80 | } 81 | } 82 | } 83 | 84 | TERMINAL * 85 | set_curterm(TERMINAL *nterm) 86 | { 87 | TERMINAL *oterm; 88 | size_t l, n; 89 | char *p; 90 | 91 | oterm = cur_term; 92 | cur_term = nterm; 93 | 94 | ospeed = 0; 95 | if (cur_term == NULL) 96 | PC = '\0'; 97 | else { 98 | if (pad_char == NULL) 99 | PC = '\0'; 100 | else 101 | PC = *pad_char; 102 | _ti_setospeed(nterm); 103 | ospeed = nterm->_ospeed; 104 | 105 | p = ttytype; 106 | l = sizeof(ttytype); 107 | if ((n = strlcpy(p, nterm->name, l)) == strlen(p)) { 108 | p += n; 109 | l -= n; 110 | *p++ = '|'; 111 | l--; 112 | if (nterm->_alias && 113 | (n = strlcpy(p, nterm->_alias, l)) == strlen(p)) 114 | { 115 | p += n; 116 | l -= n; 117 | *p++ = '|'; 118 | l--; 119 | } 120 | if (nterm->desc && 121 | (n = strlcpy(p, nterm->desc, l)) == strlen(p)) 122 | { 123 | p += n; 124 | l -= n; 125 | *p++ = '|'; 126 | l--; 127 | } 128 | p--; 129 | } 130 | *p = '\0'; 131 | } 132 | 133 | return oterm; 134 | } 135 | 136 | int 137 | del_curterm(TERMINAL *oterm) 138 | { 139 | 140 | if (oterm == NULL) 141 | return ERR; 142 | free(oterm->_area); 143 | free(oterm->strs); 144 | free(oterm->nums); 145 | free(oterm->flags); 146 | free(oterm->_userdefs); 147 | free(oterm->_buf); 148 | free(oterm); 149 | if (oterm == cur_term) 150 | cur_term = NULL; 151 | return OK; 152 | } 153 | 154 | char * 155 | termname(void) 156 | { 157 | 158 | return __UNCONST(cur_term->name); 159 | } 160 | 161 | static const char * nullname = ""; 162 | 163 | char * 164 | longname(void) 165 | { 166 | 167 | if (cur_term->desc == NULL) 168 | return __UNCONST(nullname); 169 | return __UNCONST(cur_term->desc); 170 | } 171 | -------------------------------------------------------------------------------- /mg/dir.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: dir.c,v 1.31 2019/06/28 13:35:02 deraadt Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * Name: MG 2a 7 | * Directory management functions 8 | * Created: Ron Flax (ron@vsedev.vse.com) 9 | * Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "def.h" 20 | 21 | static char mgcwd[NFILEN]; 22 | 23 | /* 24 | * Initialize anything the directory management routines need. 25 | */ 26 | void 27 | dirinit(void) 28 | { 29 | mgcwd[0] = '\0'; 30 | if (getcwd(mgcwd, sizeof(mgcwd)) == NULL) 31 | ewprintf("Can't get current directory!"); 32 | if (mgcwd[0] != '\0' && !(mgcwd[0] == '/' && mgcwd[1] == '\0')) 33 | (void)strlcat(mgcwd, "/", sizeof(mgcwd)); 34 | } 35 | 36 | /* 37 | * Change current working directory. 38 | */ 39 | /* ARGSUSED */ 40 | int 41 | changedir(int f, int n) 42 | { 43 | char bufc[NFILEN], *bufp; 44 | 45 | (void)strlcpy(bufc, mgcwd, sizeof(bufc)); 46 | if ((bufp = eread("Change default directory: ", bufc, NFILEN, 47 | EFDEF | EFNEW | EFCR | EFFILE)) == NULL) 48 | return (ABORT); 49 | else if (bufp[0] == '\0') 50 | return (FALSE); 51 | /* Append trailing slash */ 52 | if (chdir(bufc) == -1) { 53 | dobeep(); 54 | ewprintf("Can't change dir to %s", bufc); 55 | return (FALSE); 56 | } 57 | if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL) { 58 | if (bufc[0] == '/') 59 | (void)strlcpy(mgcwd, bufc, sizeof(mgcwd)); 60 | else 61 | (void)strlcat(mgcwd, bufc, sizeof(mgcwd)); 62 | } 63 | if (mgcwd[strlen(mgcwd) - 1] != '/') 64 | (void)strlcat(mgcwd, "/", sizeof(mgcwd)); 65 | ewprintf("Current directory is now %s", mgcwd); 66 | return (TRUE); 67 | } 68 | 69 | /* 70 | * Show current directory. 71 | */ 72 | /* ARGSUSED */ 73 | int 74 | showcwdir(int f, int n) 75 | { 76 | ewprintf("Current directory: %s", mgcwd); 77 | return (TRUE); 78 | } 79 | 80 | int 81 | getcwdir(char *buf, size_t len) 82 | { 83 | if (strlcpy(buf, mgcwd, len) >= len) 84 | return (FALSE); 85 | 86 | return (TRUE); 87 | } 88 | 89 | /* Create the directory and it's parents. */ 90 | /* ARGSUSED */ 91 | int 92 | makedir(int f, int n) 93 | { 94 | return (ask_makedir()); 95 | } 96 | 97 | int 98 | ask_makedir(void) 99 | { 100 | 101 | char bufc[NFILEN]; 102 | char *path; 103 | 104 | if (getbufcwd(bufc, sizeof(bufc)) != TRUE) 105 | return (ABORT); 106 | if ((path = eread("Make directory: ", bufc, NFILEN, 107 | EFDEF | EFNEW | EFCR | EFFILE)) == NULL) 108 | return (ABORT); 109 | else if (path[0] == '\0') 110 | return (FALSE); 111 | 112 | return (do_makedir(path)); 113 | } 114 | 115 | int 116 | do_makedir(char *path) 117 | { 118 | #ifndef ENGINEBASIC 119 | struct stat sb; 120 | #endif 121 | int finished, ishere; 122 | mode_t dir_mode, f_mode, oumask; 123 | char *slash; 124 | 125 | if ((path = adjustname(path, TRUE)) == NULL) 126 | return (FALSE); 127 | 128 | /* Remove trailing slashes */ 129 | slash = strrchr(path, '\0'); 130 | while (--slash > path && *slash == '/') 131 | *slash = '\0'; 132 | 133 | slash = path; 134 | 135 | #ifdef ENGINEBASIC 136 | oumask = 0222; 137 | #else 138 | oumask = umask(0); 139 | #endif 140 | f_mode = 0777 & ~oumask; 141 | dir_mode = f_mode | S_IWUSR | S_IXUSR; 142 | 143 | for (;;) { 144 | slash += strspn(slash, "/"); 145 | slash += strcspn(slash, "/"); 146 | 147 | finished = (*slash == '\0'); 148 | *slash = '\0'; 149 | 150 | #ifdef ENGINEBASIC 151 | ishere = eb_file_exists(path); 152 | #else 153 | ishere = !stat(path, &sb); 154 | #endif 155 | if (finished && ishere) { 156 | dobeep(); 157 | ewprintf("Cannot create directory %s: file exists", 158 | path); 159 | return(FALSE); 160 | #ifdef ENGINEBASIC 161 | } else if (!finished && ishere && eb_is_directory(path)) { 162 | #else 163 | } else if (!finished && ishere && S_ISDIR(sb.st_mode)) { 164 | #endif 165 | *slash = '/'; 166 | continue; 167 | } 168 | 169 | if (mkdir(path, finished ? f_mode : dir_mode) == 0) { 170 | #ifndef ENGINEBASIC 171 | if (f_mode > 0777 && chmod(path, f_mode) == -1) { 172 | umask(oumask); 173 | return (ABORT); 174 | } 175 | #endif 176 | } else { 177 | #ifdef ENGINEBASIC 178 | if (!ishere || !eb_is_directory(path)) { 179 | #else 180 | if (!ishere || !S_ISDIR(sb.st_mode)) { 181 | #endif 182 | if (!ishere) { 183 | dobeep(); 184 | ewprintf("Creating directory: " 185 | "permission denied, %s", path); 186 | } else 187 | eerase(); 188 | 189 | #ifndef ENGINEBASIC 190 | umask(oumask); 191 | #endif 192 | return (FALSE); 193 | } 194 | } 195 | 196 | if (finished) 197 | break; 198 | 199 | *slash = '/'; 200 | } 201 | 202 | eerase(); 203 | #ifndef ENGINEBASIC 204 | umask(oumask); 205 | #endif 206 | return (TRUE); 207 | } 208 | -------------------------------------------------------------------------------- /mg/dragonfly.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DragonFly BSD-specific support. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | /* Defines */ 9 | #define MAXNAMLEN 255 10 | -------------------------------------------------------------------------------- /mg/enginebasic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include "def.h" 11 | 12 | //#define dbg_print(x...) fprintf(stderr, x) 13 | #define dbg_print(x...) do {} while (0) 14 | 15 | char *keystrings[] = {NULL}; 16 | 17 | int ncol; 18 | int nrow; 19 | int tceeol; 20 | int tcinsl; 21 | int tcdell; 22 | volatile int winch_flag; 23 | 24 | void panic(char *s) 25 | { 26 | printf("WAAAAAAAAAAAAAAH!!! %s\n", s); 27 | exit(1); 28 | } 29 | 30 | void ttinit(void) 31 | { 32 | dbg_print("ttinit\n"); 33 | eb_cls(); 34 | ncol = eb_csize_width(); 35 | nrow = eb_csize_height(); 36 | tceeol = ncol; 37 | tcinsl = nrow * ncol; 38 | tcdell = nrow * ncol; 39 | ttresize(); 40 | } 41 | 42 | void ttclose(void) 43 | { 44 | dbg_print("ebclose\n"); 45 | eb_enable_scrolling(1); 46 | eb_show_cursor(0); 47 | } 48 | 49 | void tttidy(void) 50 | { 51 | dbg_print("tttidy\n"); 52 | } 53 | 54 | static int saved = -1; 55 | 56 | int charswaiting(void) 57 | { 58 | return saved != -1 || eb_kbhit(); 59 | } 60 | 61 | int ttgetc(void) 62 | { 63 | int c, m; 64 | if (saved != -1) { 65 | c = saved; 66 | saved = -1; 67 | } else { 68 | c = eb_getch(); 69 | if (c >= 256) { 70 | saved = (c & 0x7f) + 1; 71 | c = 27; 72 | } else if (c == CCHR('H')) 73 | c = CCHR('?'); 74 | 75 | m = eb_last_key_event(); 76 | if (m & KEY_EVENT_ALT) 77 | c |= 0x80;//METABIT; 78 | } 79 | dbg_print("gc %d\n", c); 80 | return c; 81 | } 82 | 83 | int ttputc(int ch) 84 | { 85 | eb_putch(ch); 86 | if (ch == '\\') 87 | eb_putch(ch); 88 | return ch; 89 | } 90 | 91 | void ttmove(int row, int col) 92 | { 93 | dbg_print("ttmove %d %d\n", row, col); 94 | eb_locate(col, row); 95 | ttcol = col; 96 | ttrow = row; 97 | } 98 | 99 | void tteeol(void) 100 | { 101 | dbg_print("tteeol\n"); 102 | eb_clrtoeol(); 103 | ttrow = eb_pos_y(); 104 | ttcol = eb_pos_x(); 105 | } 106 | 107 | void tteeop(void) 108 | { 109 | dbg_print("tteeop\n"); 110 | // Only used in a single place, to clear the whole screen. 111 | eb_cls(); 112 | ttrow = eb_pos_y(); 113 | ttcol = eb_pos_x(); 114 | } 115 | 116 | void ttbeep(void) 117 | { 118 | dbg_print("beep!\n"); 119 | } 120 | 121 | void ttrev(int state) 122 | { 123 | if (state) 124 | eb_puts("\\R"); 125 | else 126 | eb_puts("\\N"); 127 | } 128 | 129 | void ttcres(char *res) 130 | { 131 | dbg_print("cres %s\n", res); 132 | } 133 | 134 | void ttcolor(int color) 135 | { 136 | dbg_print("color %d\n", color); 137 | if (color == CMODE) 138 | eb_color(eb_rgb(255,255,255), eb_rgb(192, 0, 0)); 139 | else 140 | eb_color(eb_theme_color(COL_FG), eb_theme_color(COL_BG)); 141 | } 142 | 143 | void ttnowindow(void) 144 | { 145 | dbg_print("nowindow\n"); 146 | } 147 | 148 | void ttflush(void) 149 | { 150 | dbg_print("flush\n"); 151 | } 152 | 153 | void ttopen(void) 154 | { 155 | dbg_print("ebopen\n"); 156 | eb_enable_scrolling(0); 157 | eb_show_cursor(1); 158 | } 159 | 160 | void ttinsl(int row, int bot, int nchunk) 161 | { 162 | dbg_print("ttinsl row %d bot %d nchunk %d\n", row, bot, nchunk); 163 | } 164 | 165 | void ttdell(int row, int bot, int nchunk) 166 | { 167 | dbg_print("ttdell row %d bot %d nchunk %d\n", row, bot, nchunk); 168 | } 169 | 170 | int ttwait(int msec) 171 | { 172 | dbg_print("ttwait %d\n", msec); 173 | unsigned int until = eb_tick() + msec; 174 | while (eb_tick() < until) { 175 | if (saved != -1 || eb_kbhit()) 176 | return 0; 177 | eb_process_events(); 178 | } 179 | return 1; 180 | } 181 | 182 | void ttresize(void) 183 | { 184 | dbg_print("ttresize\n"); 185 | vtresize(1, nrow, ncol); 186 | } 187 | 188 | #include "kbd.h" 189 | 190 | void ttykeymapinit(void) 191 | { 192 | dbg_print("ttykeymapinit\n"); 193 | dobindkey(fundamental_map, "previous-line", "\\E\x02"); 194 | dobindkey(fundamental_map, "next-line", "\\E\x01"); 195 | dobindkey(fundamental_map, "backward-char", "\\E\x03"); 196 | dobindkey(fundamental_map, "forward-char", "\\E\x04"); 197 | dobindkey(fundamental_map, "beginning-of-line", "\\E\x05"); 198 | dobindkey(fundamental_map, "end-of-line", "\\E\x0a"); 199 | dobindkey(fundamental_map, "scroll-up", "\\E\x08"); 200 | dobindkey(fundamental_map, "scroll-down", "\\E\x09"); 201 | dobindkey(fundamental_map, "overwrite-mode", "\\E\x07"); 202 | dobindkey(fundamental_map, "delete-char", "\\E\x06"); 203 | } 204 | -------------------------------------------------------------------------------- /mg/extensions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is in the public domain. 3 | * 4 | * Authors: 5 | * * Brian Callahan 6 | * * Kyle Isom 7 | */ 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | #include "def.h" 15 | 16 | /* 17 | * Extensions unique to Mg portable. 18 | */ 19 | 20 | int shownlprompt = TRUE; 21 | 22 | /* Check for a newline at the end of a file? */ 23 | int 24 | togglenewlineprompt(int f, int n) 25 | { 26 | 27 | if (shownlprompt == TRUE) 28 | shownlprompt = FALSE; 29 | else 30 | shownlprompt = TRUE; 31 | 32 | return (TRUE); 33 | } 34 | -------------------------------------------------------------------------------- /mg/freebsd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeBSD-specific support. 3 | */ 4 | 5 | #include 6 | #include 7 | -------------------------------------------------------------------------------- /mg/fstatat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fstatat(2) wrapper for systems that do not support it. 3 | * Written by Brian Callahan and released 4 | * into the Public Domain. 5 | */ 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "config.h" 12 | 13 | #ifndef HAVE_FSTATAT 14 | 15 | int 16 | fstatat(int fd, const char *path, struct stat *buf, int flag) 17 | { 18 | 19 | return stat(path, buf); 20 | } 21 | 22 | #endif /* !HAVE_FSTATAT */ 23 | -------------------------------------------------------------------------------- /mg/funmap.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: funmap.h,v 1.8 2019/07/11 18:20:18 lum Exp $ */ 2 | 3 | /* This file is in the public domain */ 4 | 5 | void funmap_init(void); 6 | PF name_function(const char *); 7 | const char *function_name(PF); 8 | struct list *complete_function_list(const char *); 9 | int funmap_add(PF, const char *, int); 10 | int numparams_function(PF); 11 | -------------------------------------------------------------------------------- /mg/futimens.c: -------------------------------------------------------------------------------- 1 | /* This file is in the public domain. */ 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include "config.h" 8 | 9 | #ifndef HAVE_FUTIMENS 10 | 11 | int 12 | futimens(int fildes, const struct timespec times[2]) 13 | { 14 | struct timeval timevals[2]; 15 | TIMESPEC_TO_TIMEVAL(&timevals[0], ×[0]); 16 | TIMESPEC_TO_TIMEVAL(&timevals[1], ×[1]); 17 | return futimes(fildes, timevals); 18 | } 19 | 20 | #endif /* !HAVE_FUTIMENS */ 21 | -------------------------------------------------------------------------------- /mg/getline.c: -------------------------------------------------------------------------------- 1 | /* $NetBSD: getdelim.c,v 1.2 2015/12/25 20:12:46 joerg Exp $ */ 2 | /* NetBSD-src: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp */ 3 | 4 | /*- 5 | * Copyright (c) 2011 The NetBSD Foundation, Inc. 6 | * All rights reserved. 7 | * 8 | * This code is derived from software contributed to The NetBSD Foundation 9 | * by Christos Zoulas. 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions 13 | * are met: 14 | * 1. Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | * POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | 36 | #include "config.h" 37 | 38 | #ifndef HAVE_GETLINE 39 | 40 | static ssize_t 41 | getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) 42 | { 43 | char *ptr, *eptr; 44 | 45 | 46 | if (*buf == NULL || *bufsiz == 0) { 47 | *bufsiz = BUFSIZ; 48 | if ((*buf = malloc(*bufsiz)) == NULL) 49 | return -1; 50 | } 51 | 52 | for (ptr = *buf, eptr = *buf + *bufsiz;;) { 53 | int c = fgetc(fp); 54 | if (c == -1) { 55 | if (feof(fp)) { 56 | ssize_t diff = (ssize_t)(ptr - *buf); 57 | if (diff != 0) { 58 | *ptr = '\0'; 59 | return diff; 60 | } 61 | } 62 | return -1; 63 | } 64 | *ptr++ = c; 65 | if (c == delimiter) { 66 | *ptr = '\0'; 67 | return ptr - *buf; 68 | } 69 | if (ptr + 2 >= eptr) { 70 | char *nbuf; 71 | size_t nbufsiz = *bufsiz * 2; 72 | ssize_t d = ptr - *buf; 73 | if ((nbuf = realloc(*buf, nbufsiz)) == NULL) 74 | return -1; 75 | *buf = nbuf; 76 | *bufsiz = nbufsiz; 77 | eptr = nbuf + nbufsiz; 78 | ptr = nbuf + d; 79 | } 80 | } 81 | } 82 | 83 | ssize_t 84 | getline(char **buf, size_t *bufsiz, FILE *fp) 85 | { 86 | return getdelim(buf, bufsiz, '\n', fp); 87 | } 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /mg/graph2.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: graph2.h,v 1.1 2009/08/15 16:21:05 joerg Exp $ */ 2 | /*- 3 | * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 | * All rights reserved. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Joerg Sonnenberger. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in 17 | * the documentation and/or other materials provided with the 18 | * distribution. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | /* 35 | * Implementation of common 2-graph routines: 36 | * - build a 2-graph with hash-pairs as edges 37 | * - check a 2-graph for acyclicness and compute an output order 38 | */ 39 | 40 | struct vertex2 { 41 | uint32_t l_edge, r_edge; 42 | }; 43 | 44 | struct edge2 { 45 | uint32_t left, right; 46 | uint32_t l_prev, l_next; 47 | uint32_t r_prev, r_next; 48 | }; 49 | 50 | struct graph2 { 51 | struct vertex2 *verts; 52 | struct edge2 *edges; 53 | uint32_t output_index; 54 | uint32_t *output_order; 55 | uint8_t *visited; 56 | uint32_t e, v; 57 | }; 58 | 59 | void graph2_setup(struct graph2 *, uint32_t, uint32_t); 60 | void graph2_free(struct graph2 *); 61 | 62 | int graph2_hash(struct nbperf *, struct graph2 *); 63 | int graph2_output_order(struct graph2 *graph); 64 | -------------------------------------------------------------------------------- /mg/graph3.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: graph3.h,v 1.1 2009/08/15 16:21:05 joerg Exp $ */ 2 | /*- 3 | * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 | * All rights reserved. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Joerg Sonnenberger. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in 17 | * the documentation and/or other materials provided with the 18 | * distribution. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | /* 35 | * Implementation of common 3-graph routines: 36 | * - build a 3-graph with hash-triple as edges 37 | * - check a 3-graph for acyclicness and compute an output order 38 | */ 39 | 40 | struct vertex3 { 41 | uint32_t l_edge, m_edge, r_edge; 42 | }; 43 | 44 | struct edge3 { 45 | uint32_t left, middle, right; 46 | uint32_t l_prev, m_prev, l_next; 47 | uint32_t r_prev, m_next, r_next; 48 | }; 49 | 50 | struct graph3 { 51 | struct vertex3 *verts; 52 | struct edge3 *edges; 53 | uint32_t output_index; 54 | uint32_t *output_order; 55 | uint32_t e, v; 56 | }; 57 | 58 | void graph3_setup(struct graph3 *, uint32_t, uint32_t); 59 | void graph3_free(struct graph3 *); 60 | 61 | int graph3_hash(struct nbperf *, struct graph3 *); 62 | int graph3_output_order(struct graph3 *); 63 | -------------------------------------------------------------------------------- /mg/kbd.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: kbd.h,v 1.19 2015/03/19 21:48:05 bcallah Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * kbd.h: type definitions for symbol.c and kbd.c for mg experimental 7 | */ 8 | 9 | struct map_element { 10 | KCHAR k_base; /* first key in element */ 11 | KCHAR k_num; /* last key in element */ 12 | PF *k_funcp; /* pointer to array of pointers */ 13 | /* to functions */ 14 | struct keymap_s *k_prefmap; /* keymap of ONLY prefix key in */ 15 | /* element */ 16 | }; 17 | 18 | /* 19 | * Predefined keymaps are NOT type KEYMAP because final array needs 20 | * dimension. If any changes are made to this struct, they must be reflected 21 | * in all keymap declarations. 22 | */ 23 | 24 | #define KEYMAPE(NUM) { \ 25 | short map_num; /* elements used */ \ 26 | short map_max; /* elements allocated */\ 27 | PF map_default; /* default function */ \ 28 | struct map_element map_element[NUM]; /* really [e_max] */ \ 29 | } 30 | typedef struct keymap_s KEYMAPE(1) KEYMAP; 31 | 32 | /* Number of map_elements to grow an overflowed keymap by */ 33 | #define MAPGROW 3 34 | #define MAPINIT (MAPGROW+1) 35 | 36 | /* Max number of default bindings added to avoid creating new element */ 37 | #define MAPELEDEF 4 38 | 39 | struct maps_s { 40 | KEYMAP *p_map; 41 | const char *p_name; 42 | struct maps_s *p_next; 43 | }; 44 | 45 | extern struct maps_s *maps; 46 | extern struct maps_s fundamental_mode; 47 | #define fundamental_map (fundamental_mode.p_map) 48 | 49 | int dobindkey(KEYMAP *, const char *, const char *); 50 | KEYMAP *name_map(const char *); 51 | struct maps_s *name_mode(const char *); 52 | PF doscan(KEYMAP *, int, KEYMAP **); 53 | void maps_init(void); 54 | int maps_add(KEYMAP *, const char *); 55 | 56 | extern struct map_element *ele; 57 | extern struct maps_s *defb_modes[]; 58 | -------------------------------------------------------------------------------- /mg/key.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: key.h,v 1.6 2019/06/22 15:38:15 lum Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* key.h: Insert file for mg 2 functions that need to reference key pressed */ 6 | 7 | #define MAXKEY 8 /* maximum number of prefix chars */ 8 | 9 | struct key { /* the character sequence in a key */ 10 | int k_count; /* number of chars */ 11 | KCHAR k_chars[MAXKEY]; /* chars */ 12 | }; 13 | 14 | extern struct key key; 15 | -------------------------------------------------------------------------------- /mg/linux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Linux-specific support. (Cygwin as well.) 3 | */ 4 | 5 | #ifdef __CYGWIN__ 6 | #include 7 | #endif 8 | #include 9 | 10 | #include 11 | 12 | /* Defines */ 13 | #define TCSASOFT 0 14 | -------------------------------------------------------------------------------- /mg/log.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: log.h,v 1.5 2019/07/18 10:50:24 lum Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * Specifically for mg logging functionality. 7 | * 8 | */ 9 | int mglog(PF, KEYMAP *); 10 | int mgloginit(void); 11 | int mglog_execbuf( const char* const, 12 | const char* const, 13 | const char* const, 14 | const char* const, 15 | const int, 16 | const int, 17 | const char* const, 18 | const char* const, 19 | const char* const 20 | ); 21 | 22 | int mglog_isvar( const char* const, 23 | const char* const, 24 | const int 25 | ); 26 | 27 | char *mglogpath_lines; 28 | char *mglogpath_undo; 29 | char *mglogpath_window; 30 | char *mglogpath_key; 31 | char *mglogpath_interpreter; 32 | -------------------------------------------------------------------------------- /mg/macro.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: macro.c,v 1.16 2015/03/19 21:22:15 bcallah Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * Keyboard macros. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "def.h" 15 | #include "key.h" 16 | #include "macro.h" 17 | 18 | int inmacro = FALSE; /* Macro playback in progess */ 19 | int macrodef = FALSE; /* Macro recording in progress */ 20 | int macrocount = 0; 21 | 22 | struct line *maclhead = NULL; 23 | struct line *maclcur; 24 | 25 | union macrodef macro[MAXMACRO]; 26 | 27 | /* ARGSUSED */ 28 | int 29 | definemacro(int f, int n) 30 | { 31 | struct line *lp1, *lp2; 32 | 33 | macrocount = 0; 34 | 35 | if (macrodef) { 36 | ewprintf("already defining macro"); 37 | return (macrodef = FALSE); 38 | } 39 | 40 | /* free lines allocated for string arguments */ 41 | if (maclhead != NULL) { 42 | for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) { 43 | lp2 = lp1->l_fp; 44 | free(lp1); 45 | } 46 | free(lp1); 47 | } 48 | 49 | if ((maclhead = lp1 = lalloc(0)) == NULL) 50 | return (FALSE); 51 | 52 | ewprintf("Defining Keyboard Macro..."); 53 | maclcur = lp1->l_fp = lp1->l_bp = lp1; 54 | return (macrodef = TRUE); 55 | } 56 | 57 | /* ARGSUSED */ 58 | int 59 | finishmacro(int f, int n) 60 | { 61 | if (macrodef == TRUE) { 62 | macrodef = FALSE; 63 | ewprintf("End Keyboard Macro Definition"); 64 | return (TRUE); 65 | } 66 | return (FALSE); 67 | } 68 | 69 | /* ARGSUSED */ 70 | int 71 | executemacro(int f, int n) 72 | { 73 | int i, j, flag, num; 74 | PF funct; 75 | 76 | if (macrodef || 77 | (macrocount >= MAXMACRO && macro[MAXMACRO - 1].m_funct 78 | != finishmacro)) { 79 | dobeep(); 80 | ewprintf("Macro too long. Aborting."); 81 | return (FALSE); 82 | } 83 | 84 | if (macrocount == 0) 85 | return (TRUE); 86 | 87 | inmacro = TRUE; 88 | 89 | for (i = n; i > 0; i--) { 90 | maclcur = maclhead->l_fp; 91 | flag = 0; 92 | num = 1; 93 | for (j = 0; j < macrocount - 1; j++) { 94 | funct = macro[j].m_funct; 95 | if (funct == universal_argument) { 96 | flag = FFARG; 97 | num = macro[++j].m_count; 98 | continue; 99 | } 100 | if ((*funct)(flag, num) != TRUE) { 101 | inmacro = FALSE; 102 | return (FALSE); 103 | } 104 | lastflag = thisflag; 105 | thisflag = 0; 106 | flag = 0; 107 | num = 1; 108 | } 109 | } 110 | inmacro = FALSE; 111 | return (TRUE); 112 | } 113 | -------------------------------------------------------------------------------- /mg/macro.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: macro.h,v 1.7 2005/11/18 20:56:53 deraadt Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* definitions for keyboard macros */ 6 | 7 | #define MAXMACRO 256 /* maximum functs in a macro */ 8 | 9 | extern int inmacro; 10 | extern int macrodef; 11 | extern int macrocount; 12 | 13 | union macrodef { 14 | PF m_funct; 15 | int m_count; /* for count-prefix */ 16 | }; 17 | 18 | extern union macrodef macro[MAXMACRO]; 19 | 20 | extern struct line *maclhead; 21 | extern struct line *maclcur; 22 | -------------------------------------------------------------------------------- /mg/mg.bas: -------------------------------------------------------------------------------- 1 | tcc "autoexec.c" 2 | tcc "basic.c" 3 | tcc "bell.c" 4 | tcc "buffer.c" 5 | tcc "cinfo.c" 6 | tcc "cmode.c" 7 | tcc "dir.c" 8 | tcc "display.c" 9 | tcc "echo.c" 10 | tcc "extend.c" 11 | tcc "file.c" 12 | tcc "fileio.c" 13 | tcc "funmap.c" 14 | tcc "help.c" 15 | tcc "kbd.c" 16 | tcc "keymap.c" 17 | tcc "line.c" 18 | tcc "macro.c" 19 | tcc "match.c" 20 | tcc "modes.c" 21 | tcc "paragraph.c" 22 | tcc "region.c" 23 | tcc "re_search.c" 24 | tcc "search.c" 25 | tcc "undo.c" 26 | tcc "version.c" 27 | tcc "window.c" 28 | tcc "word.c" 29 | tcc "yank.c" 30 | tcc "enginebasic.c" 31 | tcc "main.c" 32 | tcc "util.c" 33 | tcc "reallocarray.c" 34 | tcc "strtonum.c" 35 | tcc "strlcat.c" 36 | tcc "strlcpy.c" 37 | tcc "basename.c" 38 | tcc "interpreter.c" 39 | tcc "extensions.c" 40 | tcc "regcomp.c" 41 | tcc "regerror.c" 42 | tcc "regexec.c" 43 | tcc "regfree.c" 44 | tcclink "mg" 45 | -------------------------------------------------------------------------------- /mg/modes.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: modes.c,v 1.21 2017/05/30 07:05:22 florian Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * Commands to toggle modes. Without an argument, these functions will 7 | * toggle the given mode. A negative or zero argument will turn the mode 8 | * off. A positive argument will turn the mode on. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "def.h" 17 | #include "kbd.h" 18 | 19 | int changemode(int, int, char *); 20 | 21 | int defb_nmodes = 0; 22 | struct maps_s *defb_modes[PBMODES] = { &fundamental_mode }; 23 | int defb_flag = 0; 24 | 25 | int 26 | changemode(int f, int n, char *newmode) 27 | { 28 | int i; 29 | struct maps_s *m; 30 | 31 | if ((m = name_mode(newmode)) == NULL) { 32 | dobeep(); 33 | ewprintf("Can't find mode %s", newmode); 34 | return (FALSE); 35 | } 36 | if (!(f & FFARG)) { 37 | for (i = 0; i <= curbp->b_nmodes; i++) 38 | if (curbp->b_modes[i] == m) { 39 | /* mode already set */ 40 | n = 0; 41 | break; 42 | } 43 | } 44 | if (n > 0) { 45 | for (i = 0; i <= curbp->b_nmodes; i++) 46 | if (curbp->b_modes[i] == m) 47 | /* mode already set */ 48 | return (TRUE); 49 | if (curbp->b_nmodes >= PBMODES - 1) { 50 | dobeep(); 51 | ewprintf("Too many modes"); 52 | return (FALSE); 53 | } 54 | curbp->b_modes[++(curbp->b_nmodes)] = m; 55 | } else { 56 | /* fundamental is b_modes[0] and can't be unset */ 57 | for (i = 1; i <= curbp->b_nmodes && m != curbp->b_modes[i]; 58 | i++); 59 | if (i > curbp->b_nmodes) 60 | return (TRUE); /* mode wasn't set */ 61 | for (; i < curbp->b_nmodes; i++) 62 | curbp->b_modes[i] = curbp->b_modes[i + 1]; 63 | curbp->b_nmodes--; 64 | } 65 | upmodes(curbp); 66 | return (TRUE); 67 | } 68 | 69 | int 70 | indentmode(int f, int n) 71 | { 72 | return (changemode(f, n, "indent")); 73 | } 74 | 75 | int 76 | fillmode(int f, int n) 77 | { 78 | return (changemode(f, n, "fill")); 79 | } 80 | 81 | #ifdef NOTAB 82 | int 83 | notabmode(int f, int n) 84 | { 85 | if (changemode(f, n, "notab") == FALSE) 86 | return (FALSE); 87 | if (f & FFARG) { 88 | if (n <= 0) 89 | curbp->b_flag &= ~BFNOTAB; 90 | else 91 | curbp->b_flag |= BFNOTAB; 92 | } else 93 | curbp->b_flag ^= BFNOTAB; 94 | return (TRUE); 95 | } 96 | #endif /* NOTAB */ 97 | 98 | int 99 | overwrite_mode(int f, int n) 100 | { 101 | if (changemode(f, n, "overwrite") == FALSE) 102 | return (FALSE); 103 | if (f & FFARG) { 104 | if (n <= 0) 105 | curbp->b_flag &= ~BFOVERWRITE; 106 | else 107 | curbp->b_flag |= BFOVERWRITE; 108 | } else 109 | curbp->b_flag ^= BFOVERWRITE; 110 | return (TRUE); 111 | } 112 | 113 | int 114 | set_default_mode(int f, int n) 115 | { 116 | int i; 117 | struct maps_s *m; 118 | char modebuf[32], *bufp; 119 | 120 | if ((bufp = eread("Set Default Mode: ", modebuf, sizeof(modebuf), 121 | EFNEW)) == NULL) 122 | return (ABORT); 123 | else if (bufp[0] == '\0') 124 | return (FALSE); 125 | if ((m = name_mode(modebuf)) == NULL) { 126 | dobeep(); 127 | ewprintf("can't find mode %s", modebuf); 128 | return (FALSE); 129 | } 130 | if (!(f & FFARG)) { 131 | for (i = 0; i <= defb_nmodes; i++) 132 | if (defb_modes[i] == m) { 133 | /* mode already set */ 134 | n = 0; 135 | break; 136 | } 137 | } 138 | if (n > 0) { 139 | for (i = 0; i <= defb_nmodes; i++) 140 | if (defb_modes[i] == m) 141 | /* mode already set */ 142 | return (TRUE); 143 | if (defb_nmodes >= PBMODES - 1) { 144 | dobeep(); 145 | ewprintf("Too many modes"); 146 | return (FALSE); 147 | } 148 | defb_modes[++defb_nmodes] = m; 149 | } else { 150 | /* fundamental is defb_modes[0] and can't be unset */ 151 | for (i = 1; i <= defb_nmodes && m != defb_modes[i]; i++); 152 | if (i > defb_nmodes) 153 | /* mode was not set */ 154 | return (TRUE); 155 | for (; i < defb_nmodes; i++) 156 | defb_modes[i] = defb_modes[i + 1]; 157 | defb_nmodes--; 158 | } 159 | if (strcmp(modebuf, "overwrite") == 0) { 160 | if (n <= 0) 161 | defb_flag &= ~BFOVERWRITE; 162 | else 163 | defb_flag |= BFOVERWRITE; 164 | } 165 | #ifdef NOTAB 166 | if (strcmp(modebuf, "notab") == 0) { 167 | if (n <= 0) 168 | defb_flag &= ~BFNOTAB; 169 | else 170 | defb_flag |= BFNOTAB; 171 | } 172 | #endif /* NOTAB */ 173 | return (TRUE); 174 | } 175 | -------------------------------------------------------------------------------- /mg/nbperf-chm3.c: -------------------------------------------------------------------------------- 1 | /* $NetBSD: nbperf-chm3.c,v 1.1 2009/08/15 16:21:05 joerg Exp $ */ 2 | 3 | #define BUILD_CHM3 4 | #include "nbperf-chm.c" 5 | -------------------------------------------------------------------------------- /mg/nbperf.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: nbperf.h,v 1.4 2013/01/31 16:32:02 joerg Exp $ */ 2 | /*- 3 | * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 | * All rights reserved. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Joerg Sonnenberger. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in 17 | * the documentation and/or other materials provided with the 18 | * distribution. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | #define NBPERF_MAX_HASH_SIZE 3 35 | 36 | struct nbperf { 37 | FILE *output; 38 | FILE *map_output; 39 | const char *hash_name; 40 | int static_hash; 41 | size_t n; 42 | const void * __restrict * keys; 43 | const size_t *keylens; 44 | int first_round, has_duplicates; 45 | 46 | double c; 47 | 48 | size_t hash_size; 49 | void (*seed_hash)(struct nbperf *); 50 | void (*print_hash)(struct nbperf *, const char *, const char *, const char *, 51 | const char *); 52 | void (*compute_hash)(struct nbperf *, const void *, size_t, 53 | uint32_t *); 54 | uint32_t seed[1]; 55 | }; 56 | 57 | int chm_compute(struct nbperf *); 58 | int chm3_compute(struct nbperf *); 59 | int bpz_compute(struct nbperf *); 60 | void mi_vector_hash(const void * __restrict, size_t, uint32_t, 61 | uint32_t[3]); 62 | -------------------------------------------------------------------------------- /mg/netbsd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * NetBSD-specific support. 3 | */ 4 | 5 | #include 6 | -------------------------------------------------------------------------------- /mg/pathnames.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: pathnames.h,v 1.1 2012/06/18 07:14:55 jasper Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * standard path names 7 | */ 8 | 9 | #define _PATH_MG_DIR "~/.mg.d" 10 | #define _PATH_MG_STARTUP "%s/.mg" 11 | #define _PATH_MG_TERM "%s/.mg-%s" 12 | -------------------------------------------------------------------------------- /mg/reallocarray.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ 2 | /* 3 | * Copyright (c) 2008 Otto Moerbeek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "config.h" 24 | 25 | #ifndef HAVE_REALLOCARRAY 26 | 27 | /* 28 | * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX 29 | * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW 30 | */ 31 | #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) 32 | 33 | void * 34 | reallocarray(void *optr, size_t nmemb, size_t size) 35 | { 36 | if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && 37 | nmemb > 0 && SIZE_MAX / nmemb < size) { 38 | errno = ENOMEM; 39 | return NULL; 40 | } 41 | return realloc(optr, size * nmemb); 42 | } 43 | 44 | #endif /* !HAVE_REALLOCARRAY */ 45 | -------------------------------------------------------------------------------- /mg/regfree.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1992, 1993, 1994 Henry Spencer. 3 | * Copyright (c) 1992, 1993, 1994 4 | * The Regents of the University of California. All rights reserved. 5 | * 6 | * This code is derived from software contributed to Berkeley by 7 | * Henry Spencer. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 4. Neither the name of the University nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | * 33 | * @(#)regfree.c 8.3 (Berkeley) 3/20/94 34 | */ 35 | 36 | #ifndef _NO_REGEX 37 | 38 | #if defined(LIBC_SCCS) && !defined(lint) 39 | static char sccsid[] = "@(#)regfree.c 8.3 (Berkeley) 3/20/94"; 40 | #endif /* LIBC_SCCS and not lint */ 41 | #include 42 | 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | 49 | #include "utils.h" 50 | #include "regex2.h" 51 | 52 | /* 53 | - regfree - free everything 54 | = extern void regfree(regex_t *); 55 | */ 56 | void 57 | regfree(preg) 58 | regex_t *preg; 59 | { 60 | struct re_guts *g; 61 | 62 | if (preg->re_magic != MAGIC1) /* oops */ 63 | return; /* nice to complain, but hard */ 64 | 65 | g = preg->re_g; 66 | if (g == NULL || g->magic != MAGIC2) /* oops again */ 67 | return; 68 | preg->re_magic = 0; /* mark it invalid */ 69 | g->magic = 0; /* mark it invalid */ 70 | 71 | if (g->strip != NULL) 72 | free((char *)g->strip); 73 | if (g->sets != NULL) 74 | free((char *)g->sets); 75 | if (g->setbits != NULL) 76 | free((char *)g->setbits); 77 | if (g->must != NULL) 78 | free(g->must); 79 | if (g->charjump != NULL) 80 | free(&g->charjump[CHAR_MIN]); 81 | if (g->matchjump != NULL) 82 | free(g->matchjump); 83 | free((char *)g); 84 | } 85 | 86 | #endif /* !_NO_REGEX */ 87 | -------------------------------------------------------------------------------- /mg/spawn.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: spawn.c,v 1.12 2015/03/19 21:22:15 bcallah Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * Spawn. Actually just suspends Mg. 7 | * Assumes POSIX job control. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "def.h" 17 | 18 | /* 19 | * This causes mg to send itself a stop signal. It assumes the parent 20 | * shell supports POSIX job control. If the terminal supports an alternate 21 | * screen, we will switch to it. 22 | */ 23 | /* ARGSUSED */ 24 | int 25 | spawncli(int f, int n) 26 | { 27 | sigset_t oset; 28 | 29 | /* Very similar to what vttidy() does. */ 30 | ttcolor(CTEXT); 31 | ttnowindow(); 32 | ttmove(nrow - 1, 0); 33 | if (epresf != FALSE) { 34 | tteeol(); 35 | epresf = FALSE; 36 | } 37 | if (ttcooked() == FALSE) 38 | return (FALSE); 39 | 40 | /* Exit application mode and tidy. */ 41 | tttidy(); 42 | ttflush(); 43 | (void)sigprocmask(SIG_SETMASK, NULL, &oset); 44 | (void)kill(0, SIGTSTP); 45 | (void)sigprocmask(SIG_SETMASK, &oset, NULL); 46 | ttreinit(); 47 | 48 | /* Force repaint. */ 49 | sgarbf = TRUE; 50 | return (ttraw()); 51 | } 52 | -------------------------------------------------------------------------------- /mg/strlcat.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 1998, 2015 Todd C. Miller 5 | * 6 | * Permission to use, copy, modify, and distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "config.h" 23 | 24 | #ifndef HAVE_STRLCAT 25 | 26 | /* 27 | * Appends src to string dst of size dsize (unlike strncat, dsize is the 28 | * full size of dst, not space left). At most dsize-1 characters 29 | * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). 30 | * Returns strlen(src) + MIN(dsize, strlen(initial dst)). 31 | * If retval >= dsize, truncation occurred. 32 | */ 33 | size_t 34 | strlcat(char *dst, const char *src, size_t dsize) 35 | { 36 | const char *odst = dst; 37 | const char *osrc = src; 38 | size_t n = dsize; 39 | size_t dlen; 40 | 41 | /* Find the end of dst and adjust bytes left but don't go past end. */ 42 | while (n-- != 0 && *dst != '\0') 43 | dst++; 44 | dlen = dst - odst; 45 | n = dsize - dlen; 46 | 47 | if (n-- == 0) 48 | return(dlen + strlen(src)); 49 | while (*src != '\0') { 50 | if (n != 0) { 51 | *dst++ = *src; 52 | n--; 53 | } 54 | src++; 55 | } 56 | *dst = '\0'; 57 | 58 | return(dlen + (src - osrc)); /* count does not include NUL */ 59 | } 60 | 61 | #endif /* !HAVE_STRLCAT */ 62 | -------------------------------------------------------------------------------- /mg/strlcpy.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 1998, 2015 Todd C. Miller 5 | * 6 | * Permission to use, copy, modify, and distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #include "config.h" 23 | 24 | #ifndef HAVE_STRLCPY 25 | 26 | /* 27 | * Copy string src to buffer dst of size dsize. At most dsize-1 28 | * chars will be copied. Always NUL terminates (unless dsize == 0). 29 | * Returns strlen(src); if retval >= dsize, truncation occurred. 30 | */ 31 | size_t 32 | strlcpy(char *dst, const char *src, size_t dsize) 33 | { 34 | const char *osrc = src; 35 | size_t nleft = dsize; 36 | 37 | /* Copy as many bytes as will fit. */ 38 | if (nleft != 0) { 39 | while (--nleft != 0) { 40 | if ((*dst++ = *src++) == '\0') 41 | break; 42 | } 43 | } 44 | 45 | /* Not enough room in dst, add NUL and traverse rest of src. */ 46 | if (nleft == 0) { 47 | if (dsize != 0) 48 | *dst = '\0'; /* NUL-terminate dst */ 49 | while (*src++) 50 | ; 51 | } 52 | 53 | return(src - osrc - 1); /* count does not include NUL */ 54 | } 55 | 56 | #endif /* !HAVE_STRLCPY */ 57 | -------------------------------------------------------------------------------- /mg/strndup.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: strndup.c,v 1.3 2019/01/25 00:19:25 millert Exp $ */ 2 | /* $OpenBSD: strnlen.c,v 1.9 2019/01/25 00:19:25 millert Exp $ */ 3 | 4 | /* 5 | * Copyright (c) 2010 Todd C. Miller 6 | * 7 | * Permission to use, copy, modify, and distribute this software for any 8 | * purpose with or without fee is hereby granted, provided that the above 9 | * copyright notice and this permission notice appear in all copies. 10 | * 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "config.h" 25 | 26 | #ifndef HAVE_STRNDUP 27 | 28 | static size_t 29 | strnlen(const char *str, size_t maxlen) 30 | { 31 | const char *cp; 32 | 33 | for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) 34 | ; 35 | 36 | return (size_t)(cp - str); 37 | } 38 | 39 | char * 40 | strndup(const char *str, size_t maxlen) 41 | { 42 | char *copy; 43 | size_t len; 44 | 45 | len = strnlen(str, maxlen); 46 | copy = malloc(len + 1); 47 | if (copy != NULL) { 48 | (void)memcpy(copy, str, len); 49 | copy[len] = '\0'; 50 | } 51 | 52 | return copy; 53 | } 54 | 55 | #endif /* !HAVE_STRNDUP */ 56 | -------------------------------------------------------------------------------- /mg/strtonum.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 2004 Ted Unangst and Todd Miller 5 | * All rights reserved. 6 | * 7 | * Permission to use, copy, modify, and distribute this software for any 8 | * purpose with or without fee is hereby granted, provided that the above 9 | * copyright notice and this permission notice appear in all copies. 10 | * 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "config.h" 25 | 26 | #ifndef HAVE_STRTONUM 27 | 28 | #define INVALID 1 29 | #define TOOSMALL 2 30 | #define TOOLARGE 3 31 | 32 | long long 33 | strtonum(const char *numstr, long long minval, long long maxval, 34 | const char **errstrp) 35 | { 36 | long long ll = 0; 37 | int error = 0; 38 | char *ep; 39 | struct errval { 40 | const char *errstr; 41 | int err; 42 | } ev[4] = { 43 | { NULL, 0 }, 44 | { "invalid", EINVAL }, 45 | { "too small", ERANGE }, 46 | { "too large", ERANGE }, 47 | }; 48 | 49 | ev[0].err = errno; 50 | errno = 0; 51 | if (minval > maxval) { 52 | error = INVALID; 53 | } else { 54 | ll = strtoll(numstr, &ep, 10); 55 | if (numstr == ep || *ep != '\0') 56 | error = INVALID; 57 | else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) 58 | error = TOOSMALL; 59 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) 60 | error = TOOLARGE; 61 | } 62 | if (errstrp != NULL) 63 | *errstrp = ev[error].errstr; 64 | errno = ev[error].err; 65 | if (error) 66 | ll = 0; 67 | 68 | return (ll); 69 | } 70 | 71 | #endif /* !HAVE_STRTONUM */ 72 | -------------------------------------------------------------------------------- /mg/terminfo_termcap.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: termcap.h,v 1.2 2011/04/11 21:13:09 roy Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc. 5 | * 6 | * This code is derived from software contributed to The NetBSD Foundation 7 | * by Roy Marples. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _TERMCAP_H_ 31 | #define _TERMCAP_H_ 32 | 33 | #include 34 | 35 | __BEGIN_DECLS 36 | 37 | /* Output functions. 38 | * These are still valid for terminfo. */ 39 | int putp(const char *); 40 | int tputs(const char *, int, int (*)(int)); 41 | 42 | extern short ospeed; 43 | extern char PC; 44 | extern char *BC; 45 | extern char *UP; 46 | 47 | int tgetent(char *, const char *); 48 | char * tgetstr(const char *, char **); 49 | int tgetflag(const char *); 50 | int tgetnum(const char *); 51 | char * tgoto(const char *, int, int); 52 | 53 | __END_DECLS 54 | #endif 55 | -------------------------------------------------------------------------------- /mg/ti.c: -------------------------------------------------------------------------------- 1 | /* $NetBSD: ti.c,v 1.3 2013/06/07 13:16:18 roy Exp $ */ 2 | 3 | /* 4 | * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 | * 6 | * This id is derived from software contributed to The NetBSD Foundation 7 | * by Roy Marples. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source id must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include "term_private.h" 35 | #include "terminfo_term.h" 36 | 37 | #ifndef __UNCONST 38 | #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) 39 | #endif 40 | 41 | int 42 | ti_getflag(const TERMINAL *term, const char *id) 43 | { 44 | ssize_t ind; 45 | size_t i; 46 | TERMUSERDEF *ud; 47 | 48 | ind = _ti_flagindex(id); 49 | if (ind != -1) 50 | return term->flags[ind]; 51 | for (i = 0; i < term->_nuserdefs; i++) { 52 | ud = &term->_userdefs[i]; 53 | if (ud->type == 'f' && strcmp(ud->id, id) == 0) 54 | return ud->flag; 55 | } 56 | return ABSENT_BOOLEAN; 57 | } 58 | 59 | int 60 | tigetflag(const char *id) 61 | { 62 | 63 | if (cur_term != NULL) 64 | return ti_getflag(cur_term, id); 65 | return ABSENT_BOOLEAN; 66 | } 67 | 68 | int 69 | ti_getnum(const TERMINAL *term, const char *id) 70 | { 71 | ssize_t ind; 72 | size_t i; 73 | TERMUSERDEF *ud; 74 | 75 | ind = _ti_numindex(id); 76 | if (ind != -1) { 77 | if (!VALID_NUMERIC(term->nums[ind])) 78 | return ABSENT_NUMERIC; 79 | return term->nums[ind]; 80 | } 81 | for (i = 0; i < term->_nuserdefs; i++) { 82 | ud = &term->_userdefs[i]; 83 | if (ud->type == 'n' && strcmp(ud->id, id) == 0) { 84 | if (!VALID_NUMERIC(ud->num)) 85 | return ABSENT_NUMERIC; 86 | return ud->num; 87 | } 88 | } 89 | return CANCELLED_NUMERIC; 90 | } 91 | 92 | int 93 | tigetnum(const char *id) 94 | { 95 | 96 | if (cur_term != NULL) 97 | return ti_getnum(cur_term, id); 98 | return CANCELLED_NUMERIC; 99 | } 100 | 101 | const char * 102 | ti_getstr(const TERMINAL *term, const char *id) 103 | { 104 | ssize_t ind; 105 | size_t i; 106 | TERMUSERDEF *ud; 107 | 108 | ind = _ti_strindex(id); 109 | if (ind != -1) 110 | return term->strs[ind]; 111 | for (i = 0; i < term->_nuserdefs; i++) { 112 | ud = &term->_userdefs[i]; 113 | if (ud->type == 's' && strcmp(ud->id, id) == 0) 114 | return ud->str; 115 | } 116 | return (const char *)CANCELLED_STRING; 117 | } 118 | 119 | char * 120 | tigetstr(const char *id) 121 | { 122 | 123 | if (cur_term != NULL) 124 | return __UNCONST(ti_getstr(cur_term, id)); 125 | return (char *)CANCELLED_STRING; 126 | } 127 | -------------------------------------------------------------------------------- /mg/ttykbd.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: ttykbd.c,v 1.20 2021/02/23 08:10:51 lum Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * Name: MG 2a 7 | * Terminfo keyboard driver using key files 8 | * Created: 22-Nov-1987 Mic Kaczmarczik (mic@emx.cc.utexas.edu) 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "def.h" 18 | #include "kbd.h" 19 | 20 | /* 21 | * Get keyboard character. Very simple if you use keymaps and keys files. 22 | */ 23 | 24 | char *keystrings[] = {NULL}; 25 | 26 | /* 27 | * Turn on function keys using keypad_xmit, then load a keys file, if 28 | * available. The keys file is located in the same manner as the startup 29 | * file is, depending on what startupfile() does on your system. 30 | */ 31 | void 32 | ttykeymapinit(void) 33 | { 34 | char *cp; 35 | 36 | /* Bind keypad function keys. */ 37 | if (key_left) 38 | dobindkey(fundamental_map, "backward-char", key_left); 39 | if (key_right) 40 | dobindkey(fundamental_map, "forward-char", key_right); 41 | if (key_up) 42 | dobindkey(fundamental_map, "previous-line", key_up); 43 | if (key_down) 44 | dobindkey(fundamental_map, "next-line", key_down); 45 | if (key_beg) 46 | dobindkey(fundamental_map, "beginning-of-line", key_beg); 47 | else if (key_home) 48 | dobindkey(fundamental_map, "beginning-of-line", key_home); 49 | if (key_end) 50 | dobindkey(fundamental_map, "end-of-line", key_end); 51 | if (key_npage) 52 | dobindkey(fundamental_map, "scroll-up", key_npage); 53 | if (key_ppage) 54 | dobindkey(fundamental_map, "scroll-down", key_ppage); 55 | if (key_ic) 56 | dobindkey(fundamental_map, "overwrite-mode", key_ic); 57 | if (key_dc) 58 | dobindkey(fundamental_map, "delete-char", key_dc); 59 | 60 | if ((cp = getenv("TERM"))) { 61 | if (((cp = startupfile(cp, NULL)) != NULL) && 62 | (load(cp) != TRUE)) 63 | ewprintf("Error reading key initialization file"); 64 | } 65 | if (keypad_xmit) 66 | /* turn on keypad */ 67 | putpad(keypad_xmit, 1); 68 | } 69 | 70 | /* 71 | * Clean up the keyboard -- called by tttidy() 72 | */ 73 | void 74 | ttykeymaptidy(void) 75 | { 76 | if (keypad_local) 77 | /* turn off keypad */ 78 | putpad(keypad_local, 1); 79 | } 80 | 81 | -------------------------------------------------------------------------------- /mg/util.h: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: util.h,v 1.34 2013/06/03 21:07:02 tedu Exp $ */ 2 | /* $NetBSD: util.h,v 1.2 1996/05/16 07:00:22 thorpej Exp $ */ 3 | 4 | /*- 5 | * Copyright (c) 1995 6 | * The Regents of the University of California. All rights reserved. 7 | * Portions Copyright (c) 1996, Jason Downs. All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 3. Neither the name of the University nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | */ 33 | 34 | #ifndef _UTIL_H_ 35 | #define _UTIL_H_ 36 | 37 | #include 38 | #include 39 | 40 | /* 41 | * fparseln() specific operation flags. 42 | */ 43 | #define FPARSELN_UNESCESC 0x01 44 | #define FPARSELN_UNESCCONT 0x02 45 | #define FPARSELN_UNESCCOMM 0x04 46 | #define FPARSELN_UNESCREST 0x08 47 | #define FPARSELN_UNESCALL 0x0f 48 | 49 | /* 50 | * opendev() specific operation flags. 51 | */ 52 | #define OPENDEV_PART 0x01 /* Try to open the raw partition. */ 53 | #define OPENDEV_BLCK 0x04 /* Open block, not character device. */ 54 | 55 | /* 56 | * uucplock(3) specific flags. 57 | */ 58 | #define UU_LOCK_INUSE (1) 59 | #define UU_LOCK_OK (0) 60 | #define UU_LOCK_OPEN_ERR (-1) 61 | #define UU_LOCK_READ_ERR (-2) 62 | #define UU_LOCK_CREAT_ERR (-3) 63 | #define UU_LOCK_WRITE_ERR (-4) 64 | #define UU_LOCK_LINK_ERR (-5) 65 | #define UU_LOCK_TRY_ERR (-6) 66 | #define UU_LOCK_OWNER_ERR (-7) 67 | 68 | /* 69 | * fmt_scaled(3) specific flags. 70 | */ 71 | #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */ 72 | 73 | /* 74 | * stub struct definitions. 75 | */ 76 | struct __sFILE; 77 | struct login_cap; 78 | struct passwd; 79 | struct termios; 80 | struct utmp; 81 | struct winsize; 82 | 83 | char *fparseln(FILE *, size_t *, size_t *, const char[3], int); 84 | void login(struct utmp *); 85 | int login_tty(int); 86 | int logout(const char *); 87 | void logwtmp(const char *, const char *, const char *); 88 | int opendev(const char *, int, int, char **); 89 | int pidfile(const char *); 90 | void pw_setdir(const char *); 91 | char *pw_file(const char *); 92 | int pw_lock(int); 93 | int pw_mkdb(char *, int); 94 | int pw_abort(void); 95 | void pw_init(void); 96 | void pw_edit(int, const char *); 97 | void pw_prompt(void); 98 | void pw_copy(int, int, const struct passwd *, const struct passwd *); 99 | int pw_scan(char *, struct passwd *, int *); 100 | void pw_error(const char *, int, int); 101 | int openpty(int *, int *, char *, struct termios *, struct winsize *); 102 | int opendisk(const char *, int, char *, size_t, int); 103 | pid_t forkpty(int *, char *, struct termios *, struct winsize *); 104 | int getmaxpartitions(void); 105 | int getrawpartition(void); 106 | void login_fbtab(const char *, uid_t, gid_t); 107 | int login_check_expire(struct __sFILE *, struct passwd *, char *, int); 108 | char *readlabelfs(char *, int); 109 | const char *uu_lockerr(int); 110 | int uu_lock(const char *); 111 | int uu_lock_txfr(const char *, pid_t); 112 | int uu_unlock(const char *); 113 | int fmt_scaled(long long, char *); 114 | int scan_scaled(char *, long long *); 115 | int isduid(const char *, int); 116 | int pkcs5_pbkdf2(const char *, size_t, const uint8_t *, size_t, 117 | uint8_t *, size_t, unsigned int); 118 | int bcrypt_pbkdf(const char *, size_t, const uint8_t *, size_t, 119 | uint8_t *, size_t, unsigned int); 120 | 121 | #endif /* !_UTIL_H_ */ 122 | -------------------------------------------------------------------------------- /mg/utils.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1992, 1993, 1994 Henry Spencer. 3 | * Copyright (c) 1992, 1993, 1994 4 | * The Regents of the University of California. All rights reserved. 5 | * 6 | * This code is derived from software contributed to Berkeley by 7 | * Henry Spencer. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 3. All advertising materials mentioning features or use of this software 18 | * must display the following acknowledgement: 19 | * This product includes software developed by the University of 20 | * California, Berkeley and its contributors. 21 | * 4. Neither the name of the University nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software 23 | * without specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 | * SUCH DAMAGE. 36 | * 37 | * @(#)utils.h 8.3 (Berkeley) 3/20/94 38 | * $FreeBSD: src/lib/libc/regex/utils.h,v 1.2 2002/03/22 23:41:56 obrien Exp $ 39 | */ 40 | 41 | /* utility definitions */ 42 | #define DUPMAX _POSIX2_RE_DUP_MAX /* xxx is this right? */ 43 | #define INFINITY (DUPMAX + 1) 44 | #define NC (CHAR_MAX - CHAR_MIN + 1) 45 | typedef unsigned char uch; 46 | 47 | /* switch off assertions (if not already off) if no REDEBUG */ 48 | #ifndef REDEBUG 49 | #ifndef NDEBUG 50 | #define NDEBUG /* no assertions please */ 51 | #endif 52 | #endif 53 | #include 54 | 55 | /* for old systems with bcopy() but no memmove() */ 56 | #ifdef USEBCOPY 57 | #define memmove(d, s, c) bcopy(s, d, c) 58 | #endif 59 | -------------------------------------------------------------------------------- /mg/version.c: -------------------------------------------------------------------------------- 1 | /* $OpenBSD: version.c,v 1.10 2015/03/19 21:22:15 bcallah Exp $ */ 2 | 3 | /* This file is in the public domain. */ 4 | 5 | /* 6 | * This file contains the string that gets written 7 | * out by the emacs-version command. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "def.h" 15 | 16 | const char version[] = "Mg 2a portable 6.8.1"; 17 | 18 | /* 19 | * Display the version. All this does 20 | * is copy the version string onto the echo line. 21 | */ 22 | /* ARGSUSED */ 23 | int 24 | showversion(int f, int n) 25 | { 26 | ewprintf("%s", version); 27 | return (TRUE); 28 | } 29 | -------------------------------------------------------------------------------- /playmod/README.md: -------------------------------------------------------------------------------- 1 | A MOD player extension for Engine BASIC. 2 | 3 | After you have executed `loader.bas`, you can play MOD files from BASIC 4 | using the command `|playmod "space_debris.mod"`. You can stop the music with 5 | `|stopmod`. 6 | 7 | Demonstrates how to: 8 | - add new functionality to Engine BASIC with modules written in C 9 | - hook audio streams into the mixing system 10 | 11 | [This demo requires a "next-generation" BASIC Engine platform (H3 or SDL).] 12 | -------------------------------------------------------------------------------- /playmod/loader.bas: -------------------------------------------------------------------------------- 1 | TCC "playmod.c" 2 | TCCLINK 3 | -------------------------------------------------------------------------------- /playmod/playmod.c: -------------------------------------------------------------------------------- 1 | #define POCKETMOD_IMPLEMENTATION 2 | #include "pocketmod.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | sts_mixer_stream_t mod_stream; 10 | uint8_t samples[2048 * sizeof(float)]; 11 | void *mod_data = 0; 12 | pocketmod_context mod_ctx; 13 | 14 | static void refill_stream_mod(sts_mixer_sample_t *sample, void *userdata) 15 | { 16 | int i = 0; 17 | while (i < sizeof(samples)) { 18 | i += pocketmod_render(userdata, samples + i, sizeof(samples) - i); 19 | } 20 | } 21 | 22 | int playmod(const char *file) 23 | { 24 | int size = eb_file_size(file); 25 | if (size <= 0) 26 | return -1; 27 | 28 | free(mod_data); 29 | mod_data = malloc(size); 30 | if (!mod_data) 31 | return -1; 32 | 33 | FILE *fp = fopen(file, "rb"); 34 | fread(mod_data, 1, size, fp); 35 | fclose(fp); 36 | 37 | if (!pocketmod_init(&mod_ctx, mod_data, size, 48000)) { 38 | printf("error: '%s' is not a valid MOD file\n", file); 39 | return -1; 40 | } 41 | 42 | mod_stream.callback = refill_stream_mod; 43 | mod_stream.sample.frequency = 48000; 44 | mod_stream.sample.audio_format = STS_MIXER_SAMPLE_FORMAT_FLOAT; 45 | mod_stream.sample.length = 2048; 46 | mod_stream.sample.data = samples; 47 | mod_stream.userdata = &mod_ctx; 48 | 49 | sts_mixer_play_stream(eb_get_mixer(), &mod_stream, 1.0f); 50 | } 51 | 52 | int stopmod(void) 53 | { 54 | sts_mixer_stop_stream(eb_get_mixer(), &mod_stream); 55 | } 56 | -------------------------------------------------------------------------------- /playmod/space_debris.mod: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/playmod/space_debris.mod -------------------------------------------------------------------------------- /shmup/README.md: -------------------------------------------------------------------------------- 1 | Translation of the [shmup demo from HuC](https://github.com/uli/huc/tree/master/examples/shmup) 2 | to (Engine) BASIC. 3 | 4 | The version in `shmup.bas` has been written for the classic BASIC Engine, but 5 | works on next generation systems as well. 6 | 7 | The version in `shmup_ng.bas` takes advantage of true color screen modes and 8 | sprite rotation only available on NG platforms. 9 | -------------------------------------------------------------------------------- /shmup/bullet.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/shmup/bullet.pcx -------------------------------------------------------------------------------- /shmup/charwalk.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/shmup/charwalk.pcx -------------------------------------------------------------------------------- /shmup/explosion.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/shmup/explosion.pcx -------------------------------------------------------------------------------- /shmup/scene.bg: -------------------------------------------------------------------------------- 1 |  2 |   !"#$!%&'(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()*)*)*)*)*)*)*)*+,+,+,+,+,+,+,+,-.-.-.-.-.-.-.-. -------------------------------------------------------------------------------- /shmup/scene.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/shmup/scene.pcx -------------------------------------------------------------------------------- /shmup/ship.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/shmup/ship.pcx -------------------------------------------------------------------------------- /sokoban/README.md: -------------------------------------------------------------------------------- 1 | Conversion of: 2 | 3 | > Sokoban, for FreeBASIC QB mode/Qbasic/QB64 4 | > 5 | > web: http://rudih.info 6 | 7 | - `sokoban_text.bas` is a straightforward port of the text-based original. 8 | - `sokoban_simplegfx.bas` shows how to add basic tile and sprite graphics. 9 | - `sokoban.bas` adds animation and game controller support. 10 | 11 | Graphics ripped from Sokoban World on PC Engine. 12 | -------------------------------------------------------------------------------- /sokoban/sokoban.pcx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sokoban/sokoban.pcx -------------------------------------------------------------------------------- /sys/modules/famicom/Makefile: -------------------------------------------------------------------------------- 1 | # point this to the Engine BASIC system directory, either the one on the SD 2 | # card image (/sys), or the one created using the "init_dir" target of the 3 | # Engine BASIC Makefiles (/init_dir/sys) 4 | BE_SYSDIR = ../../../../enginebasic/init_dir/sys 5 | 6 | # prefixes of the toolchains for the different CPU architectures 7 | ARM_CROSS_COMPILE = ~/x-tools/arm-unknown-eabihf/bin/arm-unknown-eabihf- 8 | X64_CROSS_COMPILE = 9 | I386_CROSS_COMPILE = i686-linux-gnu- 10 | 11 | CFLAGS = -O3 -Wall -I$(BE_SYSDIR)/include -nostdlib -ffreestanding -DPIXEL_TYPE=uint32_t -DIPIXEL_TYPE=uint32_t 12 | ARM_CFLAGS = -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 13 | X64_CFLAGS = 14 | I386_CFLAGS = -fno-pic -m32 15 | 16 | all: famicom_arm.o famicom_x86_64.o famicom_i386.o 17 | 18 | famicom_arm.o: fami.c agnes.h 19 | $(ARM_CROSS_COMPILE)gcc $(CFLAGS) $(ARM_CFLAGS) -c -o famicom_arm.o fami.c 20 | famicom_x86_64.o: fami.c agnes.h 21 | $(X64_CROSS_COMPILE)gcc $(CFLAGS) $(X64_CFLAGS) -c -o famicom_x86_64.o fami.c 22 | famicom_i386.o: fami.c agnes.h 23 | $(I386_CROSS_COMPILE)gcc $(CFLAGS) $(I386_CFLAGS) -c -o famicom_i386.o fami.c 24 | 25 | clean: 26 | rm -f famicom_arm.o famicom_x86_64.o famicom_i386.o 27 | -------------------------------------------------------------------------------- /sys/modules/famicom/README.md: -------------------------------------------------------------------------------- 1 | Module that extends Engine BASIC with a scriptable Famicom emulator. 2 | 3 | If the module resides in `/sys/modules/famicom`, it can be loaded using 4 | `LOADMOD "famicom"`, or by adding a `#REQUIRE "famicom"` directive at the 5 | start of a BASIC program. 6 | 7 | When loaded, the following additional BASIC commands/functions are 8 | available: 9 | 10 | - `FAMILOAD ""` initializes the emulator and loads a Famicom/NES 11 | ROM file in iNES format 12 | - `FAMULATE` emulates one frame. 13 | - `FAMIEND` ends the emulator. 14 | - `FAMIREAD(addr)` returns the contents of the emulated memory at `addr`. 15 | - `FAMIMOVE x,y` moves the emulated screen to the given position. 16 | 17 | This demo shows how to: 18 | - extend Engine BASIC with new commands and functions 19 | - provide custom background layers for the BG/sprite engine 20 | - cross-compile Engine BASIC modules 21 | 22 | (The module can be compiled from source from within Engine BASIC using `TCC 23 | "fami.c"` and `TCCLINK "famicom"`, but due to the near-absence of 24 | optimizations in the built-in TCC compiler it will then not run at full 25 | speed on most devices.) 26 | 27 | Please note that the emulation engine is neither complete nor optimized for 28 | speed. 29 | -------------------------------------------------------------------------------- /sys/modules/famicom/fami.c: -------------------------------------------------------------------------------- 1 | #define AGNES_IMPLEMENTATION 2 | #define AGNES_SINGLE_HEADER 3 | #include "agnes.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | agnes_t *agnes = NULL; 17 | uint8_t *rom = NULL; 18 | agnes_input_t input; 19 | int layer = -1; 20 | 21 | int pos_x = 0, pos_y = 0; 22 | 23 | void not_initialized() { 24 | eb_set_error(ERR_FILE_NOT_OPEN, "emulator not running"); 25 | } 26 | 27 | // The painter function is run by the graphics core when it's time to draw 28 | // the BG layer (in this case, the emulator screen). 29 | 30 | void painter(pixel_t *p, int w, int h, int pitch, void *userdata) { 31 | int draw_w = w - pos_x; 32 | int draw_h = h - pos_y; 33 | 34 | if (draw_w > AGNES_SCREEN_WIDTH) 35 | draw_w = AGNES_SCREEN_WIDTH; 36 | if (draw_h > AGNES_SCREEN_HEIGHT) 37 | draw_h = AGNES_SCREEN_HEIGHT; 38 | 39 | int start_x = 0; 40 | int start_y = 0; 41 | if (pos_x < 0) 42 | start_x -= pos_x; 43 | if (pos_y < 0) 44 | start_y -= pos_y; 45 | 46 | for (int y = start_y; y < draw_h; y++) { 47 | for (int x = start_x; x < draw_w; x++) { 48 | agnes_color_t c = agnes_get_screen_pixel(agnes, x, y); 49 | p[pos_x + x + (pos_y + y) * pitch] = eb_rgb(c.r, c.g, c.b); 50 | } 51 | } 52 | } 53 | 54 | // Implementations of new BASIC commands that control the emulator. 55 | 56 | // Engine BASIC passes parameters as an array of eb_param_t. The number and 57 | // meaning of its elements depends on the syntax defined below. 58 | 59 | // NB: Only string (I_STR) and numeric (I_NUM) parameters are included in 60 | // the parameter array; static syntax elements (such as parentheses) are 61 | // not. 62 | 63 | void famiload(const eb_param_t *params) 64 | { 65 | const char *file = params[0].str; 66 | 67 | int size = eb_file_size(file); 68 | if (size <= 0) { 69 | eb_set_error(ERR_FILE_OPEN, NULL); 70 | return; 71 | } 72 | 73 | rom = realloc(rom, size); 74 | FILE *fp = fopen(file, "rb"); 75 | fread(rom, 1, size, fp); 76 | fclose(fp); 77 | 78 | if (layer >= 0) 79 | eb_remove_bg_layer(layer); 80 | 81 | if (agnes) 82 | agnes_destroy(agnes); 83 | agnes = agnes_make(); 84 | agnes_load_ines_data(agnes, rom, size); 85 | 86 | layer = eb_add_bg_layer(painter, 0, NULL); 87 | } 88 | 89 | void faminput(const eb_param_t *params) { 90 | uint32_t bits = (uint64_t)params[0].num; 91 | 92 | if (!agnes) { 93 | not_initialized(); 94 | return; 95 | } 96 | 97 | memset(&input, 0, sizeof(input)); 98 | 99 | input.up = !!(bits & EB_JOY_UP); 100 | input.down = !!(bits & EB_JOY_DOWN); 101 | input.left = !!(bits & EB_JOY_LEFT); 102 | input.right = !!(bits & EB_JOY_RIGHT); 103 | input.a = !!(bits & EB_JOY_X); 104 | input.b = !!(bits & EB_JOY_SQUARE); 105 | input.select = !!(bits & EB_JOY_SELECT); 106 | input.start = !!(bits & EB_JOY_START); 107 | 108 | agnes_set_input(agnes, &input, 0); 109 | } 110 | 111 | void famulate(const eb_param_t *params) { 112 | if (!agnes) { 113 | not_initialized(); 114 | return; 115 | } 116 | 117 | agnes_next_frame(agnes); 118 | } 119 | 120 | void famiend(const eb_param_t *params) { 121 | if (!agnes) { 122 | not_initialized(); 123 | return; 124 | } 125 | 126 | eb_remove_bg_layer(layer); 127 | free(rom); 128 | rom = NULL; 129 | agnes_destroy(agnes); 130 | agnes = NULL; 131 | } 132 | 133 | double famiread(const eb_param_t *params) { 134 | uint32_t addr = (uint64_t)params[0].num; 135 | 136 | if (!agnes) { 137 | not_initialized(); 138 | return 0; 139 | } 140 | 141 | return cpu_read8(&agnes->cpu, addr); 142 | } 143 | 144 | void famimove(const eb_param_t *params) { 145 | pos_x = params[0].num; 146 | pos_y = params[1].num; 147 | } 148 | 149 | // Register the new BASIC commands and functions with Engine BASIC. 150 | 151 | const enum token_t syn_famiload[] = { I_STR, I_EOL }; 152 | const enum token_t syn_faminput[] = { I_NUM, I_EOL }; 153 | const enum token_t syn_void[] = { I_EOL }; 154 | const enum token_t syn_famiread[] = { I_OPEN, I_NUM, I_CLOSE, I_EOL }; 155 | const enum token_t syn_famimove[] = { I_NUM, I_COMMA, I_NUM, I_EOL }; 156 | 157 | // This function is called at link time, i.e. when the TCCLINK command is 158 | // executed, or the module is loaded via LOADMOD or #REQUIRE. 159 | void __initcall(void) { 160 | eb_add_command("FAMILOAD", syn_famiload, famiload); 161 | eb_add_command("FAMINPUT", syn_faminput, faminput); 162 | eb_add_command("FAMULATE", syn_void, famulate); 163 | eb_add_command("FAMIEND", syn_void, famiend); 164 | eb_add_numfun("FAMIREAD", syn_famiread, famiread); 165 | eb_add_command("FAMIMOVE", syn_famimove, famimove); 166 | } 167 | -------------------------------------------------------------------------------- /sys/modules/famicom/famicom_arm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/famicom/famicom_arm.o -------------------------------------------------------------------------------- /sys/modules/famicom/famicom_i386.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/famicom/famicom_i386.o -------------------------------------------------------------------------------- /sys/modules/famicom/famicom_x86_64.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/famicom/famicom_x86_64.o -------------------------------------------------------------------------------- /sys/modules/lua/Makefile: -------------------------------------------------------------------------------- 1 | # point this to the Engine BASIC system directory, either the one on the SD 2 | # card image (/sys), or the one created using the "init_dir" target of the 3 | # Engine BASIC Makefiles (/init_dir/sys) 4 | BE_SYSDIR = ../../../../enginebasic/init_dir/sys 5 | 6 | # prefixes of the toolchains for the different CPU architectures 7 | ARM_CROSS_COMPILE = ~/x-tools/arm-unknown-eabihf/bin/arm-unknown-eabihf- 8 | X64_CROSS_COMPILE = 9 | I386_CROSS_COMPILE = i686-linux-gnu- 10 | 11 | CFLAGS = -O3 -fpic -Wall -Wno-unused-function -DENGINEBASIC -I$(BE_SYSDIR)/include -nostdlib -ffreestanding -DPIXEL_TYPE=uint32_t -DIPIXEL_TYPE=uint32_t -D_GNU_SOURCE 12 | ARM_CFLAGS = $(CFLAGS) -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 13 | X64_CFLAGS = $(CFLAGS) 14 | I386_CFLAGS = $(CFLAGS) -fno-pic -fno-exceptions -fno-asynchronous-unwind-tables -m32 15 | 16 | SRCS = lapi.c lauxlib.c lbaselib.c lcode.c lcorolib.c lctype.c \ 17 | ldblib.c ldebug.c ldo.c ldump.c lfunc.c lgc.c linit.c \ 18 | liolib.c llex.c lmathlib.c lmem.c loadlib.c lobject.c \ 19 | lopcodes.c loslib.c lparser.c lstate.c lstring.c lstrlib.c \ 20 | ltable.c ltablib.c ltests.c ltm.c lundump.c lutf8lib.c lvm.c \ 21 | lzio.c \ 22 | lua_be.c lua_bg.c lua_img.c lua_input.c lua_io.c lua_video.c \ 23 | sys.c 24 | 25 | OBJS = $(SRCS:.c=.o) 26 | 27 | ARM_OUT_OBJS = $(addprefix build_arm/, $(OBJS)) 28 | X64_OUT_OBJS = $(addprefix build_x86_64/, $(OBJS)) 29 | I386_OUT_OBJS = $(addprefix build_i386/, $(OBJS)) 30 | 31 | build_arm/%.o: %.c 32 | @mkdir -p $(@D) 33 | $(ARM_CROSS_COMPILE)gcc $(ARM_CFLAGS) -o $@ -c $< 34 | 35 | build_arm/%.o: %.cpp 36 | @mkdir -p $(@D) 37 | $(ARM_CROSS_COMPILE)g++ $(ARM_CFLAGS) -o $@ -c $< 38 | 39 | build_arm/%.o: %.S 40 | @mkdir -p $(@D) 41 | $(ARM_CROSS_COMPILE)gcc $(ARM_CFLAGS) -o $@ -c $< 42 | 43 | build_x86_64/%.o: %.c 44 | @mkdir -p $(@D) 45 | $(X64_CROSS_COMPILE)gcc $(X64_CFLAGS) -o $@ -c $< 46 | 47 | build_x86_64/%.o: %.cpp 48 | @mkdir -p $(@D) 49 | $(X64_CROSS_COMPILE)g++ $(X64_CFLAGS) -o $@ -c $< 50 | 51 | build_x86_64/%.o: %.S 52 | @mkdir -p $(@D) 53 | $(X64_CROSS_COMPILE)gcc $(X64_CFLAGS) -o $@ -c $< 54 | 55 | build_i386/%.o: %.c 56 | @mkdir -p $(@D) 57 | $(I386_CROSS_COMPILE)gcc $(I386_CFLAGS) -o $@ -c $< 58 | 59 | build_i386/%.o: %.cpp 60 | @mkdir -p $(@D) 61 | $(I386_CROSS_COMPILE)g++ $(I386_CFLAGS) -o $@ -c $< 62 | 63 | build_i386/%.o: %.S 64 | @mkdir -p $(@D) 65 | $(I386_CROSS_COMPILE)gcc $(I386_CFLAGS) -o $@ -c $< 66 | 67 | all: lua_arm.o lua_x86_64.o lua_i386.o 68 | 69 | lua_arm.o: $(ARM_OUT_OBJS) 70 | $(ARM_CROSS_COMPILE)ld -r -o lua_arm.o $(ARM_OUT_OBJS) 71 | 72 | lua_x86_64.o: $(X64_OUT_OBJS) 73 | $(X64_CROSS_COMPILE)ld -r -o lua_x86_64.o $(X64_OUT_OBJS) 74 | 75 | lua_i386.o: $(I386_OUT_OBJS) 76 | $(I386_CROSS_COMPILE)ld -r -o lua_i386.o $(I386_OUT_OBJS) 77 | 78 | clean: 79 | rm -fr build_arm build_x86_64 build_i386 lua_arm.o lua_x86_64.o lua_i386.o 80 | -------------------------------------------------------------------------------- /sys/modules/lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | /* 25 | ** To reduce the overhead of returning from C functions, the presence of 26 | ** to-be-closed variables in these functions is coded in the CallInfo's 27 | ** field 'nresults', in a way that functions with no to-be-closed variables 28 | ** with zero, one, or "all" wanted results have no overhead. Functions 29 | ** with other number of wanted results, as well as functions with 30 | ** variables to be closed, have an extra check. 31 | */ 32 | 33 | #define hastocloseCfunc(n) ((n) < LUA_MULTRET) 34 | 35 | #define codeNresults(n) (-(n) - 3) 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /sys/modules/lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) 41 | 42 | 43 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 44 | 45 | 46 | /* get (pointer to) instruction of given 'expdesc' */ 47 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 48 | 49 | 50 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 51 | 52 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 53 | 54 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 55 | LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); 56 | LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, 57 | int B, int C, int k); 58 | LUAI_FUNC int luaK_isKint (expdesc *e); 59 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 60 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 61 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 62 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 63 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 64 | LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); 65 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 66 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 67 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 68 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 69 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 70 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 71 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 72 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 73 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 74 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 75 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 76 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 77 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 78 | LUAI_FUNC int luaK_jump (FuncState *fs); 79 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 80 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 81 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 82 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 83 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 84 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 85 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 86 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 87 | expdesc *v2, int line); 88 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 89 | LUAI_FUNC void luaK_finish (FuncState *fs); 90 | LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); 91 | 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /sys/modules/lua/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /sys/modules/lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /sys/modules/lua/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) 15 | 16 | #define resethookcount(L) (L->hookcount = L->basehookcount) 17 | 18 | /* 19 | ** mark for entries in 'lineinfo' array that has absolute information in 20 | ** 'abslineinfo' array 21 | */ 22 | #define ABSLINEINFO (-0x80) 23 | 24 | LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); 25 | LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, 26 | StkId *pos); 27 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 28 | const char *opname); 29 | LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, 30 | const char *what); 31 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 32 | const TValue *p2); 33 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 34 | const TValue *p2, 35 | const char *msg); 36 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 37 | const TValue *p2); 38 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 39 | const TValue *p2); 40 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 41 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 42 | TString *src, int line); 43 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 44 | LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /sys/modules/lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 21 | ** at every check. 22 | */ 23 | #define luaD_checkstackaux(L,n,pre,pos) \ 24 | if (L->stack_last - L->top <= (n)) \ 25 | { pre; luaD_growstack(L, n, 1); pos; } \ 26 | else { condmovestack(L,pre,pos); } 27 | 28 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 29 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 30 | 31 | 32 | 33 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 34 | #define restorestack(L,n) ((StkId)((char *)L->stack + (n))) 35 | 36 | 37 | /* macro to check stack size, preserving 'p' */ 38 | #define checkstackp(L,n,p) \ 39 | luaD_checkstackaux(L, n, \ 40 | ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ 41 | luaC_checkGC(L), /* stack grow uses memory */ \ 42 | p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ 43 | 44 | 45 | /* macro to check stack size and GC */ 46 | #define checkstackGC(L,fsize) \ 47 | luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L)) 48 | 49 | 50 | /* type of protected functions, to be ran by 'runprotected' */ 51 | typedef void (*Pfunc) (lua_State *L, void *ud); 52 | 53 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 54 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 55 | const char *mode); 56 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, 57 | int fTransfer, int nTransfer); 58 | LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); 59 | LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); 60 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 61 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 62 | LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); 63 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 64 | ptrdiff_t oldtop, ptrdiff_t ef); 65 | LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); 66 | LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); 67 | LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); 68 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 69 | LUAI_FUNC void luaD_inctop (lua_State *L); 70 | 71 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 72 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 73 | 74 | #endif 75 | 76 | -------------------------------------------------------------------------------- /sys/modules/lua/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ 15 | cast_int(sizeof(TValue)) * (n)) 16 | 17 | #define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ 18 | cast_int(sizeof(TValue *)) * (n)) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | #define upisopen(up) ((up)->v != &(up)->u.value) 33 | 34 | 35 | #define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) 36 | 37 | 38 | /* 39 | ** maximum number of misses before giving up the cache of closures 40 | ** in prototypes 41 | */ 42 | #define MAXMISS 10 43 | 44 | 45 | /* 46 | ** Special "status" for 'luaF_close' 47 | */ 48 | 49 | /* close upvalues without running their closing methods */ 50 | #define NOCLOSINGMETH (-1) 51 | 52 | /* close upvalues running all closing methods in protected mode */ 53 | #define CLOSEPROTECT (-2) 54 | 55 | 56 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 57 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 58 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 59 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 60 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 61 | LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); 62 | LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); 63 | LUAI_FUNC void luaF_unlinkupval (UpVal *uv); 64 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 65 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 66 | int pc); 67 | 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /sys/modules/lua/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {LUA_GNAME, luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | {NULL, NULL} 54 | }; 55 | 56 | 57 | LUALIB_API void luaL_openlibs (lua_State *L) { 58 | const luaL_Reg *lib; 59 | /* "require" functions from 'loadedlibs' and set results to global table */ 60 | for (lib = loadedlibs; lib->func; lib++) { 61 | luaL_requiref(L, lib->name, lib->func, 1); 62 | lua_pop(L, 1); /* remove lib */ 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /sys/modules/lua/ljumptab.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ljumptab.h $ 3 | ** Jump Table for the Lua interpreter 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #undef vmdispatch 9 | #undef vmcase 10 | #undef vmbreak 11 | 12 | #define vmdispatch(x) goto *disptab[x]; 13 | 14 | #define vmcase(l) L_##l: 15 | 16 | #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); 17 | 18 | 19 | static void *disptab[NUM_OPCODES] = { 20 | 21 | #if 0 22 | ** you can update the following list with this command: 23 | ** 24 | ** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h 25 | ** 26 | #endif 27 | 28 | &&L_OP_MOVE, 29 | &&L_OP_LOADI, 30 | &&L_OP_LOADF, 31 | &&L_OP_LOADK, 32 | &&L_OP_LOADKX, 33 | &&L_OP_LOADBOOL, 34 | &&L_OP_LOADNIL, 35 | &&L_OP_GETUPVAL, 36 | &&L_OP_SETUPVAL, 37 | &&L_OP_GETTABUP, 38 | &&L_OP_GETTABLE, 39 | &&L_OP_GETI, 40 | &&L_OP_GETFIELD, 41 | &&L_OP_SETTABUP, 42 | &&L_OP_SETTABLE, 43 | &&L_OP_SETI, 44 | &&L_OP_SETFIELD, 45 | &&L_OP_NEWTABLE, 46 | &&L_OP_SELF, 47 | &&L_OP_ADDI, 48 | &&L_OP_SUBI, 49 | &&L_OP_MULI, 50 | &&L_OP_MODI, 51 | &&L_OP_POWI, 52 | &&L_OP_DIVI, 53 | &&L_OP_IDIVI, 54 | &&L_OP_ADDK, 55 | &&L_OP_SUBK, 56 | &&L_OP_MULK, 57 | &&L_OP_MODK, 58 | &&L_OP_POWK, 59 | &&L_OP_DIVK, 60 | &&L_OP_IDIVK, 61 | &&L_OP_BANDK, 62 | &&L_OP_BORK, 63 | &&L_OP_BXORK, 64 | &&L_OP_SHRI, 65 | &&L_OP_SHLI, 66 | &&L_OP_ADD, 67 | &&L_OP_SUB, 68 | &&L_OP_MUL, 69 | &&L_OP_MOD, 70 | &&L_OP_POW, 71 | &&L_OP_DIV, 72 | &&L_OP_IDIV, 73 | &&L_OP_BAND, 74 | &&L_OP_BOR, 75 | &&L_OP_BXOR, 76 | &&L_OP_SHL, 77 | &&L_OP_SHR, 78 | &&L_OP_UNM, 79 | &&L_OP_BNOT, 80 | &&L_OP_NOT, 81 | &&L_OP_LEN, 82 | &&L_OP_CONCAT, 83 | &&L_OP_CLOSE, 84 | &&L_OP_TBC, 85 | &&L_OP_JMP, 86 | &&L_OP_EQ, 87 | &&L_OP_LT, 88 | &&L_OP_LE, 89 | &&L_OP_EQK, 90 | &&L_OP_EQI, 91 | &&L_OP_LTI, 92 | &&L_OP_LEI, 93 | &&L_OP_GTI, 94 | &&L_OP_GEI, 95 | &&L_OP_TEST, 96 | &&L_OP_TESTSET, 97 | &&L_OP_CALL, 98 | &&L_OP_TAILCALL, 99 | &&L_OP_RETURN, 100 | &&L_OP_RETURN0, 101 | &&L_OP_RETURN1, 102 | &&L_OP_FORLOOP1, 103 | &&L_OP_FORPREP1, 104 | &&L_OP_FORLOOP, 105 | &&L_OP_FORPREP, 106 | &&L_OP_TFORPREP, 107 | &&L_OP_TFORCALL, 108 | &&L_OP_TFORLOOP, 109 | &&L_OP_SETLIST, 110 | &&L_OP_CLOSURE, 111 | &&L_OP_VARARG, 112 | &&L_OP_PREPVARARG, 113 | &&L_OP_EXTRAARG 114 | 115 | }; 116 | -------------------------------------------------------------------------------- /sys/modules/lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | } LexState; 73 | 74 | 75 | LUAI_FUNC void luaX_init (lua_State *L); 76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 77 | TString *source, int firstchar); 78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 79 | LUAI_FUNC void luaX_next (LexState *ls); 80 | LUAI_FUNC int luaX_lookahead (LexState *ls); 81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 83 | 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /sys/modules/lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /sys/modules/lua/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 21 | /* OT IT T A mode opcode */ 22 | opmode(0, 0, 0, 1, iABC) /* OP_MOVE */ 23 | ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */ 24 | ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */ 25 | ,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */ 26 | ,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */ 27 | ,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */ 28 | ,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */ 29 | ,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */ 30 | ,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */ 31 | ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */ 32 | ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */ 33 | ,opmode(0, 0, 0, 1, iABC) /* OP_GETI */ 34 | ,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */ 35 | ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */ 36 | ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */ 37 | ,opmode(0, 0, 0, 0, iABC) /* OP_SETI */ 38 | ,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */ 39 | ,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */ 40 | ,opmode(0, 0, 0, 1, iABC) /* OP_SELF */ 41 | ,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */ 42 | ,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */ 43 | ,opmode(0, 0, 0, 1, iABC) /* OP_MULI */ 44 | ,opmode(0, 0, 0, 1, iABC) /* OP_MODI */ 45 | ,opmode(0, 0, 0, 1, iABC) /* OP_POWI */ 46 | ,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */ 47 | ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */ 48 | ,opmode(0, 0, 0, 1, iABC) /* OP_ADDK */ 49 | ,opmode(0, 0, 0, 1, iABC) /* OP_SUBK */ 50 | ,opmode(0, 0, 0, 1, iABC) /* OP_MULK */ 51 | ,opmode(0, 0, 0, 1, iABC) /* OP_MODK */ 52 | ,opmode(0, 0, 0, 1, iABC) /* OP_POWK */ 53 | ,opmode(0, 0, 0, 1, iABC) /* OP_DIVK */ 54 | ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVK */ 55 | ,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */ 56 | ,opmode(0, 0, 0, 1, iABC) /* OP_BORK */ 57 | ,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */ 58 | ,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */ 59 | ,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */ 60 | ,opmode(0, 0, 0, 1, iABC) /* OP_ADD */ 61 | ,opmode(0, 0, 0, 1, iABC) /* OP_SUB */ 62 | ,opmode(0, 0, 0, 1, iABC) /* OP_MUL */ 63 | ,opmode(0, 0, 0, 1, iABC) /* OP_MOD */ 64 | ,opmode(0, 0, 0, 1, iABC) /* OP_POW */ 65 | ,opmode(0, 0, 0, 1, iABC) /* OP_DIV */ 66 | ,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */ 67 | ,opmode(0, 0, 0, 1, iABC) /* OP_BAND */ 68 | ,opmode(0, 0, 0, 1, iABC) /* OP_BOR */ 69 | ,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */ 70 | ,opmode(0, 0, 0, 1, iABC) /* OP_SHL */ 71 | ,opmode(0, 0, 0, 1, iABC) /* OP_SHR */ 72 | ,opmode(0, 0, 0, 1, iABC) /* OP_UNM */ 73 | ,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */ 74 | ,opmode(0, 0, 0, 1, iABC) /* OP_NOT */ 75 | ,opmode(0, 0, 0, 1, iABC) /* OP_LEN */ 76 | ,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */ 77 | ,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */ 78 | ,opmode(0, 0, 0, 0, iABC) /* OP_TBC */ 79 | ,opmode(0, 0, 0, 0, isJ) /* OP_JMP */ 80 | ,opmode(0, 0, 1, 0, iABC) /* OP_EQ */ 81 | ,opmode(0, 0, 1, 0, iABC) /* OP_LT */ 82 | ,opmode(0, 0, 1, 0, iABC) /* OP_LE */ 83 | ,opmode(0, 0, 1, 0, iABC) /* OP_EQK */ 84 | ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */ 85 | ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */ 86 | ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */ 87 | ,opmode(0, 0, 1, 0, iABC) /* OP_GTI */ 88 | ,opmode(0, 0, 1, 0, iABC) /* OP_GEI */ 89 | ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ 90 | ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ 91 | ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ 92 | ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ 93 | ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ 94 | ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ 95 | ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ 96 | ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */ 97 | ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP1 */ 98 | ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */ 99 | ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */ 100 | ,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */ 101 | ,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */ 102 | ,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */ 103 | ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ 104 | ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ 105 | ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ 106 | ,opmode(0, 0, 0, 1, iABC) /* OP_PREPVARARG */ 107 | ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ 108 | }; 109 | 110 | -------------------------------------------------------------------------------- /sys/modules/lua/lopnames.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopnames.h $ 3 | ** Opcode names 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #if !defined(lopnames_h) 8 | #define lopnames_h 9 | 10 | /* ORDER OP */ 11 | 12 | static const char *const opnames[] = { 13 | "MOVE", 14 | "LOADI", 15 | "LOADF", 16 | "LOADK", 17 | "LOADKX", 18 | "LOADBOOL", 19 | "LOADNIL", 20 | "GETUPVAL", 21 | "SETUPVAL", 22 | "GETTABUP", 23 | "GETTABLE", 24 | "GETI", 25 | "GETFIELD", 26 | "SETTABUP", 27 | "SETTABLE", 28 | "SETI", 29 | "SETFIELD", 30 | "NEWTABLE", 31 | "SELF", 32 | "ADDI", 33 | "SUBI", 34 | "MULI", 35 | "MODI", 36 | "POWI", 37 | "DIVI", 38 | "IDIVI", 39 | "ADDK", 40 | "SUBK", 41 | "MULK", 42 | "MODK", 43 | "POWK", 44 | "DIVK", 45 | "IDIVK", 46 | "BANDK", 47 | "BORK", 48 | "BXORK", 49 | "SHRI", 50 | "SHLI", 51 | "ADD", 52 | "SUB", 53 | "MUL", 54 | "MOD", 55 | "POW", 56 | "DIV", 57 | "IDIV", 58 | "BAND", 59 | "BOR", 60 | "BXOR", 61 | "SHL", 62 | "SHR", 63 | "UNM", 64 | "BNOT", 65 | "NOT", 66 | "LEN", 67 | "CONCAT", 68 | "CLOSE", 69 | "TBC", 70 | "JMP", 71 | "EQ", 72 | "LT", 73 | "LE", 74 | "EQK", 75 | "EQI", 76 | "LTI", 77 | "LEI", 78 | "GTI", 79 | "GEI", 80 | "TEST", 81 | "TESTSET", 82 | "CALL", 83 | "TAILCALL", 84 | "RETURN", 85 | "RETURN0", 86 | "RETURN1", 87 | "FORLOOP1", 88 | "FORPREP1", 89 | "FORLOOP", 90 | "FORPREP", 91 | "TFORPREP", 92 | "TFORCALL", 93 | "TFORLOOP", 94 | "SETLIST", 95 | "CLOSURE", 96 | "VARARG", 97 | "PREPVARARG", 98 | "EXTRAARG", 99 | NULL 100 | }; 101 | 102 | #endif 103 | 104 | -------------------------------------------------------------------------------- /sys/modules/lua/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /sys/modules/lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | /* 16 | ** Memory-allocation error message must be preallocated (it cannot 17 | ** be created after memory is exhausted) 18 | */ 19 | #define MEMERRMSG "not enough memory" 20 | 21 | 22 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 23 | 24 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 25 | (sizeof(s)/sizeof(char))-1)) 26 | 27 | 28 | /* 29 | ** test whether a string is a reserved word 30 | */ 31 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 32 | 33 | 34 | /* 35 | ** equality for short strings, which are always internalized 36 | */ 37 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 38 | 39 | 40 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 41 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 42 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 43 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 44 | LUAI_FUNC void luaS_clearcache (global_State *g); 45 | LUAI_FUNC void luaS_init (lua_State *L); 46 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 47 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); 48 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 49 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 50 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /sys/modules/lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->u.next) 16 | 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | 21 | /* true when 't' is using 'dummynode' as its hash part */ 22 | #define isdummy(t) ((t)->lastfree == NULL) 23 | 24 | 25 | /* allocated size for hash nodes */ 26 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 27 | 28 | 29 | /* returns the Node, given the value of a table entry */ 30 | #define nodefromval(v) cast(Node *, (v)) 31 | 32 | 33 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 34 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 35 | TValue *value); 36 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 37 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 38 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 39 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 40 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 41 | LUAI_FUNC Table *luaH_new (lua_State *L); 42 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 43 | unsigned int nhsize); 44 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 45 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 46 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 47 | LUAI_FUNC lua_Unsigned luaH_getn (Table *t); 48 | LUAI_FUNC unsigned int luaH_realasize (const Table *t); 49 | 50 | 51 | #if defined(LUA_DEBUG) 52 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 53 | LUAI_FUNC int luaH_isdummy (const Table *t); 54 | #endif 55 | 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /sys/modules/lua/ltests.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltests.h $ 3 | ** Internal Header for Debugging of the Lua Implementation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltests_h 8 | #define ltests_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | /* test Lua with compatibility code */ 15 | #define LUA_COMPAT_MATHLIB 16 | #define LUA_COMPAT_LT_LE 17 | 18 | 19 | #define LUA_DEBUG 20 | 21 | 22 | /* turn on assertions */ 23 | #undef NDEBUG 24 | #include 25 | #define lua_assert(c) assert(c) 26 | 27 | 28 | /* include opcode names */ 29 | #define LUAI_DEFOPNAMES 30 | 31 | 32 | /* compiled with -O0, Lua uses a lot of C stack space... */ 33 | #undef LUAI_MAXCCALLS 34 | #define LUAI_MAXCCALLS 400 35 | 36 | /* to avoid warnings, and to make sure value is really unused */ 37 | #define UNUSED(x) (x=0, (void)(x)) 38 | 39 | 40 | /* test for sizes in 'l_sprintf' (make sure whole buffer is available) */ 41 | #undef l_sprintf 42 | #if !defined(LUA_USE_C89) 43 | #define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), snprintf(s,sz,f,i)) 44 | #else 45 | #define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), sprintf(s,f,i)) 46 | #endif 47 | 48 | 49 | /* get a chance to test code without jump tables */ 50 | #define LUA_USE_JUMPTABLE 0 51 | 52 | 53 | /* use 32-bit integers in random generator */ 54 | #define LUA_RAND32 55 | 56 | 57 | /* memory-allocator control variables */ 58 | typedef struct Memcontrol { 59 | unsigned long numblocks; 60 | unsigned long total; 61 | unsigned long maxmem; 62 | unsigned long memlimit; 63 | unsigned long countlimit; 64 | unsigned long objcount[LUA_NUMTAGS]; 65 | } Memcontrol; 66 | 67 | LUA_API Memcontrol l_memcontrol; 68 | 69 | 70 | /* 71 | ** generic variable for debug tricks 72 | */ 73 | extern void *l_Trick; 74 | 75 | 76 | 77 | /* 78 | ** Function to traverse and check all memory used by Lua 79 | */ 80 | int lua_checkmemory (lua_State *L); 81 | 82 | 83 | /* test for lock/unlock */ 84 | 85 | struct L_EXTRA { int lock; int *plock; }; 86 | #undef LUA_EXTRASPACE 87 | #define LUA_EXTRASPACE sizeof(struct L_EXTRA) 88 | #define getlock(l) cast(struct L_EXTRA*, lua_getextraspace(l)) 89 | #define luai_userstateopen(l) \ 90 | (getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock)) 91 | #define luai_userstateclose(l) \ 92 | lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock)) 93 | #define luai_userstatethread(l,l1) \ 94 | lua_assert(getlock(l1)->plock == getlock(l)->plock) 95 | #define luai_userstatefree(l,l1) \ 96 | lua_assert(getlock(l)->plock == getlock(l1)->plock) 97 | #define lua_lock(l) lua_assert((*getlock(l)->plock)++ == 0) 98 | #define lua_unlock(l) lua_assert(--(*getlock(l)->plock) == 0) 99 | 100 | 101 | 102 | LUA_API int luaB_opentests (lua_State *L); 103 | 104 | LUA_API void *debug_realloc (void *ud, void *block, 105 | size_t osize, size_t nsize); 106 | 107 | #if defined(lua_c) 108 | #define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol) 109 | #define luaL_openlibs(L) \ 110 | { (luaL_openlibs)(L); \ 111 | luaL_requiref(L, "T", luaB_opentests, 1); \ 112 | lua_pop(L, 1); } 113 | #endif 114 | 115 | 116 | 117 | /* change some sizes to give some bugs a chance */ 118 | 119 | #undef LUAL_BUFFERSIZE 120 | #define LUAL_BUFFERSIZE 23 121 | #define MINSTRTABSIZE 2 122 | #define MAXIWTHABS 3 123 | 124 | 125 | /* make stack-overflow tests run faster */ 126 | #undef LUAI_MAXSTACK 127 | #define LUAI_MAXSTACK 50000 128 | 129 | 130 | #undef LUAI_USER_ALIGNMENT_T 131 | #define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } 132 | 133 | 134 | #define STRCACHE_N 23 135 | #define STRCACHE_M 5 136 | 137 | #endif 138 | 139 | -------------------------------------------------------------------------------- /sys/modules/lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Test whether there is no tagmethod. 50 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 51 | */ 52 | #define notm(tm) ttisnil(tm) 53 | 54 | 55 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 56 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 57 | 58 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 59 | 60 | #define ttypename(x) luaT_typenames_[(x) + 1] 61 | 62 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTAGS];) 63 | 64 | 65 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 66 | 67 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 68 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 69 | TMS event); 70 | LUAI_FUNC void luaT_init (lua_State *L); 71 | 72 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 73 | const TValue *p2, const TValue *p3); 74 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 75 | const TValue *p1, const TValue *p2, StkId p3); 76 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 77 | StkId res, TMS event); 78 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 79 | const TValue *p2, StkId res, int inv, TMS event); 80 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 81 | int inv, StkId res, TMS event); 82 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 83 | const TValue *p2, TMS event); 84 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 85 | int inv, TMS event); 86 | 87 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 88 | struct CallInfo *ci, const Proto *p); 89 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 90 | StkId where, int wanted); 91 | 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /sys/modules/lua/lua_arm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/lua/lua_arm.o -------------------------------------------------------------------------------- /sys/modules/lua/lua_be.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2019 Ulrich Hecht 3 | 4 | #include "lua_defs.h" 5 | #include 6 | #include 7 | #include 8 | 9 | static int l_cls(lua_State *l) { 10 | eb_cls(); 11 | eb_locate(0, 0); 12 | return 0; 13 | } 14 | 15 | static int l_basic(lua_State *l) { 16 | luaL_error(l, "return to basic"); 17 | return 0; 18 | } 19 | 20 | int luaopen_be(lua_State *l) { 21 | lua_pushcfunction(l, l_cls); 22 | lua_setglobal(l, "cls"); 23 | lua_pushcfunction(l, l_basic); 24 | lua_setglobal(l, "basic"); 25 | return 0; 26 | } 27 | 28 | void do_lua_line(const char *lbuf) { 29 | char *retline; 30 | asprintf(&retline, "return %s;", lbuf); 31 | if ((luaL_loadstring(lua, retline) != LUA_OK &&luaL_loadstring(lua, lbuf) != LUA_OK) || 32 | lua_pcall(lua, 0, LUA_MULTRET, 0) != LUA_OK) { 33 | const char *err_str = lua_tostring(lua, -1); 34 | if (strstr(err_str, "return to basic")) { 35 | lua_close(lua); 36 | lua = NULL; 37 | } else 38 | printf("error: %s\n", err_str); 39 | } else { 40 | int retvals = lua_gettop(lua); 41 | if (retvals > 0) { 42 | luaL_checkstack(lua, LUA_MINSTACK, "too many results to print"); 43 | lua_getglobal(lua, "print"); 44 | lua_insert(lua, 1); 45 | if (lua_pcall(lua, retvals, 0, 0) != LUA_OK) 46 | printf("error calling 'print' (%s)\n", lua_tostring(lua, -1)); 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /sys/modules/lua/lua_defs.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2019-2021 Ulrich Hecht 3 | 4 | #ifndef __LUA_DEFS_H 5 | #define __LUA_DEFS_H 6 | 7 | #include "lua.h" 8 | #include "lualib.h" 9 | #include "lauxlib.h" 10 | 11 | extern lua_State *lua; 12 | 13 | int luaopen_be(lua_State *l); 14 | int luaopen_bg(lua_State *l); 15 | int luaopen_img(lua_State *l); 16 | int luaopen_input(lua_State *l); 17 | int luaopen_video(lua_State *l); 18 | int luaopen_hwio(lua_State *l); 19 | 20 | static void lua_pushret(lua_State *l, int ret) { 21 | if (ret) 22 | lua_pushnil(l); 23 | else 24 | lua_pushboolean(l, 1); 25 | } 26 | 27 | void do_lua_line(const char *lbuf); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /sys/modules/lua/lua_i386.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/lua/lua_i386.o -------------------------------------------------------------------------------- /sys/modules/lua/lua_img.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2019 Ulrich Hecht 3 | 4 | #include "lua_defs.h" 5 | #include 6 | 7 | static int l_load_image(lua_State *l) { 8 | const char *file = luaL_checkstring(l, 1); 9 | uint32_t key = 0; 10 | int32_t dx = -1, dy = -1; 11 | int32_t sx = 0, sy = 0; 12 | int32_t w = -1, h = -1; 13 | 14 | if (lua_gettop(l) > 1) { 15 | key = luaL_checkinteger(l, 2); 16 | } 17 | if (lua_gettop(l) > 2) { 18 | dx = luaL_checkinteger(l, 3); 19 | dy = luaL_checkinteger(l, 4); 20 | } 21 | if (lua_gettop(l) > 4) { 22 | sx = luaL_checkinteger(l, 5); 23 | sy = luaL_checkinteger(l, 6); 24 | } 25 | if (lua_gettop(l) > 6) { 26 | w = luaL_checkinteger(l, 7); 27 | h = luaL_checkinteger(l, 8); 28 | } 29 | 30 | struct eb_image_spec loc = { 31 | .dst_x = dx, 32 | .dst_y = dy, 33 | .src_x = sx, 34 | .src_y = sy, 35 | .w = w, 36 | .h = h, 37 | .scale_x = 1, 38 | .scale_y = 1, 39 | .key = key, 40 | }; 41 | 42 | int ret = eb_load_image(file, &loc); 43 | 44 | if (ret == 0) { 45 | lua_pushinteger(l, loc.dst_x); 46 | lua_pushinteger(l, loc.dst_y); 47 | lua_pushinteger(l, loc.w); 48 | lua_pushinteger(l, loc.h); 49 | } else { 50 | lua_pushnil(l); 51 | lua_pushnil(l); 52 | lua_pushnil(l); 53 | lua_pushnil(l); 54 | } 55 | 56 | return 4; 57 | } 58 | 59 | int luaopen_img(lua_State *l) { 60 | lua_pushcfunction(l, l_load_image); 61 | lua_setglobal(l, "load_image"); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /sys/modules/lua/lua_input.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2019 Ulrich Hecht 3 | 4 | #include "lua_defs.h" 5 | #include 6 | 7 | static int l_inkey(lua_State *l) { 8 | lua_pushret(l, eb_inkey()); 9 | return 1; 10 | } 11 | 12 | static int l_pad_state(lua_State *l) { 13 | int32_t num = luaL_checkinteger(l, 1); 14 | lua_pushinteger(l, eb_pad_state(num)); 15 | return 1; 16 | } 17 | 18 | static int l_key_state(lua_State *l) { 19 | int32_t scancode = luaL_checkinteger(l, 1); 20 | lua_pushinteger(l, eb_key_state(scancode)); 21 | return 1; 22 | } 23 | 24 | int luaopen_input(lua_State *l) { 25 | lua_pushcfunction(l, l_inkey); 26 | lua_setglobal(l, "inkey"); 27 | lua_pushcfunction(l, l_pad_state); 28 | lua_setglobal(l, "pad_state"); 29 | lua_pushcfunction(l, l_key_state); 30 | lua_setglobal(l, "key_state"); 31 | lua_pushinteger(l, EB_JOY_UP); 32 | lua_setglobal(l, "PAD_UP"); 33 | lua_pushinteger(l, EB_JOY_DOWN); 34 | lua_setglobal(l, "PAD_DOWN"); 35 | lua_pushinteger(l, EB_JOY_LEFT); 36 | lua_setglobal(l, "PAD_LEFT"); 37 | lua_pushinteger(l, EB_JOY_RIGHT); 38 | lua_setglobal(l, "PAD_RIGHT"); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /sys/modules/lua/lua_io.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2021 Ulrich Hecht 3 | 4 | #include "eb_io.h" 5 | #include "lua_defs.h" 6 | #include "ltable.h" 7 | #include 8 | 9 | static int l_gpio_set_pin(lua_State *l) { 10 | uint32_t portno = luaL_checkinteger(l, 1); 11 | uint32_t pinno = luaL_checkinteger(l, 2); 12 | uint32_t data = luaL_checkinteger(l, 3); 13 | 14 | lua_pushret(l, eb_gpio_set_pin(portno, pinno, data)); 15 | return 1; 16 | } 17 | 18 | static int l_gpio_get_pin(lua_State *l) { 19 | uint32_t portno = luaL_checkinteger(l, 1); 20 | uint32_t pinno = luaL_checkinteger(l, 2); 21 | 22 | lua_pushret(l, eb_gpio_get_pin(portno, pinno)); 23 | return 1; 24 | } 25 | 26 | static int l_gpio_set_pin_mode(lua_State *l) { 27 | uint32_t portno = luaL_checkinteger(l, 1); 28 | uint32_t pinno = luaL_checkinteger(l, 2); 29 | uint32_t mode = luaL_checkinteger(l, 3); 30 | 31 | lua_pushret(l, eb_gpio_set_pin_mode(portno, pinno, mode)); 32 | return 1; 33 | } 34 | 35 | static int l_i2c_write(lua_State *l) { 36 | int addr = luaL_checkinteger(l, 1); 37 | 38 | luaL_checktype(l, 2, LUA_TTABLE); 39 | int count = lua_rawlen(l, 2); 40 | 41 | char out_data[count]; 42 | for (int i = 1; i <= count; ++i) { 43 | lua_rawgeti(l, 2, i); 44 | out_data[i - 1] = lua_tointeger(l, -1); 45 | lua_pop(l, 1); 46 | //printf("%02X ", out_data[i]); 47 | } 48 | 49 | eb_i2c_write(addr, out_data, count); 50 | 51 | return 0; 52 | } 53 | 54 | static int l_i2c_read(lua_State *l) { 55 | int addr = luaL_checkinteger(l, 1); 56 | int count = luaL_checkinteger(l, 2); 57 | 58 | char in_data[count]; 59 | 60 | if (eb_i2c_read(addr, in_data, count) != 0) { 61 | lua_pushnil(l); 62 | } else { 63 | lua_newtable(l); 64 | for (int i = 1; i <= count; ++i) { 65 | lua_pushinteger(l, i); 66 | lua_pushinteger(l, in_data[i - 1]); 67 | lua_rawset(l, -3); 68 | } 69 | } 70 | 71 | return 1; 72 | } 73 | 74 | static int l_spi_write(lua_State *l) { 75 | luaL_checktype(l, 1, LUA_TTABLE); 76 | int count = lua_rawlen(l, 1); 77 | 78 | char out_data[count]; 79 | for (int i = 1; i <= count; ++i) { 80 | lua_rawgeti(l, 1, i); 81 | out_data[i - 1] = lua_tointeger(l, -1); 82 | lua_pop(l, 1); 83 | //printf("%02X ", out_data[i]); 84 | } 85 | 86 | eb_spi_write(out_data, count); 87 | 88 | return 0; 89 | } 90 | 91 | static int l_spi_transfer(lua_State *l) { 92 | luaL_checktype(l, 1, LUA_TTABLE); 93 | int count = lua_rawlen(l, 1); 94 | 95 | char out_data[count]; 96 | for (int i = 1; i <= count; ++i) { 97 | lua_rawgeti(l, 1, i); 98 | out_data[i - 1] = lua_tointeger(l, -1); 99 | lua_pop(l, 1); 100 | //printf("%02X ", out_data[i]); 101 | } 102 | 103 | char in_data[count]; 104 | eb_spi_transfer(out_data, in_data, count); 105 | 106 | lua_newtable(l); 107 | for (int i = 1; i <= count; ++i) { 108 | lua_pushinteger(l, i); 109 | lua_pushinteger(l, in_data[i - 1]); 110 | lua_rawset(l, -3); 111 | } 112 | 113 | return 1; 114 | } 115 | 116 | int luaopen_hwio(lua_State *l) { 117 | lua_pushcfunction(l, l_gpio_set_pin); 118 | lua_setglobal(l, "gpio_set_pin"); 119 | lua_pushcfunction(l, l_gpio_get_pin); 120 | lua_setglobal(l, "gpio_get_pin"); 121 | lua_pushcfunction(l, l_gpio_set_pin_mode); 122 | lua_setglobal(l, "gpio_get_pin_mode"); 123 | lua_pushcfunction(l, l_i2c_write); 124 | lua_setglobal(l, "i2c_write"); 125 | lua_pushcfunction(l, l_i2c_read); 126 | lua_setglobal(l, "i2c_read"); 127 | lua_pushcfunction(l, l_spi_write); 128 | lua_setglobal(l, "spi_write"); 129 | lua_pushcfunction(l, l_spi_transfer); 130 | lua_setglobal(l, "spi_transfer"); 131 | 132 | return 0; 133 | } 134 | -------------------------------------------------------------------------------- /sys/modules/lua/lua_x86_64.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/lua/lua_x86_64.o -------------------------------------------------------------------------------- /sys/modules/lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_MATHLIBNAME "math" 39 | LUAMOD_API int (luaopen_math) (lua_State *L); 40 | 41 | #define LUA_DBLIBNAME "debug" 42 | LUAMOD_API int (luaopen_debug) (lua_State *L); 43 | 44 | #define LUA_LOADLIBNAME "package" 45 | LUAMOD_API int (luaopen_package) (lua_State *L); 46 | 47 | 48 | /* open all previous libraries */ 49 | LUALIB_API void (luaL_openlibs) (lua_State *L); 50 | 51 | 52 | 53 | #if !defined(lua_assert) 54 | #define lua_assert(x) ((void)0) 55 | #endif 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /sys/modules/lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 27 | 28 | /* dump one chunk; from ldump.c */ 29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 30 | void* data, int strip); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /sys/modules/lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I 0 37 | #endif 38 | 39 | 40 | /* convert an object to a float (including string coercion) */ 41 | #define tonumber(o,n) \ 42 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 43 | 44 | 45 | /* convert an object to a float (without string coercion) */ 46 | #define tonumberns(o,n) \ 47 | (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ 48 | (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) 49 | 50 | 51 | /* convert an object to an integer (including string coercion) */ 52 | #define tointeger(o,i) \ 53 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 54 | 55 | 56 | /* convert an object to an integer (without string coercion) */ 57 | #define tointegerns(o,i) \ 58 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) 59 | 60 | 61 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 62 | 63 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 64 | 65 | 66 | /* 67 | ** fast track for 'gettable': if 't' is a table and 't[k]' is present, 68 | ** return 1 with 'slot' pointing to 't[k]' (position of final result). 69 | ** Otherwise, return 0 (meaning it will have to check metamethod) 70 | ** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL 71 | ** (otherwise). 'f' is the raw get function to use. 72 | */ 73 | #define luaV_fastget(L,t,k,slot,f) \ 74 | (!ttistable(t) \ 75 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 76 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 77 | !isempty(slot))) /* result not empty? */ 78 | 79 | 80 | /* 81 | ** Special case of 'luaV_fastget' for integers, inlining the fast case 82 | ** of 'luaH_getint'. 83 | */ 84 | #define luaV_fastgeti(L,t,k,slot) \ 85 | (!ttistable(t) \ 86 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 87 | : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ 88 | ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ 89 | !isempty(slot))) /* result not empty? */ 90 | 91 | 92 | /* 93 | ** Finish a fast set operation (when fast get succeeds). In that case, 94 | ** 'slot' points to the place to put the value. 95 | */ 96 | #define luaV_finishfastset(L,t,slot,v) \ 97 | { setobj2t(L, cast(TValue *,slot), v); \ 98 | luaC_barrierback(L, gcvalue(t), v); } 99 | 100 | 101 | 102 | 103 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 104 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 105 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 106 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 107 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); 108 | LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode); 109 | LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode); 110 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 111 | StkId val, const TValue *slot); 112 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 113 | TValue *val, const TValue *slot); 114 | LUAI_FUNC void luaV_finishOp (lua_State *L); 115 | LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); 116 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 117 | LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); 118 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 119 | LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); 120 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 121 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 122 | 123 | #endif 124 | -------------------------------------------------------------------------------- /sys/modules/lua/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /sys/modules/lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /sys/modules/lua/sys.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2021 Ulrich Hecht 3 | 4 | #include "lua_defs.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | lua_State *lua = NULL; 13 | 14 | static void lhook(lua_State *L, lua_Debug *ar) { 15 | (void)ar; 16 | if (eb_process_events_check()) 17 | luaL_error(L, "interrupted!"); 18 | } 19 | 20 | void lua_cmd(const eb_param_t *params) { 21 | lua = luaL_newstate(); 22 | if (!lua) { 23 | eb_set_error(ERR_SYS, NULL); 24 | return; 25 | } 26 | 27 | luaL_openlibs(lua); 28 | luaopen_be(lua); 29 | luaopen_bg(lua); 30 | luaopen_img(lua); 31 | luaopen_input(lua); 32 | luaopen_video(lua); 33 | luaopen_hwio(lua); 34 | lua_sethook(lua, lhook, LUA_MASKCOUNT, 1000); 35 | 36 | char *line; 37 | while (lua != NULL) { 38 | printf("ok\n"); 39 | eb_show_cursor(1); 40 | line = eb_screened_get_line(); 41 | eb_show_cursor(0); 42 | eb_putch('\n'); 43 | if (line) { 44 | do_lua_line(line); 45 | free(line); 46 | } 47 | } 48 | } 49 | 50 | static const enum token_t lua_syntax[] = { I_EOL }; 51 | 52 | void __initcall(void) { 53 | eb_add_command("LUA", lua_syntax, lua_cmd); 54 | } 55 | -------------------------------------------------------------------------------- /sys/modules/sam/ESP8266SAM.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266SAM 3 | Port of SAM to the ESP8266 4 | 5 | Copyright (C) 2017 Earle F. Philhower, III 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | //#include 20 | #include 21 | #include 22 | 23 | uint8_t ESP8266SAM::getSample() 24 | { 25 | if (finished()) 26 | return 128; 27 | if (bufptr_read >= bufptr_write) { 28 | bufptr_read = bufptr_write = 0; 29 | if (!moreSamples()) 30 | return 128; 31 | } 32 | return buffer[bufptr_read++]; 33 | } 34 | 35 | #ifdef PC_HOSTED 36 | void fill_audio(void *udata, Uint8 *stream, int len) 37 | { 38 | ESP8266SAM *sam = (ESP8266SAM *)udata; 39 | while (len--) { 40 | *stream++ = sam->getSample(); 41 | } 42 | } 43 | #endif 44 | // Thunk from C to C++ with a this-> pointer 45 | void ESP8266SAM::OutputByteCallback(void *cbdata, unsigned char b) 46 | { 47 | ESP8266SAM *sam = static_cast(cbdata); 48 | sam->OutputByte(b); 49 | } 50 | 51 | void ESP8266SAM::OutputByte(unsigned char b) 52 | { 53 | // Xvert unsigned 8 to signed 16... 54 | int16_t s16 = b;// s16 -= 128; //s16 *= 128; 55 | uint8_t sample[2]; 56 | sample[0] = s16; 57 | sample[1] = s16; 58 | buffer[bufptr_write++] = sample[0]; 59 | #ifdef PC_HOSTED 60 | if (bufptr_write >= SAMPLE_BUF_SIZE) { 61 | dbg_sam("bufof! %d %d\n", bufptr_read, bufptr_write); 62 | abort(); 63 | } 64 | #endif 65 | } 66 | 67 | void ESP8266SAM::Say(const char *str) 68 | { 69 | // dump remaining samples 70 | while (moreSamples()) { bufptr_write = 0; } 71 | 72 | if (!str || strlen(str)>254) return; // Only can speak up to 1 page worth of data... 73 | 74 | // Input massaging 75 | char input[256]; 76 | memset(input, 0, 256); 77 | for (int i=0; str[i]; i++) 78 | input[i] = toupper((int)str[i]); 79 | input[strlen(str)] = 0; 80 | 81 | // To phonemes 82 | if (phonetic) { 83 | strncat(input, "\x9b", 255); 84 | } else { 85 | strncat(input, "[", 255); 86 | if (!TextToPhonemes(input)) return; // ERROR 87 | } 88 | dbg_sam("ph %s\n", input); 89 | 90 | // Say it! 91 | // output = out; 92 | SetInput(input); 93 | SAMMain(OutputByteCallback, (void*)this); 94 | render_state = RENDER_PREP; 95 | } 96 | 97 | bool __attribute__((optimize ("no-jump-tables"))) ESP8266SAM::moreSamples() 98 | { 99 | for (;;) { 100 | switch (render_state) { 101 | case RENDER_IDLE: 102 | return false; 103 | case RENDER_PREP: 104 | case RENDER_PREP_RESTORE: 105 | PrepareOutput(); 106 | break; 107 | case RENDER_REND: 108 | Render(); 109 | break; 110 | case RENDER_LOOP: 111 | case RENDER_SAMPLE_END: 112 | RenderLoop(); 113 | return true; 114 | case RENDER_SAMPLE_1: 115 | case RENDER_SAMPLE_2: 116 | case RENDER_SAMPLE_3: 117 | RenderSample(); 118 | return true; 119 | default: 120 | return false; 121 | } 122 | } 123 | } 124 | 125 | void ESP8266SAM::SetVoice(enum SAMVoice voice) 126 | { 127 | switch (voice) { 128 | case VOICE_ELF: SetSpeed(72); SetPitch(64); SetThroat(110); SetMouth(160); break; 129 | case VOICE_ROBOT: SetSpeed(92); SetPitch(60); SetThroat(190); SetMouth(190); break; 130 | case VOICE_STUFFY: SetSpeed(82); SetPitch(72); SetThroat(110); SetMouth(105); break; 131 | case VOICE_OLDLADY: SetSpeed(82); SetPitch(32); SetThroat(145); SetMouth(145); break; 132 | case VOICE_ET: SetSpeed(100); SetPitch(64); SetThroat(150); SetMouth(200); break; 133 | default: 134 | case VOICE_SAM: SetSpeed(72); SetPitch(64); SetThroat(128); SetMouth(128); break; 135 | } 136 | } 137 | 138 | -------------------------------------------------------------------------------- /sys/modules/sam/Makefile: -------------------------------------------------------------------------------- 1 | # point this to the Engine BASIC system directory, either the one on the SD 2 | # card image (/sys), or the one created using the "init_dir" target of the 3 | # Engine BASIC Makefiles (/init_dir/sys) 4 | BE_SYSDIR = ../../../../enginebasic/init_dir/sys 5 | 6 | # prefixes of the toolchains for the different CPU architectures 7 | ARM_CROSS_COMPILE = ~/x-tools/arm-unknown-eabihf/bin/arm-unknown-eabihf- 8 | X64_CROSS_COMPILE = 9 | I386_CROSS_COMPILE = i686-linux-gnu- 10 | 11 | CFLAGS = -O3 -Wall -I. -I$(BE_SYSDIR)/include -nostdlib -ffreestanding -D_GNU_SOURCE -DPIXEL_TYPE=uint32_t -DIPIXEL_TYPE=uint32_t 12 | ARM_CFLAGS = -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 13 | X64_CFLAGS = 14 | I386_CFLAGS = -fno-pic -fno-exceptions -fno-asynchronous-unwind-tables -m32 15 | 16 | OBJS_ARM = debug_arm.o ESP8266SAM_arm.o reciter_arm.o render_arm.o sam_core_arm.o module_arm.o 17 | OBJS_X64 = debug_x86_64.o ESP8266SAM_x86_64.o reciter_x86_64.o render_x86_64.o sam_core_x86_64.o module_x86_64.o 18 | OBJS_I386 = debug_i386.o ESP8266SAM_i386.o reciter_i386.o render_i386.o sam_core_i386.o module_i386.o 19 | 20 | all: sam_arm.o sam_x86_64.o sam_i386.o 21 | 22 | sam_arm.o: $(OBJS_ARM) 23 | $(ARM_CROSS_COMPILE)ld -o $@ -r $(OBJS_ARM) 24 | 25 | sam_x86_64.o: $(OBJS_X64) 26 | $(X64_CROSS_COMPILE)ld -o $@ -r $(OBJS_X64) 27 | 28 | sam_i386.o: $(OBJS_I386) 29 | $(I386_CROSS_COMPILE)ld -o $@ -r $(OBJS_I386) 30 | 31 | %_arm.o: %.c 32 | $(ARM_CROSS_COMPILE)gcc $(CFLAGS) $(ARM_CFLAGS) -c -o $@ $< 33 | %_x86_64.o: %.c 34 | $(X64_CROSS_COMPILE)gcc $(CFLAGS) $(X64_CFLAGS) -c -o $@ $< 35 | %_i386.o: %.c 36 | $(I386_CROSS_COMPILE)gcc $(CFLAGS) $(I386_CFLAGS) -c -o $@ $< 37 | %_arm.o: %.cpp 38 | $(ARM_CROSS_COMPILE)g++ $(CFLAGS) $(ARM_CFLAGS) -c -o $@ $< 39 | %_x86_64.o: %.cpp 40 | $(X64_CROSS_COMPILE)g++ $(CFLAGS) $(X64_CFLAGS) -c -o $@ $< 41 | %_i386.o: %.cpp 42 | $(I386_CROSS_COMPILE)g++ $(CFLAGS) $(I386_CFLAGS) -c -o $@ $< 43 | 44 | clean: 45 | rm -f *.o 46 | 47 | -------------------------------------------------------------------------------- /sys/modules/sam/README.md: -------------------------------------------------------------------------------- 1 | This module contains the SAM speech synthesizer that was previously built 2 | into the BASIC Engine firmware. 3 | 4 | To be able to use the `SAY` command, you have to either run `LOADMOD "sam"`, 5 | or add the line `#REQUIRE "sam"` at the start of your BASIC program. 6 | 7 | This module also shows how to implement modules in C++ using the (very 8 | limited) run-time support provided by the Engine BASIC API. It cannot be 9 | compiled using the on-board TCC compiler because TCC does not support C++. 10 | 11 | To re-compile the SAM module using a cross-compiler, edit `Makefile` and 12 | change `BE_SYSDIR` to point to the `/sys/include` file of a BASIC Engine SD 13 | card image. 14 | -------------------------------------------------------------------------------- /sys/modules/sam/build.ninja: -------------------------------------------------------------------------------- 1 | be_sysdir = ../../../../enginebasic/init_dir/sys 2 | arm_cross_compile = ~/x-tools/arm-unknown-eabihf/bin/arm-unknown-eabihf- 3 | x64_cross_compile = 4 | i386_cross_compile = i686-linux-gnu- 5 | 6 | common_flags = -O3 -Wall -I. -I${be_sysdir}/include -nostdlib -ffreestanding -D_GNU_SOURCE -DPIXEL_TYPE=uint32_t -DIPIXEL_TYPE=uint32_t 7 | arm_cflags = $common_flags -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 8 | x64_cflags = $common_flags 9 | i386_cflags = $common_flags -fno-pic -fno-exceptions -fno-asynchronous-unwind-tables -m32 10 | 11 | rule link 12 | command = ${cross_compile}ld -o $out -r $in 13 | 14 | rule cxx 15 | command = ${cross_compile}g++ $cflags -c -o $out $in 16 | 17 | build sam_arm.o: link debug_arm.o ESP8266SAM_arm.o reciter_arm.o render_arm.o sam_core_arm.o module_arm.o 18 | cross_compile = $arm_cross_compile 19 | build sam_x86_64.o: link debug_x86_64.o ESP8266SAM_x86_64.o reciter_x86_64.o render_x86_64.o sam_core_x86_64.o module_x86_64.o 20 | cross_compile = $x64_cross_compile 21 | build sam_i386.o: link debug_i386.o ESP8266SAM_i386.o reciter_i386.o render_i386.o sam_core_i386.o module_i386.o 22 | cross_compile = $i386_cross_compile 23 | 24 | default sam_arm.o sam_x86_64.o sam_i386.o 25 | 26 | build debug_arm.o: cxx debug.cpp 27 | cflags = $arm_cflags 28 | cross_compile = $arm_cross_compile 29 | 30 | build ESP8266SAM_arm.o: cxx ESP8266SAM.cpp 31 | cflags = $arm_cflags 32 | cross_compile = $arm_cross_compile 33 | 34 | build module_arm.o: cxx module.cpp 35 | cflags = $arm_cflags 36 | cross_compile = $arm_cross_compile 37 | 38 | build reciter_arm.o: cxx reciter.cpp 39 | cflags = $arm_cflags 40 | cross_compile = $arm_cross_compile 41 | 42 | build render_arm.o: cxx render.cpp 43 | cflags = $arm_cflags 44 | cross_compile = $arm_cross_compile 45 | 46 | build sam_core_arm.o: cxx sam_core.cpp 47 | cflags = $arm_cflags 48 | cross_compile = $arm_cross_compile 49 | 50 | build debug_x86_64.o: cxx debug.cpp 51 | cflags = $x64_cflags 52 | cross_compile = $x64_cross_compile 53 | 54 | build ESP8266SAM_x86_64.o: cxx ESP8266SAM.cpp 55 | cflags = $x64_cflags 56 | cross_compile = $x64_cross_compile 57 | 58 | build module_x86_64.o: cxx module.cpp 59 | cflags = $x64_cflags 60 | cross_compile = $x64_cross_compile 61 | 62 | build reciter_x86_64.o: cxx reciter.cpp 63 | cflags = $x64_cflags 64 | cross_compile = $x64_cross_compile 65 | 66 | build render_x86_64.o: cxx render.cpp 67 | cflags = $x64_cflags 68 | cross_compile = $x64_cross_compile 69 | 70 | build sam_core_x86_64.o: cxx sam_core.cpp 71 | cflags = $x64_cflags 72 | cross_compile = $x64_cross_compile 73 | 74 | build debug_i386.o: cxx debug.cpp 75 | cflags = $i386_cflags 76 | cross_compile = $i386_cross_compile 77 | 78 | build ESP8266SAM_i386.o: cxx ESP8266SAM.cpp 79 | cflags = $i386_cflags 80 | cross_compile = $i386_cross_compile 81 | 82 | build module_i386.o: cxx module.cpp 83 | cflags = $i386_cflags 84 | cross_compile = $i386_cross_compile 85 | 86 | build reciter_i386.o: cxx reciter.cpp 87 | cflags = $i386_cflags 88 | cross_compile = $i386_cross_compile 89 | 90 | build render_i386.o: cxx render.cpp 91 | cflags = $i386_cflags 92 | cross_compile = $i386_cross_compile 93 | 94 | build sam_core_i386.o: cxx sam_core.cpp 95 | cflags = $i386_cflags 96 | cross_compile = $i386_cross_compile 97 | -------------------------------------------------------------------------------- /sys/modules/sam/debug.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "debug.h" 3 | 4 | extern const unsigned char signInputTable1[]; 5 | extern const unsigned char signInputTable2[]; 6 | 7 | void PrintPhonemes(unsigned char *phonemeindex, unsigned char *phonemeLength, unsigned char *stress) 8 | { 9 | #ifdef DEBUG_SAM 10 | int i = 0; 11 | printf("===========================================\n"); 12 | 13 | printf("Internal Phoneme presentation:\n\n"); 14 | printf(" idx phoneme length stress\n"); 15 | printf("------------------------------\n"); 16 | 17 | while((phonemeindex[i] != 255) && (i < 255)) 18 | { 19 | if (phonemeindex[i] < 81) 20 | { 21 | printf(" %3i %c%c %3i %i\n", 22 | phonemeindex[i], 23 | signInputTable1[phonemeindex[i]], 24 | signInputTable2[phonemeindex[i]], 25 | phonemeLength[i], 26 | stress[i] 27 | ); 28 | } else 29 | { 30 | printf(" %3i ?? %3i %i\n", phonemeindex[i], phonemeLength[i], stress[i]); 31 | } 32 | i++; 33 | } 34 | printf("===========================================\n"); 35 | printf("\n"); 36 | #endif 37 | } 38 | 39 | void PrintOutput( 40 | unsigned char *flag, 41 | unsigned char *f1, 42 | unsigned char *f2, 43 | unsigned char *f3, 44 | unsigned char *a1, 45 | unsigned char *a2, 46 | unsigned char *a3, 47 | unsigned char *p) 48 | { 49 | #ifdef DEBUG_SAM 50 | printf("===========================================\n"); 51 | printf("Final data for speech output:\n\n"); 52 | int i = 0; 53 | printf(" flags ampl1 freq1 ampl2 freq2 ampl3 freq3 pitch\n"); 54 | printf("------------------------------------------------\n"); 55 | while(i < 255) 56 | { 57 | printf("%5i %5i %5i %5i %5i %5i %5i %5i\n", flag[i], a1[i], f1[i], a2[i], f2[i], a3[i], f3[i], p[i]); 58 | i++; 59 | } 60 | printf("===========================================\n"); 61 | #endif 62 | } 63 | 64 | extern unsigned char GetRuleByte(unsigned short mem62, unsigned char Y); 65 | 66 | void PrintRule(int offset) 67 | { 68 | #ifdef DEBUG_SAM 69 | int i = 1; 70 | unsigned char A = 0; 71 | printf("Applying rule: "); 72 | do 73 | { 74 | A = GetRuleByte(offset, i); 75 | if ((A&127) == '=') printf(" -> "); else printf("%c", A&127); 76 | i++; 77 | } while ((A&128)==0); 78 | printf("\n"); 79 | #endif 80 | } 81 | -------------------------------------------------------------------------------- /sys/modules/sam/debug.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUG_H 2 | #define DEBUG_H 3 | 4 | void PrintPhonemes(unsigned char *phonemeindex, unsigned char *phonemeLength, unsigned char *stress); 5 | void PrintOutput( 6 | unsigned char *flag, 7 | unsigned char *f1, 8 | unsigned char *f2, 9 | unsigned char *f3, 10 | unsigned char *a1, 11 | unsigned char *a2, 12 | unsigned char *a3, 13 | unsigned char *p); 14 | 15 | void PrintRule(int offset); 16 | 17 | #define debug 0 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /sys/modules/sam/module.cpp: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | // Copyright (c) 2021 Ulrich Hecht 3 | 4 | #include "ESP8266SAM.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | ESP8266SAM *sam; 16 | sts_mixer_sample_t sample; 17 | 18 | int buf_size; 19 | uint8_t *buf; 20 | 21 | uint32_t end_tick; 22 | 23 | extern "C" int sam_done(void) 24 | { 25 | if (eb_utick() >= end_tick) 26 | return 1; 27 | else 28 | return 0; 29 | } 30 | 31 | extern "C" void sam_speak(const char *s) 32 | { 33 | sam->Say(s); 34 | int bp = 0; 35 | while (!sam->finished()) { 36 | uint8_t samp = sam->getSample(); 37 | if (bp >= buf_size) { 38 | buf_size *= 2; 39 | buf = (uint8_t *)realloc(buf, buf_size); 40 | } 41 | // fprintf(stdout, "s %d %02X\n", bp, samp); 42 | buf[bp++] = samp; 43 | } 44 | 45 | if (sample.length > 0) 46 | sts_mixer_stop_sample(eb_get_mixer(), &sample); 47 | 48 | sample.data = buf; 49 | sample.length = bp; 50 | sts_mixer_play_sample(eb_get_mixer(), &sample, 1.0f, 1.0f, 0.0f); 51 | end_tick = eb_utick() + bp * 100000 / 2205; 52 | } 53 | 54 | EXPORT(sam_speak) 55 | EXPORT(sam_done) 56 | 57 | void say(const eb_param_t *params) 58 | { 59 | sam_speak(params[0].str); 60 | } 61 | 62 | double samdone(const eb_param_t *params) 63 | { 64 | return sam_done(); 65 | } 66 | 67 | const token_t syn_say[] = { I_STR, I_EOL }; 68 | const token_t syn_samdone[] = { I_OPEN, I_CLOSE, I_EOL }; 69 | 70 | extern "C" void __initcall(void) 71 | { 72 | sam = new ESP8266SAM; 73 | if (!sam) { 74 | err = ERR_OOM; 75 | return; 76 | } 77 | 78 | sample.frequency = 22050; 79 | sample.audio_format = STS_MIXER_SAMPLE_FORMAT_8; 80 | sample.length = 0; 81 | 82 | buf_size = 512; 83 | buf = new uint8_t[buf_size]; 84 | 85 | end_tick = 0; 86 | 87 | eb_add_command("SAY", syn_say, say); 88 | eb_add_numfun("SAMDONE", syn_samdone, samdone); 89 | } 90 | -------------------------------------------------------------------------------- /sys/modules/sam/reciter.h: -------------------------------------------------------------------------------- 1 | #ifndef RECITER_C 2 | #define RECITER_C 3 | 4 | //int TextToPhonemes(char *input, char *output); 5 | 6 | int TextToPhonemes(char *input); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /sys/modules/sam/render.h: -------------------------------------------------------------------------------- 1 | #ifndef RENDER_H 2 | #define RENDER_H 3 | 4 | //void Render(); 5 | //void SetMouthThroat(unsigned char mouth, unsigned char throat); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /sys/modules/sam/sam_arm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/sam/sam_arm.o -------------------------------------------------------------------------------- /sys/modules/sam/sam_i386.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/sam/sam_i386.o -------------------------------------------------------------------------------- /sys/modules/sam/sam_x86_64.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uli/basicengine-demos/ef6fb4b7155a106c766055bd84b509ab81c7e2d3/sys/modules/sam/sam_x86_64.o -------------------------------------------------------------------------------- /tetris/README.md: -------------------------------------------------------------------------------- 1 | Conversion of David Murray's Tetris for Commodore BASIC to the BASIC Engine. 2 | --------------------------------------------------------------------------------