summaryrefslogtreecommitdiff
path: root/src/bin/main.c
blob: a969956422ea1d492786f8a5faba6af624e1765a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* 
 * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
 *
 * This file is part of aplwc.
 *
 * Aplwc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Aplwc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aplwc.  If not, see <https://www.gnu.org/licenses/>.
 */

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "aplwc_repl.h"

static unsigned long failsim_start;
static unsigned long failsim_count;

static void *
my_alloc(size_t size, size_t *actual)
{
	size_t target = size;
	void *p;

	if(actual){
		if(NEVER(target == 0))
			target = 1;
		else{
			target--;
			target |= target >> 1;
			target |= target >> 2;
			target |= target >> 4;
			target |= target >> 8;
			target |= target >> 16;
			target |= target >> 32;
			target++;
		}
	}


	p = FAILCASE(malloc(target), NULL);
	if((p == NULL) && (size < target)){
		target = size;
		p = FAILCASE(malloc(target), NULL);
	}

	if(actual)
		*actual = target;
	return p;
}

static void
my_dealloc(void *p)
{
	free(p);
}

static unsigned long 
my_setup_failsim(enum aplwc_failsim_parameter p, unsigned long def)
{
	switch(p){
	case APLWC_FAILSIM_START:
		return failsim_start = getenv_ulong("APLWC_SIMULATE_FAILURE_START", def);
	case APLWC_FAILSIM_COUNT:
		return failsim_count = getenv_ulong("APLWC_SIMULATE_FAILURE_COUNT", def);
	default:
		return def;
	}
}

int
main(int argc, char *argv[])
{
	struct aplwc_fns fns;
	struct aplwc *aplwc;
	enum aplwc_rc ret;
	AplwcLine line;
	AplwcValue value;
	char *input, *src;
	int line_number;
	size_t input_size, src_size, i;
	int opt, xflag, input_file;
	long tmp;
	FILE *f;
	unsigned long failsim_count, failsim_fails;

	aplwc_clear_line(&line);
	aplwc_clear_value(&value);
	input = NULL;
	xflag = 0;
	input_file = 0;
	line_number = 0;
	aplwc = NULL;

	memset(&fns, 0, sizeof(fns));
	fns.alloc = my_alloc;
	fns.dealloc = my_dealloc;
	fns.setup_failsim = my_setup_failsim;

	while((opt = getopt(argc, argv, "x")) != -1){
		switch(opt){
		case 'x':
			xflag = 1;
			break;
		default:
usage:
			fprintf(stderr, "Usage: %s [-x] [file]\n", argv[0]);
			ret = APLWC_OTHER_FAILURE;
			goto end;
		}
	}

	if((optind+1) == argc){
		input_file = 1;
		f = fopen(argv[optind], "r");
		if(f == NULL){
			fprintf(stderr, "Could not open file: %s: %s\n", argv[optind], strerror(errno));
			ret = APLWC_OTHER_FAILURE;
			goto end;
		}
	}else if(optind == argc)
		f = stdin;
	else
		goto usage;

	aplwc = aplwc_new(fns, &ret);
	if(ret){
		fprintf(stderr, "Couldn't create new aplwc instance: %s\n", aplwc_errstr_rc(ret));
		goto end;
	}

	if(input_file){
		if(FAILCASE(fseek(f, 0, SEEK_END), -1)){
file_size:
			fprintf(stderr, "Failed to determine file size\n");
			ret = APLWC_OTHER_FAILURE;
			goto end;
		}
		tmp = FAILCASE(ftell(f), -1);
		if(tmp < 0)
			goto file_size;
		input_size = tmp;
		rewind(f);
		input = FAILCASE(malloc(input_size), NULL);
		if(input == NULL){
			fprintf(stderr, "Failed to allocate space for file contents\n");
			ret = APLWC_OTHER_FAILURE;
			goto end;
		}
		if(FAILCASE(fread(input, 1, input_size, f), 0) < input_size){
			fprintf(stderr, "Failed to read file contents\n");
			ret = APLWC_OTHER_FAILURE;
			goto end;
		}
		fclose(f);

		src = input;
		src_size = input_size;
		if((input_size > 2) && (input[0] == '#') && (input[1] == '!')){
			for(i = 0; i < input_size; i++){
				if(input[i] == '\n'){
					src += i;
					src_size -= i;
					xflag = 1;
					break;
				}
			}
		}

		ret = aplwc_load_module(aplwc, "Main", src, src_size);
		if(ret){
			fprintf(stderr, "Couldn't load Main module from file %s: %s\n", argv[optind], aplwc_errstr_rc(ret));
			goto end;
		}
		free(input);
		input = NULL;
	}

	if(xflag)
		goto end;

	for(;;){
		if(input)
			free(input);
		if(aplwc_valid_line(&line))
			aplwc_free_line(&line);
		if(aplwc_valid_value(&value))
			aplwc_free_value(&value);

		input = NULL;
		if(getline(&input, &input_size, stdin) < 0){
			free(input);
			input = NULL;
			break;
		}else{
			line_number++;
			aplwc_read(aplwc, input, &line);
			if(!aplwc_valid_line(&line)){
line_error:
				fprintf(stderr, "error on line %d: %s: %s\n", line_number, aplwc_errstr(aplwc), input);
				if(aplwc_failsim_query(APLWC_FAILSIM_COUNT) > 0){
					ret = aplwc_last_error(aplwc);
					goto end;
				}else
					continue;
			}
			aplwc_eval(&line, &value);
			if(!aplwc_valid_value(&value))
				goto line_error;
			fprintf(stdout, "%s\n", aplwc_value_string(&value));
		}
	}

end:
	if(input)
		free(input);
	if(NEVER(aplwc_valid_line(&line)))
		aplwc_free_line(&line);
	if(NEVER(aplwc_valid_value(&value)))
		aplwc_free_value(&value);
	if(aplwc)
		aplwc_destroy(aplwc);

	failsim_count = aplwc_failsim_query(APLWC_FAILSIM_COUNT);
	failsim_fails = aplwc_failsim_query(APLWC_FAILSIM_FAILS);
	if(failsim_count > 0){
		switch(ret){
		case APLWC_OK:
		case APLWC_OTHER_FAILURE:
		case APLWC_OUT_OF_MEMORY:
			break;
		default:
			return 97;
		}

		if(failsim_count > failsim_fails)
			return 98;
		else
			return 99;
	}

	return (ret != APLWC_OK);
}