;; -*- Mode: Irken -*-

(include "lib/core.scm")
(include "lib/pair.scm")
(include "lib/string.scm")
(include "lib/os.scm")

;; How this needs to be done: I should be able to say something like
;;  (%%cexp (-> (buffer (struct utsname))) ...)
;; ... then have irken put all the rights casts in there.
;; Another thing: declaring the struct like we did in Pyrex?
;; Can we do that with rows?  Clearly this could eat a lot of time,
;;   so let's put it off until after self-hosting works.

(define (make-buffer n)
  (%%cexp
   (int -> buffer)
   "alloc_no_clear (TC_BUFFER, HOW_MANY (%0, sizeof(object)))"
   n))

;(make-buffer 20)

(cinclude "sys/utsname.h")

;;(define (uname)
;;  (%%cexp (-> int) "({ struct utsname uts; uname(&uts); fprintf (stderr, \"%%s\\n\", uts.sysname); 34;})"))

(define (uname)
  (let ((buffer (%%cexp (-> buffer) "alloc_no_clear (TC_BUFFER, HOW_MANY (sizeof (struct utsname), sizeof (object)))")))
    (printn (%%cexp (buffer -> int) "uname ((struct utsname*)%0)" buffer))
    {sysname= (copy-cstring (%%cexp (buffer -> cstring) "((struct utsname *)%0)->sysname" buffer))
     nodename= (copy-cstring (%%cexp (buffer -> cstring) "((struct utsname *)%0)->nodename" buffer))
     release= (copy-cstring (%%cexp (buffer -> cstring) "((struct utsname *)%0)->release" buffer))
     version= (copy-cstring (%%cexp (buffer -> cstring) "((struct utsname *)%0)->version" buffer))
     machine= (copy-cstring (%%cexp (buffer -> cstring) "((struct utsname *)%0)->machine" buffer))
     }))

(uname)