summaryrefslogtreecommitdiff
path: root/src/tibdecode.c
blob: 3ec916ed60b946365168b8d07d75724430287cc1 (plain)
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
/*
 *  tibdecode - Decompile a TI BASIC program
 *  Copyright (C) 2015-2017 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "tiberr.h"
#include "tibtranscode.h"

#define USAGE_INFO "USAGE: tibdecode [options]\n\n\
tibdecode reads a TI-82 or TI-83 program from stdin and prints to stdout.\n\n\
OPTIONS:\n\
\t-d\tShows the decimal value of unknown characters in {}\n\
\t-h\tPrints this help message and exits\n\
\t-v\tPrints version info and exits\n"

#define VERSION_INFO "tibdecode (Delwink LiberTI) 0.0.0\n\
Copyright (C) 2015-2017 Delwink, LLC\n\
License AGPLv3: GNU AGPL version 3 only <http://gnu.org/licenses/agpl.html>.\n\
This is libre software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n\n\
Written by David McMackins II."

#if _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE
#include <ctype.h>
#else
static int
isascii(int c)
{
	return c & 0x7F;
}
#endif

int
main(int argc, char *argv[])
{
	bool debug = false;

	if (argc > 1)
	{
		int c;
		while ((c = getopt(argc, argv, "dhv")) != -1)
		{
			switch (c)
			{
			case 'd':
				debug = true;
				break;

			case 'h':
				puts(USAGE_INFO);
				return 0;

			case 'v':
				puts(VERSION_INFO);
				return 0;

			case '?':
				return 1;
			}
		}
	}

	struct tib_expr translated;
	unsigned long parsed;
	tib_errno = tib_fread(&translated, stdin, &parsed);
	if (tib_errno)
	{
		fprintf(stderr,
			"tibdecode: Error %d occurred while processing. Parsed %lu characters.\n",
			tib_errno, parsed);
		return 1;
	}

	char *s = tib_expr_tostr(&translated);
	tib_expr_destroy(&translated);
	if (NULL == s)
	{
		fprintf(stderr,
			"tibdecode: Error %d occurred while processing\n",
			tib_errno);
		return 1;
	}

	size_t len = strlen(s);
	for (size_t i = 0; i < len; ++i)
	{
		if (debug && !isascii(s[i]))
			printf("`%d`", s[i]);
		else
			putchar(s[i]);
	}

	free(s);
	return 0;
}