aboutsummaryrefslogtreecommitdiff
path: root/sys/gio/stdgraph/stgopenws.x
blob: a70a51f77600e2c4548760695ebf97048c1b7f20 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<gki.h>
include	<ttset.h>
include	<error.h>
include	<chars.h>
include	<finfo.h>
include	"stdgraph.h"

# STG_OPENWS -- Open the named workstation.  Once a workstation has been
# opened we leave it open until some other workstation is opened or the
# kernel is closed.  Opening a workstation involves initialization of the
# kernel data structures, followed by initialization of the device itself.

procedure stg_openws (devname, n, mode)

short	devname[ARB]		#I device name (actually device[,uifname])
int	n			#I length of device name
int	mode			#I access mode

bool	reinit
long	fi[LEN_FINFO]
int	dummy, init_file
pointer	sp, ip, op, buf, device, uifname, fname

pointer	ttygdes(), ttyodes()
bool	ttygetb(), strne(), streq()
int	ttygets(), open(), ttstati(), finfo(), gstrcpy()
int	nowhite(), envfind(), strlen(), fnroot(), access()
extern	stg_onerror()
include	"stdgraph.com"
define	ow_ 91

begin
	call smark (sp)
	call salloc (buf, max (SZ_PATHNAME, n), TY_CHAR)
	call salloc (fname, SZ_PATHNAME, TY_CHAR)

	# Open a termcap descriptor for the terminal too, in case we need
	# to talk to the terminal as a terminal.

	if (g_term == NULL)
	    iferr (g_term = ttyodes ("terminal"))
		g_term = NULL

	# If we are appending merely reactivate the device without performing
	# any initialization.

	if (g_sg != NULL && mode == APPEND) {
	    if (g_active == NO) {
		g_ucaseout = ttstati (g_out, TT_UCASEOUT)
		if (g_ucaseout == YES)
		    call ttseti (g_out, TT_UCASEOUT, NO)

		g_active = YES
		g_enable = YES
	    }
	    goto ow_
	}

	# If a device was named when the kernel was opened then output will
	# always go to that device (g_device) regardless of the device named
	# in the OPENWS instruction.  If no device was named (null string)
	# then unpack the device name, passed as a short integer array.

	if (g_device[1] == EOS) {
	    call achtsc (devname, Memc[buf], n)
	    Memc[buf+n] = EOS
	} else
	    call strcpy (g_device, Memc[buf], SZ_FNAME)

	# Parse the "device,uifname" specification into the two fields.
	device = buf
	uifname = NULL
	for (ip=buf;  Memc[ip] != EOS;  ip=ip+1)
	    if (Memc[ip] == ',') {
		Memc[ip] = EOS
		if (Memc[ip+1] != EOS)
		    uifname = ip + 1
		if (nowhite (Memc[uifname], Memc[uifname], ARB) == 0)
		    uifname = NULL
		break
	    }

	# If the kernel is already open for this device skip most of the
	# initialization.  If already open for a different device free
	# storage before reinitialization.

	reinit = true
	if (g_sg != NULL)
	    if (strne (Memc[device], Memc[SG_DEVNAME(g_sg)])) {
		call mfree (SG_SBUF(g_sg), TY_CHAR)
		call mfree (g_sg, TY_STRUCT)
		reinit = true
	    } else
		reinit = false

	# Reinitialize the kernel datastructures.  Open graphcap descriptor
	# for the named device, allocate and initialize descriptor and common.

	if (reinit) {
	    if (g_tty != NULL) {
		call ttycdes (g_tty)
		g_tty = NULL
	    }

	    iferr (g_tty = ttygdes (Memc[device])) {
		g_tty = ttygdes ("4012")
		call erract (EA_WARN)
	    }

	    # Initialize data structures.
	    call stg_init (g_tty, Memc[device])
	}

	call stg_reset()

	if (g_active == NO) {
	    # Must disable stty ucaseout mode when in graphics mode, else
	    # plotting commands may be modified by the terminal driver.

	    g_ucaseout = ttstati (g_out, TT_UCASEOUT)
	    if (g_ucaseout == YES)
		call ttseti (g_out, TT_UCASEOUT, NO)

	    # Post ONERROR cleanup routine.
	    call onerror (stg_onerror)
	    g_active = YES
	    g_enable = YES
	}

	# If no UI file was specified but the device has the EM capability,
	# use the default UI if any specified in the graphcap entry.  If the
	# EM capability is missing, ignore any uifname specified when the
	# device was opened.

	if (ttygetb (g_tty, "EM")) {
	    if (uifname == NULL) {
		uifname = buf + strlen(Memc[device]) + 1
		if (ttygets (g_tty, "ED", Memc[uifname], ARB) <= 0)
		    uifname = NULL
	    }

	    # If the user has a version of the named UI file in their GUIDIR,
	    # use that instead.

	    if (envfind (GUIDIR, Memc[fname], SZ_PATHNAME) > 0) {
		op = fname + strlen (Memc[fname])
		op = op + fnroot (Memc[uifname], Memc[op],
		    fname + SZ_PATHNAME - op)
		op = op + gstrcpy (".gui", Memc[op], fname + SZ_PATHNAME - op)
		if (access (Memc[fname], 0, 0) == YES)
		    uifname = fname
	    }

	    # If the UI is already running and has not been modified there
	    # is no need to download it again.

	    if (g_sg != NULL)
		if (streq (Memc[uifname], Memc[SG_UIFNAME(g_sg)]))
		    if (finfo (Memc[uifname], fi) != ERR)
			if (SG_UIFDATE(g_sg) == FI_MTIME(fi))
			    uifname = NULL
	} else {
	    # Ignore UI file if no EM capability.
	    Memc[SG_UIFNAME(g_sg)] = EOS
	    SG_UIFDATE(g_sg) = 0
	    uifname = NULL
	}

	# Open and Initialize the device.  Output contents of UI definition
	# file if any, followed by graphics device initialization file,
	# if any.

	if (mode == NEW_FILE) {
	    # Output UI definition file.
	    if (uifname != NULL) {
		iferr (init_file = open (Memc[uifname], READ_ONLY, TEXT_FILE)) {
		    call erract (EA_WARN)
		    call stg_ctrl ("OW")
		} else {
		    call flush (g_out)
		    call stg_ctrl ("EM")

		    # Download the UI.
		    call putline (g_out, "server ")
		    iferr (call fcopyo (init_file, g_out))
			call erract (EA_WARN)
		    call close (init_file)

		    # Record particulars of active UI file.
		    call strcpy (Memc[uifname], Memc[SG_UIFNAME(g_sg)],
			SZ_UIFNAME)
		    if (finfo (Memc[uifname], fi) != ERR)
			SG_UIFDATE(g_sg) = FI_MTIME(fi)
		    call sgf_post_filter (g_out)

		    call putci (g_out, US)
		    call flush (g_out)
		}
	    } else
		call stg_ctrl ("OW")

	    # Output device graphics initialization file if any.
	    if (ttygets (g_tty, "IF", Memc[buf], SZ_FNAME) > 0) {
		iferr (init_file = open (Memc[buf], READ_ONLY, TEXT_FILE))
		    call erract (EA_WARN)
		iferr (call fcopyo (init_file, g_out))
		    call erract (EA_WARN)
		call close (init_file)
	    }

	    # Clear the screen if device is being opened in new_file mode.
	    call stg_clear (dummy)

	} else
ow_	    call stg_ctrl ("OW")

	call sfree (sp)
end