summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 21922e763a8b472423f7a8ddf802a634b70f4ba7 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include <signal.h>
#include <ncurses.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>   // for F_GETFL O_NONBLOCK F_SETFL
#include <locale.h>
#include <wchar.h>
#include <wordexp.h>

#include "main.h"
#include "shift.h"
#include "macros.h"
#include "screen.h"
#include "input.h"
#include "marks.h"
#include "maps.h"
#include "yank.h"
#include "file.h"
#include "utils/dictionary.h"
#include "utils/string.h"
#include "history.h"
#include "conf.h"
#include "buffer.h"
#include "cmds.h"
#include "color.h"   // for set_ucolor
#include "vmtbl.h"   // for growtbl
#include "filter.h"
#include "dep_graph.h"

#ifdef UNDO
#include "undo.h"
#endif

int currow = 0, curcol = 0;
int lastrow = 0, lastcol = 0;
int maxrows, maxcols;
int * fwidth;
int * precision;
int * realfmt;
char * col_hidden;
char * row_hidden;
char line[FBUFLEN];
int modflg;          // a change was made since last save
struct ent *** tbl;
int shall_quit = 0;
unsigned int curmode = NORMAL_MODE;
int maxrow, maxcol;
char curfile[PATHLEN];
char * exepath;

int changed;
int cellassign;
int arg = 1;
int brokenpipe = FALSE; /* Set to true if SIGPIPE is received */
char * ascext;
char * tbl0ext;
char * tblext;
char * latexext;
char * slatexext;
char * texext;
char dpoint = '.';      // decimal point
char thsep = ',';       // thousands separator
int linelim = -1;
int calc_order = BYROWS;
int optimize  = 0;      // Causes numeric expressions to be optimized
int tbl_style = 0;      // headers for T command output
int rndtoeven = 0;
int rowsinrange = 1;
int colsinrange = DEFWIDTH;
double eval_result;
char * seval_result;
FILE * fdoutput;        // output file descriptor (stdout or file)

struct block * buffer;
struct block * lastcmd_buffer;
struct dictionary * user_conf_d;
struct dictionary * predefined_conf_d;
struct history * commandline_history;

/*********************************************************************
   MAIN LOOP
 *********************************************************************/
int main (int argc, char ** argv) {

#ifdef USELOCALE
    // pass LC_CTYPE env variable to libraries
    //setlocale(LC_ALL, "");
    setlocale(LC_CTYPE, "");
#endif

    // set up signals so we can catch them
    signals();

    // start configuration dictionaries
    user_conf_d = (struct dictionary *) create_dictionary();
    predefined_conf_d = (struct dictionary *) create_dictionary();
    store_default_config_values();

    // create command line history structure
#ifdef HISTORY_FILE
    if (! atoi(get_conf_value("nocurses"))) {
        commandline_history = (struct history *) create_history(':');
        load_history(commandline_history);
    }
#endif

    // create basic structures that will depend on the loaded file
    create_structures();

    // setup the spreadsheet arrays (tbl)
    if (! growtbl(GROWNEW, 0, 0)) return exit_app(1);

    // we save parameters and use them to replace conf-values in config dictionary !
    read_argv(argc, argv);

    // initiate NCURSES if that is what is wanted
    if (! atoi(get_conf_value("nocurses")))
        start_screen();

#ifdef USECOLORS
    //if (has_colors() && get_d_colors_param() == NULL) {
    if (get_d_colors_param() == NULL) {
            start_default_ucolors();
            // in case we decide to change colors
            // this creates a dictionary and stores in it
            // the relationship between macros and the keys values
            // that are defined in .sc files
            set_colors_param_dict();
    }
#endif


    // If the 'output' parameter is defined, SC-IM saves its output to that file.
    // To achieve that, we open the output file and keep it open until exit.
    // otherwise, SC-IM will output to stdout.
    if (get_conf_value("output") != NULL) {
        fdoutput = fopen(get_conf_value("output"), "w+");
        if (fdoutput == NULL) {
            sc_error("Cannot open file: %s.", get_conf_value("output"));
            return exit_app(-1);
        }

        if (! atoi(get_conf_value("nocurses"))) { // WE MUST STOP SCREEN!
            stop_screen();

            // if output is set, nocurses should always be 1 !
            put(user_conf_d, "nocurses", "1");
        }
    }

    // load sc file
    load_sc();

    // handle input from stdin (pipeline) in sc format
    // and send it to interp
    //FILE * f = fopen("/dev/stdin", "r");
    FILE * f = stdin;
    int fd = fileno(f);
    int flags;
    flags = fcntl(fd, F_GETFL, 0);
    flags |= O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);
    wchar_t stdin_buffer[BUFFERSIZE] = { L'\0' };
    while (f != NULL && fgetws(stdin_buffer, BUFFERSIZE, f) != NULL) {
        send_to_interp(stdin_buffer);
    }
    if (!freopen("/dev/tty", "rw", stdin)) {
        perror(NULL);
        exit(-1);
    }
    flags = fcntl(fd, F_GETFL, 0);
    flags &= ~O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);

    // Enable this for message debugging
    // wtimeout(input_win, 1000);

    // initiate gui
    if ( ! atoi(get_conf_value("nocurses"))) {
        //start_screen();         // Already done above

        // we show welcome screen if no spreadsheet was passed to SC-IM
        // and no input was sent throw pipeline
        do_welcome();
        if ( ! curfile[0] && ! wcslen(stdin_buffer)) {
            //do_welcome();

            // show mode and cell's details in status bar
            show_celldetails(input_win);
            print_mode(input_win);
            wrefresh(input_win);
        } else {
            update(TRUE);
        }
    }

    // handle input from keyboard
    buffer = (struct block *) create_buf(); // TODO: this should only take place if curses ui
    // Enable for debugging purposes
    // wtimeout(input_win, TIMEOUT_CURSES);

    wchar_t nocurses_buffer[BUFFERSIZE];

    while ( ! shall_quit && ! atoi(get_conf_value("quit_afterload"))) {
        // if we are in ncurses
        if (! atoi(get_conf_value("nocurses"))) {
            handle_input(buffer);


        // if we are not in ncurses
        } else {
            if (fgetws(nocurses_buffer, BUFFERSIZE, stdin) != NULL) {
                //wprintf(L"Interp will receive: %ls", nocurses_buffer);
                sc_info("Interp will receive: %ls", nocurses_buffer);
                send_to_interp(nocurses_buffer);
            }
        }

        /* shall_quit=1 means :q
           shall_quit=2 means :q! */
        if (shall_quit == 1 && modcheck()) shall_quit = 0;
    }

    return shall_quit == -1 ? exit_app(-1) : exit_app(0);
}
/*********************************************************************
   END OF MAIN LOOP
 *********************************************************************/

extern graphADT graph;

void create_structures() {

    // initiate mark array
    create_mark_array();

    // create last command buffer
    lastcmd_buffer = (struct block *) create_buf();

    // create yank list structure
    init_yanklist();

    /* Assign NULL to colformats
    int c;
    for (c = 0; c < COLFORMATS; c++)
        colformat[c] = NULL;
    */

    // init calc chain graph
    graph = GraphCreate();
}



// delete basic structures that depend on the loaded file
void delete_structures() {

    // Free marks array
    free_marks_array();

    // Free yanklist
    free_yanklist();

    // Erase last_command buffer
    erase_buf(lastcmd_buffer);

    // Free ranges
    free_ranges();

    // Free filters
    free_filters();

    // Free undo list - from start of list
#ifdef UNDO
    clear_undo_list();
#endif

    // free deleted ents
    flush_saved();

    // free calc chain graph
    destroy_graph(graph);

    // Free ents of tbl
    erasedb();
}



int exit_app(int status) {

    // free history
#ifdef HISTORY_FILE
    if (! atoi(get_conf_value("nocurses")) && save_history(commandline_history) != 0 ) {
        sc_error("Cannot save command line history");
    }
    if (commandline_history != NULL) destroy_history(commandline_history);
#endif

    // erase structures
    delete_structures();

    // Free mappings
    del_maps();

    // Erase stdin
    erase_buf(buffer);

    //timeout(-1);

    // stop CURSES screen