summaryrefslogtreecommitdiff
path: root/src/state.c
blob: f280e0f7e6f7efb9ed06272909004130ae197d1c (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 *  LiberTI - TI-like calculator designed for LibreCalc
 *  Copyright (C) 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 <libconfig.h>
#include <stdlib.h>

#include "state.h"
#include "tibchar.h"
#include "tiberr.h"
#include "tibeval.h"
#include "tibvar.h"
#include "util.h"

static void
add_history (struct state *state, struct tib_expr *in, struct tib_expr *ans_s,
             TIB *ans)
{
  if (MAX_HISTORY == state->history_len)
    {
      tib_expr_destroy (&state->history[0].entry);
      tib_expr_destroy (&state->history[0].answer_string);
      tib_decref (state->history[0].answer);

      for (unsigned int i = 0; i < MAX_HISTORY - 1; ++i)
        state->history[i] = state->history[i + 1];
    }
  else
    {
      ++state->history_len;
    }

  unsigned int i = state->history_len - 1;
  state->history[i].entry = *in;
  state->history[i].answer_string = *ans_s;
  state->history[i].answer = ans;
}

int
load_state (struct state *dest, const char *path)
{
  int rc = 0;

  if (!dest)
    return TIB_ENULLPTR;

  dest->action_state = STATE_NORMAL;
  dest->entry_cursor = 0;
  dest->history_len = 0;
  dest->blink_state = true;
  dest->insert_mode = false;

  rc = tib_expr_init (&dest->entry);
  if (rc)
    return rc;

  if (!path)
    return 0;

  config_t conf;
  config_init (&conf);

  rc = config_read_file (&conf, path);
  if (CONFIG_FALSE == rc)
    {
      config_destroy (&conf);
      return TIB_EBADFILE;
    }

  config_setting_t *setting = config_lookup (&conf, "history");
  if (setting)
    {
      if (config_setting_type (setting) != CONFIG_TYPE_LIST)
        {
          rc = TIB_EBADFILE;
          goto fail;
        }

      unsigned int i = 0;
      config_setting_t *line;

      while ((line = config_setting_get_elem (setting, i++)))
        {
          if (config_setting_type (line) != CONFIG_TYPE_GROUP)
            {
              rc = TIB_EBADFILE;
              goto fail;
            }

          config_setting_t *e = config_setting_get_member (line, "input");
          if (!e || config_setting_type (e) != CONFIG_TYPE_STRING)
            {
              rc = TIB_EBADFILE;
              goto fail;
            }

          const char *s = config_setting_get_string (e);
          rc = tib_encode_str (&dest->entry, s);
          if (rc)
            goto fail;

          rc = state_calc_entry (dest);
          if (rc)
            goto fail;
        }
    }

  config_destroy (&conf);
  return 0;

 fail:
  state_destroy (dest);
  config_destroy (&conf);
  return rc;
}

int
save_state (const struct state *state, const char *path)
{
  int rc = 0;

  if (!state || !path)
    return TIB_ENULLPTR;

  config_t conf;
  config_init (&conf);

  config_setting_t * const root = config_root_setting (&conf);

#define CHECK_NULL(P) if (!(P)) { rc = TIB_EALLOC; goto end; }

  {
    config_setting_t *history = config_setting_add (root, "history",
                                                    CONFIG_TYPE_LIST);
    CHECK_NULL (history);

    for (unsigned int i = 0; i < state->history_len; ++i)
      {
        config_setting_t * const line = config_setting_add (history, NULL,
                                                            CONFIG_TYPE_GROUP);
        CHECK_NULL (line);

        config_setting_t *info = config_setting_add (line, "input",
                                                     CONFIG_TYPE_STRING);
        CHECK_NULL (info);

        char *s = tib_expr_tostr (&state->history[i].entry);
        if (!s)
          {
            rc = tib_errno;
            goto end;
          }

        rc = config_setting_set_string (info, s);
        free (s);
        if (CONFIG_FALSE == rc)
          {
            rc = TIB_EALLOC;
            goto end;
          }
      }
  }

#undef CHECK_NULL

  rc = 0;
  config_write_file (&conf, path);

 end:
  config_destroy (&conf);
  return rc;
}

void
state_destroy (struct state *state)
{
  tib_expr_destroy (&state->entry);
  state_clear_history (state);
}

void
entry_move_cursor (struct state *state, int distance)
{
  state->entry_cursor += distance;

  if (state->entry_cursor > state->entry.len)
    state->entry_cursor = state->entry.len;
  else if (state->entry_cursor < 0)
    state->entry_cursor = 0;

  state->action_state = STATE_NORMAL;
}

static int
entry_insert (struct state *state, int c)
{
  int rc = tib_expr_insert (&state->entry, state->entry_cursor, c);
  if (!rc)
    {
      ++state->entry_cursor;
      state->action_state = STATE_NORMAL;
      state->insert_mode = false;
    }

  return rc;
}

int
entry_write (struct state *state, int c)
{
  if (state->insert_mode || state->entry_cursor == state->entry.len)
    return entry_insert (state, c);

  state->entry.data[state->entry_cursor++] = c;
  state->action_state = STATE_NORMAL;
  return 0;
}

int
entry_recall (struct state *state)
{
  if (state->history_len)
    {
      int rc = tib_exprcpy (&state->entry,
                            &state->history[state->history_len - 1].entry);
      if (rc)
        return rc;

      state->entry_cursor = state->entry.len;
    }
  else
    {
      state->entry.len = 0;
    }

  return 0;
}

void
change_action_state (struct state *state, enum action_state action_state)
{
  if (state->action_state != action_state)
    state->action_state = action_state;
  else
    state->action_state = STATE_NORMAL;
}

int
state_calc_entry (struct state *state)
{
  TIB *ans = tib_eval (&state->entry);
  if (!ans)
    return tib_errno;

  int rc = state_add_history (state, &state->entry, ans);
  if (rc)
    {
      tib_decref (ans);
      return rc;
    }

  state->entry_cursor = 0;
  state->entry.len = 0;

  rc = tib_var_set (TIB_CHAR_ANS, ans);
  tib_decref (ans);
  return rc;
}

int
state_add_history (struct state *state, const struct tib_expr *in,
                   const TIB *answer)
{
  struct tib_expr in_copy = { .bufsize = 0 };
  int rc = tib_exprcpy (&in_copy, in);
  if (rc)
    return rc;

  TIB *ans_copy = tib_copy (answer);
  if (!ans_copy)
    {
      tib_expr_destroy (&in_copy);
      return tib_errno;
    }

  struct tib_expr ans_s;
  rc = tib_toexpr (&ans_s, ans_copy);
  if (rc)
    {
      rc = load_expr (&ans_s, "Error");
      if (rc)
        {
          tib_expr_destroy (&in_copy);
          tib_decref (ans_copy);
          return rc;
        }
    }

  add_history (state, &in_copy, &ans_s, ans_copy);
  return 0;
}

void
state_clear_history (struct state *state)
{
  for (unsigned int i = 0; i < state->history_len; ++i)
    {
      tib_expr_destroy (&state->history[i].entry);
      tib_expr_destroy (&state->history[i].answer_string);
      tib_decref (state->history[i].answer);
    }

  state->history_len = 0;
}