beneroth changed the topic of #picolisp to: PicoLisp language | The scalpel of software development | Channel Log: https://libera.irclog.whitequark.org/picolisp | Check www.picolisp.com for more information
bjorkint0sh has quit [Quit: "Every day, computers are making people easier to use." David Temkin]
bjorkintosh has joined #picolisp
bjorkintosh has joined #picolisp
<geri> inline structs would be great
<geri> (%@ "connect" 'I Sockfd '(NIL (12) `(- AF_UNIX) `SOCKET_PATH) 50) is still a -1 though
<geri> i remember there was some option to write inline C or something, no?
<abu[7]> Öops, forget my above "'(NIL (12) -3 "abc")", this is wrong
<abu[7]> But "(buf P 256 (buf S 4 (struct S .." works
<abu[7]> I begin to remember ;)
<geri> it works for printing, but i need a pointer to a string
<abu[7]> printing?
<geri> and you said struct in native call is wrong so bg
<abu[7]> You can extract the pointer instead of the symbol mith 'P instead of 'S
<geri> yeah, i just dont know how to print contents of such a pointer
<abu[7]> struct in native call is is ok, the above struct call is wrong
<geri> im confused
<geri> xd
<geri> (also brb breakfast)
<abu[7]> you can extract the elements of the struct with a struct call ;)
<abu[7]> 'struct' can only handle a single structure. {int; char[8]}
<abu[7]> {int; char*} is *two* stuctures (= memory areas)
<abu[7]> the first is (-3 ("abc" . 8))
<abu[7]> the second is created with (buf P 256 (buf S 8 ..
<abu[7]> Flat structures can be passed to and returned from 'native' directly, without "malloc" or 'buff'
<geri> so what do i do to pass {unsighed int; char*} to %@?
<geri> s/sigh/sign/
<abu[7]> I'll make an example a little later
<abu[7]> Let me get up first ;)
<geri> :D
<abu[7]> I did not test, but how about http://pb1n.de/?614b0c ?
<geri> still -1
<geri> sockpath should be a char*, no?
<geri> this stuff is so painful not gonna lie
<abu[7]> Yes, SockPath is a char*
<abu[7]> (buf S 4 (struct S NIL '("abc" . 4)) (struct S 'S)) -> "abc"
<abu[7]> Creates a 4 byte string
<geri> shouldnt it be '(Nil (`Siz) `(- AF_UNIX) SockPath)?
<geri> well, that doesnt work either but still
<abu[7]> We need (cons SockPath 8)) to set the 8-byte char pointer
<abu[7]> "a pair (num . cnt) where 'num' is stored in a field of 'cnt' bytes"
<geri> okay
<abu[7]> I also have to read and re-read the docs again and again
<geri> thank god you wrote them for yourself
<geri> struct should be of size 12 i think
<geri> int + char*
<geri> (still doesnt work though)
<abu[7]> Yes, 12
<abu[7]> Siz is nonsense
<abu[7]> Can you strace the connect call?
<geri> if i knew how :D
<abu[7]> Start in repl, *Pid
<abu[7]> start "strace -p pid" in a terminal
<abu[7]> call connect in the repl
<abu[7]> For the records, now we have http://pb1n.de/?0c1b8c
<abu[7]> Or http://pb1n.de/?66f236 with correct indentation
<abu[7]> Oh!!!
<abu[7]> wrong parens then
<geri> oh i fixed them on my side already
<geri> :D
<abu[7]> Good that I hit comma :)
<abu[7]> OK :)
<geri> still -1 :(
<geri> strace of your last example
<geri> actually weird, i saw some sun_ stuff on previous examples
<geri> give me a sec
<abu[7]> There is no connect in
<geri> yeah sec
<geri> connection refused
<geri> also addr seems scuffed
<geri> SockPath
<abu[7]> sun_path=@"\0`3\260z\376\177\0\0"}
<geri> yeah this
<geri> i cant really strace my C binary hmm
<abu[7]> But it says "sun_path = SOCKET_PATH"
<abu[7]> it is char* right?
<abu[7]> 12 bytes struct as I understand it
<geri> C code on top
rob_w has joined #picolisp
<geri> its a string literal i guess
<abu[7]> so char[] ?
<abu[7]> Not clear from the code
<abu[7]> Let's try char[] then
<geri> oh
<geri> #define SOCKET_PATH "/tmp/music"
<abu[7]> 👍
<abu[7]> let me write
<abu[7]> Siz is not needed
<abu[7]> Wait
<geri> that's still uzing siz btw
<abu[7]> Needed twice nom
<abu[7]> now
<abu[7]> I forgot to set (Siz) in the struct
<geri> okay let me strace it rq
<abu[7]> strace
<geri> why does the string start with \0?
<geri> (also brb)
<abu[7]> Yes, \0 is wrong
<abu[7]> I wonder how sockaddr_un is exactly defined
<abu[7]> sun_family is really a 4-byte int?
<abu[7]> (NIL (Siz) -AF_UNIX ("/tmp/music" . 10)) looks right to me
<abu[7]> (NIL (Siz) -AF_UNIX ("/tmp/music" . 11))
<abu[7]> BTW, we should use 'size' instead of 'length' if the path might contain multi-byte chars
<abu[7]> Not relevant here though
<abu[7]> Ah!
<abu[7]> That's it! Only 2 bytes
<abu[7]> a short
<abu[7]> This explains the \0 byte
<abu[7]> Also sa_data[14];
<geri> i just got a 0 returned from the call
<abu[7]> 👍
<geri> i did the short stuff and had to remove minus from AF_UNIX
<abu[7]> Ah, yes
<abu[7]> and we need no 'let'
<geri> eyyyyyyyyyyyyyyyyyy
<geri> cycle pause works
<geri> conssitently now
<geri> finally
<abu[7]> So we just have http://pb1n.de/?5c911a ?
<geri> what's the standard way to declare global variables?
<geri> setq on top level?
<abu[7]> Yes, or 'de'
<geri> (de THING . value)?
<abu[7]> T
<abu[7]> so we can (vi 'THING)
<geri> here's working code
<geri> ill clean it up later
<geri> finally ill have music script not be written in emacs lisp or cl xd
<geri> cl is good but honestly this is most likely gonna be faster cause it doesnt optimize startup speed
<abu[7]> Len and Siz are no longer needed
<abu[7]> So we just have http://pb1n.de/?5c911a ?
<geri> Siz is 13
<geri> so i think we do need it
<abu[7]> The struct size is fixed
<abu[7]> 16
<abu[7]> path is 13 + 1
<geri> and what if path becomes longer?
<abu[7]> Booom :)
<geri> exactly
<geri> it finally works though 🥳
<abu[7]> So the def in sys/socket.h is just an example
<abu[7]> Siz may vary probably
<geri> yeah
<abu[7]> Bad C style
<geri> okay i gotta work for a little bit, gonna be back later
<abu[7]> Me too
<geri> enjoy
<abu[7]> ☺
<geri> i love how you can just do (de quote2 X X)
<geri> and (de traditional_quote X (car X))
<geri> how do i discard all output from a shell command?
<geri> (in '(some command) NIL)?
<geri> in particular im trying to make sure nohup wont be outputting to a file
<geri> actually the thing we did with exec + fork may be more useful here than nohup
<geri> yup, okay
<abu[7]> Perhaps also (err "/dev/null" (in '(some command)))
<geri> like 3 or 4 function names i usually use for functions in this script are occupied by pil builtins
<geri> im sad
<abu[7]> The use a namespace and (local)
rob_w has quit [Remote host closed the connection]
<beneroth> hey abu[7] geri o/
<beneroth> congrats :)
<abu[7]> Ahoi beneroth!
<geri> hi-hi
<geri> yeah, it finally works 🥳
<geri> i got query command working too, although its very bad
<geri> so now just need to polish stuff up and ill have my script rewritten
<geri> script is about using mpv as a music server using json ipc btw
<geri> and problem ill need to figure out is that mpv actually sends events asyncronously, so ill have to figure out how to handle that
<geri> (p.s. brb in ~20)
f8l has joined #picolisp
<geri> silly question - how do i packJson a 0 to be 0 and not an empty object?
<geri> actually nvm, i was using wrong pattern XD
fuxoft has joined #picolisp
<fuxoft> Hello there.
<fuxoft> Is there a function similar to (chop) that splits the string to single bytes instead to UTF characters?
<abu[7]> Hi fuxoft! Not out of the box. I would use 'buf', 'struct' and 'byte'
f8l has quit [Ping timeout: 252 seconds]
<fuxoft> Will look into those, thanks.
<abu[7]> Let me try an example. It is similar to the above discussion.
<fuxoft> And what about (char)? Can I read a single byte (not character) from the file?
<abu[7]> Yes, thats easy
<abu[7]> (rd 1)
<fuxoft> Oooh, rd with a parameter, I missed that
<abu[7]> (let S "a☺c" (buf B (inc (size S)) (struct B NIl (cons S (size S))) (for (P B (n0 (byte P)) (inc P)) (printsp (byte P)))))
<abu[7]> Could be optimized a little
<fuxoft> Without parameter, (pr) and (rd) use some specific packed format, is it described somewhere?
<fuxoft> Oh that looks advanced, I will have to study that...
f8l has joined #picolisp
<abu[7]> I think the format (PLIO) is not really documented
<fuxoft> BTW is there a way to analyze (in pure PicoLisp) the low-level Cells in memory, e.g. seeing the actual pointer addresses and type bits...? It might help me understand the basic structures (symbols, lists) better.
<beneroth> there is (adr)
<beneroth> but probably not exactly what you are looking for
<beneroth> (view) is also handy for lists and tree structures
<abu[7]> I think (adr) can analyze anything
<fuxoft> Yeah, (adr) looks like something I had in mind. Like "peek" on ZX Spectrum :)
<abu[7]> eg, (struct (- (adr Sym) 8) T) gets the internal tail of a symbol
<fuxoft> That emoji probably shouldn't be there...?
<abu[7]> It is an example for a multi-byte char
<fuxoft> And what's "Sym"  here?
<fuxoft> No, wait, there is definitely something wrong with that line, the parentheses are not matched
<beneroth> fuxoft, it's an 8 followed by ), maybe your client shows a smiley instead
<fuxoft> Yes, I am on web.libera.chat
<fuxoft> When I tried that I got segfault...
<abu[7]> Probably non-UTF
<abu[7]> by copy/paste
<abu[7]> Try some other chars
<fuxoft> Instead of "Sym"? Sorry I am lost about what I should do.
<fuxoft> I don't understand what's "Internal tail of a symbol" and what "Sym" represents.
<beneroth> have you checked out @doc/Structures?
<abu[7]> Ah, I talked about the chars
<fuxoft> No, not at all.
<beneroth> abu[7], 8) is displayed as a smiley picture for fuxoft, so your code is scrambled for him
<abu[7]> Yes, understood it that way
<fuxoft> I understand that it should be number 8 and closed parentheses but I have no idea what it should do and what should I put into "Sym" (if anything)
<fuxoft> If I just enter that line in to REPL, it segfaults.
<beneroth> Sym is the symbol you want to look at, extracting the tail from it in binary
<abu[7]> Any symbol, perhaps with properties
<beneroth> you first need to create it, else struct is reading nothing which causes the segfault
<abu[7]> it was NIL probably which should be ok
<abu[7]> You use pil21 ?
<abu[7]> In pil64 it was different
<abu[7]> T was not supported by 'struct' and 'adr' was different
<fuxoft> Oh, the segfault was because i've put an integer in Sym before...
<abu[7]> Ah, perfect :)
<fuxoft> That's more food for my thought, I'll go back to studying, thanks :)
<abu[7]> Good :)
<abu[7]> (I avoid ☺)
fuxoft has quit [Quit: Client closed]
<geri> avoid studyng? abu[7]
<abu[7]> Avoid smileys
<geri> :(
<abu[7]> fuxofts client
<geri> i wrote a very complex version of my script that uses sockets xd
<geri> question
<geri> how do i read bytes until newline?
<geri> it seems that ascii chars and japanese characters occupy different amount of bytes
<geri> so then everything is misaligned and i cry
<abu[7]> Yes
<abu[7]> size vs. length
<geri> i read with (%@ "read" SOCKFD P 1)
<abu[7]> but (rd 1) and (= "\n" B) works
<geri> oh wait
<geri> i should be able to read from file descriptor with picolisp's functions, no?
<abu[7]> yes
<abu[7]> "read" 1 is not so good
<geri> need (line)
<abu[7]> ok
<abu[7]> (in SOCKFD ..
<geri> hm
<geri> (in SOCKFD (line T)) returns NIL
<geri> reading with %@ returns chars as is needed
<abu[7]> NIL means EOF
<geri> its not eof though
<geri> as in, file isnt closed
<abu[7]> hmm
<abu[7]> T
<abu[7]> For TCP it works
<geri> eh
<abu[7]> Perhaps you need to set to unblocking
<geri> how do i do that again?
<geri> you can apparently set options like that onto fd's
<geri> hm
<abu[7]> I think @lib/net.l does it
<geri> (%@ "nonBlocking" 'I Sd)
<geri> okay..
<abu[7]> Yeah
<geri> returned a 2
<geri> reading from fd with line still returns nil
<geri> eh
<abu[7]> I thought you need the opposite
<abu[7]> blocking
<geri> isnt it the default?
<abu[7]> Right ...
<abu[7]> probably
<geri> i mean, i can probably get myself a buffer and put those 1 bytes into it until i find a newline and then use struct to extract chars properly or something
<abu[7]> Read more
<abu[7]> 1 is not good
<beneroth> can you not just need normal (till) and (from) and (line) with the fd you opened?
<abu[7]> Then use byte
<abu[7]> as above
<beneroth> you should not mix (rd 'num) and text reading functions
<abu[7]> T
<geri> uh
<geri> im using %@ "read""
<beneroth> why
<geri> for the same reason why i use %@ write
<geri> xd
<geri> (ie probably cause im stupid)
<geri> let me try to send a command to fd using pico functions
<beneroth> there might be a good reason for it. but why not use (rd 'num) and (wr 'num) if you need binary io, or (prinl)/(prin)/(char)/(till)/(line) if you do text io?
<beneroth> yeah
<geri> cause i know 0 of these?
<beneroth> ah
<beneroth> thats a good reason
<geri> okay i have a socket file descriptor
<geri> how do i send a string to it
<beneroth> I thought you know them and considered some reason not to use them :D
<beneroth> (prinl)
<geri> (out SOCKFD (prinl "cycle pause"))
<beneroth> or just (prin)
<geri> doesnt work
<geri> says bad file descryptor
<geri> descriptor
<beneroth> hmmm
<geri> i can %@ write to it fine
<abu[7]> strange
<geri> cffi do be strage :D
<geri> so this works: (let S "cycle pause\n" (%@ "write" 'I SOCKFD S (length S)))
<geri> this doesnt: (out SOCKFD (prinl "cycle pause\n"))
<abu[7]> Something special with unix socks
<abu[7]> Ah
<beneroth> or something that is checked when using (out) ?
<abu[7]> Not ah
<abu[7]> I thought you need (flush)
<beneroth> good point
<abu[7]> but 'out' takes care of it
<abu[7]> (flushes in the end)
<geri> 3 -- Bad FD
<geri> let me try to grep through the code base...
<abu[7]> Perhaps strace helps here too
<beneroth> badFd is defined in io.l
<geri> yeah i foudn that
<geri> called in wrOpen
<geri> rough
<geri> guess using builtins is not an option
<abu[7]> Sorry, busy ;)
<geri> how do i set a value at address to be \0
<abu[7]> (byte P 0)
<geri> great
<abu[7]> The strace has no read() or write() except stdio (?)
<geri> well, it failed to do (out 3 ...)
<geri> so thats kind of expected
<abu[7]> the badFD ?
<abu[7]> I wonder why it is "bad"
<geri> same here
<abu[7]> Ah, of course!
<abu[7]> in wrOpen (>= N (val $OutFDs))
<geri> because its opened via C side?
<abu[7]> It is not in the file structures
<abu[7]> No
<geri> hm
<abu[7]> lib/net also does so
<abu[7]> It needs some call
<abu[7]> "initInFile" and "initOutFile"
<abu[7]> Like 'connect' and 'accept'
<geri> yup that worked
<abu[7]> 👍
<geri> i had 10 line read entry function with a ton of logic and stuff
<geri> vs 4 tiny lines
<geri> XD
<abu[7]> Great :)
<beneroth> awesome geri :)
<geri> (de q @
<geri> (while (args)
<geri> (let (Path (next)
<geri> Full-path (%@ "realpath" 'S Path))
<geri> (prinl Full-path))))
<geri> this gives me a core dump for some reason xd
<geri> > Segmentation fault (core dumped)
<beneroth> (%@) will be the reason
<geri> first realpath call succeeds
<geri> next one segfaults
<geri> hmm
<beneroth> picolisp strings are not NULL-terminated
<beneroth> I suspect you have first to copy Path into a NULL-terminated Buffer
<abu[7]> Vip has an example for "realpath"
<beneroth> you can use (buf) and (byte) for that
<abu[7]> T
<abu[7]> vip creates a struct
<abu[7]> "realpath
<abu[7]> oops
<abu[7]> (%@ "realpath" 'N (glue "/" @) '(R (`PATH_MAX C . `PATH_MAX)))
<abu[7]> (%@ "realpath" 'N File '(R (`PATH_MAX C . `PATH_MAX)))
<beneroth> so File is a picolisp string, handed in as a long number?
<beneroth> wat
<abu[7]> Yes, a string
<beneroth> so it must not be too long (not occupy multiple strings), or doesn't that matter?
<beneroth> s/multiple strings/multiple cells
<abu[7]> Limited to PATH_MAX
<abu[7]> (sysdefs "unistd")
<geri> i need the actual file path right
<geri> (sysdefs "unistd")
<geri> (de readlink (File)
<geri> (%@ "realpath" 'S File
<geri> '(NIL (`PATH_MAX C . `PATH_MAX))))
<geri> from my old bootstrap script, actually works
<beneroth> nice
<abu[7]> But NIL means to ignore the result ..
<geri> i honestly have no idea, it works XD
<beneroth> that's a horrible thing to say xD
<abu[7]> ok, the buffer is also returned
<abu[7]> But I think this may be dangerous
<beneroth> I guess in case of error the returned buffer will also be NIL
<beneroth> or random values
<abu[7]> The buffer is freed when native returns
<abu[7]> Not sure
<abu[7]> In 'R' you have a proper Lisp result, not just a pointer
<abu[7]> But perhaps it is ok
<abu[7]> I needed a list of chars in R
<abu[7]> So it is built directly and needs no chop
<geri> okay it doesnt segfault with R anymore
<geri> idk why it did
<abu[7]> perfect
<geri> gotta sleep
<geri> thanks for the help xd
<abu[7]> o/
bjorkintosh has quit [Quit: "Every day, computers are making people easier to use." David Temkin]
bjorkintosh has joined #picolisp
bjorkintosh has joined #picolisp
rob_w has joined #picolisp
rob_w has quit [Quit: Leaving]