-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathcommand.c
More file actions
4001 lines (3513 loc) · 104 KB
/
command.c
File metadata and controls
4001 lines (3513 loc) · 104 KB
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
/* GNUPLOT - command.c */
/*[
* Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
]*/
/*
* Changes:
*
* 19 September 1992 Lawrence Crowl ([email protected])
* Added user-specified bases for log scaling.
*
* April 1999 Franz Bakan ([email protected])
* Added code to support mouse-input from OS/2 PM window
* Changes marked by USE_MOUSE
*
* May 1999, update by Petr Mikulik
* Use gnuplot's pid in shared mem name
*
* August 1999 Franz Bakan and Petr Mikulik
* Encapsulating read_line into a thread, acting on input when thread or
* gnupmdrv posts an event semaphore. Thus mousing works even when gnuplot
* is used as a plotting device (commands passed via pipe).
*
* May 2011 Ethan A Merritt
* Introduce block structure defined by { ... }, which may span multiple lines.
* In order to have the entire block available at one time we now count
* +/- curly brackets during input and keep extending the current input line
* until the net count is zero. This is done in do_line() for interactive
* input, and load_file() for non-interactive input.
*/
#include "command.h"
#include "axis.h"
#include "alloc.h"
#include "datablock.h"
#include "eval.h"
#include "fit.h"
#include "datafile.h"
#include "getcolor.h"
#include "gp_hist.h"
#include "gplocale.h"
#include "loadpath.h"
#include "misc.h"
#include "parse.h"
#include "plot.h"
#include "plot2d.h"
#include "plot3d.h"
#include "readline.h"
#include "save.h"
#include "scanner.h"
#include "setshow.h"
#include "stats.h"
#include "tables.h"
#include "term_api.h"
#include "util.h"
#include "voxelgrid.h"
#include "external.h"
#ifdef USE_MOUSE
# include "mouse.h"
int paused_for_mouse = 0;
#endif
#define PROMPT "gnuplot> "
#ifdef OS2_IPC
# define INCL_DOSMEMMGR
# define INCL_DOSPROCESS
# define INCL_DOSSEMAPHORES
# include <os2.h>
static char *input_line_SharedMem = NULL; /* pointer to the shared memory for mouse messages */
static HEV semInputReady = 0; /* mouse event semaphore */
static HEV semPause = 0; /* pause event semaphore */
static TBOOLEAN thread_rl_Running = FALSE; /* running status */
static int thread_rl_RetCode = -1; /* return code from readline input thread */
static int thread_pause_RetCode = -1; /* return code from pause input thread */
static TBOOLEAN pause_internal; /* flag to indicate not to use a dialog box */
#endif /* OS2_IPC */
#ifndef _WIN32
# include "help.h"
#endif
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# ifdef _MSC_VER
# include <malloc.h>
# include <direct.h> /* getcwd() */
# else
# include <alloc.h>
# endif
# include <htmlhelp.h>
# include "win/winmain.h"
#endif /* _WIN32 */
#ifdef __DJGPP__
# include <pc.h> /* getkey() */
# define useconds_t unsigned
#endif
#ifdef __WATCOMC__
# include <conio.h> /* for getch() */
#endif
typedef enum ifstate {IF_INITIAL=1, IF_TRUE, IF_FALSE} ifstate;
/* static prototypes */
static void command(void);
static TBOOLEAN is_array_assignment(void);
static int changedir(char *path);
static char* fgets_ipc(char* dest, int len);
static char* gp_get_string(char *, size_t, const char *);
static int read_line(const char *prompt, int start);
static void do_system(const char *);
static void test_palette_subcommand(void);
static int find_clause(int *, int *);
static void if_else_command(ifstate if_state);
static void old_if_command(struct at_type *expr);
static int report_error(int ierr);
static void load_or_call_command( TBOOLEAN call );
void do_shell(void);
static int expand_1level_macros(void);
struct lexical_unit *token;
int token_table_size;
char *gp_input_line;
size_t gp_input_line_len;
int inline_num; /* input line number */
/* Points to structure holding dummy parameter values
* to be used during function evaluation
*/
struct udft_entry *dummy_func;
/* support for replot command */
char *replot_line = NULL;
int plot_token = 0; /* start of 'plot' command */
/* flag to disable `replot` when some data are sent through stdin;
* used by mouse/hotkey capable terminals
*/
TBOOLEAN replot_disabled = FALSE;
/* flag to show we are inside a plot/splot/replot/refresh/stats
* command and therefore should not allow starting another one
* e.g. from a function block
*/
TBOOLEAN inside_plot_command = FALSE;
/* output file for the print command */
FILE *print_out = NULL;
struct udvt_entry *print_out_var = NULL;
char *print_out_name = NULL;
char *print_sep = NULL;
/* input data, parsing variables */
int num_tokens, c_token;
TBOOLEAN if_open_for_else = FALSE;
static int clause_depth = 0;
/* support for 'break' and 'continue' commands */
static int iteration_depth = 0;
static TBOOLEAN requested_break = FALSE;
static TBOOLEAN requested_continue = FALSE;
/* set when an "exit" command is encountered */
static int command_exit_requested = 0;
/* support for dynamic size of input line */
void
extend_input_line()
{
if (gp_input_line_len == 0) {
/* first time */
gp_input_line = gp_alloc(MAX_LINE_LEN, "gp_input_line");
gp_input_line_len = MAX_LINE_LEN;
gp_input_line[0] = NUL;
} else {
gp_input_line = gp_realloc(gp_input_line, gp_input_line_len + MAX_LINE_LEN,
"extend input line");
gp_input_line_len += MAX_LINE_LEN;
FPRINTF((stderr, "extending input line to %d chars\n",
gp_input_line_len));
}
}
/* constant by which token table grows */
#define MAX_TOKENS 400
void
extend_token_table()
{
if (token_table_size == 0) {
/* first time */
token = (struct lexical_unit *) gp_alloc(MAX_TOKENS * sizeof(struct lexical_unit), "token table");
token_table_size = MAX_TOKENS;
/* HBB: for checker-runs: */
memset(token, 0, MAX_TOKENS * sizeof(*token));
} else {
token = gp_realloc(token, (token_table_size + MAX_TOKENS) * sizeof(struct lexical_unit), "extend token table");
memset(token+token_table_size, 0, MAX_TOKENS * sizeof(*token));
token_table_size += MAX_TOKENS;
FPRINTF((stderr, "extending token table to %d elements\n", token_table_size));
}
}
#ifdef OS2_IPC
void
thread_read_line(void *arg)
{
(void) arg;
thread_rl_Running = TRUE;
thread_rl_RetCode = read_line(PROMPT, 0);
thread_rl_Running = FALSE;
DosPostEventSem(semInputReady);
}
void
thread_pause(void *arg)
{
int rc = 2;
if (!pause_internal)
rc = PM_pause(arg != NULL ? arg : "paused");
if (rc == 2) {
/* rc==2: no dialog, listen to stdin */
int junk;
if (arg != NULL) fputs(arg, stderr);
do {
junk = fgetc(stdin);
} while (junk != EOF && junk != '\r' && junk != '\n');
thread_pause_RetCode = (junk == EOF) ? 0 : 1;
} if (rc == 3) {
/* dialog/menu active, wait for event semaphore */
ULONG u;
DosResetEventSem(semPause, &u);
DosWaitEventSem(semPause, SEM_INDEFINITE_WAIT);
/* now query result */
thread_pause_RetCode = PM_pause(NULL);
} else {
/* rc==1: OK; rc==0: cancel */
thread_pause_RetCode = rc;
}
DosPostEventSem(semInputReady);
}
void
os2_ipc_setup(void)
{
APIRET rc;
char name[40];
/* create input event semaphore */
sprintf(name, "\\SEM32\\GP%i_Input_Ready", getpid());
rc = DosCreateEventSem(name, &semInputReady, 0, 0);
if (rc != 0)
fputs("DosCreateEventSem error\n", stderr);
/* create pause event semaphore */
sprintf(name, "\\SEM32\\GP%i_Pause_Ready", getpid());
rc = DosCreateEventSem(name, &semPause, 0, 0);
if (rc != 0)
fputs("DosCreateEventSem error\n", stderr);
/* allocate shared memory */
sprintf(name, "\\SHAREMEM\\GP%i_Mouse_Input", getpid());
rc = DosAllocSharedMem((PPVOID) &input_line_SharedMem,
name, MAX_LINE_LEN,
PAG_READ | PAG_WRITE | PAG_COMMIT);
if (rc != 0)
fputs("DosAllocSharedMem ERROR\n", stderr);
else
*input_line_SharedMem = 0;
}
int
os2_ipc_dispatch_event(void)
{
if (input_line_SharedMem == NULL || !*input_line_SharedMem)
return 0;
if (*input_line_SharedMem == '%') {
struct gp_event_t ge;
/* copy event data immediately */
memcpy(&ge, input_line_SharedMem + 1, sizeof(ge));
*input_line_SharedMem = 0; /* discard the event data */
thread_rl_RetCode = 0;
/* process event */
do_event(&ge);
/* end pause mouse? */
if ((ge.type == GE_buttonrelease) && (paused_for_mouse & PAUSE_CLICK) &&
(((ge.par1 == 1) && (paused_for_mouse & PAUSE_BUTTON1)) ||
((ge.par1 == 2) && (paused_for_mouse & PAUSE_BUTTON2)) ||
((ge.par1 == 3) && (paused_for_mouse & PAUSE_BUTTON3)))) {
paused_for_mouse = 0;
}
if ((ge.type == GE_keypress) && (paused_for_mouse & PAUSE_KEYSTROKE) &&
(ge.par1 != NUL)) {
paused_for_mouse = 0;
}
return 0;
}
if (*input_line_SharedMem &&
strstr(input_line_SharedMem, "plot") != NULL &&
(strcmp(term->name, "pm") != 0 && strcmp(term->name, "x11") != 0)) {
/* avoid plotting if terminal is not PM or X11 */
fprintf(stderr, "\n\tCommand(s) ignored for other than PM and X11 terminals\a\n");
if (interactive)
fputs(PROMPT, stderr);
*input_line_SharedMem = 0; /* discard the event data */
return 0;
}
strcpy(gp_input_line, input_line_SharedMem);
input_line_SharedMem[0] = 0;
thread_rl_RetCode = 0;
return 1;
}
int
os2_ipc_waitforinput(int mode)
{
ULONG u;
if (mode == TERM_ONLY_CHECK_MOUSING) {
if (semInputReady == 0)
return 0;
if (DosWaitEventSem(semInputReady, SEM_IMMEDIATE_RETURN) == 0) {
os2_ipc_dispatch_event();
DosResetEventSem(semInputReady, &u);
}
}
return 0;
}
#endif /* OS2_IPC */
int
com_line()
{
if (multiplot) {
/* calls int_error() if it is not happy */
term_check_multiplot_okay(interactive);
if (read_line("multiplot> ", 0))
return (1);
} else {
#if defined(OS2_IPC) && defined(USE_MOUSE)
ULONG u;
if (!thread_rl_Running) {
int res = _beginthread(thread_read_line, NULL, 32768, NULL);
if (res == -1)
fputs("error command.c could not begin thread\n", stderr);
}
/* wait until a line is read or gnupmdrv makes shared mem available */
DosWaitEventSem(semInputReady, SEM_INDEFINITE_WAIT);
DosResetEventSem(semInputReady, &u);
if (thread_rl_Running) {
/* input thread still running, this must be a "mouse" event */
if (os2_ipc_dispatch_event() == 0)
return 0;
}
if (thread_rl_RetCode)
return 1;
#else /* The normal case */
if (read_line(PROMPT, 0))
return 1;
#endif /* defined(OS2_IPC) && defined(USE_MOUSE) */
}
/* So we can flag any new output: if false at time of error,
* we reprint the command line before printing caret.
* TRUE for interactive terminals, since the command line is typed.
* FALSE for non-terminal stdin, so command line is printed anyway.
* (DFK 11/89)
*/
screen_ok = interactive;
if (do_line())
return (1);
else
return (0);
}
int
do_line()
{
/* Line continuation has already been handled by read_line().
* Expand any string variables in the current input line.
*/
string_expand_macros();
/* Remove leading whitespace */
{
char *inlptr = gp_input_line;
while (isspace((unsigned char) *inlptr))
inlptr++;
if (inlptr != gp_input_line) {
memmove(gp_input_line, inlptr, strlen(inlptr));
gp_input_line[strlen(inlptr)] = NUL;
}
}
/* Leading '!' indicates a shell command that bypasses normal gnuplot
* tokenization and parsing.
* This doesn't work inside a bracketed clause or in a function block.
*/
if (is_system(*gp_input_line)) {
if (evaluate_inside_functionblock)
int_error(NO_CARET, "bare shell commands not accepted in a function block");
do_system(gp_input_line + 1);
return (0);
}
/* Strip off trailing comment */
if (strchr(gp_input_line, '#')) {
num_tokens = scanner(&gp_input_line, &gp_input_line_len);
if (gp_input_line[token[num_tokens].start_index] == '#')
gp_input_line[token[num_tokens].start_index] = NUL;
}
num_tokens = scanner(&gp_input_line, &gp_input_line_len);
/*
* Expand line if necessary to contain a complete bracketed clause {...}
* Insert a ';' after current line and append the next input line.
* NB: This may leave an "else" condition on the next line.
*/
if (curly_brace_count < 0)
int_error(NO_CARET,"Unexpected }");
while (curly_brace_count > 0) {
if (lf_head && lf_head->depth > 0) {
/* This catches the case that we are inside a "load foo" operation
* and therefore requesting interactive input is not an option.
*/
int_error(NO_CARET, "Syntax error: missing block terminator }");
}
else if (interactive || noinputfiles) {
/* If we are really in interactive mode and there are unterminated blocks,
* then we want to display a "more>" prompt to get the rest of the block.
* However, there are two more cases that must be dealt here:
* One is when commands are piped to gnuplot - on the command line,
* the other is when commands are piped to gnuplot which is opened
* as a slave process. The test for noinputfiles is for the latter case.
* If we didn't have that test here, unterminated blocks sent via a pipe
* would trigger the error message in the else branch below. */
int retval;
strcat(gp_input_line,";");
retval = read_line("more> ", strlen(gp_input_line));
if (retval)
int_error(NO_CARET, "Syntax error: missing block terminator }");
/* Expand any string variables in the current input line */
string_expand_macros();
num_tokens = scanner(&gp_input_line, &gp_input_line_len);
if (gp_input_line[token[num_tokens].start_index] == '#')
gp_input_line[token[num_tokens].start_index] = NUL;
}
else {
/* Non-interactive mode here means that we got a string from -e.
* Having curly_brace_count > 0 means that there are at least one
* unterminated blocks in the string.
* Likely user error, so we die with an error message. */
int_error(NO_CARET, "Syntax error: missing block terminator }");
}
}
c_token = 0;
while (c_token < num_tokens) {
command();
if (command_exit_requested) {
command_exit_requested = 0; /* yes this is necessary */
return 1;
}
if (iteration_early_exit()) {
c_token = num_tokens;
break;
}
if (c_token < num_tokens) { /* something after command */
if (equals(c_token, ";")) {
c_token++;
} else if (equals(c_token, "{")) {
begin_clause();
} else if (equals(c_token, "}")) {
end_clause();
} else
int_error(c_token, "unexpected or unrecognized token: %s",
token_to_string(c_token));
}
}
/* This check allows event handling inside load/eval/while statements */
check_for_mouse_events();
return (0);
}
void
do_string(const char *s)
{
char *cmdline = gp_strdup(s);
do_string_and_free(cmdline);
}
void
do_string_and_free(char *cmdline)
{
#ifdef USE_MOUSE
if (display_ipc_commands())
fprintf(stderr, "%s\n", cmdline);
#endif
lf_push(NULL, NULL, cmdline); /* save state for errors and recursion */
while (gp_input_line_len < strlen(cmdline) + 1)
extend_input_line();
strcpy(gp_input_line, cmdline);
screen_ok = FALSE;
command_exit_requested = do_line();
/* "exit" is supposed to take us out of the current file from a
* "load <file>" command. But the LFS stack holds both files and
* bracketed clauses, so we have to keep popping until we hit an
* actual file.
*/
if (command_exit_requested) {
while (lf_head && !lf_head->name) {
FPRINTF((stderr,"pop one level of non-file LFS\n"));
lf_pop();
}
} else
lf_pop();
}
#ifdef USE_MOUSE
void
toggle_display_of_ipc_commands()
{
if (mouse_setting.verbose)
mouse_setting.verbose = 0;
else
mouse_setting.verbose = 1;
}
int
display_ipc_commands()
{
return mouse_setting.verbose;
}
void
do_string_replot(const char *s)
{
do_string(s);
if (volatile_data && (E_REFRESH_NOT_OK != refresh_ok)) {
if (display_ipc_commands())
fprintf(stderr, "refresh\n");
refresh_request();
} else if (!replot_disabled)
replotrequest();
else
int_warn(NO_CARET, "refresh not possible and replot is disabled");
}
void
restore_prompt()
{
if (interactive) {
#if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE)
# if !defined(MISSING_RL_FORCED_UPDATE_DISPLAY)
rl_forced_update_display();
# else
rl_redisplay();
# endif
#else
fputs(PROMPT, stderr);
fflush(stderr);
#endif
}
}
#endif /* USE_MOUSE */
void
define()
{
int start_token; /* the 1st token in the function definition */
struct udvt_entry *udv;
struct udft_entry *udf;
struct value result;
if (equals(c_token + 1, "(")) {
/* function ! */
int dummy_num = 0;
struct at_type *at_tmp;
char *tmpnam;
char save_dummy[MAX_NUM_VAR][MAX_ID_LEN+1];
memcpy(save_dummy, c_dummy_var, sizeof(save_dummy));
start_token = c_token;
do {
c_token += 2; /* skip to the next dummy */
copy_str(c_dummy_var[dummy_num++], c_token, MAX_ID_LEN);
} while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR));
if (equals(c_token + 1, ","))
int_error(c_token + 2, "function contains too many parameters");
c_token += 3; /* skip (, dummy, ) and = */
if (END_OF_COMMAND)
int_error(c_token, "function definition expected");
udf = dummy_func = add_udf(start_token);
udf->dummy_num = dummy_num;
if ((at_tmp = perm_at()) == (struct at_type *) NULL)
int_error(start_token, "not enough memory for function");
if (udf->at) /* already a dynamic a.t. there */
free_at(udf->at); /* so free it first */
udf->at = at_tmp; /* before re-assigning it. */
memcpy(c_dummy_var, save_dummy, sizeof(save_dummy));
m_capture(&(udf->definition), start_token, c_token - 1);
dummy_func = NULL; /* dont let anyone else use our workspace */
/* Save function definition in a user-accessible variable */
tmpnam = gp_alloc(8+strlen(udf->udf_name), "varname");
strcpy(tmpnam, "GPFUN_");
strcat(tmpnam, udf->udf_name);
fill_gpval_string(tmpnam, udf->definition);
free(tmpnam);
} else {
/* variable ! */
char *varname = gp_input_line + token[c_token].start_index;
if (!strncmp(varname, "GPVAL_", 6)
|| !strncmp(varname, "GPFUN_", 6)
|| !strncmp(varname, "MOUSE_", 6))
int_error(c_token, "Cannot set internal variables GPVAL_ GPFUN_ MOUSE_");
start_token = c_token;
c_token += 2;
const_express(&result);
/* Special handling needed to safely return an array */
if (result.type == ARRAY)
make_array_permanent(&result);
/* If the variable name was previously in use then depending on its
* old type it may have attached memory that needs to be freed.
* Note: supposedly the old variable type cannot be datablock because
* the syntax $name = foo is not accepted so we would not be here.
* However, weird cases like FOO = value($datablock) violate this rule
* and leave FOO as a datablock whose name does not start with $.
*/
udv = add_udv(start_token);
free_value(&udv->udv_value);
udv->udv_value = result;
}
}
void
undefine_command()
{
char key[MAX_ID_LEN+1];
TBOOLEAN wildcard;
c_token++; /* consume the command name */
while (!END_OF_COMMAND) {
/* copy next var name into key */
copy_str(key, c_token, MAX_ID_LEN);
/* Peek ahead - must do this, because a '*' is returned as a
separate token, not as part of the 'key' */
wildcard = equals(c_token+1,"*");
if (wildcard)
c_token++;
/* The '$' starting a data block name is a separate token */
else if (*key == '$')
copy_str(&key[1], ++c_token, MAX_ID_LEN-1);
/* Other strange stuff on command line */
else if (!isletter(c_token))
int_error(c_token, "Not a variable name");
/* This command cannot deal with array elements or functions */
if (equals(c_token+1, "[") || equals(c_token+1, "("))
int_error(c_token, "Cannot undefine function or array element");
/* ignore internal variables */
if (strncmp(key, "GPVAL_", 6) && strncmp(key, "MOUSE_", 6))
del_udv_by_name( key, wildcard );
c_token++;
}
}
static void
command()
{
int i;
for (i = 0; i < MAX_NUM_VAR; i++)
c_dummy_var[i][0] = NUL; /* no dummy variables */
if (is_definition(c_token))
define();
else if (is_array_assignment())
;
else
(*lookup_ftable(&command_ftbl[0],c_token))();
return;
}
/* process the 'raise' or 'lower' command */
void
raise_lower_command(int lower)
{
++c_token;
if (END_OF_COMMAND) {
if (lower) {
#ifdef OS2
pm_lower_terminal_window();
#endif
#ifdef X11
x11_lower_terminal_group();
#endif
#ifdef _WIN32
win_lower_terminal_group();
#endif
#ifdef WXWIDGETS
wxt_lower_terminal_group();
#endif
} else {
#ifdef OS2
pm_raise_terminal_window();
#endif
#ifdef X11
x11_raise_terminal_group();
#endif
#ifdef _WIN32
win_raise_terminal_group();
#endif
#ifdef WXWIDGETS
wxt_raise_terminal_group();
#endif
}
return;
} else {
int number;
int negative = equals(c_token, "-");
if (negative || equals(c_token, "+")) c_token++;
if (!END_OF_COMMAND && isanumber(c_token)) {
number = real_expression();
if (negative)
number = -number;
if (lower) {
#ifdef OS2
pm_lower_terminal_window();
#endif
#ifdef X11
x11_lower_terminal_window(number);
#endif
#ifdef _WIN32
win_lower_terminal_window(number);
#endif
#ifdef WXWIDGETS
wxt_lower_terminal_window(number);
#endif
} else {
#ifdef OS2
pm_raise_terminal_window();
#endif
#ifdef X11
x11_raise_terminal_window(number);
#endif
#ifdef _WIN32
win_raise_terminal_window(number);
#endif
#ifdef WXWIDGETS
wxt_raise_terminal_window(number);
#endif
}
++c_token;
return;
}
}
if (lower)
int_error(c_token, "usage: lower {plot_id}");
else
int_error(c_token, "usage: raise {plot_id}");
}
void
raise_command(void)
{
raise_lower_command(0);
}
void
lower_command(void)
{
raise_lower_command(1);
}
/*
* Arrays are declared using the syntax
* array A[size] { = [ element, element, ... ] }
* array A = [ .., .. ]
* array A = <expression> (only valid if <expression> returns an array)
* where size is an integer and space is reserved for elements A[1] through A[size]
* The size itself is stored in A[0].v.int_val.A
* The list of initial values is optional.
* Any element that is not initialized is set to NOTDEFINED.
*
* Elements in an existing array can be accessed like any other gnuplot variable.
* Each element can be one of INTGR, CMPLX, STRING.
*/
void
array_command()
{
int nsize = 0; /* Size of array when we leave */
int est_size = 0; /* Estimated size */
TBOOLEAN empty_array = FALSE;
struct udvt_entry *array;
struct value *A;
int i;
/* Create or recycle a udv containing an array with the requested name */
if (!isletter(++c_token))
int_error(c_token, "illegal variable name");
array = add_udv(c_token++);
if (equals(c_token, "[")) {
c_token++;
nsize = int_expression();
if (!equals(c_token++,"]"))
int_error(c_token-1, "expecting array[size>0]");
} else if (equals(c_token, "=") && equals(c_token+1, "[")) {
if (equals(c_token+2,"]"))
empty_array = TRUE;
/* Estimate size of array by counting commas in the initializer */
for ( i = c_token+2; i < num_tokens; i++) {
if (equals(i,",") || equals(i,"]"))
est_size++;
if (equals(i,"]"))
break;
}
nsize = est_size;
} else if (equals(c_token, "=")) {
/* array A = <expression> */
struct value a;
int save_token = ++c_token;
const_express(&a);
if (a.type != ARRAY) {
free_value(&a);
int_error(save_token, "not an array expression");
}
make_array_permanent(&a);
array->udv_value = a;
return;
}
if (nsize > 0)
init_array(array, nsize);
else
int_error(c_token-1, "expecting array[size>0]");
/* Element zero can also hold an indicator that this is a colormap */
A = array->udv_value.v.value_array;
if (equals(c_token, "colormap")) {
c_token++;
if (nsize >= 2) /* Need at least 2 entries to calculate range */
A[0].type = COLORMAP_ARRAY;
}
/* Initializer syntax: array A[10] = [x,y,z,,"foo",] */
if (equals(c_token, "=") && equals(c_token+1, "[")) {
int initializers = 0;
c_token += 2;
for (i = 1; i <= nsize; i++) {
if (equals(c_token, "]"))
break;
if (equals(c_token, ",")) {
initializers++;
c_token++;
continue;
}
const_express(&A[i]);
if (A[i].type == ARRAY) {
if (A[i].v.value_array[0].type == TEMP_ARRAY)
gpfree_array(&(A[i]));
A[i].type = NOTDEFINED;
int_error(c_token, "Cannot nest arrays");
}
initializers++;
if (equals(c_token, "]"))
break;
if (equals(c_token, ","))
c_token++;
else
int_error(c_token, "expecting Array[size] = [x,y,...]");
}
c_token++;
/* If the size is determined by the number of initializers */
if (empty_array)
A[0].v.int_val = 0;
else if (A[0].v.int_val == 0)
A[0].v.int_val = initializers;
}
return;
}
/*
* Check for command line beginning with
* Array[<expr>] = <expr>
* This routine is modeled on command.c:define()
*/
TBOOLEAN
is_array_assignment()
{
udvt_entry *udv;
struct value newvalue;
int index;
TBOOLEAN looks_OK = FALSE;
int brackets;
if (!isletter(c_token) || !equals(c_token+1, "["))
return FALSE;
/* There are other legal commands where the 2nd token is [
* e.g. "plot [min:max] foo"
* so we check that the closing ] is immediately followed by =.
*/
for (index=c_token+2, brackets=1; index < num_tokens; index++) {
if (equals(index,";"))
return FALSE;
if (equals(index,"["))
brackets++;
if (equals(index,"]"))
brackets--;
if (brackets == 0) {
if (!equals(index+1,"="))
return FALSE;
looks_OK = TRUE;
break;
}