(mouse-drag-region): Handle scroll off top/bottom.
[emacs.git] / src / vmsfns.c
blob6a9c2b2b449d9b92564a25d15f7777da7270f50b
1 /* VMS subprocess and command interface.
2 Copyright (C) 1987, 1988 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Written by Mukesh Prasad. */
23 * INTERFACE PROVIDED BY EMACS FOR VMS SUBPROCESSES:
25 * Emacs provides the following functions:
27 * "spawn-subprocess", which takes as arguments:
29 * (i) an integer to identify the spawned subprocess in future
30 * operations,
31 * (ii) A function to process input from the subprocess, and
32 * (iii) A function to be called upon subprocess termination.
34 * First argument is required. If second argument is missing or nil,
35 * the default action is to insert all received messages at the current
36 * location in the current buffer. If third argument is missing or nil,
37 * no action is taken upon subprocess termination.
38 * The input-handler is called as
39 * (input-handler num string)
40 * where num is the identifying integer for the subprocess and string
41 * is a string received from the subprocess. exit-handler is called
42 * with the identifying integer as the argument.
44 * "send-command-to-subprocess" takes two arguments:
46 * (i) Subprocess identifying integer.
47 * (ii) String to send as a message to the subprocess.
49 * "stop-subprocess" takes the subprocess identifying integer as
50 * argument.
52 * Implementation is done by spawning an asynchronous subprocess, and
53 * communicating to it via mailboxes.
56 #ifdef VMS
58 #include <stdio.h>
59 #include <ctype.h>
60 #undef NULL
62 #include <config.h>
63 #include "lisp.h"
64 #include <descrip.h>
65 #include <dvidef.h>
66 #include <prvdef.h>
67 /* #include <clidef.h> */
68 #include <iodef.h>
69 #include <ssdef.h>
70 #include <errno.h>
72 #ifdef VMS4_4 /* I am being cautious; perhaps this exists in older versions */
73 #include <jpidef.h>
74 #endif
76 /* #include <syidef.h> */
78 #define CLI$M_NOWAIT 1 /* clidef.h is missing from C library */
79 #define SYI$_VERSION 4096 /* syidef.h is missing from C library */
80 #define JPI$_CLINAME 522 /* JPI$_CLINAME is missing from jpidef.h */
81 #define JPI$_MASTER_PID 805 /* JPI$_MASTER_PID missing from jpidef.h */
82 #define LIB$_NOSUCHSYM 1409892 /* libclidef.h missing */
84 #define MSGSIZE 160 /* Maximum size for mailbox operations */
86 #ifndef PRV$V_ACNT
88 /* these defines added as hack for VMS 5.1-1. SJones, 8-17-89 */
89 /* this is _really_ nasty and needs to be changed ASAP - should see about
90 using the union defined in SYS$LIBRARY:PRVDEF.H under v5 */
92 #define PRV$V_ACNT 0x09
93 #define PRV$V_ALLSPOOL 0x04
94 #define PRV$V_ALTPRI 0x0D
95 #define PRV$V_BUGCHK 0x17
96 #define PRV$V_BYPASS 0x1D
97 #define PRV$V_CMEXEC 0x01
98 #define PRV$V_CMKRNL 0x00
99 #define PRV$V_DETACH 0x05
100 #define PRV$V_DIAGNOSE 0x06
101 #define PRV$V_DOWNGRADE 0x21
102 #define PRV$V_EXQUOTA 0x13
103 #define PRV$V_GROUP 0x08
104 #define PRV$V_GRPNAM 0x03
105 #define PRV$V_GRPPRV 0x22
106 #define PRV$V_LOG_IO 0x07
107 #define PRV$V_MOUNT 0x11
108 #define PRV$V_NETMBX 0x14
109 #define PRV$V_NOACNT 0x09
110 #define PRV$V_OPER 0x12
111 #define PRV$V_PFNMAP 0x1A
112 #define PRV$V_PHY_IO 0x16
113 #define PRV$V_PRMCEB 0x0A
114 #define PRV$V_PRMGBL 0x18
115 #define PRV$V_PRMJNL 0x25
116 #define PRV$V_PRMMBX 0x0B
117 #define PRV$V_PSWAPM 0x0C
118 #define PRV$V_READALL 0x23
119 #define PRV$V_SECURITY 0x26
120 #define PRV$V_SETPRI 0x0D
121 #define PRV$V_SETPRV 0x0E
122 #define PRV$V_SHARE 0x1F
123 #define PRV$V_SHMEM 0x1B
124 #define PRV$V_SYSGBL 0x19
125 #define PRV$V_SYSLCK 0x1E
126 #define PRV$V_SYSNAM 0x02
127 #define PRV$V_SYSPRV 0x1C
128 #define PRV$V_TMPJNL 0x24
129 #define PRV$V_TMPMBX 0x0F
130 #define PRV$V_UPGRADE 0x20
131 #define PRV$V_VOLPRO 0x15
132 #define PRV$V_WORLD 0x10
133 #endif
135 /* IO status block for mailbox operations. */
136 struct mbx_iosb
138 short status;
139 short size;
140 int pid;
143 /* Structure for maintaining linked list of subprocesses. */
144 struct process_list
146 int name; /* Numeric identifier for subprocess */
147 int process_id; /* VMS process address */
148 int process_active; /* 1 iff process has not exited yet */
149 int mbx_chan; /* Mailbox channel to write to process */
150 struct mbx_iosb iosb; /* IO status block for write operations */
151 Lisp_Object input_handler; /* Input handler for subprocess */
152 Lisp_Object exit_handler; /* Exit handler for subprocess */
153 struct process_list * next; /* Linked list chain */
156 /* Structure for privilege list. */
157 struct privilege_list
159 char * name;
160 int mask;
163 /* Structure for finding VMS related information. */
164 struct vms_objlist
166 char * name; /* Name of object */
167 Lisp_Object (* objfn)(); /* Function to retrieve VMS object */
170 static int exit_ast (); /* Called upon subprocess exit */
171 static int create_mbx (); /* Creates mailbox */
172 static void mbx_msg (); /* Writes null terminated string to mbx */
173 static void write_to_mbx (); /* Writes message to string */
174 static void start_mbx_input (); /* Queues I/O request to mailbox */
176 static int input_mbx_chan = 0; /* Channel to read subprocess input on */
177 static char input_mbx_name[20];
178 /* Storage for mailbox device name */
179 static struct dsc$descriptor_s input_mbx_dsc;
180 /* Descriptor for mailbox device name */
181 static struct process_list * process_list = 0;
182 /* Linked list of subprocesses */
183 static char mbx_buffer[MSGSIZE];
184 /* Buffer to read from subprocesses */
185 static struct mbx_iosb input_iosb;
186 /* IO status block for mailbox reads */
188 int have_process_input, /* Non-zero iff subprocess input pending */
189 process_exited; /* Non-zero iff suprocess exit pending */
191 /* List of privilege names and mask offsets */
192 static struct privilege_list priv_list[] = {
194 { "ACNT", PRV$V_ACNT },
195 { "ALLSPOOL", PRV$V_ALLSPOOL },
196 { "ALTPRI", PRV$V_ALTPRI },
197 { "BUGCHK", PRV$V_BUGCHK },
198 { "BYPASS", PRV$V_BYPASS },
199 { "CMEXEC", PRV$V_CMEXEC },
200 { "CMKRNL", PRV$V_CMKRNL },
201 { "DETACH", PRV$V_DETACH },
202 { "DIAGNOSE", PRV$V_DIAGNOSE },
203 { "DOWNGRADE", PRV$V_DOWNGRADE }, /* Isn't VMS as low as you can go? */
204 { "EXQUOTA", PRV$V_EXQUOTA },
205 { "GRPPRV", PRV$V_GRPPRV },
206 { "GROUP", PRV$V_GROUP },
207 { "GRPNAM", PRV$V_GRPNAM },
208 { "LOG_IO", PRV$V_LOG_IO },
209 { "MOUNT", PRV$V_MOUNT },
210 { "NETMBX", PRV$V_NETMBX },
211 { "NOACNT", PRV$V_NOACNT },
212 { "OPER", PRV$V_OPER },
213 { "PFNMAP", PRV$V_PFNMAP },
214 { "PHY_IO", PRV$V_PHY_IO },
215 { "PRMCEB", PRV$V_PRMCEB },
216 { "PRMGBL", PRV$V_PRMGBL },
217 { "PRMJNL", PRV$V_PRMJNL },
218 { "PRMMBX", PRV$V_PRMMBX },
219 { "PSWAPM", PRV$V_PSWAPM },
220 { "READALL", PRV$V_READALL },
221 { "SECURITY", PRV$V_SECURITY },
222 { "SETPRI", PRV$V_SETPRI },
223 { "SETPRV", PRV$V_SETPRV },
224 { "SHARE", PRV$V_SHARE },
225 { "SHMEM", PRV$V_SHMEM },
226 { "SYSGBL", PRV$V_SYSGBL },
227 { "SYSLCK", PRV$V_SYSLCK },
228 { "SYSNAM", PRV$V_SYSNAM },
229 { "SYSPRV", PRV$V_SYSPRV },
230 { "TMPJNL", PRV$V_TMPJNL },
231 { "TMPMBX", PRV$V_TMPMBX },
232 { "UPGRADE", PRV$V_UPGRADE },
233 { "VOLPRO", PRV$V_VOLPRO },
234 { "WORLD", PRV$V_WORLD },
238 static Lisp_Object
239 vms_account(), vms_cliname(), vms_owner(), vms_grp(), vms_image(),
240 vms_parent(), vms_pid(), vms_prcnam(), vms_terminal(), vms_uic_int(),
241 vms_uic_str(), vms_username(), vms_version_fn(), vms_trnlog(),
242 vms_symbol(), vms_proclist();
244 /* Table of arguments to Fvms_object, and the handlers that get the data. */
246 static struct vms_objlist vms_object [] = {
247 { "ACCOUNT", vms_account }, /* Returns account name as a string */
248 { "CLINAME", vms_cliname }, /* Returns CLI name (string) */
249 { "OWNER", vms_owner }, /* Returns owner process's PID (int) */
250 { "GRP", vms_grp }, /* Returns group number of UIC (int) */
251 { "IMAGE", vms_image }, /* Returns executing image (string) */
252 { "PARENT", vms_parent }, /* Returns parent proc's PID (int) */
253 { "PID", vms_pid }, /* Returns process's PID (int) */
254 { "PRCNAM", vms_prcnam }, /* Returns process's name (string) */
255 { "TERMINAL", vms_terminal }, /* Returns terminal name (string) */
256 { "UIC", vms_uic_int }, /* Returns UIC as integer */
257 { "UICGRP", vms_uic_str }, /* Returns UIC as string */
258 { "USERNAME", vms_username }, /* Returns username (string) */
259 { "VERSION", vms_version_fn },/* Returns VMS version (string) */
260 { "LOGICAL", vms_trnlog }, /* Translates VMS logical name */
261 { "DCL-SYMBOL", vms_symbol }, /* Translates DCL symbol */
262 { "PROCLIST", vms_proclist }, /* Returns list of all PIDs on system */
265 Lisp_Object Qdefault_subproc_input_handler;
267 extern int process_ef; /* Event flag for subprocess operations */
269 DEFUN ("default-subprocess-input-handler",
270 Fdefault_subproc_input_handler, Sdefault_subproc_input_handler,
271 2, 2, 0,
272 "Default input handler for input from spawned subprocesses.")
273 (name, input)
274 Lisp_Object name, input;
276 /* Just insert in current buffer */
277 insert1 (input);
278 insert ("\n", 1);
281 DEFUN ("spawn-subprocess", Fspawn_subprocess, Sspawn_subprocess, 1, 3, 0,
282 "Spawn an asynchronous VMS suprocess for command processing.")
283 (name, input_handler, exit_handler)
284 Lisp_Object name, input_handler, exit_handler;
286 int status;
287 char output_mbx_name[20];
288 struct dsc$descriptor_s output_mbx_dsc;
289 struct process_list *ptr, *p, *prev;
291 CHECK_NUMBER (name, 0);
292 if (! input_mbx_chan)
294 if (! create_mbx (&input_mbx_dsc, input_mbx_name, &input_mbx_chan, 1))
295 return Qnil;
296 start_mbx_input ();
298 ptr = 0;
299 prev = 0;
300 while (ptr)
302 struct process_list *next = ptr->next;
303 if (ptr->name == XFASTINT (name))
305 if (ptr->process_active)
306 return Qt;
308 /* Delete this process and run its exit handler. */
309 if (prev)
310 prev->next = next;
311 else
312 process_list = next;
313 if (! NILP (ptr->exit_handler))
314 Feval (Fcons (ptr->exit_handler, Fcons (make_number (ptr->name),
315 Qnil)));
316 sys$dassgn (ptr->mbx_chan);
317 break;
319 else
320 prev = ptr;
321 ptr = next;
323 if (! ptr)
324 ptr = xmalloc (sizeof (struct process_list));
325 if (! create_mbx (&output_mbx_dsc, output_mbx_name, &ptr->mbx_chan, 2))
327 free (ptr);
328 return Qnil;
330 if (NILP (input_handler))
331 input_handler = Qdefault_subproc_input_handler;
332 ptr->input_handler = input_handler;
333 ptr->exit_handler = exit_handler;
334 message ("Creating subprocess...");
335 status = lib$spawn (0, &output_mbx_dsc, &input_mbx_dsc, &CLI$M_NOWAIT, 0,
336 &ptr->process_id, 0, 0, exit_ast, &ptr->process_active);
337 if (! (status & 1))
339 sys$dassgn (ptr->mbx_chan);
340 free (ptr);
341 error ("Unable to spawn subprocess");
342 return Qnil;
344 ptr->name = XFASTINT (name);
345 ptr->next = process_list;
346 ptr->process_active = 1;
347 process_list = ptr;
348 message ("Creating subprocess...done");
349 return Qt;
352 static void
353 mbx_msg (ptr, msg)
354 struct process_list *ptr;
355 char *msg;
357 write_to_mbx (ptr, msg, strlen (msg));
360 DEFUN ("send-command-to-subprocess",
361 Fsend_command_to_subprocess, Ssend_command_to_subprocess, 2, 2,
362 "sSend command to subprocess: \nsSend subprocess %s command: ",
363 "Send to VMS subprocess named NAME the string COMMAND.")
364 (name, command)
365 Lisp_Object name, command;
367 struct process_list * ptr;
369 CHECK_NUMBER (name, 0);
370 CHECK_STRING (command, 1);
371 for (ptr = process_list; ptr; ptr = ptr->next)
372 if (XFASTINT (name) == ptr->name)
374 write_to_mbx (ptr, XSTRING (command)->data,
375 XSTRING (command)->size);
376 return Qt;
378 return Qnil;
381 DEFUN ("stop-subprocess", Fstop_subprocess, Sstop_subprocess, 1, 1,
382 "sStop subprocess: ", "Stop VMS subprocess named NAME.")
383 (name)
384 Lisp_Object name;
386 struct process_list * ptr;
388 CHECK_NUMBER (name, 0);
389 for (ptr = process_list; ptr; ptr = ptr->next)
390 if (XFASTINT (name) == ptr->name)
392 ptr->exit_handler = Qnil;
393 if (sys$delprc (&ptr->process_id, 0) & 1)
394 ptr->process_active = 0;
395 return Qt;
397 return Qnil;
400 static int
401 exit_ast (active)
402 int * active;
404 process_exited = 1;
405 *active = 0;
406 sys$setef (process_ef);
409 /* Process to handle input on the input mailbox.
410 * Searches through the list of processes until the matching PID is found,
411 * then calls its input handler.
414 process_command_input ()
416 struct process_list * ptr;
417 char * msg;
418 int msglen;
419 Lisp_Object expr;
421 msg = mbx_buffer;
422 msglen = input_iosb.size;
423 /* Hack around VMS oddity of sending extraneous CR/LF characters for
424 * some of the commands (but not most).
426 if (msglen > 0 && *msg == '\r')
428 msg++;
429 msglen--;
431 if (msglen > 0 && msg[msglen - 1] == '\n')
432 msglen--;
433 if (msglen > 0 && msg[msglen - 1] == '\r')
434 msglen--;
435 /* Search for the subprocess in the linked list.
437 expr = Qnil;
438 for (ptr = process_list; ptr; ptr = ptr->next)
439 if (ptr->process_id == input_iosb.pid)
441 expr = Fcons (ptr->input_handler,
442 Fcons (make_number (ptr->name),
443 Fcons (make_string (msg, msglen),
444 Qnil)));
445 break;
447 have_process_input = 0;
448 start_mbx_input ();
449 clear_waiting_for_input (); /* Otherwise Ctl-g will cause crash. JCB */
450 if (! NILP (expr))
451 Feval (expr);
454 /* Searches process list for any processes which have exited. Calls their
455 * exit handlers and removes them from the process list.
458 process_exit ()
460 struct process_list * ptr, * prev, * next;
462 process_exited = 0;
463 prev = 0;
464 ptr = process_list;
465 while (ptr)
467 next = ptr->next;
468 if (! ptr->process_active)
470 if (prev)
471 prev->next = next;
472 else
473 process_list = next;
474 if (! NILP (ptr->exit_handler))
475 Feval (Fcons (ptr->exit_handler, Fcons (make_number (ptr->name),
476 Qnil)));
477 sys$dassgn (ptr->mbx_chan);
478 free (ptr);
480 else
481 prev = ptr;
482 ptr = next;
486 /* Called at emacs exit.
489 kill_vms_processes ()
491 struct process_list * ptr;
493 for (ptr = process_list; ptr; ptr = ptr->next)
494 if (ptr->process_active)
496 sys$dassgn (ptr->mbx_chan);
497 sys$delprc (&ptr->process_id, 0);
499 sys$dassgn (input_mbx_chan);
500 process_list = 0;
501 input_mbx_chan = 0;
504 /* Creates a temporary mailbox and retrieves its device name in 'buf'.
505 * Makes the descriptor pointed to by 'dsc' refer to this device.
506 * 'buffer_factor' is used to allow sending messages asynchronously
507 * till some point.
510 static int
511 create_mbx (dsc, buf, chan, buffer_factor)
512 struct dsc$descriptor_s *dsc;
513 char *buf;
514 int *chan;
515 int buffer_factor;
517 int strval[2];
518 int status;
520 status = sys$crembx (0, chan, MSGSIZE, MSGSIZE * buffer_factor, 0, 0, 0);
521 if (! (status & 1))
523 message ("Unable to create mailbox. Need TMPMBX privilege.");
524 return 0;
526 strval[0] = 16;
527 strval[1] = buf;
528 status = lib$getdvi (&DVI$_DEVNAM, chan, 0, 0, strval,
529 &dsc->dsc$w_length);
530 if (! (status & 1))
531 return 0;
532 dsc->dsc$b_dtype = DSC$K_DTYPE_T;
533 dsc->dsc$b_class = DSC$K_CLASS_S;
534 dsc->dsc$a_pointer = buf;
535 return 1;
536 } /* create_mbx */
538 /* AST routine to be called upon receiving mailbox input.
539 * Sets flag telling keyboard routines that input is available.
542 static int
543 mbx_input_ast ()
545 have_process_input = 1;
548 /* Issue a QIO request on the input mailbox.
550 static void
551 start_mbx_input ()
553 sys$qio (process_ef, input_mbx_chan, IO$_READVBLK, &input_iosb,
554 mbx_input_ast, 0, mbx_buffer, sizeof (mbx_buffer),
555 0, 0, 0, 0);
558 /* Send a message to the subprocess input mailbox, without blocking if
559 * possible.
561 static void
562 write_to_mbx (ptr, buf, len)
563 struct process_list *ptr;
564 char *buf;
565 int len;
567 sys$qiow (0, ptr->mbx_chan, IO$_WRITEVBLK | IO$M_NOW, &ptr->iosb,
568 0, 0, buf, len, 0, 0, 0, 0);
571 DEFUN ("setprv", Fsetprv, Ssetprv, 1, 3, 0,
572 "Set or reset a VMS privilege. First arg is privilege name.\n\
573 Second arg is t or nil, indicating whether the privilege is to be\n\
574 set or reset. Default is nil. Returns t if success, nil if not.\n\
575 If third arg is non-nil, does not change privilege, but returns t\n\
576 or nil depending upon whether the privilege is already enabled.")
577 (priv, value, getprv)
578 Lisp_Object priv, value, getprv;
580 int prvmask[2], prvlen, newmask[2];
581 char * prvname;
582 int found, i;
583 struct privilege_list * ptr;
585 CHECK_STRING (priv, 0);
586 priv = Fupcase (priv);
587 prvname = XSTRING (priv)->data;
588 prvlen = XSTRING (priv)->size;
589 found = 0;
590 prvmask[0] = 0;
591 prvmask[1] = 0;
592 for (i = 0; i < sizeof (priv_list) / sizeof (priv_list[0]); i++)
594 ptr = &priv_list[i];
595 if (prvlen == strlen (ptr->name) &&
596 bcmp (prvname, ptr->name, prvlen) == 0)
598 if (ptr->mask >= 32)
599 prvmask[1] = 1 << (ptr->mask % 32);
600 else
601 prvmask[0] = 1 << ptr->mask;
602 found = 1;
603 break;
606 if (! found)
607 error ("Unknown privilege name %s", XSTRING (priv)->data);
608 if (NILP (getprv))
610 if (sys$setprv (NILP (value) ? 0 : 1, prvmask, 0, 0) == SS$_NORMAL)
611 return Qt;
612 return Qnil;
614 /* Get old priv value */
615 if (sys$setprv (0, 0, 0, newmask) != SS$_NORMAL)
616 return Qnil;
617 if ((newmask[0] & prvmask[0])
618 || (newmask[1] & prvmask[1]))
619 return Qt;
620 return Qnil;
623 /* Retrieves VMS system information. */
625 #ifdef VMS4_4 /* I don't know whether these functions work in old versions */
627 DEFUN ("vms-system-info", Fvms_system_info, Svms_system_info, 1, 3, 0,
628 "Retrieve VMS process and system information.\n\
629 The first argument (a string) specifies the type of information desired.\n\
630 The other arguments depend on the type you select.\n\
631 For information about a process, the second argument is a process ID\n\
632 or a process name, with the current process as a default.\n\
633 These are the possibilities for the first arg (upper or lower case ok):\n\
634 account Returns account name\n\
635 cliname Returns CLI name\n\
636 owner Returns owner process's PID\n\
637 grp Returns group number\n\
638 parent Returns parent process's PID\n\
639 pid Returns process's PID\n\
640 prcnam Returns process's name\n\
641 terminal Returns terminal name\n\
642 uic Returns UIC number\n\
643 uicgrp Returns formatted [UIC,GRP]\n\
644 username Returns username\n\
645 version Returns VMS version\n\
646 logical Translates VMS logical name (second argument)\n\
647 dcl-symbol Translates DCL symbol (second argument)\n\
648 proclist Returns list of all PIDs on system (needs WORLD privilege)." )
649 (type, arg1, arg2)
650 Lisp_Object type, arg1, arg2;
652 int i, typelen;
653 char * typename;
654 struct vms_objlist * ptr;
656 CHECK_STRING (type, 0);
657 type = Fupcase (type);
658 typename = XSTRING (type)->data;
659 typelen = XSTRING (type)->size;
660 for (i = 0; i < sizeof (vms_object) / sizeof (vms_object[0]); i++)
662 ptr = &vms_object[i];
663 if (typelen == strlen (ptr->name)
664 && bcmp (typename, ptr->name, typelen) == 0)
665 return (* ptr->objfn)(arg1, arg2);
667 error ("Unknown object type %s", typename);
670 /* Given a reference to a VMS process, returns its process id. */
672 static int
673 translate_id (pid, owner)
674 Lisp_Object pid;
675 int owner; /* if pid is null/0, return owner. If this
676 * flag is 0, return self. */
678 int status, code, id, i, numeric, size;
679 char * p;
680 int prcnam[2];
682 if (NILP (pid)
683 || XTYPE (pid) == Lisp_String && XSTRING (pid)->size == 0
684 || XTYPE (pid) == Lisp_Int && XFASTINT (pid) == 0)
686 code = owner ? JPI$_OWNER : JPI$_PID;
687 status = lib$getjpi (&code, 0, 0, &id);
688 if (! (status & 1))
689 error ("Cannot find %s: %s",
690 owner ? "owner process" : "process id",
691 vmserrstr (status));
692 return (id);
694 if (XTYPE (pid) == Lisp_Int)
695 return (XFASTINT (pid));
696 CHECK_STRING (pid, 0);
697 pid = Fupcase (pid);
698 size = XSTRING (pid)->size;
699 p = XSTRING (pid)->data;
700 numeric = 1;
701 id = 0;
702 for (i = 0; i < size; i++, p++)
703 if (isxdigit (*p))
705 id *= 16;
706 if (*p >= '0' && *p <= '9')
707 id += *p - '0';
708 else
709 id += *p - 'A' + 10;
711 else
713 numeric = 0;
714 break;
716 if (numeric)
717 return (id);
718 prcnam[0] = XSTRING (pid)->size;
719 prcnam[1] = XSTRING (pid)->data;
720 status = lib$getjpi (&JPI$_PID, 0, prcnam, &id);
721 if (! (status & 1))
722 error ("Cannot find process id: %s",
723 vmserrstr (status));
724 return (id);
725 } /* translate_id */
727 /* VMS object retrieval functions. */
729 static Lisp_Object
730 getjpi (jpicode, arg, numeric)
731 int jpicode; /* Type of GETJPI information */
732 Lisp_Object arg;
733 int numeric; /* 1 if numeric value expected */
735 int id, status, numval;
736 char str[128];
737 int strdsc[2] = { sizeof (str), str };
738 short strlen;
740 id = translate_id (arg, 0);
741 status = lib$getjpi (&jpicode, &id, 0, &numval, strdsc, &strlen);
742 if (! (status & 1))
743 error ("Unable to retrieve information: %s",
744 vmserrstr (status));
745 if (numeric)
746 return (make_number (numval));
747 return (make_string (str, strlen));
750 static Lisp_Object
751 vms_account (arg1, arg2)
752 Lisp_Object arg1, arg2;
754 return getjpi (JPI$_ACCOUNT, arg1, 0);
757 static Lisp_Object
758 vms_cliname (arg1, arg2)
759 Lisp_Object arg1, arg2;
761 return getjpi (JPI$_CLINAME, arg1, 0);
764 static Lisp_Object
765 vms_grp (arg1, arg2)
766 Lisp_Object arg1, arg2;
768 return getjpi (JPI$_GRP, arg1, 1);
771 static Lisp_Object
772 vms_image (arg1, arg2)
773 Lisp_Object arg1, arg2;
775 return getjpi (JPI$_IMAGNAME, arg1, 0);
778 static Lisp_Object
779 vms_owner (arg1, arg2)
780 Lisp_Object arg1, arg2;
782 return getjpi (JPI$_OWNER, arg1, 1);
785 static Lisp_Object
786 vms_parent (arg1, arg2)
787 Lisp_Object arg1, arg2;
789 return getjpi (JPI$_MASTER_PID, arg1, 1);
792 static Lisp_Object
793 vms_pid (arg1, arg2)
794 Lisp_Object arg1, arg2;
796 return getjpi (JPI$_PID, arg1, 1);
799 static Lisp_Object
800 vms_prcnam (arg1, arg2)
801 Lisp_Object arg1, arg2;
803 return getjpi (JPI$_PRCNAM, arg1, 0);
806 static Lisp_Object
807 vms_terminal (arg1, arg2)
808 Lisp_Object arg1, arg2;
810 return getjpi (JPI$_TERMINAL, arg1, 0);
813 static Lisp_Object
814 vms_uic_int (arg1, arg2)
815 Lisp_Object arg1, arg2;
817 return getjpi (JPI$_UIC, arg1, 1);
820 static Lisp_Object
821 vms_uic_str (arg1, arg2)
822 Lisp_Object arg1, arg2;
824 return getjpi (JPI$_UIC, arg1, 0);
827 static Lisp_Object
828 vms_username (arg1, arg2)
829 Lisp_Object arg1, arg2;
831 return getjpi (JPI$_USERNAME, arg1, 0);
834 static Lisp_Object
835 vms_version_fn (arg1, arg2)
836 Lisp_Object arg1, arg2;
838 char str[40];
839 int status;
840 int strdsc[2] = { sizeof (str), str };
841 short strlen;
843 status = lib$getsyi (&SYI$_VERSION, 0, strdsc, &strlen, 0, 0);
844 if (! (status & 1))
845 error ("Unable to obtain version: %s", vmserrstr (status));
846 return (make_string (str, strlen));
849 static Lisp_Object
850 vms_trnlog (arg1, arg2)
851 Lisp_Object arg1, arg2;
853 char str[256]; /* Max logical translation is 255 bytes. */
854 int status, symdsc[2];
855 int strdsc[2] = { sizeof (str), str };
856 short length, level;
858 CHECK_STRING (arg1, 0);
859 symdsc[0] = XSTRING (arg1)->size;
860 symdsc[1] = XSTRING (arg1)->data;
861 status = lib$sys_trnlog (symdsc, &length, strdsc);
862 if (! (status & 1))
863 error ("Unable to translate logical name: %s", vmserrstr (status));
864 if (status == SS$_NOTRAN)
865 return (Qnil);
866 return (make_string (str, length));
869 static Lisp_Object
870 vms_symbol (arg1, arg2)
871 Lisp_Object arg1, arg2;
873 char str[1025]; /* Max symbol translation is 1024 bytes. */
874 int status, symdsc[2];
875 int strdsc[2] = { sizeof (str), str };
876 short length, level;
878 CHECK_STRING (arg1, 0);
879 symdsc[0] = XSTRING (arg1)->size;
880 symdsc[1] = XSTRING (arg1)->data;
881 status = lib$get_symbol (symdsc, strdsc, &length, &level);
882 if (! (status & 1)) {
883 if (status == LIB$_NOSUCHSYM)
884 return (Qnil);
885 else
886 error ("Unable to translate symbol: %s", vmserrstr (status));
888 return (make_string (str, length));
891 static Lisp_Object
892 vms_proclist (arg1, arg2)
893 Lisp_Object arg1, arg2;
895 Lisp_Object retval;
896 int id, status, pid;
898 retval = Qnil;
899 pid = -1;
900 for (;;)
902 status = lib$getjpi (&JPI$_PID, &pid, 0, &id);
903 if (status == SS$_NOMOREPROC)
904 break;
905 if (! (status & 1))
906 error ("Unable to get process ID: %s", vmserrstr (status));
907 retval = Fcons (make_number (id), retval);
909 return (Fsort (retval, intern ("<")));
912 DEFUN ("shrink-to-icon", Fshrink_to_icon, Sshrink_to_icon, 0, 0, 0,
913 "If emacs is running in a workstation window, shrink to an icon.")
916 static char result[128];
917 static $DESCRIPTOR (result_descriptor, result);
918 static $DESCRIPTOR (tt_name, "TT:");
919 static int chan = 0;
920 static int buf = 0x9d + ('2'<<8) + ('2'<<16) + (0x9c<<24);
921 int status;
922 static int temp = JPI$_TERMINAL;
924 status = lib$getjpi (&temp, 0, 0, 0, &result_descriptor, 0);
925 if (status != SS$_NORMAL)
926 error ("Unable to determine terminal type.");
927 if (result[0] != 'W' || result[1] != 'T') /* see if workstation */
928 error ("Can't shrink-to-icon on a non workstation terminal");
929 if (!chan) /* assign channel if not assigned */
930 if ((status = sys$assign (&tt_name, &chan, 0, 0)) != SS$_NORMAL)
931 error ("Can't assign terminal, %d", status);
932 status = sys$qiow (0, chan, IO$_WRITEVBLK+IO$M_BREAKTHRU, 0, 0, 0,
933 &buf, 4, 0, 0, 0, 0);
934 if (status != SS$_NORMAL)
935 error ("Can't shrink-to-icon, %d", status);
938 #endif /* VMS4_4 */
940 init_vmsfns ()
942 process_list = 0;
943 input_mbx_chan = 0;
946 syms_of_vmsfns ()
948 defsubr (&Sdefault_subproc_input_handler);
949 defsubr (&Sspawn_subprocess);
950 defsubr (&Ssend_command_to_subprocess);
951 defsubr (&Sstop_subprocess);
952 defsubr (&Ssetprv);
953 #ifdef VMS4_4
954 defsubr (&Svms_system_info);
955 defsubr (&Sshrink_to_icon);
956 #endif /* VMS4_4 */
957 Qdefault_subproc_input_handler = intern ("default-subprocess-input-handler");
958 staticpro (&Qdefault_subproc_input_handler);
960 #endif /* VMS */