summaryrefslogtreecommitdiffstats
path: root/src/history.c
blob: 5b8eb0dea66cc7375fd2056b2fcc7dc1c8a649bd (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*******************************************************************************
 * Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com>             *
 * All rights reserved.                                                        *
 *                                                                             *
 * This file is a part of sc-im                                                *
 *                                                                             *
 * sc-im is a spreadsheet program that is based on sc. The original authors    *
 * of sc are James Gosling and Mark Weiser, and mods were later added by       *
 * Chuck Martin.                                                               *
 *                                                                             *
 * Redistribution and use in source and binary forms, with or without          *
 * modification, are permitted provided that the following conditions are met: *
 * 1. Redistributions of source code must retain the above copyright           *
 *    notice, this list of conditions and the following disclaimer.            *
 * 2. Redistributions in binary form must reproduce the above copyright        *
 *    notice, this list of conditions and the following disclaimer in the      *
 *    documentation and/or other materials provided with the distribution.     *
 * 3. All advertising materials mentioning features or use of this software    *
 *    must display the following acknowledgement:                              *
 *    This product includes software developed by Andrés Martinelli            *
 *    <andmarti@gmail.com>.                                                    *
 * 4. Neither the name of the Andrés Martinelli nor the                        *
 *   names of other contributors may be used to endorse or promote products    *
 *   derived from this software without specific prior written permission.     *
 *                                                                             *
 * THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY            *
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      *
 * DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY           *
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       *
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
 *******************************************************************************/

/**
 * \file history.c
 * \author Andrés Martinelli <andmarti@gmail.com>
 * \date 2017-07-18
 * \brief TODO Write a tbrief file description.
 */

#include <string.h>
#include <wchar.h>
#include "macros.h"

// current command before tab completion
static wchar_t curcmd [BUFFERSIZE];

/**
 * \brief TODO Document copy_to_curcmd()
 *
 * \param[in] inputline
 *
 * \return none
 */

void copy_to_curcmd(wchar_t * inputline) {
    wcscpy(curcmd, inputline);
}

/**
 * \brief TODO Document get_curcmd()
 *
 * \return curcmd
 */

wchar_t * get_curcmd() {
    return curcmd;
}

// this comp mark is used to mark when tab completion is started
static int comp = 0;

/**
 * \brief TODO Document get_comp()
 *
 * \return none
 */

int get_comp() {
    return comp;
}

/**
 * \brief TODO Document set_comp()
 *
 * \param[in] i
 *
 * \return none
 */

void set_comp(int i) {
    comp = i;
}

#if defined HISTORY_FILE
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include "history.h"
#include "sc.h"
#include "utils/string.h"

struct history * create_history(char mode) {
    struct history * h = (struct history *) malloc (sizeof (struct history));
    h->len = 0;
    h->pos = 0;
    h->mode = mode;
    h->list = NULL;
    return h;
}

/**
 * \brief TODO Document destroy_history()
 *
 * \param[in] h
 *
 * \return none
 */

void destroy_history(struct history * h) {
    struct hlist * nl;
    struct hlist * n_sig;
    nl = h->list;
    while (nl != NULL) {
        n_sig = nl->pnext;
        free(nl->line);
        free(nl);
        nl = n_sig;
    }
    free(h);
    return;
}

/**
 * \brief TODO Document load_history
 *
 * \details Read the specified history file from the user's home directory
 * and load it into the specified history struct.
 *
 * \param[in] h
 * \param[in] mode
 *
 * returns: none
 */

void load_history(struct history * h, wchar_t mode) {
    char infofile[PATHLEN];
    wchar_t linea[FBUFLEN];
    int c;
    char * home;
    FILE * f;

    if ((home = getenv("XDG_CACHE_HOME"))) {
        /* Try XDG_CACHE_HOME first */
         sprintf(infofile, "%s/%s", home,HISTORY_FILE);
    } else if ((home = getenv("HOME"))) {
        /* Fallback */
        sprintf(infofile, "%s/%s/%s", home,HISTORY_DIR,HISTORY_FILE);
    }

    if ((c = open(infofile, O_RDONLY)) > -1) {
        close(c);
        f = fopen(infofile, "r");
        if (f == NULL) return;
        while ( feof(f) == 0 ) {
            if (!fgetws(linea, sizeof(linea) / sizeof(*linea), f)) break;
            int s = wcslen(linea)-1;
            del_range_wchars(linea, s, s);

            if (linea[0] == mode && mode == L':') {
                del_range_wchars(linea, 0, 0);
                add(h, linea);
            } else if (mode != L':' && (linea[0] == L'=' || linea[0] == L'<' || linea[0] == L'>' || linea[0] == L'\\')) {
                add(h, linea);
            }
        }
        fclose(f);
    }

    return;
}

/**
 * @brief Save history to file
 *
 * \return 0 on success; -1 otherwise
 */

int save_history(struct history * h, char * mode) {
    char infofile [PATHLEN];
    char * home;
    FILE * f;
    int i;
    struct hlist * nl = h->list;
    char history_dir[PATHLEN-(sizeof HISTORY_FILE)];

    if ((home = getenv("XDG_CACHE_HOME"))) {
        snprintf(history_dir, PATHLEN-(sizeof HISTORY_FILE), "%s", home);
        mkdir(history_dir,0777);
        snprintf(infofile, PATHLEN, "%s/%s", history_dir, HISTORY_FILE);
    } else if ((home = getenv("HOME"))) {
        snprintf(history_dir, PATHLEN-(sizeof HISTORY_FILE), "%s/%s", home, HISTORY_DIR);
        mkdir(history_dir,0777);
        snprintf(infofile, PATHLEN, "%s/%s", history_dir, HISTORY_FILE);
    } else {
        /* If both HOME and XDG_CACHE_HOME aren't set, abandon all hope */
        return 0;
    }

    f = fopen(infofile, mode);
    if (f == NULL) return 0;
    // Go to the end
    for (i=1; i < h->len; i++) {
        nl = nl->pnext;
    }
    // Traverse list back to front, so the history is saved in chronological order
    for (i=0; i < h->len; i++) {
        if (! strcmp(mode, "w")) fwprintf(f, L":"); // mode 'w' means we are saving the command mode history
        fwprintf(f, L"%ls\n", nl->line);
        nl = nl->pant;
    }
    fclose(f);
    return 1;
}

/**
 * \brief Remove history element
 *
 * \return 0 first element; -1 second element
 */

void del_item_from_history(struct history * h, int pos) {
    if (h->len - 1 < -pos) return;

    struct hlist * nl = h->list;
    struct hlist * n_ant = NULL;
    int i;

    if (pos == 0) {
        h->list = nl->pnext;
        if (nl->pnext != NULL) nl->pnext->pant = NULL;
    } else {
        for (i=0; i<-pos; i++) {
            n_ant = nl;
            nl = nl->pnext;
        }
        n_ant->pnext = nl->pnext;
        if (nl->pnext != NULL) nl->pnext->pant = n_ant;
    }
    free(nl->line);
    free(nl);
    h->len--;

    return;
}

/**
 * \brief TODO <brief function description>
 *
 * \details Find a history element and move it. Starts from POS
 * pos=0 first element, pos=-1 second element. Returns 1 if moved,
 * 0 otherwise
 *
 * \param[in] h
 * \param[in] item
 * \param[in] pos
 *
 * \returns: 1 if moved; 0 otherwise
 */

int move_item_from_history_by_str(struct history * h, wchar_t * item, int pos) {
    if (h->len - 1 < -pos || pos == 0 || ! wcslen(item)) return 0; // Move the first element is not allowed
    struct hlist * nl = h->list;
    if (nl != NULL && !wcscmp(item, nl->line)) return 1;
    struct hlist * n_ant = NULL;
    int i;

    for (i=0; i<-pos; i++) {
        n_ant = nl;
        nl = nl->pnext;
    }
    for (i=-pos; i < h->len ; i++) {
        if (wcscmp(item, nl->line) == 0) break;