Skip to content

Commit 0c615b0

Browse files
authored
adds more primus lisp stubs and fixes some existing (#1337)
1 parent 96ab02c commit 0c615b0

File tree

10 files changed

+147
-38
lines changed

10 files changed

+147
-38
lines changed

plugins/api/api/c/posix.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ long a64l(const char *)
2121
void abort(void) __attribute__((noreturn));
2222
int abs(int x) __attribute__((__const__, warn_unused_result));
2323
int atexit(void (*callback)(void)) __attribute__((nonnull(1)));
24+
int __cxa_atexit(void (*callback)(void)) __attribute__((nonnull(1)));
2425
double atof(const char *) __attribute__((pure, nonnull(1), warn_unused_result));
2526
int atoi(const char *) __attribute__((pure, nonnull(1), warn_unused_result));
2627
long atol(const char *) __attribute__((pure, nonnull(1), warn_unused_result));
@@ -117,25 +118,39 @@ int feof(FILE *stream) __attribute__((warn_unused_result));
117118
int ferror(FILE *stream) __attribute__((warn_unused_result));
118119
int fgetc(FILE *stream);
119120
int fgetc(FILE *stream);
121+
int fgetc_unlocked(FILE *stream);
122+
int fgetc_unlocked(FILE *stream);
120123
int fileno(FILE *stream) __attribute__((warn_unused_result));
121124
int fputc(int c, FILE *stream);
122125
int fputs(const char *s, FILE *stream);
126+
int fputc_unlocked(int c, FILE *stream);
127+
int fputs_unlocked(const char *s, FILE *stream);
123128
int getc(FILE *stream);
129+
int getc_unlocked(FILE *stream);
124130
int getchar(void);
125131
int putchar(int c);
132+
int getchar_unlocked(void);
133+
int putchar_unlocked(int c);
126134
int putc(int c, FILE *stream);
127135
int puts(const char *s);
136+
int putc_unlocked(int c, FILE *stream);
137+
int puts_unlocked(const char *s);
128138
int remove(const char *);
129139
int rename(const char *, const char *);
130140
int ungetc(int c, FILE *stream);
131141

132142
size_t fread(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
133143
__attribute__((warn_unused_result, storage(1,2,3)));
144+
size_t fread_unlocked(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
145+
__attribute__((warn_unused_result, storage(1,2,3)));
134146
size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
135147
__attribute__((warn_unused_result, storage(1,2,3)));
148+
size_t fwrite_unlocked(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
149+
__attribute__((warn_unused_result, storage(1,2,3)));
136150
void clearerr(FILE *stream);
137151

138152
int fflush(FILE *stream);
153+
int fflush_unlocked(FILE *stream);
139154

140155
int __isoc99_fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __attribute__((warn_unused_result));
141156
int __isoc99_scanf (const char *__restrict __format, ...) __attribute__((warn_unused_result));
@@ -354,13 +369,22 @@ int gethostname(char *buf, size_t len) __attribute__((nonnull(1), stora
354369
char *getlogin(void) __attribute__((warn_unused_result));
355370
int getlogin_r(char *buf, size_t size) __attribute__((warn_unused_result, nonnull(1), storage(1,2)));
356371
int getopt(int argc, char * const *argv, const char *shortopts);
372+
int getopt_long(int argc, char * const argv[],
373+
const char *optstring,
374+
const struct option *longopts, int *longindex);
375+
376+
int getopt_long_only(int argc, char * const argv[],
377+
const char *optstring,
378+
const struct option *longopts, int *longindex);
357379
pid_t getpgid(pid_t pid) __attribute__((warn_unused_result));
358380
pid_t getpgrp(void) __attribute__((warn_unused_result));
359381
pid_t getpid(void) __attribute__((warn_unused_result));
360382
pid_t getppid(void) __attribute__((warn_unused_result));
361383
pid_t getsid(pid_t pid) __attribute__((warn_unused_result));
362384
uid_t getuid(void) __attribute__((warn_unused_result));
363385
int isatty(int fd);
386+
int ioctl(int fd, unsigned long request, ...);
387+
int getpagesize(void);
364388
int lchown(const char *file, uid_t owner, gid_t group) __attribute__((warn_unused_result,nonnull(1)));
365389
int link(const char *from, const char *to) __attribute__((warn_unused_result,nonnull(1,2)));
366390
int linkat(int fromfd, const char *from, int tofd, const char *to, int flags) __attribute__((warn_unused_result,nonnull(2,4)));
@@ -448,3 +472,25 @@ int isblank(int c);
448472

449473
int toupper(int c);
450474
int tolower(int c);
475+
476+
477+
// locale.h
478+
479+
char *setlocale(int category, const char *locale);
480+
struct lconv *localeconv(void);
481+
482+
// #include <libintl.h>
483+
char * bindtextdomain (const char * domainname, const char * dirname);
484+
char * textdomain (const char * domainname);
485+
486+
// #include <setjmp.h>
487+
typedef void * jmp_buf;
488+
typedef void * sigjmp_buf;
489+
int setjmp(jmp_buf env);
490+
int sigsetjmp(sigjmp_buf env, int savesigs);
491+
void longjmp(jmp_buf env, int val);
492+
void siglongjmp(sigjmp_buf env, int val);
493+
int _setjmp(jmp_buf env);
494+
int _sigsetjmp(sigjmp_buf env, int savesigs);
495+
void _longjmp(jmp_buf env, int val);
496+
void _siglongjmp(sigjmp_buf env, int val);

plugins/primus_lisp/primus_lisp_io.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type state = {
1818
let default_channels = Int.Map.of_alist_exn [
1919
0, {
2020
input = Some In_channel.stdin;
21-
output = None;
21+
output = Some Out_channel.stdout;
2222
};
2323
1, {
2424
input = None;
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
(require type)
2-
(require array)
3-
(require strstr)
1+
(require types)
2+
(require pointers)
43

54
(in-package posix)
65
(declare (visibility :private))
76

8-
9-
(declare (global (optind opterr optopt optarg)))
7+
(declare (global optind opterr optopt optarg last-ofs))
108

119
(defun getopt-arg (argv)
1210
(array-get ptr_t argv optind))
1311

1412
(defun getopt-arg-char (argv n)
15-
(array-get byte (getopt-arg argv) n))
13+
(array-get char (getopt-arg argv) n))
1614

1715
(defun points-to-dash (p)
18-
(points-to char_t p ?-))
16+
(points-to char p ?-))
1917
(defun points-to-colon (p)
20-
(points-to char_t p ?:))
18+
(points-to char p ?:))
2119

2220
(defun getopt-finished (argc argv)
2321
(or (> optind argc)
@@ -27,8 +25,8 @@
2725

2826
(defun getopt-nearly-finished (argv)
2927
(let ((p (getopt-arg argv)))
30-
(and (points-to-dash (ptr+ p 1))
31-
(points-to-null (ptr+ p 2)))))
28+
(and (points-to-dash (ptr+ char p 1))
29+
(points-to-null (ptr+ char p 2)))))
3230

3331
(defun getopt-update-optopt (argv last-ofs)
3432
(set optopt (getopt-arg-char argv (+1 last-ofs))))
@@ -37,15 +35,15 @@
3735
(set optarg (array-get ptr_t argv (+1 optind))))
3836

3937
(defun getopt-found (argv p last-ofs)
40-
(or (points-to-colon (ptr+ 2 p))
38+
(or (points-to-colon (ptr+ char 2 p))
4139
(getopt-arg-char argv (+ 2 last-ofs))))
4240

4341
(defun getopt-reset-optarg-if-needed (argv last-ofs)
4442
(when (points-to-null (getopt-arg-char argv (+ 2 last-ofs)))
4543
(set optarg 0)))
4644

4745
(defun getopt-expects-argument (p)
48-
(points-to-colon (ptr+ 1 p)))
46+
(points-to-colon (ptr+ char 1 p)))
4947

5048
(defun getopt-missing-argument ())
5149

@@ -56,22 +54,21 @@
5654
?:)
5755

5856
(defun getopt-with-argument (argv opts p last-ofs)
59-
(if (getopt-found argv p last-ofs)
57+
(when (getopt-found argv p last-ofs)
6058
(getopt-reset-optarg-if-needed argv last-ofs))
61-
(prog
62-
(getopt-update-optarg argv)
63-
(when (is-zero optarg)
64-
(getopt-no-argument argv opts))
65-
(incr optind)))
59+
(getopt-update-optarg argv)
60+
(when (is-zero optarg)
61+
(getopt-no-argument argv opts))
62+
(incr optind))
6663

6764
(defun getopt (argc argv opts)
6865
(declare
6966
(visibility :public)
7067
(external "getopt"))
7168
(when (= 0 optind)
7269
(set optind 1)
73-
(set lastidx 0))
74-
(if (getopt-finished) -1
70+
(set last-ofs 0))
71+
(if (getopt-finished argc argv) -1
7572
(if (getopt-nearly-finished argv)
7673
(prog (incr optind) -1)
7774
(let ((p 0) )
@@ -84,7 +81,12 @@
8481
(if (getopt-expects-argument p)
8582
(getopt-with-argument argv opts p last-ofs)
8683
(prog (incr last-ofs) optopt)))
87-
(prog
88-
(getopt-unknown-option)
89-
(incr opting)
90-
??))))))
84+
(incr optind)
85+
??)))))
86+
87+
88+
(defun getopt_long (argc argv opts _ _)
89+
(declare
90+
(visibility :public)
91+
(external "getopt_long" "getopt_long_only"))
92+
(getopt argc argv opts))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(in-package posix)
2+
3+
(defun bindtextdomain (_ dir)
4+
(declare (external "bindtextdomain"))
5+
dir)
6+
7+
(defun textdomain (dom)
8+
(declare (external "textdomain"))
9+
dom)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(in-package posix)
2+
3+
(require types)
4+
5+
(defparameter *lconv-size* (* 20 (sizeof ptr_t)))
6+
7+
(declare (static LCONV))
8+
9+
(defmethod init ()
10+
(set LCONV brk)
11+
(+= brk *lconv-size*))
12+
13+
(defun setlocale (_ locale)
14+
(declare (external "setlocale"))
15+
locale)
16+
17+
(defun lconv ()
18+
(declare (external "lconv"))
19+
LCONV)

plugins/primus_lisp/site-lisp/posix.lisp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
(require string)
44
(require errno)
55
(require ascii)
6+
(require locale)
7+
(require libintl)
8+
(require unistd)
9+
(require setjmp)
10+
(require getopt)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(in-package posix)
2+
3+
(defun setjmp (_)
4+
(declare (external "setjmp" "_setjmp"))
5+
0)
6+
7+
(defun sigsetjmp (_ _)
8+
(declare (external "sigsetjmp" "_sigsetjmp"))
9+
0)
10+
11+
(defun longjmp (_ _)
12+
(declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
13+
(exit 1))

plugins/primus_lisp/site-lisp/stdio.lisp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
(in-package posix)
66

77
(defun fputc (char stream)
8-
(declare (external "fputc" "putc"))
8+
(declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
99
(if (= 0 (channel-output stream char)) char -1))
1010

1111
(defun putchar (char)
12-
(declare (external "putchar"))
12+
(declare (external "putchar" "putchar_unlocked"))
1313
(fputc char *standard-output*))
1414

1515
(defun fputs (p stream)
16-
(declare (external "fputs"))
16+
(declare (external "fputs" "fputs_unlocked"))
1717
(while (not (points-to-null p))
1818
(fputc (cast int (memory-read p)) stream)
1919
(incr p))
@@ -22,11 +22,11 @@
2222
r))
2323

2424
(defun puts (p)
25-
(declare (external "puts"))
25+
(declare (external "puts" "puts_unlocked"))
2626
(fputs p *standard-output*))
2727

2828
(defun fflush (s)
29-
(declare (external "fflush"))
29+
(declare (external "fflush" "fflush_unlocked"))
3030
(channel-flush s))
3131

3232

@@ -36,13 +36,13 @@
3636
;; ignoring modes, we will add them later, of course.
3737
(defun fopen (path mode)
3838
(declare (external "fopen" "open" "fdopen"))
39-
(channel-open path))
39+
(let ((file (channel-open path)))
40+
(if (< file 0) 0 file)))
4041

4142
(defun fileno (stream)
4243
(declare (external "fileno"))
4344
stream)
4445

45-
4646
(defun open3 (path flags mode)
4747
(declare (external "open"))
4848
(fopen path mode))
@@ -61,7 +61,7 @@
6161
i))
6262

6363
(defun fwrite (buf size n stream)
64-
(declare (external "fwrite"))
64+
(declare (external "fwrite" "fwrite_unlocked"))
6565
(let ((i 0))
6666
(while (and (< i n)
6767
(= size (output-item buf size i stream)))
@@ -90,7 +90,7 @@
9090
i))
9191

9292
(defun fread (ptr size n stream)
93-
(declare (external "fread"))
93+
(declare (external "fread" "fread_unlocked"))
9494
(let ((i 0))
9595
(while (and
9696
(< i n)
@@ -104,7 +104,7 @@
104104

105105

106106
(defun fgetc (stream)
107-
(declare (external "fgetc" "getc"))
107+
(declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
108108
(channel-input stream))
109109

110110
(defun terminate-string-and-return-null (ptr)
@@ -113,7 +113,7 @@
113113
0)
114114

115115
(defun fgets (ptr len str)
116-
(declare (external "fgets"))
116+
(declare (external "fgets" "fgets_unlocked"))
117117
(if (= len 0) (terminate-string-and-return-null ptr)
118118
(let ((i 0)
119119
(n (-1 len))
@@ -132,7 +132,7 @@
132132

133133

134134
(defun getchar ()
135-
(declare (external "getchar"))
135+
(declare (external "getchar" "getchar_unlocked"))
136136
(fgetc *standard-input*))
137137

138138
(defmethod primus:machine-kill ()

plugins/primus_lisp/site-lisp/stdlib.lisp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
(defun atexit (cb)
35-
(declare (external "atexit"))
35+
(declare (external "atexit" "__cxa_atexit"))
3636
0)
3737

3838
(defun abs (x)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(in-package posix)
2+
3+
(defun isatty (fd)
4+
(declare (external "isatty"))
5+
(< fd 3))
6+
7+
(defun ioctl (_ _) (declare (external "ioctl")) 0)
8+
(defun ioctl (_ _ _) (declare (external "ioctl")) 0)
9+
(defun ioctl (_ _ _ _) (declare (external "ioctl")) 0)
10+
(defun ioctl (_ _ _ _ _) (declare (external "ioctl")) 0)
11+
(defun ioctl (_ _ _ _ _ _) (declare (external "ioctl")) 0)
12+
13+
(defun getpagesize ()
14+
(declare (external "getpagesize"))
15+
(* 64 1024))

0 commit comments

Comments
 (0)