blob: 24ff5d07d4f52778472302d1c93b349c4e0fb2a3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * VMS port by Henk Elbers
5 * VMS deport by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12#include "vim.h"
13
Bram Moolenaar4ffa0702013-12-11 17:12:37 +010014/* define _generic_64 for use in time functions */
15#ifndef VAX
16# include <gen64def.h>
17#else
18/* based on Alpha's gen64def.h; the file is absent on VAX */
19typedef struct _generic_64 {
20# pragma __nomember_alignment
21 __union { /* You can treat me as... */
22 /* long long is not available on VAXen */
23 /* unsigned __int64 gen64$q_quadword; ...a single 64-bit value, or */
24
25 unsigned int gen64$l_longword [2]; /* ...two 32-bit values, or */
26 unsigned short int gen64$w_word [4]; /* ...four 16-bit values */
27 } gen64$r_quad_overlay;
28} GENERIC_64;
29#endif
30
Bram Moolenaar071d4272004-06-13 20:20:40 +000031typedef struct
32{
33 char class;
34 char type;
35 short width;
36 union
37 {
38 struct
39 {
40 char _basic[3];
41 char length;
42 } y;
43 int basic;
44 } x;
45 int extended;
46} TT_MODE;
47
48typedef struct
49{
50 short buflen;
51 short itemcode;
52 char *bufadrs;
53 int *retlen;
54} ITEM;
55
56typedef struct
57{
58 ITEM equ;
59 int nul;
60} ITMLST1;
61
62typedef struct
63{
64 ITEM index;
65 ITEM string;
66 int nul;
67} ITMLST2;
68
69static TT_MODE orgmode;
70static short iochan; /* TTY I/O channel */
71static short iosb[4]; /* IO status block */
72
73static int vms_match_num = 0;
74static int vms_match_free = 0;
75static char_u **vms_fmatch = NULL;
76static char *Fspec_Rms; /* rms file spec, passed implicitly between routines */
77
78
79
80static TT_MODE get_tty __ARGS((void));
81static void set_tty __ARGS((int row, int col));
82
83#define EXPL_ALLOC_INC 64
84
85#define EQN(S1,S2,LN) (strncmp(S1,S2,LN) == 0)
86#define SKIP_FOLLOWING_SLASHES(Str) while (Str[1] == '/') ++Str
87
88
89/*
90 * vul_desc vult een descriptor met een string en de lengte
91 * hier van.
92 */
93 static void
94vul_desc(DESC *des, char *str)
95{
96 des->dsc$b_dtype = DSC$K_DTYPE_T;
97 des->dsc$b_class = DSC$K_CLASS_S;
98 des->dsc$a_pointer = str;
99 des->dsc$w_length = str ? strlen(str) : 0;
100}
101
102/*
103 * vul_item vult een item met een aantal waarden
104 */
105 static void
106vul_item(ITEM *itm, short len, short cod, char *adr, int *ret)
107{
108 itm->buflen = len;
109 itm->itemcode = cod;
110 itm->bufadrs = adr;
111 itm->retlen = ret;
112}
113
114 void
115mch_settmode(int tmode)
116{
117 int status;
118
119 if ( tmode == TMODE_RAW )
120 set_tty(0, 0);
121 else{
122 switch (orgmode.width)
123 {
124 case 132: OUT_STR_NF((char_u *)"\033[?3h\033>"); break;
125 case 80: OUT_STR_NF((char_u *)"\033[?3l\033>"); break;
126 default: break;
127 }
128 out_flush();
129 status = sys$qiow(0, iochan, IO$_SETMODE, iosb, 0, 0,
130 &orgmode, sizeof(TT_MODE), 0,0,0,0);
131 if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
132 return;
133 (void)sys$dassgn(iochan);
134 iochan = 0;
135 }
136}
137
138 static void
139set_tty(int row, int col)
140{
141 int status;
142 TT_MODE newmode; /* New TTY mode bits */
143 static short first_time = TRUE;
144
145 if (first_time)
146 {
147 orgmode = get_tty();
148 first_time = FALSE;
149 }
150 newmode = get_tty();
151 if (col)
152 newmode.width = col;
153 if (row)
154 newmode.x.y.length = row;
155 newmode.x.basic |= (TT$M_NOECHO | TT$M_HOSTSYNC);
156 newmode.x.basic &= ~TT$M_TTSYNC;
157 newmode.extended |= TT2$M_PASTHRU;
158 status = sys$qiow(0, iochan, IO$_SETMODE, iosb, 0, 0,
159 &newmode, sizeof(newmode), 0, 0, 0, 0);
160 if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
161 return;
162}
163
164 static TT_MODE
165get_tty(void)
166{
167
168 static $DESCRIPTOR(odsc,"SYS$OUTPUT"); /* output descriptor */
169
170 int status;
171 TT_MODE tt_mode;
172
173 if (!iochan)
174 status = sys$assign(&odsc,&iochan,0,0);
175
176 status = sys$qiow(0, iochan, IO$_SENSEMODE, iosb, 0, 0,
177 &tt_mode, sizeof(tt_mode), 0, 0, 0, 0);
178 if (status != SS$_NORMAL || (iosb[0] & 0xFFFF) != SS$_NORMAL)
179 {
180 tt_mode.width = 0;
181 tt_mode.type = 0;
182 tt_mode.class = 0;
183 tt_mode.x.basic = 0;
184 tt_mode.x.y.length = 0;
185 tt_mode.extended = 0;
186 }
187 return(tt_mode);
188}
189
190/*
191 * Get the current window size in Rows and Columns.
192 */
193 int
194mch_get_shellsize(void)
195{
196 TT_MODE tmode;
197
198 tmode = get_tty(); /* get size from VMS */
199 Columns = tmode.width;
200 Rows = tmode.x.y.length;
201 return OK;
202}
203
204/*
205 * Try to set the window size to Rows and new_Columns.
206 */
207 void
208mch_set_shellsize(void)
209{
210 set_tty(Rows, Columns);
211 switch (Columns)
212 {
213 case 132: OUT_STR_NF((char_u *)"\033[?3h\033>"); break;
214 case 80: OUT_STR_NF((char_u *)"\033[?3l\033>"); break;
215 default: break;
216 }
217 out_flush();
218 screen_start();
219}
220
221 char_u *
222mch_getenv(char_u *lognam)
223{
224 DESC d_file_dev, d_lognam ;
225 static char buffer[LNM$C_NAMLENGTH+1];
226 char_u *cp = NULL;
227 unsigned long attrib;
228 int lengte = 0, dum = 0, idx = 0;
229 ITMLST2 itmlst;
230 char *sbuf = NULL;
231
232 vul_desc(&d_lognam, (char *)lognam);
233 vul_desc(&d_file_dev, "LNM$FILE_DEV");
234 attrib = LNM$M_CASE_BLIND;
235 vul_item(&itmlst.index, sizeof(int), LNM$_INDEX, (char *)&idx, &dum);
236 vul_item(&itmlst.string, LNM$C_NAMLENGTH, LNM$_STRING, buffer, &lengte);
237 itmlst.nul = 0;
238 if (sys$trnlnm(&attrib, &d_file_dev, &d_lognam, NULL,&itmlst) == SS$_NORMAL)
239 {
240 buffer[lengte] = '\0';
241 if (cp = (char_u *)alloc((unsigned)(lengte+1)))
242 strcpy((char *)cp, buffer);
243 return(cp);
244 }
245 else if ((sbuf = getenv((char *)lognam)))
246 {
247 lengte = strlen(sbuf) + 1;
Bram Moolenaar12806c82008-11-12 12:36:30 +0000248 cp = (char_u *)alloc((size_t)lengte);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (cp)
250 strcpy((char *)cp, sbuf);
251 return cp;
252 }
253 else
254 return(NULL);
255}
256
257/*
258 * mch_setenv VMS version of setenv()
259 */
260 int
261mch_setenv(char *var, char *value, int x)
262{
263 int res, dum;
264 long attrib = 0L;
265 char acmode = PSL$C_SUPER; /* needs SYSNAM privilege */
266 DESC tabnam, lognam;
267 ITMLST1 itmlst;
268
269 vul_desc(&tabnam, "LNM$JOB");
270 vul_desc(&lognam, var);
271 vul_item(&itmlst.equ, value ? strlen(value) : 0, value ? LNM$_STRING : 0,
272 value, &dum);
273 itmlst.nul = 0;
274 res = sys$crelnm(&attrib, &tabnam, &lognam, &acmode, &itmlst);
275 return((res == 1) ? 0 : -1);
276}
277
278 int
279vms_sys(char *cmd, char *out, char *inp)
280{
281 DESC cdsc, odsc, idsc;
282 long status;
283
284 if (cmd)
285 vul_desc(&cdsc, cmd);
286 if (out)
287 vul_desc(&odsc, out);
288 if (inp)
289 vul_desc(&idsc, inp);
290
291 lib$spawn(cmd ? &cdsc : NULL, /* command string */
292 inp ? &idsc : NULL, /* input file */
293 out ? &odsc : NULL, /* output file */
294 0, 0, 0, &status, 0, 0, 0, 0, 0, 0);
295 return status;
296}
297
298/*
299 * Convert VMS system() or lib$spawn() return code to Unix-like exit value.
300 */
301 int
302vms_sys_status(int status)
303{
304 if (status != SS$_NORMAL && (status & STS$M_SUCCESS) == 0)
305 return status; /* Command failed. */
306 return 0;
307}
308
309/*
310 * vms_read()
311 * function for low level char input
312 *
313 * Returns: input length
314 */
315 int
316vms_read(char *inbuf, size_t nbytes)
317{
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000318 int status, function, len;
319 TT_MODE tt_mode;
320 ITEM itmlst[2]; /* terminates on everything */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 static long trm_mask[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
322
323 /* whatever happened earlier we need an iochan here */
324 if (!iochan)
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000325 tt_mode = get_tty();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000327 /* important: clean the inbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 memset(inbuf, 0, nbytes);
329
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000330 /* set up the itemlist for the first read */
331 vul_item(&itmlst[0], 0, TRM$_MODIFIERS,
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000332 (char *)( TRM$M_TM_NOECHO | TRM$M_TM_NOEDIT |
333 TRM$M_TM_NOFILTR | TRM$M_TM_TRMNOECHO |
334 TRM$M_TM_NORECALL) , 0);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000335 vul_item(&itmlst[1], sizeof(trm_mask), TRM$_TERM, (char *)&trm_mask, 0);
336
337 /* wait forever for a char */
338 function = (IO$_READLBLK | IO$M_EXTEND);
339 status = sys$qiow(0, iochan, function, &iosb, 0, 0,
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000340 inbuf, nbytes-1, 0, 0, &itmlst, sizeof(itmlst));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000341 len = strlen(inbuf); /* how many chars we got? */
342
Bram Moolenaar82038d72007-05-10 17:15:45 +0000343 /* read immediately the rest in the IO queue */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000344 function = (IO$_READLBLK | IO$M_TIMED | IO$M_ESCAPE | IO$M_NOECHO | IO$M_NOFILTR);
345 status = sys$qiow(0, iochan, function, &iosb, 0, 0,
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000346 inbuf+len, nbytes-1-len, 0, 0, 0, 0);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000347
348 len = strlen(inbuf); /* return the total length */
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 return len;
351}
352
353/*
354 * vms_wproc() is called for each matching filename by decc$to_vms().
355 * We want to save each match for later retrieval.
356 *
357 * Returns: 1 - continue finding matches
Bram Moolenaar82038d72007-05-10 17:15:45 +0000358 * 0 - stop trying to find any further matches
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 */
360 static int
361vms_wproc(char *name, int val)
362{
363 int i;
364 int nlen;
365 static int vms_match_alloced = 0;
366
Bram Moolenaar82038d72007-05-10 17:15:45 +0000367 if (val != DECC$K_FILE) /* Directories and foreign non VMS files are not
368 counting */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 return 1;
370
371 if (vms_match_num == 0) {
372 /* first time through, setup some things */
373 if (NULL == vms_fmatch) {
374 vms_fmatch = (char_u **)alloc(EXPL_ALLOC_INC * sizeof(char *));
375 if (!vms_fmatch)
376 return 0;
377 vms_match_alloced = EXPL_ALLOC_INC;
378 vms_match_free = EXPL_ALLOC_INC;
379 }
380 else {
381 /* re-use existing space */
382 vms_match_free = vms_match_alloced;
383 }
384 }
385
386 vms_remove_version(name);
387
388 /* convert filename to lowercase */
389 nlen = strlen(name);
390 for (i = 0; i < nlen; i++)
391 name[i] = TOLOWER_ASC(name[i]);
392
393 /* if name already exists, don't add it */
394 for (i = 0; i<vms_match_num; i++) {
395 if (0 == STRCMP((char_u *)name,vms_fmatch[i]))
396 return 1;
397 }
398 if (--vms_match_free == 0) {
399 /* add more space to store matches */
400 vms_match_alloced += EXPL_ALLOC_INC;
Bram Moolenaar12806c82008-11-12 12:36:30 +0000401 vms_fmatch = (char_u **)vim_realloc(vms_fmatch,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 sizeof(char **) * vms_match_alloced);
403 if (!vms_fmatch)
404 return 0;
405 vms_match_free = EXPL_ALLOC_INC;
406 }
407 vms_fmatch[vms_match_num] = vim_strsave((char_u *)name);
408
409 ++vms_match_num;
410 return 1;
411}
412
413/*
414 * mch_expand_wildcards this code does wild-card pattern
415 * matching NOT using the shell
416 *
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000417 * return OK for success, FAIL for error (you may lose some
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * memory) and put an error message in *file.
419 *
420 * num_pat number of input patterns
421 * pat array of pointers to input patterns
422 * num_file pointer to number of matched file names
423 * file pointer to array of pointers to matched file names
424 *
425 */
426 int
427mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags)
428{
Bram Moolenaar64404472010-06-26 06:24:45 +0200429 int i, cnt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 char_u buf[MAXPATHL];
431 int dir;
432 int files_alloced, files_free;
433
434 *num_file = 0; /* default: no files found */
435 files_alloced = EXPL_ALLOC_INC;
436 files_free = EXPL_ALLOC_INC;
437 *file = (char_u **) alloc(sizeof(char_u **) * files_alloced);
438 if (*file == NULL)
439 {
440 *num_file = 0;
441 return FAIL;
442 }
443 for (i = 0; i < num_pat; i++)
444 {
445 /* expand environment var or home dir */
446 if (vim_strchr(pat[i],'$') || vim_strchr(pat[i],'~'))
447 expand_env(pat[i],buf,MAXPATHL);
448 else
449 STRCPY(buf,pat[i]);
450
451 vms_match_num = 0; /* reset collection counter */
452 cnt = decc$to_vms(decc$translate_vms(vms_fixfilename(buf)), vms_wproc, 1, 0);
453 /* allow wild, no dir */
454 if (cnt > 0)
455 cnt = vms_match_num;
456
457 if (cnt < 1)
458 continue;
459
460 for (i = 0; i < cnt; i++)
461 {
462 /* files should exist if expanding interactively */
463 if (!(flags & EW_NOTFOUND) && mch_getperm(vms_fmatch[i]) < 0)
464 continue;
Bram Moolenaara2031822006-03-07 22:29:51 +0000465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 /* do not include directories */
467 dir = (mch_isdir(vms_fmatch[i]));
468 if (( dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
469 continue;
Bram Moolenaara2031822006-03-07 22:29:51 +0000470
471 /* Skip files that are not executable if we check for that. */
472 if (!dir && (flags & EW_EXEC) && !mch_can_exe(vms_fmatch[i]))
473 continue;
474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 /* allocate memory for pointers */
476 if (--files_free < 1)
477 {
478 files_alloced += EXPL_ALLOC_INC;
Bram Moolenaar12806c82008-11-12 12:36:30 +0000479 *file = (char_u **)vim_realloc(*file,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 sizeof(char_u **) * files_alloced);
481 if (*file == NULL)
482 {
483 *file = (char_u **)"";
484 *num_file = 0;
485 return(FAIL);
486 }
487 files_free = EXPL_ALLOC_INC;
488 }
489
490 (*file)[*num_file++] = vms_fmatch[i];
491 }
492 }
493 return OK;
494}
495
496 int
497mch_expandpath(garray_T *gap, char_u *path, int flags)
498{
499 int i,cnt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 vms_match_num = 0;
501
502 cnt = decc$to_vms(decc$translate_vms(vms_fixfilename(path)), vms_wproc, 1, 0);
503 /* allow wild, no dir */
504 if (cnt > 0)
505 cnt = vms_match_num;
506 for (i = 0; i < cnt; i++)
507 {
508 if (mch_getperm(vms_fmatch[i]) >= 0) /* add existing file */
509 addfile(gap, vms_fmatch[i], flags);
510 }
511 return cnt;
512}
513
514/*
515 * attempt to translate a mixed unix-vms file specification to pure vms
516 */
517 static void
518vms_unix_mixed_filespec(char *in, char *out)
519{
520 char *lastcolon;
521 char *end_of_dir;
522 char ch;
523 int len;
524
525 /* copy vms filename portion up to last colon
526 * (node and/or disk)
527 */
528 lastcolon = strrchr(in, ':'); /* find last colon */
529 if (lastcolon != NULL) {
530 len = lastcolon - in + 1;
531 strncpy(out, in, len);
532 out += len;
533 in += len;
534 }
535
536 end_of_dir = NULL; /* default: no directory */
537
538 /* start of directory portion */
539 ch = *in;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000540 if ((ch == '[') || (ch == '/') || (ch == '<')) { /* start of directory(s) ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 ch = '[';
542 SKIP_FOLLOWING_SLASHES(in);
543 } else if (EQN(in, "../", 3)) { /* Unix parent directory? */
544 *out++ = '[';
545 *out++ = '-';
546 end_of_dir = out;
547 ch = '.';
548 in += 2;
549 SKIP_FOLLOWING_SLASHES(in);
550 } else { /* not a special character */
551 while (EQN(in, "./", 2)) { /* Ignore Unix "current dir" */
552 in += 2;
553 SKIP_FOLLOWING_SLASHES(in);
554 }
555 if (strchr(in, '/') == NULL) { /* any more Unix directories ? */
556 strcpy(out, in); /* No - get rest of the spec */
557 return;
558 } else {
559 *out++ = '['; /* Yes, denote a Vms subdirectory */
560 ch = '.';
561 --in;
562 }
563 }
564
565 /* if we get here, there is a directory part of the filename */
566
567 /* initialize output file spec */
568 *out++ = ch;
569 ++in;
570
571 while (*in != '\0') {
572 ch = *in;
573 if ((ch == ']') || (ch == '/') || (ch == '>') ) { /* end of (sub)directory ? */
574 end_of_dir = out;
575 ch = '.';
576 SKIP_FOLLOWING_SLASHES(in);
577 }
578 else if (EQN(in, "../", 3)) { /* Unix parent directory? */
579 *out++ = '-';
580 end_of_dir = out;
581 ch = '.';
582 in += 2;
583 SKIP_FOLLOWING_SLASHES(in);
584 }
585 else {
586 while (EQN(in, "./", 2)) { /* Ignore Unix "current dir" */
587 end_of_dir = out;
588 in += 2;
589 SKIP_FOLLOWING_SLASHES(in);
590 ch = *in;
591 }
592 }
593
594 /* Place next character into output file spec */
595 *out++ = ch;
596 ++in;
597 }
598
599 *out = '\0'; /* Terminate output file spec */
600
601 if (end_of_dir != NULL) /* Terminate directory portion */
602 *end_of_dir = ']';
603}
604
605
606/*
607 * for decc$to_vms in vms_fixfilename
608 */
609 static int
610vms_fspec_proc(char *fil, int val)
611{
612 strcpy(Fspec_Rms,fil);
613 return(1);
614}
615
616/*
617 * change unix and mixed filenames to VMS
618 */
619 void *
620vms_fixfilename(void *instring)
621{
622 static char *buf = NULL;
623 static size_t buflen = 0;
624 size_t len;
625
626 /* get a big-enough buffer */
627 len = strlen(instring) + 1;
628 if (len > buflen)
629 {
630 buflen = len + 128;
631 if (buf)
Bram Moolenaar12806c82008-11-12 12:36:30 +0000632 buf = (char *)vim_realloc(buf, buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 else
Bram Moolenaar12806c82008-11-12 12:36:30 +0000634 buf = (char *)alloc(buflen * sizeof(char));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636
637#ifdef DEBUG
638 char *tmpbuf = NULL;
Bram Moolenaar12806c82008-11-12 12:36:30 +0000639 tmpbuf = (char *)alloc(buflen * sizeof(char));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 strcpy(tmpbuf, instring);
641#endif
642
643 Fspec_Rms = buf; /* for decc$to_vms */
644
Bram Moolenaar9964e462007-05-05 17:54:07 +0000645 if (strchr(instring,'/') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /* It is already a VMS file spec */
647 strcpy(buf, instring);
Bram Moolenaar9964e462007-05-05 17:54:07 +0000648 else if (strchr(instring,'"') == NULL) /* password in the path? */
649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 /* Seems it is a regular file, let guess that it is pure Unix fspec */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000651 if (decc$to_vms(instring, vms_fspec_proc, 0, 0) <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 /* No... it must be mixed */
653 vms_unix_mixed_filespec(instring, buf);
654 }
655 else
656 /* we have a password in the path */
657 /* decc$ functions can not handle */
658 /* this is our only hope to resolv */
659 vms_unix_mixed_filespec(instring, buf);
660
661 return buf;
662}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664/*
665 * Remove version number from file name
666 * we need it in some special cases as:
667 * creating swap file name and writing new file
668 */
669 void
670vms_remove_version(void * fname)
671{
672 char_u *cp;
673 char_u *fp;
674
675 if ((cp = vim_strchr( fname, ';')) != NULL) /* remove version */
676 *cp = '\0';
677 else if ((cp = vim_strrchr( fname, '.')) != NULL )
678 {
679 if ((fp = vim_strrchr( fname, ']')) != NULL ) {;}
680 else if ((fp = vim_strrchr( fname, '>')) != NULL ) {;}
681 else fp = fname;
682
683 while ( *fp != '\0' && fp < cp )
684 if ( *fp++ == '.' )
685 *cp = '\0';
686 }
687 return ;
688}
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100689
690struct typeahead_st {
691 unsigned short numchars;
692 unsigned char firstchar;
693 unsigned char reserved0;
694 unsigned long reserved1;
695} typeahead;
696
697/*
698 * Wait "msec" msec until a character is available from file descriptor "fd".
699 * "msec" == 0 will check for characters once.
700 * "msec" == -1 will block until a character is available.
701 */
702 int
703RealWaitForChar(fd, msec, check_for_gpm)
704 int fd UNUSED; /* always read from iochan */
705 long msec;
706 int *check_for_gpm UNUSED;
707{
708 int status;
709 struct _generic_64 time_curr;
710 struct _generic_64 time_diff;
711 struct _generic_64 time_out;
712 unsigned int convert_operation = LIB$K_DELTA_SECONDS_F;
713 float sec = (float) msec / 1000;
714
715 /* make sure the iochan is set */
716 if (!iochan)
717 get_tty();
718
719 if (msec > 0) {
720 /* time-out specified; convert it to absolute time */
721
722 /* get current time (number of 100ns ticks since the VMS Epoch) */
723 status = sys$gettim(&time_curr);
724 if (status != SS$_NORMAL)
725 return 0; /* error */
726
727 /* construct the delta time */
728 status = lib$cvtf_to_internal_time(
729 &convert_operation, &sec, &time_diff);
730 if (status != LIB$_NORMAL)
731 return 0; /* error */
732
733 /* add them up */
734 status = lib$add_times(
735 &time_curr,
736 &time_diff,
737 &time_out);
738 if (status != LIB$_NORMAL)
739 return 0; /* error */
740 }
741
742 while (TRUE) {
743 /* select() */
744 status = sys$qiow(0, iochan, IO$_SENSEMODE | IO$M_TYPEAHDCNT, iosb,
745 0, 0, &typeahead, 8, 0, 0, 0, 0);
746 if (status != SS$_NORMAL || (iosb[0] & 0xFFFF) != SS$_NORMAL)
747 return 0; /* error */
748
749 if (typeahead.numchars)
750 return 1; /* ready to read */
751
752 /* there's nothing to read; what now? */
753 if (msec == 0) {
754 /* immediate time-out; return impatiently */
755 return 0;
756 }
757 else if (msec < 0) {
758 /* no time-out; wait on indefinitely */
759 continue;
760 }
761 else {
762 /* time-out needs to be checked */
763 status = sys$gettim(&time_curr);
764 if (status != SS$_NORMAL)
765 return 0; /* error */
766
767 status = lib$sub_times(
768 &time_out,
769 &time_curr,
770 &time_diff);
771 if (status != LIB$_NORMAL)
772 return 0; /* error, incl. time_diff < 0 (i.e. time-out) */
773
774 /* otherwise wait some more */
775 }
776 }
777}