annotate keiko/lib.c @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /* lib.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 /* Copyright (c) 2017 J. M. Spivey */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 #include "primtab.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 int lib_argc(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 return saved_argc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 void lib_argv(int n, char *s) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 /* Buffer overflow waiting to happen */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 strcpy(s, saved_argv[n]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 void lib_print_num(int n) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 printf("%d", n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 void lib_print_string(char *s) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 fputs(s, stdout);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 void lib_print_char(char c) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 putchar(c);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 static FILE *infile = NULL;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 int lib_open_in(char *name) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 FILE *f = fopen(name, "r");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 if (f == NULL) return 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 if (infile != NULL) fclose(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 infile = f;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 return 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 void lib_close_in(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 if (infile == NULL) return;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 fclose(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 infile = NULL;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 void lib_read_char(char *p) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 FILE *f = (infile == NULL ? stdin : infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 int ch = fgetc(f);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 *p = (ch == EOF ? 127 : ch);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 #define PRIMS(direct, indirect, wrapper) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 wrapper(scratch_alloc, P, I) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 wrapper(lib_argc, I) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 wrapper(lib_argv, V, I, P) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 wrapper(lib_print_num, V, I) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 wrapper(lib_print_string, V, P) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 wrapper(lib_print_char, V, C) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 wrapper(lib_open_in, I, P) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 wrapper(lib_close_in, V) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 wrapper(lib_read_char, V, P) \
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 wrapper(exit, V, I)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 /* Using PPRIMTAB adds an offset to compensate for static links */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 PPRIMTAB(PRIMS)