blob: 8845b28a9f13c368024ab6fbbeb56fbe319f7aca [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 */
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010015#if !defined(VAX) && !defined(PROTO)
Bram Moolenaar4ffa0702013-12-11 17:12:37 +010016# 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
Bram Moolenaar792f0e32018-02-27 17:27:13 +010021 __union { /* You can treat me as... */
Bram Moolenaar4ffa0702013-12-11 17:12:37 +010022 /* 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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static TT_MODE get_tty(void);
81static void set_tty(int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
83#define EXPL_ALLOC_INC 64
84
85#define EQN(S1,S2,LN) (strncmp(S1,S2,LN) == 0)
Bram Moolenaar6f470022018-04-10 18:47:20 +020086#define SKIP_FOLLOWING_SLASHES(Str) do { while (Str[1] == '/') ++Str; } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
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';
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200241 if (cp = alloc(lengte + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 strcpy((char *)cp, buffer);
243 return(cp);
244 }
245 else if ((sbuf = getenv((char *)lognam)))
246 {
247 lengte = strlen(sbuf) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200248 cp = alloc(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/*
Bram Moolenaar206f0112014-03-12 16:51:55 +0100299 * Convert string to lowercase - most often filename
300 */
301 char *
302vms_tolower( char *name )
303{
304 int i,nlen = strlen(name);
305 for (i = 0; i < nlen; i++)
306 name[i] = TOLOWER_ASC(name[i]);
307 return name;
308}
309
310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 * Convert VMS system() or lib$spawn() return code to Unix-like exit value.
312 */
313 int
314vms_sys_status(int status)
315{
316 if (status != SS$_NORMAL && (status & STS$M_SUCCESS) == 0)
317 return status; /* Command failed. */
318 return 0;
319}
320
321/*
322 * vms_read()
323 * function for low level char input
324 *
325 * Returns: input length
326 */
327 int
328vms_read(char *inbuf, size_t nbytes)
329{
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000330 int status, function, len;
331 TT_MODE tt_mode;
332 ITEM itmlst[2]; /* terminates on everything */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 static long trm_mask[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
334
335 /* whatever happened earlier we need an iochan here */
336 if (!iochan)
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000337 tt_mode = get_tty();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000339 /* important: clean the inbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 memset(inbuf, 0, nbytes);
341
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000342 /* set up the itemlist for the first read */
343 vul_item(&itmlst[0], 0, TRM$_MODIFIERS,
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000344 (char *)( TRM$M_TM_NOECHO | TRM$M_TM_NOEDIT |
345 TRM$M_TM_NOFILTR | TRM$M_TM_TRMNOECHO |
346 TRM$M_TM_NORECALL) , 0);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000347 vul_item(&itmlst[1], sizeof(trm_mask), TRM$_TERM, (char *)&trm_mask, 0);
348
349 /* wait forever for a char */
350 function = (IO$_READLBLK | IO$M_EXTEND);
351 status = sys$qiow(0, iochan, function, &iosb, 0, 0,
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000352 inbuf, nbytes-1, 0, 0, &itmlst, sizeof(itmlst));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000353 len = strlen(inbuf); /* how many chars we got? */
354
Bram Moolenaar82038d72007-05-10 17:15:45 +0000355 /* read immediately the rest in the IO queue */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000356 function = (IO$_READLBLK | IO$M_TIMED | IO$M_ESCAPE | IO$M_NOECHO | IO$M_NOFILTR);
357 status = sys$qiow(0, iochan, function, &iosb, 0, 0,
Bram Moolenaara83c3e02006-03-17 23:10:44 +0000358 inbuf+len, nbytes-1-len, 0, 0, 0, 0);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000359
360 len = strlen(inbuf); /* return the total length */
361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return len;
363}
364
365/*
366 * vms_wproc() is called for each matching filename by decc$to_vms().
367 * We want to save each match for later retrieval.
368 *
369 * Returns: 1 - continue finding matches
Bram Moolenaar82038d72007-05-10 17:15:45 +0000370 * 0 - stop trying to find any further matches
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 */
372 static int
373vms_wproc(char *name, int val)
374{
375 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 static int vms_match_alloced = 0;
377
Bram Moolenaar206f0112014-03-12 16:51:55 +0100378 if (val == DECC$K_FOREIGN ) /* foreign non VMS files are not counting */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 return 1;
380
Bram Moolenaar206f0112014-03-12 16:51:55 +0100381 /* accept all DECC$K_FILE and DECC$K_DIRECTORY */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 if (vms_match_num == 0) {
383 /* first time through, setup some things */
384 if (NULL == vms_fmatch) {
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +0100385 vms_fmatch = ALLOC_MULT(char_u *, EXPL_ALLOC_INC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (!vms_fmatch)
387 return 0;
388 vms_match_alloced = EXPL_ALLOC_INC;
389 vms_match_free = EXPL_ALLOC_INC;
390 }
391 else {
392 /* re-use existing space */
393 vms_match_free = vms_match_alloced;
394 }
395 }
396
Bram Moolenaar206f0112014-03-12 16:51:55 +0100397 /* make matches look uniform */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 vms_remove_version(name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100399 name=vms_tolower(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
401 /* if name already exists, don't add it */
402 for (i = 0; i<vms_match_num; i++) {
403 if (0 == STRCMP((char_u *)name,vms_fmatch[i]))
404 return 1;
405 }
406 if (--vms_match_free == 0) {
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100407 char_u **old_vms_fmatch = vms_fmatch;
408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 /* add more space to store matches */
410 vms_match_alloced += EXPL_ALLOC_INC;
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100411 vms_fmatch = vim_realloc(old_vms_fmatch,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 sizeof(char **) * vms_match_alloced);
413 if (!vms_fmatch)
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100414 {
415 vim_free(old_vms_fmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return 0;
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 vms_match_free = EXPL_ALLOC_INC;
419 }
420 vms_fmatch[vms_match_num] = vim_strsave((char_u *)name);
421
422 ++vms_match_num;
423 return 1;
424}
425
426/*
427 * mch_expand_wildcards this code does wild-card pattern
428 * matching NOT using the shell
429 *
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000430 * return OK for success, FAIL for error (you may lose some
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * memory) and put an error message in *file.
432 *
433 * num_pat number of input patterns
434 * pat array of pointers to input patterns
435 * num_file pointer to number of matched file names
436 * file pointer to array of pointers to matched file names
437 *
438 */
439 int
440mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags)
441{
Bram Moolenaar64404472010-06-26 06:24:45 +0200442 int i, cnt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 char_u buf[MAXPATHL];
Bram Moolenaar206f0112014-03-12 16:51:55 +0100444 char *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 int dir;
446 int files_alloced, files_free;
447
448 *num_file = 0; /* default: no files found */
449 files_alloced = EXPL_ALLOC_INC;
450 files_free = EXPL_ALLOC_INC;
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +0100451 *file = ALLOC_MULT(char_u *, files_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (*file == NULL)
453 {
454 *num_file = 0;
455 return FAIL;
456 }
457 for (i = 0; i < num_pat; i++)
458 {
459 /* expand environment var or home dir */
460 if (vim_strchr(pat[i],'$') || vim_strchr(pat[i],'~'))
461 expand_env(pat[i],buf,MAXPATHL);
462 else
463 STRCPY(buf,pat[i]);
464
465 vms_match_num = 0; /* reset collection counter */
Bram Moolenaar206f0112014-03-12 16:51:55 +0100466 result = decc$translate_vms(vms_fixfilename(buf));
467 if ( (int) result == 0 || (int) result == -1 ) {
468 cnt = 0;
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100469 } else {
Bram Moolenaar206f0112014-03-12 16:51:55 +0100470 cnt = decc$to_vms(result, vms_wproc, 1 /*allow wild*/ , (flags & EW_DIR ? 0:1 ) /*allow directory*/) ;
471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (cnt > 0)
473 cnt = vms_match_num;
474
475 if (cnt < 1)
476 continue;
477
478 for (i = 0; i < cnt; i++)
479 {
480 /* files should exist if expanding interactively */
481 if (!(flags & EW_NOTFOUND) && mch_getperm(vms_fmatch[i]) < 0)
482 continue;
Bram Moolenaara2031822006-03-07 22:29:51 +0000483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 /* do not include directories */
485 dir = (mch_isdir(vms_fmatch[i]));
486 if (( dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
487 continue;
Bram Moolenaara2031822006-03-07 22:29:51 +0000488
489 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +0100490 if (!dir && (flags & EW_EXEC)
491 && !mch_can_exe(vms_fmatch[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +0000492 continue;
493
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 /* allocate memory for pointers */
495 if (--files_free < 1)
496 {
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100497 char_u **old_file = *file;
498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 files_alloced += EXPL_ALLOC_INC;
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100500 *file = vim_realloc(old_file, sizeof(char_u **) * files_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (*file == NULL)
502 {
Bram Moolenaar9625d3d2019-11-30 22:57:53 +0100503 vim_free(old_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 *file = (char_u **)"";
505 *num_file = 0;
506 return(FAIL);
507 }
508 files_free = EXPL_ALLOC_INC;
509 }
510
511 (*file)[*num_file++] = vms_fmatch[i];
512 }
513 }
514 return OK;
515}
516
517 int
518mch_expandpath(garray_T *gap, char_u *path, int flags)
519{
520 int i,cnt = 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +0100521 char *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
Bram Moolenaar206f0112014-03-12 16:51:55 +0100523 vms_match_num = 0;
524 /* the result from the decc$translate_vms needs to be handled */
525 /* otherwise it might create ACCVIO error in decc$to_vms */
526 result = decc$translate_vms(vms_fixfilename(path));
527 if ( (int) result == 0 || (int) result == -1 ) {
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100528 cnt = 0;
529 } else {
530 cnt = decc$to_vms(result, vms_wproc, 1 /*allow_wild*/, (flags & EW_DIR ? 0:1 ) /*allow directory*/);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (cnt > 0)
533 cnt = vms_match_num;
534 for (i = 0; i < cnt; i++)
535 {
536 if (mch_getperm(vms_fmatch[i]) >= 0) /* add existing file */
537 addfile(gap, vms_fmatch[i], flags);
538 }
539 return cnt;
540}
541
542/*
543 * attempt to translate a mixed unix-vms file specification to pure vms
544 */
545 static void
546vms_unix_mixed_filespec(char *in, char *out)
547{
548 char *lastcolon;
549 char *end_of_dir;
550 char ch;
551 int len;
Bram Moolenaar206f0112014-03-12 16:51:55 +0100552 char *out_str=out;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 /* copy vms filename portion up to last colon
555 * (node and/or disk)
556 */
557 lastcolon = strrchr(in, ':'); /* find last colon */
558 if (lastcolon != NULL) {
559 len = lastcolon - in + 1;
560 strncpy(out, in, len);
561 out += len;
562 in += len;
563 }
564
565 end_of_dir = NULL; /* default: no directory */
566
567 /* start of directory portion */
568 ch = *in;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000569 if ((ch == '[') || (ch == '/') || (ch == '<')) { /* start of directory(s) ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 ch = '[';
571 SKIP_FOLLOWING_SLASHES(in);
572 } else if (EQN(in, "../", 3)) { /* Unix parent directory? */
573 *out++ = '[';
574 *out++ = '-';
575 end_of_dir = out;
576 ch = '.';
577 in += 2;
578 SKIP_FOLLOWING_SLASHES(in);
579 } else { /* not a special character */
580 while (EQN(in, "./", 2)) { /* Ignore Unix "current dir" */
581 in += 2;
582 SKIP_FOLLOWING_SLASHES(in);
583 }
584 if (strchr(in, '/') == NULL) { /* any more Unix directories ? */
585 strcpy(out, in); /* No - get rest of the spec */
586 return;
587 } else {
588 *out++ = '['; /* Yes, denote a Vms subdirectory */
589 ch = '.';
590 --in;
591 }
592 }
593
594 /* if we get here, there is a directory part of the filename */
595
596 /* initialize output file spec */
597 *out++ = ch;
598 ++in;
599
600 while (*in != '\0') {
601 ch = *in;
602 if ((ch == ']') || (ch == '/') || (ch == '>') ) { /* end of (sub)directory ? */
603 end_of_dir = out;
604 ch = '.';
605 SKIP_FOLLOWING_SLASHES(in);
606 }
607 else if (EQN(in, "../", 3)) { /* Unix parent directory? */
608 *out++ = '-';
609 end_of_dir = out;
610 ch = '.';
611 in += 2;
612 SKIP_FOLLOWING_SLASHES(in);
613 }
614 else {
615 while (EQN(in, "./", 2)) { /* Ignore Unix "current dir" */
616 end_of_dir = out;
617 in += 2;
618 SKIP_FOLLOWING_SLASHES(in);
619 ch = *in;
620 }
621 }
622
623 /* Place next character into output file spec */
624 *out++ = ch;
625 ++in;
626 }
627
628 *out = '\0'; /* Terminate output file spec */
629
630 if (end_of_dir != NULL) /* Terminate directory portion */
631 *end_of_dir = ']';
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
635 * for decc$to_vms in vms_fixfilename
636 */
637 static int
638vms_fspec_proc(char *fil, int val)
639{
640 strcpy(Fspec_Rms,fil);
641 return(1);
642}
643
644/*
645 * change unix and mixed filenames to VMS
646 */
647 void *
648vms_fixfilename(void *instring)
649{
650 static char *buf = NULL;
651 static size_t buflen = 0;
652 size_t len;
653
654 /* get a big-enough buffer */
655 len = strlen(instring) + 1;
656 if (len > buflen)
657 {
658 buflen = len + 128;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200659 buf = vim_realloc(buf, buflen * sizeof(char));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661
662#ifdef DEBUG
663 char *tmpbuf = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200664 tmpbuf = ALLOC_MULT(char, buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 strcpy(tmpbuf, instring);
666#endif
667
668 Fspec_Rms = buf; /* for decc$to_vms */
669
Bram Moolenaar9964e462007-05-05 17:54:07 +0000670 if (strchr(instring,'/') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /* It is already a VMS file spec */
672 strcpy(buf, instring);
Bram Moolenaar9964e462007-05-05 17:54:07 +0000673 else if (strchr(instring,'"') == NULL) /* password in the path? */
674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 /* Seems it is a regular file, let guess that it is pure Unix fspec */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000676 if (decc$to_vms(instring, vms_fspec_proc, 0, 0) <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /* No... it must be mixed */
678 vms_unix_mixed_filespec(instring, buf);
679 }
680 else
681 /* we have a password in the path */
682 /* decc$ functions can not handle */
683 /* this is our only hope to resolv */
684 vms_unix_mixed_filespec(instring, buf);
685
686 return buf;
687}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689/*
690 * Remove version number from file name
691 * we need it in some special cases as:
692 * creating swap file name and writing new file
693 */
694 void
695vms_remove_version(void * fname)
696{
697 char_u *cp;
698 char_u *fp;
699
700 if ((cp = vim_strchr( fname, ';')) != NULL) /* remove version */
701 *cp = '\0';
702 else if ((cp = vim_strrchr( fname, '.')) != NULL )
703 {
704 if ((fp = vim_strrchr( fname, ']')) != NULL ) {;}
705 else if ((fp = vim_strrchr( fname, '>')) != NULL ) {;}
706 else fp = fname;
707
708 while ( *fp != '\0' && fp < cp )
709 if ( *fp++ == '.' )
710 *cp = '\0';
711 }
712 return ;
713}
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100714
715struct typeahead_st {
716 unsigned short numchars;
717 unsigned char firstchar;
718 unsigned char reserved0;
719 unsigned long reserved1;
720} typeahead;
721
722/*
723 * Wait "msec" msec until a character is available from file descriptor "fd".
724 * "msec" == 0 will check for characters once.
725 * "msec" == -1 will block until a character is available.
726 */
727 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100728RealWaitForChar(
729 int fd UNUSED, /* always read from iochan */
730 long msec,
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100731 int *check_for_gpm UNUSED,
732 int *interrupted)
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100733{
734 int status;
735 struct _generic_64 time_curr;
736 struct _generic_64 time_diff;
737 struct _generic_64 time_out;
738 unsigned int convert_operation = LIB$K_DELTA_SECONDS_F;
Bram Moolenaar206f0112014-03-12 16:51:55 +0100739 float sec =(float) msec/1000;
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100740
741 /* make sure the iochan is set */
742 if (!iochan)
743 get_tty();
744
Bram Moolenaar206f0112014-03-12 16:51:55 +0100745 if (sec > 0) {
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100746 /* time-out specified; convert it to absolute time */
Bram Moolenaar206f0112014-03-12 16:51:55 +0100747 /* sec>0 requirement of lib$cvtf_to_internal_time()*/
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100748
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100749 /* get current time (number of 100ns ticks since the VMS Epoch) */
750 status = sys$gettim(&time_curr);
751 if (status != SS$_NORMAL)
752 return 0; /* error */
753 /* construct the delta time */
Bram Moolenaar206f0112014-03-12 16:51:55 +0100754#if __G_FLOAT==0
755# ifndef VAX
756 /* IEEE is default on IA64, but can be used on Alpha too - but not on VAX */
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100757 status = lib$cvts_to_internal_time(
758 &convert_operation, &sec, &time_diff);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100759# endif
760#else /* default on Alpha and VAX */
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100761 status = lib$cvtf_to_internal_time(
Bram Moolenaar206f0112014-03-12 16:51:55 +0100762 &convert_operation, &sec, &time_diff);
763#endif
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100764 if (status != LIB$_NORMAL)
765 return 0; /* error */
766 /* add them up */
767 status = lib$add_times(
768 &time_curr,
769 &time_diff,
770 &time_out);
771 if (status != LIB$_NORMAL)
772 return 0; /* error */
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100773 }
774
775 while (TRUE) {
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100776 /* select() */
777 status = sys$qiow(0, iochan, IO$_SENSEMODE | IO$M_TYPEAHDCNT, iosb,
778 0, 0, &typeahead, 8, 0, 0, 0, 0);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100779 if (status != SS$_NORMAL || (iosb[0] & 0xFFFF) != SS$_NORMAL)
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100780 return 0; /* error */
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100781
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100782 if (typeahead.numchars)
783 return 1; /* ready to read */
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100784
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100785 /* there's nothing to read; what now? */
786 if (msec == 0) {
787 /* immediate time-out; return impatiently */
788 return 0;
789 } else if (msec < 0) {
790 /* no time-out; wait on indefinitely */
Bram Moolenaar6537c422018-04-23 20:46:16 +0200791 return 1; /* fakeout to force a wait in vms_read() */
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100792 } else {
793 /* time-out needs to be checked */
794 status = sys$gettim(&time_curr);
795 if (status != SS$_NORMAL)
796 return 0; /* error */
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100797
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100798 status = lib$sub_times(
799 &time_out,
800 &time_curr,
801 &time_diff);
802 if (status != LIB$_NORMAL)
803 return 0; /* error, incl. time_diff < 0 (i.e. time-out) */
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100804
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100805 /* otherwise wait some more */
806 }
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100807 }
808}