File: pm.trm

package info (click to toggle)
gnuplot 3.7.1p1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,808 kB
  • ctags: 4,418
  • sloc: ansic: 43,279; lisp: 661; makefile: 647; asm: 539; sh: 416; objc: 379; csh: 297; pascal: 194; perl: 138
file content (627 lines) | stat: -rw-r--r-- 16,645 bytes parent folder | download | duplicates (2)
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
/*
 * $Id: pm.trm,v 1.13 1998/12/23 21:53:26 lhecking Exp $
 */

/* GNUPLOT - pm.trm */

/*[
 * Copyright 1992, 1993, 1998
 *
 * 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.
]*/

/*
 *    pm.trm  --- inboard terminal driver for Presentation Manager
 *            --- after X-11 driver, by R.W.Fearick 31/1/92.
 *    v1.1 11/8/92 -- speed things up
 */

#include "driver.h"

#ifdef TERM_REGISTER
register_term(pm)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void PM_init __PROTO((void));
TERM_PUBLIC void PM_options __PROTO((void));
TERM_PUBLIC void PM_reset __PROTO((void));
TERM_PUBLIC void PM_text __PROTO((void));
TERM_PUBLIC void PM_graphics __PROTO((void));
TERM_PUBLIC void PM_linetype __PROTO((int lt));
TERM_PUBLIC void PM_move __PROTO((unsigned int x, unsigned int y));
TERM_PUBLIC void PM_vector __PROTO((unsigned int x, unsigned int y));
TERM_PUBLIC int PM_text_angle __PROTO((int ang));
TERM_PUBLIC void PM_put_text __PROTO((unsigned int x, unsigned int y, char *str));
TERM_PUBLIC int PM_justify_text __PROTO((enum JUSTIFY mode));
TERM_PUBLIC void PM_point __PROTO((unsigned int x, unsigned int y, int number));
TERM_PUBLIC void PM_suspend __PROTO((void));
TERM_PUBLIC void PM_resume __PROTO((void));
TERM_PUBLIC void PM_linewidth __PROTO((double linewidth));

/* define PM world coordinate limits */

#define PM_XMAX 19500
#define PM_YMAX 12500

/* approximations for typical font/screen sizes */
#define PM_VCHAR (550)
#define PM_HCHAR (220)
#define PM_VTIC (200)
#define PM_HTIC (200)
#endif

#ifdef TERM_BODY

#include <stdio.h>
#include <process.h>
#include <io.h>
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#define INCL_DOSMISC
#define INCL_DOSMODULEMGR
#include <os2.h>


/* graphics commands */
#define SET_GRAPHICS    'G'
#define SET_TEXT        'E'
#define SET_LINE        'L'
#define SET_LINEWIDTH   'W'
#define SET_ANGLE       'A'
#define SET_JUSTIFY     'J'
#define SET_POINTMODE   'D'
#define SET_FONT        'F'
#define SET_OPTIONS     'O'
#define GR_QUERY        'Q'
#define GR_SUSPEND      'E'	/*'s' */
#define GR_RESUME       'r'
#define GR_MOVE         'M'
#define GR_DRAW         'V'
#define GR_RESET        'R'
#define GR_TEXT         'T'
#define GR_PAUSE        'P'
#define GR_HELP         'H'
#define PM_nopts 1

/* path for pm program */
static char PM_path[256] = "";
/* track mode to avoid redraw after hitting break */
static int PM_mode = 0;
static HEV hev;
static int PM_termmode = 0;
static int PM_must_reset_opts = FALSE;
static int PM_must_abort = 0;

static char PM_opts[256] = "";
static int PM_optargs = 0;
static int PM_plot_number = 0;
static char PM_term_title[128] = "";

FILE *fopen();
static FILE *PM_pipe = NULL;
static FILE *PM_savepipe = NULL;

int PM_pause __PROTO((char *str));
void PM_setup(void);
static void PM_reset_opts(void);
static void PM_query(void);
static void PM_make_servername(char *);
static void PM_abortplot();

TERM_PUBLIC void PM_init()
{
    static char buffer[1024];
    int pid;
    int rc;
    int spawnmode;
    PPIB pib;
    PTIB tib;
    char semname[32];
    char pipename[32];
    char tempname[32];

    term_force_init = TRUE;
    if (PM_savepipe != NULL && PM_termmode == 0) {
	PM_pipe = PM_savepipe;
    }
    if ((PM_pipe == NULL) && (PM_termmode & 2)) {
	/* check if term is running */
	PM_make_servername(tempname);
	strcpy(pipename, "\\pipe\\");
	strcat(pipename, tempname);
//        sprintf( pipename, "\\pipe\\gpServ%d", PM_plot_number ) ;
	DosGetInfoBlocks(&tib, &pib);
	PM_pipe = fopen(pipename, "r+b");
	if (PM_pipe != NULL) {
	    setvbuf(PM_pipe, buffer, _IOFBF, 1024);
	    pid = pib->pib_ulpid;
	    fwrite(&pid, 1, 4, PM_pipe);
	    fflush(PM_pipe);
	    /* set new options */
//            PM_reset_opts() ;
	}
    }
    /* else we start up term here */
    if (PM_pipe == NULL) {
	if (PM_termmode & 2) {
	    PM_make_servername(tempname);
//            sprintf( tempname, "gpServ%d", PM_plot_number ) ;
	} else {
	    static int gpid = 0;
	    gpid++;
	    sprintf(tempname, "gp%X%d", getpid(), gpid);
	}
	strcpy(semname, "\\sem32\\");
	strcpy(pipename, "\\pipe\\");
	strcat(semname, tempname);
	strcat(pipename, tempname);
	strcat(PM_path, "\\gnupmdrv.exe");
	rc = access(PM_path, 0);
	/* find exe file */
	if (rc != 0)
	    rc = DosSearchPath(0x0002,	/* search GNUPLOT environment */
			       "GNUPLOT",
			       "gnupmdrv.exe",
			       PM_path,
			       256);

	if (rc != 0)
	    rc = DosSearchPath(0x0003,	/* then try current directory & path */
			       "PATH",
			       "gnupmdrv.exe",
			       PM_path,
			       256);
	if (rc != 0) {
	    fputs("Cannot find gnupmdrv.exe !\n", stderr);
	    exit(3);
	}
	rc = DosCreateEventSem(semname, &hev, 1, 0);
	if (rc != 0) {
	    fputs("Cannot create semaphore !\n", stderr);
	    exit(3);
	}
	spawnmode = P_SESSION | P_DEFAULT;
	if (PM_optargs != 0)
	    spawnmode |= P_UNRELATED;
	pid = spawnl(spawnmode, PM_path, "GnuplotPM", tempname, PM_opts, NULL);
	if (pid == -1) {
	    fputs("Cannot spawn gnupmdrv.exe !\n", stderr);
	    exit(3);
	}
	DosGetInfoBlocks(&tib, &pib);
	DosWaitEventSem(hev, 10000);
	DosCloseEventSem(hev);
	PM_pipe = fopen(pipename, "r+b");
	if (PM_pipe == NULL) {
	    fputs("Cannot open pipe to gnupmdrv.exe !\n", stderr);
	    exit(3);
	} else if (PM_termmode == 0)
	    PM_savepipe = PM_pipe;
	setvbuf(PM_pipe, buffer, _IOFBF, 1024);
	pid = pib->pib_ulpid;
	fwrite(&pid, 1, 4, PM_pipe);
	fflush(PM_pipe);
    } else {
	if (PM_must_reset_opts)
	    PM_reset_opts();
    }
    PM_query();
}

static void PM_make_servername(char *str)
{
    if (PM_term_title[0]) {
	int hash = 0;
	char *p = PM_term_title + 1;
	int match = PM_term_title[0];
	while (*p != match) {
	    hash = (hash << 1) + hash + *p++;
	}
	hash %= (256 * 256 * 256 - 1);
	sprintf(str, "gp%x", hash);
    } else
	sprintf(str, "gpServ%d", PM_plot_number);
}


TERM_PUBLIC void PM_options()
{
    int old_termmode = PM_termmode;
    PM_termmode = 0;
    term_options[0] = NUL;
    PM_term_title[0] = NUL;
    PM_opts[0] = NUL;
    PM_optargs = 0;
    while (!END_OF_COMMAND) {
	if (almost_equals(c_token, "pe$rsist")) {
	    strcat(PM_opts, "-p ");
	    strcat(term_options, "persist ");
	    PM_termmode |= 1;
	    PM_optargs = 1;
	    if (!(old_termmode & 1))
		PM_pipe = NULL;
	} else if (almost_equals(c_token, "s$erver")) {
	    strcat(PM_opts, "-s ");
	    strcat(term_options, "server ");
	    PM_termmode |= 2;
	    PM_optargs = 1;
	    if (!(old_termmode & 2))
		PM_pipe = NULL;
	    if (isanumber(c_token + 1)) {
		struct value t;
		char *p = PM_opts + strlen(PM_opts);
		c_token++;
		PM_plot_number = (int) real(const_express(&t));
		sprintf(p, "%d", PM_plot_number);
		sprintf(term_options + strlen(term_options), "%d", PM_plot_number);
	    }
	} else if (almost_equals(c_token, "w$idelines")) {
	    strcat(PM_opts, "-w ");
	    strcat(term_options, "widelines ");
	    PM_optargs = 1;
	} else if (almost_equals(c_token, "e$nhanced")) {
	    strcat(PM_opts, "-e ");
	    strcat(term_options, "enhanced ");
	    PM_optargs = 1;
	} else if (isstring(c_token)) {
	    copy_str(PM_term_title, c_token, 127);
	}
#if 0
	else if (almost_equals(c_token, "po$rtrait")) {
	    strcat(PM_opts, "-l ");
	    strcat(term_options, "portrait ");
	    PM_optargs = 1;
	}
#endif
	c_token++;
    }
    if (PM_term_title[0]) {
	strcat(PM_opts, " ");
	strcat(term_options, " ");
	strcat(PM_opts, PM_term_title);
	strcat(term_options, PM_term_title);
    }
    PM_must_reset_opts = TRUE;
}

static void PM_reset_opts()
{
    int len;
    putc(SET_OPTIONS, PM_pipe);
    len = strlen(PM_opts) + 1;
    fwrite(&len, sizeof(int), 1, PM_pipe);
    fwrite(PM_opts, 1, len, PM_pipe);
    for (len = sizeof(int) - len % sizeof(int); len > 0; len--) {
	/* pad rest of int with zeros */
	putc(NUL, PM_pipe);
    }
    fflush(PM_pipe);
    PM_must_reset_opts = FALSE;
}

static void PM_query()
{
    int rc;
    ULONG cbR;
    putc(GR_QUERY, PM_pipe);
    fflush(PM_pipe);
    rc = DosRead(fileno(PM_pipe), &term->h_char, sizeof(int), &cbR);
    rc = DosRead(fileno(PM_pipe), &term->v_char, sizeof(int), &cbR);
}

TERM_PUBLIC void PM_reset()
{
    putc(GR_RESET, PM_pipe);
    fflush(PM_pipe);
    term_force_init = FALSE;
    if (PM_termmode > 0) {
	fclose(PM_pipe);
	PM_pipe = NULL;
    }
}

TERM_PUBLIC void PM_suspend()
{
    putc(GR_SUSPEND, PM_pipe);
    fflush(PM_pipe);
}

TERM_PUBLIC void PM_resume()
{
    putc(GR_RESUME, PM_pipe);
    fflush(PM_pipe);
}

TERM_PUBLIC void PM_text()
{
    fflush(PM_pipe);
    if (PM_mode != SET_TEXT) {
	putc(SET_TEXT, PM_pipe);
	fflush(PM_pipe);
//        keep_term_initialised = term_initialised ;
//        term_initialised = FALSE ; /* need to force init */
    }
    PM_mode = SET_TEXT;
}

TERM_PUBLIC void PM_graphics()
{
    putc(SET_GRAPHICS, PM_pipe);
    fflush(PM_pipe);
    PM_mode = SET_GRAPHICS;
}

TERM_PUBLIC void PM_move(unsigned int x, unsigned int y)
{
    if (PM_must_abort)
	PM_abortplot();
    putc(GR_MOVE, PM_pipe);
    fwrite(&x, sizeof(int), 1, PM_pipe);
    fwrite(&y, sizeof(int), 1, PM_pipe);
}

TERM_PUBLIC void PM_vector(unsigned int x, unsigned int y)
{
    if (PM_must_abort)
	PM_abortplot();
    putc(GR_DRAW, PM_pipe);
    fwrite(&x, sizeof(int), 1, PM_pipe);
    fwrite(&y, sizeof(int), 1, PM_pipe);
}

TERM_PUBLIC void PM_linetype(int lt)
{
    putc(SET_LINE, PM_pipe);
    fwrite(&lt, sizeof(int), 1, PM_pipe);
}

TERM_PUBLIC int PM_text_angle(int ang)
{
    putc(SET_ANGLE, PM_pipe);
    fwrite(&ang, sizeof(int), 1, PM_pipe);
    return (TRUE);
}

TERM_PUBLIC void PM_put_text(unsigned int x, unsigned int y, char *str)
{
    int len;
    if (PM_must_abort)
	PM_abortplot();
    putc(GR_TEXT, PM_pipe);
    fwrite(&x, sizeof(int), 1, PM_pipe);
    fwrite(&y, sizeof(int), 1, PM_pipe);
    len = strlen(str) + 1;
    fwrite(&len, sizeof(int), 1, PM_pipe);
    fwrite(str, 1, len, PM_pipe);
    for (len = sizeof(int) - len % sizeof(int); len > 0; len--) {
	/* pad rest of int with zeros */
	putc(NUL, PM_pipe);
    }
}

TERM_PUBLIC int PM_justify_text(enum JUSTIFY mode)
{
    putc(SET_JUSTIFY, PM_pipe);
    fwrite(&mode, sizeof(int), 1, PM_pipe);
    return (TRUE);
}

TERM_PUBLIC int PM_set_font(font) /* Entry font added by DJL */
char *font;
{
    int len, fontsize;
    char *p;
    p = strchr(font, ',');
    if (p == NULL || *p == NUL)
	fontsize = 14;
    else
	fontsize = strtol(p + 1, NULL, 10);
    if (fontsize <= 0)
	fontsize = 14;
    putc(SET_FONT, PM_pipe);
    len = strlen(font) + 1;
    fwrite(&len, sizeof(int), 1, PM_pipe);
    fwrite(font, 1, len, PM_pipe);
    for (len = sizeof(int) - len % sizeof(int); len > 0; len--) {
	/* pad rest of int with zeros */
	putc(NUL, PM_pipe);
    }
    return TRUE;
}


TERM_PUBLIC void PM_point(unsigned int x, unsigned int y, int number)
/*
** tell the driver we are plotting a point so it can decide whether to
** use colour or not
*/
{
    int mode;
    mode = 1;
    putc(SET_POINTMODE, PM_pipe);
    fwrite(&mode, sizeof(int), 1, PM_pipe);
    do_point(x, y, number);
    mode = 0;
    putc(SET_POINTMODE, PM_pipe);
    fwrite(&mode, sizeof(int), 1, PM_pipe);
}

void PM_abortplot()
{
    PM_must_abort = 0;
    term_reset();
    (void) putc('\n', stderr);
    bail_to_command_line();
}

void PM_intc_cleanup()
{
    if (PM_pipe == NULL || PM_mode == SET_TEXT)
	PM_abortplot();
    PM_must_abort = 1;
}

void PM_setup(void)
/*
** Initial terminal setup
*/
{
    char *envpath;
    PPIB pib;
    PTIB tib;
    char path[256], *p;
    DosGetInfoBlocks(&tib, &pib);
    DosQueryModuleName(pib->pib_hmte, 256, path);
    if (PM_path[0] == NUL) {
	strcpy(PM_path, path);
	p = strrchr(PM_path, '\\');
	if (p != NULL)
	    *p = NUL;
	else {
	    p = strrchr(PM_path, ':');
	    if (p != NULL)
		*(p + 1) = NUL;
	}
	if (p == NULL)
	    _getcwd2(PM_path, 256);
    }
    if (getenv("GNUHELP") == NULL) {
	int n = strlen("GNUHELP") + strlen(PM_path) + strlen("gnuplot.gih") + 3;
	envpath = (char *) malloc(n);
	if (envpath != NULL) {
	    strcpy(envpath, "GNUHELP");
	    strcat(envpath, "=");
	    strcat(envpath, PM_path);
	    strcat(envpath, "/");
	    strcat(envpath, "gnuplot.gih");
	    putenv(envpath);
	}
    }
    if (getenv("GNUPLOT") == NULL) {
	int n = strlen("GNUPLOT") + strlen(PM_path) + 2;
	envpath = (char *) malloc(n);
	if (envpath != NULL) {
	    strcpy(envpath, "GNUPLOT");
	    strcat(envpath, "=");
	    strcat(envpath, PM_path);
	    putenv(envpath);
	}
    }
}

int PM_pause(char *str)
/*
** pause - using message box on PM screen
*/
{
    int len, rc;
    ULONG cbR;
    char buf[256];
    char *bp;

    if (PM_pipe == NULL)
	return 2;
    bp = buf;
    putc(GR_PAUSE, PM_pipe);
    len = strlen(str) + 1;
    fwrite(&len, sizeof(int), 1, PM_pipe);
    fwrite(str, 1, len, PM_pipe);
    for (rc = sizeof(int) - len % sizeof(int); rc > 0; rc--) {
	/* pad rest of int with zeros */
	putc(NUL, PM_pipe);
    }
    fflush(PM_pipe);
    rc = DosRead(fileno(PM_pipe), &len, sizeof(int), &cbR);
    return len;
}

TERM_PUBLIC void PM_linewidth(double linewidth)
{
    int lw;
    lw = linewidth * 100;
    putc(SET_LINEWIDTH, PM_pipe);
    fwrite(&lw, sizeof(int), 1, PM_pipe);
}

#endif

#ifdef TERM_TABLE
TERM_TABLE_START(PM_driver)
    "pm", "OS/2 Presentation Manager",
    PM_XMAX, PM_YMAX, PM_VCHAR, PM_HCHAR,
    PM_VTIC, PM_HTIC, PM_options, PM_init, PM_reset,
    PM_text, null_scale, PM_graphics, PM_move, PM_vector,
    PM_linetype, PM_put_text, PM_text_angle,
    PM_justify_text, PM_point, do_arrow, PM_set_font,
    0 /*pointsize */ , TERM_CAN_MULTIPLOT, PM_suspend, PM_resume,
    0 /*fillbox */ , PM_linewidth
TERM_TABLE_END(PM_driver)

#undef LAST_TERM
#define LAST_TERM PM_driver

#endif /* TERM_TABLE */

#ifdef TERM_HELP
START_HELP(pm)
"1 pm",
"?commands set terminal pm",
"?set terminal pm",
"?set term pm",
"?terminal pm",
"?term pm",
"?pm",
" The `pm` terminal driver provides an OS/2 Presentation Manager window in",
" which the graph is plotted.  The window is opened when the first graph is",
" plotted.  This window has its own online help as well as facilities for",
" printing, copying to the clipboard and some line type and color adjustments.",
" The `multiplot` option is supported.",
"",
" Syntax:",
"       set terminal pm {server {n}} {persist} {widelines} {enhanced} {\"title\"}",
"",
" If `persist` is specified, each graph appears in its own window and all",
" windows remain open after `gnuplot` exits.  If `server` is specified, all",
" graphs appear in the same window, which remains open when `gnuplot` exits.",
" This option takes an optional numerical argument which specifies an instance",
" of the server process.  Thus multiple server windows can be in use at the",
" same time.",
"",
" If `widelines` is specified, all plots will be drawn with wide lines.  If",
" `enhanced` is specified, sub- and superscripts and multiple fonts are",
" enabled using the same syntax as the `enhanced postscript` option (see",
" `set terminal postscript enhanced` for details).  Font names for the basic",
" PostScript fonts may be abbreviated to single letters.",
"",
" If `title` is specified, it will be used as the title of the plot window.",
" It will also be used as the name of the server instance, and will override",
" the optional numerical argument.",
"",
" Linewidths may be changed with `set linestyle`."
END_HELP(pm)
#endif /* TERM_HELP */