forked from gnuplot/gnuplot-old
/
util.c
654 lines (559 loc) · 14.7 KB
/
util.c
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
#ifndef lint
static char *RCSid = "$Id: util.c,v 1.9 1998/11/06 22:07:07 lhecking Exp $";
#endif
/* GNUPLOT - util.c */
/*[
* Copyright 1986 - 1993, 1998 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.
]*/
#include "plot.h"
#include "setshow.h" /* for month names etc */
/* TRUE if command just typed; becomes FALSE whenever we
* send some other output to screen. If FALSE, the command line
* will be echoed to the screen before the ^ error message.
*/
TBOOLEAN screen_ok;
static char *num_to_str __PROTO((double r));
static void parse_esc __PROTO((char *instr));
/*
* chr_in_str() compares the characters in the string of token number t_num
* with c, and returns TRUE if a match was found.
*/
int chr_in_str(t_num, c)
int t_num;
int c;
{
register int i;
if (!token[t_num].is_token)
return (FALSE); /* must be a value--can't be equal */
for (i = 0; i < token[t_num].length; i++) {
if (input_line[token[t_num].start_index + i] == c)
return (TRUE);
}
return FALSE;
}
/*
* equals() compares string value of token number t_num with str[], and
* returns TRUE if they are identical.
*/
int equals(t_num, str)
int t_num;
char *str;
{
register int i;
if (!token[t_num].is_token)
return (FALSE); /* must be a value--can't be equal */
for (i = 0; i < token[t_num].length; i++) {
if (input_line[token[t_num].start_index + i] != str[i])
return (FALSE);
}
/* now return TRUE if at end of str[], FALSE if not */
return (str[i] == NUL);
}
/*
* almost_equals() compares string value of token number t_num with str[], and
* returns TRUE if they are identical up to the first $ in str[].
*/
int almost_equals(t_num, str)
int t_num;
char *str;
{
register int i;
register int after = 0;
register int start = token[t_num].start_index;
register int length = token[t_num].length;
if (!token[t_num].is_token)
return (FALSE); /* must be a value--can't be equal */
for (i = 0; i < length + after; i++) {
if (str[i] != input_line[start + i]) {
if (str[i] != '$')
return (FALSE);
else {
after = 1;
start--; /* back up token ptr */
}
}
}
/* i now beyond end of token string */
return (after || str[i] == '$' || str[i] == NUL);
}
int isstring(t_num)
int t_num;
{
return (token[t_num].is_token &&
(input_line[token[t_num].start_index] == '\'' ||
input_line[token[t_num].start_index] == '"'));
}
int isanumber(t_num)
int t_num;
{
return (!token[t_num].is_token);
}
int isletter(t_num)
int t_num;
{
return (token[t_num].is_token &&
((isalpha((int)input_line[token[t_num].start_index])) ||
(input_line[token[t_num].start_index] == '_')));
}
/*
* is_definition() returns TRUE if the next tokens are of the form
* identifier =
* -or-
* identifier ( identifer {,identifier} ) =
*/
int is_definition(t_num)
int t_num;
{
/* variable? */
if (isletter(t_num) && equals(t_num + 1, "="))
return 1;
/* function? */
/* look for dummy variables */
if (isletter(t_num) && equals(t_num + 1, "(") && isletter(t_num + 2)) {
t_num += 3; /* point past first dummy */
while (equals(t_num, ",")) {
if (!isletter(++t_num))
return 0;
t_num += 1;
}
return (equals(t_num, ")") && equals(t_num + 1, "="));
}
/* neither */
return 0;
}
/*
* copy_str() copies the string in token number t_num into str, appending
* a null. No more than max chars are copied (including \0).
*/
void copy_str(str, t_num, max)
char str[];
int t_num;
int max;
{
register int i = 0;
register int start = token[t_num].start_index;
register int count;
if ((count = token[t_num].length) >= max) {
count = max - 1;
FPRINTF((stderr, "str buffer overflow in copy_str"));
}
do {
str[i++] = input_line[start++];
} while (i != count);
str[i] = NUL;
}
/* length of token string */
int token_len(t_num)
int t_num;
{
return (token[t_num].length);
}
/*
* quote_str() does the same thing as copy_str, except it ignores the
* quotes at both ends. This seems redundant, but is done for
* efficency.
*/
void quote_str(str, t_num, max)
char str[];
int t_num;
int max;
{
register int i = 0;
register int start = token[t_num].start_index + 1;
register int count;
if ((count = token[t_num].length - 2) >= max) {
count = max - 1;
FPRINTF((stderr, "str buffer overflow in quote_str"));
}
if (count > 0) {
do {
str[i++] = input_line[start++];
} while (i != count);
}
str[i] = NUL;
/* convert \t and \nnn (octal) to char if in double quotes */
if (input_line[token[t_num].start_index] == '"')
parse_esc(str);
}
/*
* capture() copies into str[] the part of input_line[] which lies between
* the begining of token[start] and end of token[end].
*/
void capture(str, start, end, max)
char str[];
int start, end;
int max;
{
register int i, e;
e = token[end].start_index + token[end].length;
if (e - token[start].start_index >= max) {
e = token[start].start_index + max - 1;
FPRINTF((stderr, "str buffer overflow in capture"));
}
for (i = token[start].start_index; i < e && input_line[i] != NUL; i++)
*str++ = input_line[i];
*str = NUL;
}
/*
* m_capture() is similar to capture(), but it mallocs storage for the
* string.
*/
void m_capture(str, start, end)
char **str;
int start, end;
{
register int i, e;
register char *s;
if (*str) /* previous pointer to malloc'd memory there */
free(*str);
e = token[end].start_index + token[end].length;
*str = gp_alloc((unsigned long) (e - token[start].start_index + 1), "string");
s = *str;
for (i = token[start].start_index; i < e && input_line[i] != NUL; i++)
*s++ = input_line[i];
*s = NUL;
}
/*
* m_quote_capture() is similar to m_capture(), but it removes
quotes from either end if the string.
*/
void m_quote_capture(str, start, end)
char **str;
int start, end;
{
register int i, e, escflag = 0;
register char *s;
if (*str) /* previous pointer to malloc'd memory there */
free(*str);
e = token[end].start_index + token[end].length - 1;
*str = gp_alloc((unsigned long) (e - token[start].start_index + 1), "string");
s = *str;
for (i = token[start].start_index + 1; i < e && input_line[i] != NUL; i++)
if ((*s++ = input_line[i]) == '\\') ++escflag;
*s = NUL;
if (escflag) parse_esc(*str);
}
void convert(val_ptr, t_num)
struct value *val_ptr;
int t_num;
{
*val_ptr = token[t_num].l_val;
}
static char *num_to_str(r)
double r;
{
static int i = 0;
static char s[4][25];
int j = i++;
if (i > 3)
i = 0;
sprintf(s[j], "%.15g", r);
if (strchr(s[j], '.') == NULL &&
strchr(s[j], 'e') == NULL &&
strchr(s[j], 'E') == NULL)
strcat(s[j], ".0");
return s[j];
}
void disp_value(fp, val)
FILE *fp;
struct value *val;
{
switch (val->type) {
case INTGR:
fprintf(fp, "%d", val->v.int_val);
break;
case CMPLX:
if (val->v.cmplx_val.imag != 0.0)
fprintf(fp, "{%s, %s}",
num_to_str(val->v.cmplx_val.real),
num_to_str(val->v.cmplx_val.imag));
else
fprintf(fp, "%s",
num_to_str(val->v.cmplx_val.real));
break;
default:
int_error("unknown type in disp_value()", NO_CARET);
}
}
double real(val) /* returns the real part of val */
struct value *val;
{
switch (val->type) {
case INTGR:
return ((double) val->v.int_val);
case CMPLX:
return (val->v.cmplx_val.real);
}
int_error("unknown type in real()", NO_CARET);
/* NOTREACHED */
return ((double) 0.0);
}
double imag(val) /* returns the imag part of val */
struct value *val;
{
switch (val->type) {
case INTGR:
return (0.0);
case CMPLX:
return (val->v.cmplx_val.imag);
}
int_error("unknown type in imag()", NO_CARET);
/* NOTREACHED */
return ((double) 0.0);
}
double magnitude(val) /* returns the magnitude of val */
struct value *val;
{
switch (val->type) {
case INTGR:
return ((double) abs(val->v.int_val));
case CMPLX:
return (sqrt(val->v.cmplx_val.real *
val->v.cmplx_val.real +
val->v.cmplx_val.imag *
val->v.cmplx_val.imag));
}
int_error("unknown type in magnitude()", NO_CARET);
/* NOTREACHED */
return ((double) 0.0);
}
double angle(val) /* returns the angle of val */
struct value *val;
{
switch (val->type) {
case INTGR:
return ((val->v.int_val >= 0) ? 0.0 : Pi);
case CMPLX:
if (val->v.cmplx_val.imag == 0.0) {
if (val->v.cmplx_val.real >= 0.0)
return (0.0);
else
return (Pi);
}
return (atan2(val->v.cmplx_val.imag,
val->v.cmplx_val.real));
}
int_error("unknown type in angle()", NO_CARET);
/* NOTREACHED */
return ((double) 0.0);
}
struct value *
Gcomplex(a, realpart, imagpart)
struct value *a;
double realpart, imagpart;
{
a->type = CMPLX;
a->v.cmplx_val.real = realpart;
a->v.cmplx_val.imag = imagpart;
return (a);
}
struct value *
Ginteger(a, i)
struct value *a;
int i;
{
a->type = INTGR;
a->v.int_val = i;
return (a);
}
void os_error(str, t_num)
char str[];
int t_num;
{
#ifdef VMS
static status[2] =
{1, 0}; /* 1 is count of error msgs */
#endif /* VMS */
register int i;
/* reprint line if screen has been written to */
if (t_num != NO_CARET) { /* put caret under error */
if (!screen_ok)
fprintf(stderr, "\n%s%s\n", PROMPT, input_line);
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
for (i = 0; i < token[t_num].start_index; i++) {
(void) putc((input_line[i] == '\t') ? '\t' : ' ', stderr);
}
(void) putc('^', stderr);
(void) putc('\n', stderr);
}
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
fprintf(stderr, "%s\n", str);
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
if (!interactive) {
if (infile_name != NULL)
fprintf(stderr, "\"%s\", line %d: ", infile_name, inline_num);
else
fprintf(stderr, "line %d: ", inline_num);
}
#ifdef VMS
status[1] = vaxc$errno;
sys$putmsg(status);
(void) putc('\n', stderr);
#else /* VMS */
fprintf(stderr, "(%s)\n\n", strerror(errno));
#endif /* VMS */
bail_to_command_line();
}
void int_error(str, t_num)
char str[];
int t_num;
{
register int i;
/* reprint line if screen has been written to */
if (t_num != NO_CARET) { /* put caret under error */
if (!screen_ok)
fprintf(stderr, "\n%s%s\n", PROMPT, input_line);
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
for (i = 0; i < token[t_num].start_index; i++) {
(void) putc((input_line[i] == '\t') ? '\t' : ' ', stderr);
}
(void) putc('^', stderr);
(void) putc('\n', stderr);
}
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
if (!interactive) {
if (infile_name != NULL)
fprintf(stderr, "\"%s\", line %d: ", infile_name, inline_num);
else
fprintf(stderr, "line %d: ", inline_num);
}
fprintf(stderr, "%s\n\n", str);
bail_to_command_line();
}
/* Warn without bailing out to command line. Not a user error */
void int_warn(str, t_num)
char str[];
int t_num;
{
register int i;
/* reprint line if screen has been written to */
if (t_num != NO_CARET) { /* put caret under error */
if (!screen_ok)
fprintf(stderr, "\n%s%s\n", PROMPT, input_line);
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
for (i = 0; i < token[t_num].start_index; i++) {
(void) putc((input_line[i] == '\t') ? '\t' : ' ', stderr);
}
(void) putc('^', stderr);
(void) putc('\n', stderr);
}
for (i = 0; i < sizeof(PROMPT) - 1; i++)
(void) putc(' ', stderr);
if (!interactive) {
if (infile_name != NULL)
fprintf(stderr, "\"%s\", line %d: ", infile_name, inline_num);
else
fprintf(stderr, "line %d: ", inline_num);
}
fprintf(stderr, "warning: %s\n", str);
} /* int_warn */
/* Lower-case the given string (DFK) */
/* Done in place. */
void lower_case(s)
char *s;
{
register char *p = s;
while (*p != NUL) {
if (isupper((int)*p))
*p = tolower(*p);
p++;
}
}
/* Squash spaces in the given string (DFK) */
/* That is, reduce all multiple white-space chars to single spaces */
/* Done in place. */
void squash_spaces(s)
char *s;
{
register char *r = s; /* reading point */
register char *w = s; /* writing point */
TBOOLEAN space = FALSE; /* TRUE if we've already copied a space */
for (w = r = s; *r != NUL; r++) {
if (isspace((int)*r)) {
/* white space; only copy if we haven't just copied a space */
if (!space) {
space = TRUE;
*w++ = ' ';
} /* else ignore multiple spaces */
} else {
/* non-space character; copy it and clear flag */
*w++ = *r;
space = FALSE;
}
}
*w = NUL; /* null terminate string */
}
static void parse_esc(instr)
char *instr;
{
char *s = instr, *t = instr;
/* the string will always get shorter, so we can do the
* conversion in situ
*/
while (*s != NUL) {
if (*s == '\\') {
s++;
if (*s == '\\') {
*t++ = '\\';
s++;
} else if (*s == 'n') {
*t++ = '\n';
s++;
} else if (*s == 'r') {
*t++ = '\r';
s++;
} else if (*s == 't') {
*t++ = '\t';
s++;
} else if (*s == '\"') {
*t++ = '\"';
s++;
} else if (*s >= '0' && *s <= '7') {
int i, n;
if (sscanf(s, "%o%n", &i, &n) > 0) {
*t++ = i;
s += n;
} else {
/* int_error("illegal octal number ", c_token); */
*t++ = '\\';
*t++ = *s++;
}
}
} else {
*t++ = *s++;
}
}
*t = NUL;
}