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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
|
%{
#include <stdlib.h>
#include "sc.h"
#include "cmds.h"
#include "color.h"
#include "interp.h"
#include "macros.h"
#include "sort.h"
#include "filter.h"
#include "maps.h"
#include "marks.h"
#include "xmalloc.h" // for scxfree
#include "hide_show.h"
#include "cmds_normal.h"
#include "conf.h"
#include "pipe.h"
#include "main.h"
#include "tui.h"
#include "freeze.h"
#include "undo.h"
#include "dep_graph.h"
#include "utils/dictionary.h"
#include "trigger.h"
#include "shift.h"
void yyerror(char *err); // error routine for yacc (gram.y)
int yylex();
#ifdef USELOCALE
#include <locale.h>
#endif
#ifndef MSDOS
#include <unistd.h>
#endif
#define ENULL (struct enode *) 0
%}
%union {
int ival;
double fval;
struct ent_ptr ent;
struct enode * enode;
char * sval;
struct range_s rval;
}
%type <ent> var
%type <fval> num
%type <rval> range
%type <rval> var_or_range
%type <sval> strarg
%type <enode> e term expr_list
%token <sval> STRING
%token <ival> COL
%token <ival> NUMBER
%token <fval> FNUMBER
%token <rval> RANGE
%token <rval> VAR
%token <sval> WORD
%token <sval> MAPWORD
%token <sval> PLUGIN
/*
* When adding new commands, make sure that any commands that may take
* COL as an argument precede S_FORMAT in the %token list. All other
* commands must come after S_FORMAT. This is necessary so that range
* names can be less than three letters without being parsed as column
* names.
*/
%token S_SHOW
%token S_HIDE
%token S_SHOWROW
%token S_HIDEROW
%token S_SHOWCOL
%token S_HIDECOL
%token S_FREEZE
%token S_UNFREEZE
%token S_MARK
%token S_AUTOJUS
%token S_PAD
/*
token S_INSERTCOL
token S_OPENCOL
token S_DELETECOL
token S_YANKCOL
*/
%token S_DATEFMT
%token S_FORMAT
%token S_FMT
%token S_LET
%token S_LABEL
%token S_LEFTSTRING
%token S_RIGHTSTRING
%token S_LEFTJUSTIFY
%token S_RIGHTJUSTIFY
%token S_CENTER
%token S_SORT
%token S_FILTERON
%token S_GOTO
%token S_LOCK
%token S_UNLOCK
%token S_DEFINE
%token S_UNDEFINE
%token S_DETAIL
%token S_EVAL
%token S_SEVAL
%token S_ERROR
%token S_FILL
/*
token S_ADDNOTE
token S_DELNOTE
token S_GET
token S_PUT
token S_MERGE
token S_WRITE
token S_TBL
token S_COPY
token S_MOVE
token S_ERASE
token S_YANK
token S_ABBREV
token S_UNABBREV
token S_FRAME
token S_FRAMETOP
token S_FRAMEBOTTOM
token S_FRAMELEFT
token S_FRAMERIGHT
token S_UNFRAME
token S_VALUE
token S_MDIR
token S_AUTORUN
token S_FKEY
token S_SCEXT
token S_ASCEXT
token S_TBL0EXT
token S_TBLEXT
token S_LATEXEXT
token S_SLATEXEXT
token S_TEXEXT
token S_UP
token S_DOWN
token S_LEFT
token S_RIGHT
token S_ENDUP
token S_ENDDOWN
token S_ENDLEFT
token S_ENDRIGHT
token S_SELECT
token S_INSERTROW
token S_OPENROW
token S_DELETEROW
token S_YANKROW
token S_PULL
token S_PULLMERGE
token S_PULLROWS
token S_PULLCOLS
token S_PULLXCHG
token S_PULLTP
token S_PULLFMT
token S_PULLCOPY
token S_WHEREAMI
token S_FGETNUM
token S_GETFRAME
token S_GETRANGE
token S_QUERY
token S_GETKEY
token S_REDRAW
token S_STATUS
token S_RUN
token S_PLUGIN
token S_PLUGOUT
*/
%token S_SHIFT
%token S_GETNUM
%token S_GETSTRING
%token S_GETEXP
%token S_GETFMT
%token S_GETFORMAT
%token S_RECALC
%token S_QUIT
%token S_REBUILD_GRAPH
%token S_PRINT_GRAPH
%token S_SYNCREFS
%token S_REDO
%token S_UNDO
%token S_IMAP
%token S_NMAP
%token S_INOREMAP
%token S_NNOREMAP
%token S_NUNMAP
%token S_IUNMAP
%token S_COLOR
%token S_CELLCOLOR
%token S_UNFORMAT
%token S_REDEFINE_COLOR
%token S_SET
%token S_FCOPY
%token S_FSUM
%token S_TRIGGER
%token S_UNTRIGGER
%token K_AUTOCALC
%token K_NOAUTOCALC
%token K_DEBUG
%token K_NODEBUG
%token K_TRG
%token K_NOTRG
%token K_EXTERNAL_FUNCTIONS
%token K_NOEXTERNAL_FUNCTIONS
%token K_HALF_PAGE_SCROLL
%token K_NOHALF_PAGE_SCROLL
%token K_NOCURSES
%token K_CURSES
%token K_NUMERIC
%token K_NONUMERIC
%token K_NUMERIC_DECIMAL
%token K_NONUMERIC_DECIMAL
%token K_NUMERIC_ZERO
%token K_NONUMERIC_ZERO
%token K_OVERLAP
%token K_NOOVERLAP
%token K_QUIT_AFTERLOAD
%token K_NOQUIT_AFTERLOAD
%token K_XLSX_READFORMULAS
%token K_NOXLSX_READFORMULAS
%token K_IGNORECASE
%token K_NOIGNORECASE
%token K_TM_GMTOFF
%token K_NEWLINE_ACTION
%token K_ERROR
%token K_INVALID
%token K_FIXED
%token K_SUM
%token K_PROD
%token K_AVG
%token K_STDDEV
%token K_COUNT
%token K_ROWS
%token K_COLS
%token K_ABS
%token K_FROW
%token K_FCOL
%token K_ACOS
%token K_ASIN
%token K_ATAN
%token K_ATAN2
%token K_CEIL
%token K_COS
%token K_EXP
%token K_FABS
%token K_FLOOR
%token K_HYPOT
%token K_LN
%token K_LOG
%token K_PI
%token K_POW
%token K_SIN
%token K_SQRT
%token K_TAN
%token K_DTR
%token K_RTD
%token K_MAX
%token K_MIN
%token K_RND
%token K_ROUND
%token K_IF
%token K_PV
%token K_FV
%token K_PMT
%token K_HOUR
%token K_MINUTE
%token K_SECOND
%token K_MONTH
%token K_DAY
%token K_YEAR
%token K_NOW
%token K_DATE
%token K_DTS
%token K_TTS
%token K_FMT
%token K_REPLACE
%token K_SUBSTR
%token K_UPPER
%token K_LOWER
%token K_CAPITAL
%token K_STON
%token K_SLEN
%token K_EQS
%token K_EXT
%token K_LUA
%token K_NVAL
%token K_SVAL
%token K_LOOKUP
%token K_HLOOKUP
%token K_VLOOKUP
%token K_INDEX
%token K_STINDEX
/*
token K_AUTO
token K_AUTOINSERT
token K_AUTOWRAP
token K_CSLOP
token K_BYROWS
token K_BYCOLS
token K_OPTIMIZE
token K_ITERATIONS
token K_NUMERIC
token K_PRESCALE
token K_EXTFUN
token K_CELLCUR
token K_TOPROW
token K_COLOR
token K_COLORNEG
token K_COLORERR
token K_BRAILLE
token K_BLACK
token K_RED
token K_GREEN
token K_YELLOW
token K_BLUE
token K_MAGENTA
token K_CYAN
token K_WHITE
*/
%token K_TBLSTYLE
%token K_TBL
%token K_LATEX
%token K_SLATEX
%token K_TEX
%token K_FRAME
%token K_RNDTOEVEN
%token K_FILENAME
%token K_MYROW
%token K_MYCOL
%token K_LASTROW
%token K_LASTCOL
%token K_COLTOA
%token K_CRACTION
%token K_CRROW
%token K_CRCOL
%token K_ROWLIMIT
%token K_COLLIMIT
%token K_PAGESIZE
/*
token K_NUMITER
*/
%token K_ERR
%token K_REF
%token K_LOCALE
%token K_SET8BIT
%token K_ASCII
%token K_CHR
%right ';'
%left '?' ':'
%left '|'
%left '&'
%nonassoc '<' '=' '>' '!'
%left '+' '-' '#'
%left '*' '/' '%'
%left '^'
%%
command:
S_LET var_or_range '=' e { let($2.left.vp, $4);
}
| S_DETAIL var {
char det[BUFFERSIZE] = "";
struct ent * e = $2.vp;
sprintf(det + strlen(det), "row: %d\n", e->row);
sprintf(det + strlen(det), "col: %d\n", e->col);
sprintf(det + strlen(det), "cellerror: %d\n" , e->cellerror);
sprintf(det + strlen(det), "flags:\n");
sprintf(det + strlen(det), "is_valid: %d\n" , e->flags & is_valid );
sprintf(det + strlen(det), "is_deleted: %d\n" , e->flags & is_deleted);
sprintf(det + strlen(det), "is_changed: %d\n" , e->flags & is_changed);
sprintf(det + strlen(det), "is_strexpr: %d\n" , e->flags & is_strexpr);
sprintf(det + strlen(det), "is_leftflush: %d\n", e->flags & is_leftflush);
sprintf(det + strlen(det), "is_locked: %d\n" , e->flags & is_locked);
sprintf(det + strlen(det), "is_label: %d\n" , e->flags & is_label);
sprintf(det + strlen(det), "iscleared: %d\n" , e->flags & iscleared);
sprintf(det + strlen(det), "may_sync: %d\n" , e->flags & may_sync);
show_text((char *) &det);
}
| S_LET var_or_range '='
{
// TODO get this code out of gram.y
extern graphADT graph;
#ifdef UNDO
create_undo_action();
copy_to_undostruct($2.left.vp->row, $2.left.vp->col, $2.left.vp->row, $2.left.vp->col, 'd');
// here we save in undostruct, all the ents that depends on the deleted one (before change)
extern struct ent_ptr * deps;
int i, n = 0;
ents_that_depends_on_range($2.left.vp->row, $2.left.vp->col, $2.left.vp->row, $2.left.vp->col);
if (deps != NULL) {
for (i = 0, n = deps->vf; i < n; i++)
copy_to_undostruct(deps[i].vp->row, deps[i].vp->col, deps[i].vp->row, deps[i].vp->col, 'd');
}
#endif
if (getVertex(graph, lookat($2.left.vp->row, $2.left.vp->col), 0) != NULL) destroy_vertex(lookat($2.left.vp->row, $2.left.vp->col));
$2.left.vp->v = (double) 0.0;
if ($2.left.vp->expr && !($2.left.vp->flags & is_strexpr)) {
efree($2.left.vp->expr);
$2.left.vp->expr = NULL;
}
$2.left.vp->cellerror = CELLOK;
$2.left.vp->flags &= ~is_valid;
$2.left.vp->flags |= is_changed;
modflg++;
#ifdef UNDO
copy_to_undostruct($2.left.vp->row, $2.left.vp->col, $2.left.vp->row, $2.left.vp->col, 'a');
// here we save in undostruct, all the ents that depends on the deleted one (after change)
if (deps != NULL) free(deps);
ents_that_depends_on_range($2.left.vp->row, $2.left.vp->col, $2.left.vp->row, $2.left.vp->col);
if (deps != NULL) {
for (i = 0, n = deps->vf; i < n; i++)
copy_to_undostruct(deps[i].vp->row, deps[i].vp->col, deps[i].vp->row, deps[i].vp->col, 'a');
free(deps);
deps = NULL;
}
end_undo_action();
#endif
}
| S_LABEL var_or_range '=' e { slet($2.left.vp, $4, 0); }
| S_LEFTSTRING var_or_range '=' e { slet($2.left.vp, $4, -1); }
| S_RIGHTSTRING var_or_range '=' e { slet($2.left.vp, $4, 1); }
| S_LEFTJUSTIFY var_or_range { ljustify($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col); }
| S_RIGHTJUSTIFY var_or_range { rjustify($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col); }
| S_CENTER var_or_range { center($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col); }
| S_FORMAT COL NUMBER NUMBER NUMBER { doformat($2,$2,$3,$4,$5); }
| S_FMT var_or_range STRING { format_cell($2.left.vp, $2.right.vp, $3);
scxfree($3);
}
| S_DATEFMT var_or_range STRING { dateformat($2.left.vp, $2.right.vp, $3);
scxfree($3); }
| S_DATEFMT STRING { dateformat(lookat(currow, curcol), lookat(currow, curcol), $2);
scxfree($2); }
/* para compatibilidad con sc */
| S_HIDE COL { hide_col($2, 1); } // hide de una unica columna
| S_HIDE NUMBER { hide_row($2, 1); } // hide de una unica fila
| S_SHOW COL { show_col($2, 1); } // show de una unica columna
| S_SHOW NUMBER { show_row($2, 1); } // show de una unica fila
/* agregados para scim */
| S_HIDECOL COL {
hide_col($2, 1); } // hide de una unica columna
| S_SHOWCOL COL {
show_col($2, 1); } // show de una unica columna
| S_HIDEROW NUMBER {
hide_row($2, 1); } // hide de una unica fila
| S_SHOWROW NUMBER {
show_row($2, 1); } // show de una unica fila
| S_SHOWCOL COL ':' COL {
show_col($2, $4-$2+1); } // show de un rango de columnas
| S_SHOWROW NUMBER ':' NUMBER {
show_row($2, $4-$2+1); } // show de un rango de filas
| S_HIDECOL COL ':' COL {
int c = curcol, arg;
if ($2 < $4) {
curcol = $2;
arg = $4 - $2 + 1;
} else {
curcol = $4;
arg = $2 - $4 + 1;
}
hide_col($2, arg); // hide de un rango de columnas
curcol = c < curcol ? c : c < curcol + arg ? curcol : c - arg;
}
| S_HIDEROW NUMBER ':' NUMBER {
int r = currow, arg; // hide de un rango de filas
if ($2 < $4) {
currow = $2;
arg = $4 - $2 + 1;
} else {
currow = $4;
arg = $2 - $4 + 1;
}
hide_row($2, arg);
currow = r < currow ? r : r < currow + arg ? currow : r - arg;
}
| S_SHIFT var_or_range STRING {
if (strlen($3) != 1 || ($3[0] != 'h' && $3[0] != 'j' && $3[0] != 'k' && $3[0] != 'l')) {
sc_error("wrong parameter for shift command");
} else {
wchar_t wstr[2] = L"";
swprintf(wstr, BUFFERSIZE, L"%c", $3[0]);
shift($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col, wstr[0]);
}
scxfree($3);
}
| S_MARK COL var_or_range { set_cell_mark($2 + 97, $3.left.vp->row, $3.left.vp->col); }
| S_MARK COL var_or_range var_or_range { ;
srange * sr = create_range('\0', '\0', $3.left.vp, $4.left.vp);
unselect_ranges();
set_range_mark($2 + 97, sr);
}
| S_FILL var_or_range num num { fill($2.left.vp, $2.right.vp, $3, $4); }
| S_FILL num num { sc_error("Not enough parameters for fill command"); }
| S_UNFREEZE { remove_frange(); }
| S_FREEZE range { add_frange($2.left.vp, $2.right.vp, 'a'); }
| S_FREEZE NUMBER ':' NUMBER { add_frange(lookat($2, 0), lookat($4, 0), 'r'); }
| S_FREEZE NUMBER { add_frange(lookat($2, 0), lookat($2, 0), 'r'); }
| S_FREEZE COL ':' COL { add_frange(lookat(0, $2), lookat(0, $4), 'c'); }
| S_FREEZE COL { add_frange(lookat(0, $2), lookat(0, $2), 'c'); }
| S_SORT range STRING { sortrange($2.left.vp, $2.right.vp, $3);
//scxfree($3);
}
| S_FILTERON range { enable_filters($2.left.vp, $2.right.vp);
}
/* para debug
wtimeout(input_win, -1);
info(" ********* FILTERON");
int d = wgetch(input_win);
wtimeout(input_win, TIMEOUT_CURSES);
*/
/*
| S_GET strarg {
/* This tmp hack is because readfile recurses back through yyparse.
char *tmp;
tmp = $2;
readfile(tmp, 1);
scxfree(tmp);
}
*/
| S_AUTOJUS COL ':' COL { auto_justify($2, $4, DEFWIDTH); } // auto justificado de columnas
| S_AUTOJUS COL { auto_justify($2, $2, DEFWIDTH); } // auto justificado de columna
| S_GOTO var_or_range var_or_range { moveto($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col, $3.left.vp->row, $3.left.vp->col); }
| S_GOTO var_or_range { moveto($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col, -1, -1); }
| S_GOTO num { num_search($2, 0, 0, maxrow, maxcol, 0, 1); }
| S_GOTO STRING { str_search($2, 0, 0, maxrow, maxcol, 0, 1); }
//scxfree($2); shall not free here
| S_GOTO '#' STRING { str_search($3, 0, 0, maxrow, maxcol, 1, 1); }
//scxfree($3); shall not free here
| S_GOTO '%' STRING { str_search($3, 0, 0, maxrow, maxcol, 2, 1); }
//scxfree($3); shall not free here
// | S_GOTO WORD { /* don't repeat last goto on "unintelligible word" */ ; }
| S_LOCK var_or_range { lock_cells($2.left.vp, $2.right.vp); }
| S_UNLOCK var_or_range { unlock_cells($2.left.vp, $2.right.vp); }
| S_NMAP STRING STRING {
add_map($2, $3, NORMAL_MODE, 1);
scxfree($2);
scxfree($3);
}
| S_IMAP STRING STRING {
add_map($2, $3, INSERT_MODE, 1);
scxfree($2);
scxfree($3);
}
| S_NNOREMAP STRING STRING {
add_map($2, $3, NORMAL_MODE, 0);
scxfree($2);
scxfree($3);
}
| S_INOREMAP STRING STRING {
add_map($2, $3, INSERT_MODE, 0);
scxfree($2);
scxfree($3);
}
| S_NUNMAP STRING {
del_map($2, NORMAL_MODE);
scxfree($2);
}
| S_IUNMAP STRING {
del_map($2, INSERT_MODE);
scxfree($2);
}
| S_COLOR STRING {
#ifdef USECOLORS
|