summaryrefslogtreecommitdiff
path: root/src/tibdecode.c
blob: d4f009215212ab2476766c7a9610365c45de452f (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
114
115
116
117
118
119
120
121
122
/*
 *  tibdecode - Decompile a TI BASIC program
 *  Copyright (C) 2015-2016 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 <getopt.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, --debug\tShows the decimal value of unknown characters in {}\n\
\t-h, --help\tPrints this help message and exits\n\
\t-v, --version\tPrints version info and exits\n"

#define VERSION_INFO "tibdecode (Delwink LiberTI) 1.0.0\n\
Copyright (C) 2015-2016 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;

  struct option longopts[] =
    {
      {"debug",   no_argument, 0, 'd'},
      {"help",    no_argument, 0, 'h'},
      {"version", no_argument, 0, 'v'},
      {0, 0, 0, 0}
    };

  if (argc > 1)
    {
      int c;
      int longindex;
      while ((c = getopt_long (argc, argv, "dhv", longopts, &longindex)) != -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;
}