@@ -0,0 +1,110 @@
1+#include "idris_opts.h"
2+
3+#include <stdlib.h>
4+#include <stddef.h>
5+#include <stdio.h>
6+#include <string.h>
7+
8+
9+char usage[] =
10+ "\n" \
11+ "Usage: <prog> [+RTS <rtsopts> -RTS] <args>\n\n" \
12+ "Options:\n\n" \
13+ " -? Print this message and exits.\n" \
14+ " -s Summary GC statistics.\n" \
15+ " -H Initial heap size. Egs: -H4M, -H500K, -H1G\n" \
16+ " -K Sets the maximum stack size. Egs: -K8M\n" \
17+ "\n";
18+
19+void print_usage() {
20+ fprintf(stderr, usage);
21+}
22+
23+int read_size(char * str) {
24+ int size = 0;
25+ char mult = ' ';
26+
27+ int r = sscanf(str, "%u%c", &size, &mult);
28+
29+ if (r == 1)
30+ return size;
31+
32+ if (r == 2) {
33+ switch (mult) {
34+ case 'K': size = size << 10; break;
35+ case 'M': size = size << 20; break;
36+ case 'G': size = size << 30; break;
37+ default:
38+ fprintf(stderr,
39+ "RTS Opts: Unable to recognize size suffix `%c'.\n" \
40+ " Possible suffixes are K or M or G.\n",
41+ mult);
42+ print_usage();
43+ exit(EXIT_FAILURE);
44+ }
45+ return size;
46+ }
47+
48+ fprintf(stderr, "RTS Opts: Unable to parse size. Egs: 1K, 10M, 2G.\n");
49+ print_usage();
50+ exit(EXIT_FAILURE);
51+}
52+
53+
54+int parse_args(RTSOpts * opts, int argc, char *argv[])
55+{
56+ if (argc == 0)
57+ return 0;
58+
59+ if (strcmp(argv[0], "+RTS") != 0)
60+ return 0;
61+
62+ int i;
63+ for (i = 1; i < argc; i++) {
64+ if (strcmp(argv[i], "-RTS") == 0) {
65+ return i + 1;
66+ }
67+
68+ if (argv[i][0] != '-') {
69+ fprintf(stderr, "RTS options should start with '-'.\n");
70+ print_usage();
71+ exit(EXIT_FAILURE);
72+ }
73+
74+ switch (argv[i][1])
75+ {
76+ case '?':
77+ print_usage();
78+ exit(EXIT_SUCCESS);
79+ break;
80+
81+ case 's':
82+ opts->show_summary = 1;
83+ break;
84+
85+ case 'H':
86+ opts->init_heap_size = read_size(argv[i] + 2);
87+ break;
88+
89+ case 'K':
90+ opts->max_stack_size = read_size(argv[i] + 2);
91+ break;
92+
93+ default:
94+ printf("RTS opts: Wrong argument: %s\n", argv[i]);
95+ print_usage();
96+ exit(EXIT_FAILURE);
97+ }
98+ }
99+
100+ return argc;
101+}
102+
103+void parse_shift_args(RTSOpts * opts, int * argc, char ** argv[]) {
104+ size_t shift = parse_args(opts, (*argc) - 1, (*argv) + 1);
105+
106+ char *prg = (*argv)[0];
107+ *argc = *argc - shift;
108+ *argv = *argv + shift;
109+ (*argv)[0] = prg;
110+}