File: variable.c

package info (click to toggle)
gnuplot 5.0.5%2Bdfsg1-6%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,800 kB
  • ctags: 8,062
  • sloc: ansic: 78,152; cpp: 6,981; makefile: 2,075; sh: 1,343; lisp: 655; perl: 302; awk: 235; pascal: 194; tcl: 88; python: 46
file content (627 lines) | stat: -rw-r--r-- 17,154 bytes parent folder | download | duplicates (5)
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
#ifndef lint
static char *RCSid() { return RCSid("$Id: variable.c,v 1.44 2013/07/02 22:19:09 sfeam Exp $"); }
#endif

/* GNUPLOT - variable.c */

/*[
 * Copyright 1999, 2004   Lars Hecking
 *
 * 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.
]*/

/* The Death of Global Variables - part one. */

#include <string.h>

#include "variable.h"

#include "alloc.h"
#include "command.h"
#include "plot.h"
#include "util.h"
#include "term_api.h"

#define PATHSEP_TO_NUL(arg)			\
do {						\
    char *s = arg;				\
    while ((s = strchr(s, PATHSEP)) != NULL)	\
	*s++ = NUL;				\
} while (0)

#define PRINT_PATHLIST(start, limit)		\
do {						\
    char *s = start;				\
						\
    while (s < limit) {				\
	fprintf(stderr, "\"%s\" ", s);		\
	s += strlen(s) + 1;			\
    }						\
    fputc('\n',stderr);				\
} while (0)

/*
 * char *loadpath_handler (int, char *)
 *
 */
char *
loadpath_handler(int action, char *path)
{
    /* loadpath variable
     * the path elements are '\0' separated (!)
     * this way, reading out loadpath is very
     * easy to implement */
    static char *loadpath;
    /* index pointer, end of loadpath,
     * env section of loadpath, current limit, in that order */
    static char *p, *last, *envptr, *limit;
#ifdef X11
    char *appdir;
#endif

    switch (action) {
    case ACTION_CLEAR:
	/* Clear loadpath, fall through to init */
	FPRINTF((stderr, "Clear loadpath\n"));
	free(loadpath);
	loadpath = p = last = NULL;
	/* HBB 20000726: 'limit' has to be initialized to NULL, too! */
	limit = NULL;
    case ACTION_INIT:
	/* Init loadpath from environment */
	FPRINTF((stderr, "Init loadpath from environment\n"));
	assert(loadpath == NULL);
	if (!loadpath)
	{
	    char *envlib = getenv("GNUPLOT_LIB");
	    if (envlib) {
		int len = strlen(envlib);
		loadpath = gp_strdup(envlib);
		/* point to end of loadpath */
		last = loadpath + len;
		/* convert all PATHSEPs to \0 */
		PATHSEP_TO_NUL(loadpath);
	    }			/* else: NULL = empty */
	}			/* else: already initialised; int_warn (?) */
	/* point to env portion of loadpath */
	envptr = loadpath;
	break;
    case ACTION_SET:
	/* set the loadpath */
	FPRINTF((stderr, "Set loadpath\n"));
	if (path && *path != NUL) {
	    /* length of env portion */
	    size_t elen = last - envptr;
	    size_t plen = strlen(path);
	    if (loadpath && envptr) {
		/* we are prepending a path name; because
		 * realloc() preserves only the contents up
		 * to the minimum of old and new size, we move
		 * the part to be preserved to the beginning
		 * of the string; use memmove() because strings
		 * may overlap */
		memmove(loadpath, envptr, elen + 1);
	    }
	    loadpath = gp_realloc(loadpath, elen + 1 + plen + 1, "expand loadpath");
	    /* now move env part back to the end to make space for
	     * the new path */
	    memmove(loadpath + plen + 1, loadpath, elen + 1);
	    strcpy(loadpath, path);
	    /* separate new path(s) and env path(s) */
	    loadpath[plen] = PATHSEP;
	    /* adjust pointer to env part and last */
	    envptr = &loadpath[plen+1];
	    last = envptr + elen;
	    PATHSEP_TO_NUL(loadpath);
	}			/* else: NULL = empty */
	break;
    case ACTION_SHOW:
	/* print the current, full loadpath */
	FPRINTF((stderr, "Show loadpath\n"));
	if (loadpath) {
	    fputs("\tloadpath is ", stderr);
	    PRINT_PATHLIST(loadpath, envptr);
	    if (envptr) {
		/* env part */
		fputs("\tloadpath from GNUPLOT_LIB is ", stderr);
		PRINT_PATHLIST(envptr, last);
	    }
	} else
	    fputs("\tloadpath is empty\n", stderr);
#ifdef GNUPLOT_SHARE_DIR
	fprintf(stderr,"\tgnuplotrc is read from %s\n",GNUPLOT_SHARE_DIR);
#endif
#ifdef X11
	if ((appdir = getenv("XAPPLRESDIR"))) {
	    fprintf(stderr,"\tenvironmental path for X11 application defaults: \"%s\"\n",
		appdir);
	}
#ifdef XAPPLRESDIR
	else {
	    fprintf(stderr,"\tno XAPPLRESDIR found in the environment,\n");
	    fprintf(stderr,"\t    falling back to \"%s\"\n", XAPPLRESDIR);
	}
#endif
#endif
	break;
    case ACTION_SAVE:
	/* we don't save the load path taken from the
	 * environment, so don't go beyond envptr when
	 * extracting the path elements
	 */
	limit = envptr;
    case ACTION_GET:
	/* subsequent calls to get_loadpath() return all
	 * elements of the loadpath until exhausted
	 */
	FPRINTF((stderr, "Get loadpath\n"));
	if (!loadpath)
	    return NULL;
	if (!p) {
	    /* init section */
	    p = loadpath;
	    if (!limit)
		limit = last;
	} else {
	    /* skip over '\0' */
	    p += strlen(p) + 1;
	}
	if (p >= limit) 
	    limit = p = NULL;
	return p;
	break;
    case ACTION_NULL:
	/* just return */
    default:
	break;
    }

    /* should always be ignored - points to the
     * first path in the list */
    return loadpath;

}



struct path_table {
    const char *dir;
};

/* Yet, no special font paths for these operating systems:
 * MSDOS, NeXT, ultrix, VMS, _IBMR2, alliant
 *
 * Environmental variables are written as $(name).
 * Commands are written as $`command`.
 */

#if defined(OS2) && !defined(FONTPATHSET)
#  define FONTPATHSET
static const struct path_table fontpath_tbl[] =
{
    { "$(BOOTDIR)/PSFONTS" },
    /* X11 */
    { "$(X11ROOT)/X11R6/lib/X11/fonts/Type1" },
    { NULL }
};
#endif

#if defined(_Windows) && !defined(FONTPATHSET)
#  define FONTPATHSET
static const struct path_table fontpath_tbl[] =
{
    { "$(windir)\\fonts" },
    /* Ghostscript */
    { "c:\\gs\\fonts" },
    /* X11 */
    { "$(CYGWIN_ROOT)\\usr\\X11R6\\lib\\X11\\fonts\\Type1" },
#ifdef HAVE_KPSEXPAND
    /* fpTeX */
    { "$`kpsewhich -expand-path=$HOMETEXMF`\\fonts\\type1!" },
    { "$`kpsewhich -expand-path=$TEXMFLOCAL`\\fonts\\type1!" },
    { "$`kpsewhich -expand-path=$TEXMFMAIN`\\fonts\\type1!" },
    { "$`kpsewhich -expand-path=$TEXMFDIST`\\fonts\\type1!" },
#endif
    { NULL }
};
#endif

#if defined(__APPLE__) && !defined(FONTPATHSET)
#  define FONTPATHSET
static const struct path_table fontpath_tbl[] =
{
    { "/System/Library/Fonts!" },
    { "/Library/Fonts!" },
    { "$(HOME)/Library/Fonts!" },
    { NULL }
};
#endif

#if defined(VMS) && !defined(FONTPATHSET)
#  define FONTPATHSET
static const struct path_table fontpath_tbl[] =
{
    { "SYS$COMMON:[SYSFONT]!" },
    { NULL }
};
#endif

/* Fallback: Should work for unix */
#ifndef FONTPATHSET
static const struct path_table fontpath_tbl[] =
{
#ifdef HAVE_KPSEXPAND
    /* teTeX or TeXLive */
    { "$`kpsexpand '$HOMETEXMF'`/fonts/type1!" },
    { "$`kpsexpand '$TEXMFLOCAL'`/fonts/type1!" },
    { "$`kpsexpand '$TEXMFMAIN'`/fonts/type1!" },
    { "$`kpsexpand '$TEXMFDIST'`/fonts/type1!" },
#endif
    /* Linux paths */
    { "/usr/X11R6/lib/X11/fonts/Type1" },
    { "/usr/X11R6/lib/X11/fonts/truetype" },
    /* HP-UX */
    { "/usr/lib/X11/fonts!"},
    /* Ghostscript */
    { "/usr/share/ghostscript/fonts" },
    { "/usr/local/share/ghostscript/fonts" },
    { NULL }
};
#endif

#undef FONTPATHSET

static TBOOLEAN fontpath_init_done = FALSE;

/*
 * char *fontpath_handler (int, char *)
 *
 */
char *
fontpath_handler(int action, char *path)
{
    /* fontpath variable
     * the path elements are '\0' separated (!)
     * this way, reading out fontpath is very
     * easy to implement */
    static char *fontpath;
    /* index pointer, end of fontpath,
     * env section of fontpath, current limit, in that order */
    static char *p, *last, *envptr, *limit;

    if (!fontpath_init_done) {
	fontpath_init_done = TRUE;
	init_fontpath();
    }

    switch (action) {
    case ACTION_CLEAR:
	/* Clear fontpath, fall through to init */
	FPRINTF((stderr, "Clear fontpath\n"));
	free(fontpath);
	fontpath = p = last = NULL;
	/* HBB 20000726: 'limit' has to be initialized to NULL, too! */
	limit = NULL;
    case ACTION_INIT:
	/* Init fontpath from environment */
	FPRINTF((stderr, "Init fontpath from environment\n"));
	assert(fontpath == NULL);
	if (!fontpath)
	{
	    char *envlib = getenv("GNUPLOT_FONTPATH");
	    if (envlib) {
		/* get paths from environment */
		int len = strlen(envlib);
		fontpath = gp_strdup(envlib);
		/* point to end of fontpath */
		last = fontpath + len;
		/* convert all PATHSEPs to \0 */
		PATHSEP_TO_NUL(fontpath);
	    }
#if defined(HAVE_DIRENT_H) || defined(_Windows)
	    else {
		/* set hardcoded paths */
		const struct path_table *curr_fontpath = fontpath_tbl;

		while (curr_fontpath->dir) {
		    char *currdir = NULL;
		    char *envbeg = NULL;
#  if defined(PIPES)
		    char *cmdbeg = NULL;
#  endif
		    TBOOLEAN subdirs = FALSE;

		    currdir = gp_strdup( curr_fontpath->dir );

		    while ( (envbeg=strstr(currdir, "$("))
#  if defined(PIPES)
			    || (cmdbeg=strstr( currdir, "$`" ))
#  endif
			    ) {
			/* Read environment variables */
			if (envbeg) {
			    char *tmpdir = NULL;
			    char *envend = NULL, *envval = NULL;
			    unsigned int envlen;
			    envend = strchr(envbeg+2,')');
			    envend[0] = '\0';
			    envval = getenv(envbeg+2);
			    envend[0] = ')';
			    envlen = envval ? strlen(envval) : 0;
			    tmpdir = gp_alloc(strlen(currdir)+envlen
					      +envbeg-envend+1,
					      "expand fontpath");
			    strncpy(tmpdir,currdir,envbeg-currdir);
			    if (envval)
				strcpy(tmpdir+(envbeg-currdir),envval);
			    strcpy(tmpdir+(envbeg-currdir+envlen), envend+1);

			    free(currdir);
			    currdir = tmpdir;
			}
#  if defined(PIPES)
			/* Read environment variables */
			else if (cmdbeg) {
			    char *tmpdir = NULL;
			    char *envend = NULL;
			    char envval[256];
			    unsigned int envlen;
			    FILE *fcmd;
			    envend = strchr(cmdbeg+2,'`');
			    envend[0] = '\0';
			    restrict_popen();
			    fcmd = popen(cmdbeg+2,"r");
			    if (fcmd) {
				fgets(envval,255,fcmd);
				if (envval[strlen(envval)-1]=='\n')
				    envval[strlen(envval)-1]='\0';
				pclose(fcmd);
			    }
			    envend[0] = '`';
			    envlen = strlen(envval);
			    tmpdir = gp_alloc(strlen(currdir)+envlen
					      +cmdbeg-envend+1,
					      "expand fontpath");
			    strncpy(tmpdir,currdir,cmdbeg-currdir);
			    if (*envval)
				strcpy(tmpdir+(cmdbeg-currdir),envval);
			    strcpy(tmpdir+(cmdbeg-currdir+envlen), envend+1);

			    free(currdir);
			    currdir = tmpdir;
			}
#  endif
		    }

		    if ( currdir[strlen(currdir)-1] == '!' ) {
			/* search subdirectories */
			/* delete ! from directory name */
			currdir[strlen(currdir)-1] = '\0';
			subdirs = TRUE;
		    }

		    if ( existdir( currdir ) ) {
			size_t plen;
			if ( subdirs )
			    /* add ! to directory name again */
			    currdir[strlen(currdir)] = '!';
			plen = strlen(currdir);
			if (fontpath) {
			    size_t elen = strlen(fontpath);
			    fontpath = gp_realloc(fontpath,
						  elen + 1 + plen + 1,
						  "expand fontpath");
			    last = fontpath+elen;
			    *last = PATHSEP;
			    ++last;
			    *last = '\0';
			} else {
			    fontpath = gp_alloc(plen + 1,
						"expand fontpath");
			    last = fontpath;
			}

			strcpy(last, currdir );
			last += plen;
		    }
		    curr_fontpath++;
		    if (currdir) {
			free(currdir);
			currdir = NULL;
		    }
		}
		/* convert all PATHSEPs to \0 */
		if (fontpath)
		    PATHSEP_TO_NUL(fontpath);
	    }
#endif /* HAVE_DIRENT_H */

	}			/* else: already initialised; int_warn (?) */
	/* point to env portion of fontpath */
	envptr = fontpath;
	break;
    case ACTION_SET:
	/* set the fontpath */
	FPRINTF((stderr, "Set fontpath\n"));
	if (path && *path != NUL) {
	    /* length of env portion */
	    size_t elen = last - envptr;
	    size_t plen = strlen(path);
	    if (fontpath && envptr) {
		/* we are prepending a path name; because
		 * realloc() preserves only the contents up
		 * to the minimum of old and new size, we move
		 * the part to be preserved to the beginning
		 * of the string; use memmove() because strings
		 * may overlap */
		memmove(fontpath, envptr, elen + 1);
	    }
	    fontpath = gp_realloc(fontpath, elen + 1 + plen + 1, "expand fontpath");
	    /* now move env part back to the end to make space for
	     * the new path */
	    memmove(fontpath + plen + 1, fontpath, elen + 1);
	    strcpy(fontpath, path);
	    /* separate new path(s) and env path(s) */
	    fontpath[plen] = PATHSEP;
	    /* adjust pointer to env part and last */
	    envptr = &fontpath[plen+1];
	    last = envptr + elen;
	    PATHSEP_TO_NUL(fontpath);
	}			/* else: NULL = empty */
	break;
    case ACTION_SHOW:
	/* print the current, full fontpath */
	FPRINTF((stderr, "Show fontpath\n"));
	if (fontpath) {
	    fputs("\tfontpath is ", stderr);
	    PRINT_PATHLIST(fontpath, envptr);
	    if (envptr) {
		/* env part */
		fputs("\tsystem fontpath is ", stderr);
		PRINT_PATHLIST(envptr, last);
	    }
	} else
	    fputs("\tfontpath is empty\n", stderr);
	break;
    case ACTION_SAVE:
	/* we don't save the font path taken from the
	 * environment, so don't go beyond envptr when
	 * extracting the path elements
	 */
	limit = envptr;
    case ACTION_GET:
	/* subsequent calls to get_fontpath() return all
	 * elements of the fontpath until exhausted
	 */
	FPRINTF((stderr, "Get fontpath\n"));
	if (!fontpath)
	    return NULL;
	if (!p) {
	    /* init section */
	    p = fontpath;
	    if (!limit)
		limit = last;
	} else {
	    /* skip over '\0' */
	    p += strlen(p) + 1;
	}
	if (p >= limit)
	    limit = p = NULL;
	return p;
    case ACTION_NULL:
	/* just return */
    default:
	break;
    }

    /* should always be ignored - points to the
     * first path in the list */
    return fontpath;

}

/* not set or shown directly, but controlled by 'set locale'
 * defined in national.h
 */

char full_month_names[12][32] =
{ FMON01, FMON02, FMON03, FMON04, FMON05, FMON06, FMON07, FMON08, FMON09, FMON10, FMON11, FMON12 };
char abbrev_month_names[12][8] =
{ AMON01, AMON02, AMON03, AMON04, AMON05, AMON06, AMON07, AMON08, AMON09, AMON10, AMON11, AMON12 };

char full_day_names[7][32] =
{ FDAY0, FDAY1, FDAY2, FDAY3, FDAY4, FDAY5, FDAY6 };
char abbrev_day_names[7][8] =
{ ADAY0, ADAY1, ADAY2, ADAY3, ADAY4, ADAY5, ADAY6 };

char *
locale_handler(int action, char *newlocale)
{
    struct tm tm;
    int i;

    switch(action) {
    case ACTION_CLEAR:
    case ACTION_INIT:
	free(current_locale);
#ifdef HAVE_LOCALE_H
	setlocale(LC_TIME, "");
	setlocale(LC_CTYPE, "");
	current_locale = gp_strdup(setlocale(LC_TIME,NULL));
#else
	current_locale = gp_strdup(INITIAL_LOCALE);
#endif
	break;

    case ACTION_SET:
#ifdef HAVE_LOCALE_H
	if (setlocale(LC_TIME, newlocale)) {
	    free(current_locale);
	    current_locale = gp_strdup(setlocale(LC_TIME,NULL));
	} else {
	    int_error(c_token, "Locale not available");
	}

	/* we can do a *lot* better than this ; eg use system functions
	 * where available; create values on first use, etc
	 */
	memset(&tm, 0, sizeof(struct tm));
	for (i = 0; i < 7; ++i) {
	    tm.tm_wday = i;		/* hope this enough */
	    strftime(full_day_names[i], sizeof(full_day_names[i]), "%A", &tm);
	    strftime(abbrev_day_names[i], sizeof(abbrev_day_names[i]), "%a", &tm);
	}
	for (i = 0; i < 12; ++i) {
	    tm.tm_mon = i;		/* hope this enough */
	    strftime(full_month_names[i], sizeof(full_month_names[i]), "%B", &tm);
	    strftime(abbrev_month_names[i], sizeof(abbrev_month_names[i]), "%b", &tm);
	}
#else
	current_locale = gp_realloc(current_locale, strlen(newlocale) + 1, "locale");
	strcpy(current_locale, newlocale);
#endif /* HAVE_LOCALE_H */
	break;

    case ACTION_SHOW:
#ifdef HAVE_LOCALE_H
	fprintf(stderr, "\tgnuplot LC_CTYPE   %s\n", setlocale(LC_CTYPE,NULL));
	fprintf(stderr, "\tgnuplot encoding   %s\n", encoding_names[encoding]);
	fprintf(stderr, "\tgnuplot LC_TIME    %s\n", setlocale(LC_TIME,NULL));
	fprintf(stderr, "\tgnuplot LC_NUMERIC %s\n", numeric_locale ? numeric_locale : "C");
#else
	fprintf(stderr, "\tlocale is \"%s\"\n", current_locale);
#endif
	break;
    
    case ACTION_GET:
    default:
	break;
    }

    return current_locale;
}