fex2bin.c 2.37 KB
Newer Older
Alejandro Mery's avatar
Alejandro Mery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright (C) 2012  Alejandro Mery <amery@geeks.cl>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Alejandro Mery's avatar
Alejandro Mery committed
17
#include "fex2bin.h"
Alejandro Mery's avatar
Alejandro Mery committed
18

19
#include <ctype.h>
20
21
22
23
#include <errno.h>
#include <stdio.h>
#include <string.h>

24
25
26
27
28
29
30
#define MAX_LINE	127

/**
 */
static inline char *alltrim(char *s, size_t *l)
{
	char *p;
31
	while (isblank(*s))
32
33
34
35
36
		s++;
	p = s;
	while (*++p)
		; /* seek \0 */

37
38
39
40
41
	if (p>s+1 && p[-2] == '\r' && p[-1] == '\n')
		p -= 2;
	else if (p>s && p[-1] == '\n')
		p -= 1;
	*p-- = '\0';
42

43
44
	while (p>s && isblank(*p))
		*p-- = '\0';
45

46
	*l = p-s+1;
47
48
49
50
51
	return s;
}

/**
 */
52
53
static int parse_fex(FILE *in, const char *UNUSED(filename),
		     struct script *UNUSED(script))
54
55
56
57
58
59
60
61
62
63
64
{
	char buffer[MAX_LINE+1];
	int ok = 1;

	for(size_t line = 1; fgets(buffer, sizeof(buffer), in); line++) {
		size_t l, col;
		char *p = alltrim(buffer, &l);
		col = p-buffer+1;

		fputs(p, stdout);
		fputc('\n', stdout);
65
66

		(void)col;
67
68
69
70
71
72
73
	};

	if (ferror(in))
		ok = 0;
	return ok;
}

74
75
76
/**
 */
int main(int argc, char *argv[])
Alejandro Mery's avatar
Alejandro Mery committed
77
{
78
79
80
	int ret = -1;
	FILE *in = stdin, *out = stdout;
	const char *fn[] = {"stdin", "stdout"};
81
	struct script *script;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

	if (argc>1) {
		if (strcmp(argv[1],"-") == 0)
			; /* we are using stdin anyway */
		else if ((fn[0] = argv[1]) &&
			 (in = fopen(fn[0], "r")) == NULL) {
			errf("%s: %s\n", fn[0], strerror(errno));
			goto usage;
		}

		if (argc>2) {
			fn[1] = argv[2];

			if ((out = fopen(fn[1], "w")) == NULL) {
				errf("%s: %s\n", fn[1], strerror(errno));
				goto usage;
			}
		}
	}
101
102
103
104
105
106

	if ((script = script_new()) == NULL) {
		errf("malloc: %s\n", strerror(errno));
		goto done;
	}

107
108
109
	if (parse_fex(in, fn[0], script)) {
		ret = 0;
	}
110
	script_delete(script);
111
112
	goto done;
usage:
Alejandro Mery's avatar
Alejandro Mery committed
113
	errf("Usage: %s [<script.fex> [<script.bin>]]\n", argv[0]);
114
115

done:
116
117
	if (in && in != stdin) fclose(in);
	if (out && out != stdout) fclose(out);
118
	return ret;
Alejandro Mery's avatar
Alejandro Mery committed
119
}