blob: 6fd05d5038605bd9dae8c52a6fccde4927e15f06 [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
14typedef struct
15{
16 char class;
17 char type;
18 short width;
19 union
20 {
21 struct
22 {
23 char _basic[3];
24 char length;
25 } y;
26 int basic;
27 } x;
28 int extended;
29} TT_MODE;
30
31typedef struct
32{
33 short buflen;
34 short itemcode;
35 char *bufadrs;
36 int *retlen;
37} ITEM;
38
39typedef struct
40{
41 ITEM equ;
42 int nul;
43} ITMLST1;
44
45typedef struct
46{
47 ITEM index;
48 ITEM string;
49 int nul;
50} ITMLST2;
51
52static TT_MODE orgmode;
53static short iochan; /* TTY I/O channel */
54static short iosb[4]; /* IO status block */
55
56static int vms_match_num = 0;
57static int vms_match_free = 0;
58static char_u **vms_fmatch = NULL;
59static char *Fspec_Rms; /* rms file spec, passed implicitly between routines */
60
61
62
63static TT_MODE get_tty __ARGS((void));
64static void set_tty __ARGS((int row, int col));
65
66#define EXPL_ALLOC_INC 64
67
68#define EQN(S1,S2,LN) (strncmp(S1,S2,LN) == 0)
69#define SKIP_FOLLOWING_SLASHES(Str) while (Str[1] == '/') ++Str
70
71
72/*
73 * vul_desc vult een descriptor met een string en de lengte
74 * hier van.
75 */
76 static void
77vul_desc(DESC *des, char *str)
78{
79 des->dsc$b_dtype = DSC$K_DTYPE_T;
80 des->dsc$b_class = DSC$K_CLASS_S;
81 des->dsc$a_pointer = str;
82 des->dsc$w_length = str ? strlen(str) : 0;
83}
84
85/*
86 * vul_item vult een item met een aantal waarden
87 */
88 static void
89vul_item(ITEM *itm, short len, short cod, char *adr, int *ret)
90{
91 itm->buflen = len;
92 itm->itemcode = cod;
93 itm->bufadrs = adr;
94 itm->retlen = ret;
95}
96
97 void
98mch_settmode(int tmode)
99{
100 int status;
101
102 if ( tmode == TMODE_RAW )
103 set_tty(0, 0);
104 else{
105 switch (orgmode.width)
106 {
107 case 132: OUT_STR_NF((char_u *)"\033[?3h\033>"); break;
108 case 80: OUT_STR_NF((char_u *)"\033[?3l\033>"); break;
109 default: break;
110 }
111 out_flush();
112 status = sys$qiow(0, iochan, IO$_SETMODE, iosb, 0, 0,
113 &orgmode, sizeof(TT_MODE), 0,0,0,0);
114 if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
115 return;
116 (void)sys$dassgn(iochan);
117 iochan = 0;
118 }
119}
120
121 static void
122set_tty(int row, int col)
123{
124 int status;
125 TT_MODE newmode; /* New TTY mode bits */
126 static short first_time = TRUE;
127
128 if (first_time)
129 {
130 orgmode = get_tty();
131 first_time = FALSE;
132 }
133 newmode = get_tty();
134 if (col)
135 newmode.width = col;
136 if (row)
137 newmode.x.y.length = row;
138 newmode.x.basic |= (TT$M_NOECHO | TT$M_HOSTSYNC);
139 newmode.x.basic &= ~TT$M_TTSYNC;
140 newmode.extended |= TT2$M_PASTHRU;
141 status = sys$qiow(0, iochan, IO$_SETMODE, iosb, 0, 0,
142 &newmode, sizeof(newmode), 0, 0, 0, 0);
143 if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
144 return;
145}
146
147 static TT_MODE
148get_tty(void)
149{
150
151 static $DESCRIPTOR(odsc,"SYS$OUTPUT"); /* output descriptor */
152
153 int status;
154 TT_MODE tt_mode;
155
156 if (!iochan)
157 status = sys$assign(&odsc,&iochan,0,0);
158
159 status = sys$qiow(0, iochan, IO$_SENSEMODE, iosb, 0, 0,
160 &tt_mode, sizeof(tt_mode), 0, 0, 0, 0);
161 if (status != SS$_NORMAL || (iosb[0] & 0xFFFF) != SS$_NORMAL)
162 {
163 tt_mode.width = 0;
164 tt_mode.type = 0;
165 tt_mode.class = 0;
166 tt_mode.x.basic = 0;
167 tt_mode.x.y.length = 0;
168 tt_mode.extended = 0;
169 }
170 return(tt_mode);
171}
172
173/*
174 * Get the current window size in Rows and Columns.
175 */
176 int
177mch_get_shellsize(void)
178{
179 TT_MODE tmode;
180
181 tmode = get_tty(); /* get size from VMS */
182 Columns = tmode.width;
183 Rows = tmode.x.y.length;
184 return OK;
185}
186
187/*
188 * Try to set the window size to Rows and new_Columns.
189 */
190 void
191mch_set_shellsize(void)
192{
193 set_tty(Rows, Columns);
194 switch (Columns)
195 {
196 case 132: OUT_STR_NF((char_u *)"\033[?3h\033>"); break;
197 case 80: OUT_STR_NF((char_u *)"\033[?3l\033>"); break;
198 default: break;
199 }
200 out_flush();
201 screen_start();
202}
203
204 char_u *
205mch_getenv(char_u *lognam)
206{
207 DESC d_file_dev, d_lognam ;
208 static char buffer[LNM$C_NAMLENGTH+1];
209 char_u *cp = NULL;
210 unsigned long attrib;
211 int lengte = 0, dum = 0, idx = 0;
212 ITMLST2 itmlst;
213 char *sbuf = NULL;
214
215 vul_desc(&d_lognam, (char *)lognam);
216 vul_desc(&d_file_dev, "LNM$FILE_DEV");
217 attrib = LNM$M_CASE_BLIND;
218 vul_item(&itmlst.index, sizeof(int), LNM$_INDEX, (char *)&idx, &dum);
219 vul_item(&itmlst.string, LNM$C_NAMLENGTH, LNM$_STRING, buffer, &lengte);
220 itmlst.nul = 0;
221 if (sys$trnlnm(&attrib, &d_file_dev, &d_lognam, NULL,&itmlst) == SS$_NORMAL)
222 {
223 buffer[lengte] = '\0';
224 if (cp = (char_u *)alloc((unsigned)(lengte+1)))
225 strcpy((char *)cp, buffer);
226 return(cp);
227 }
228 else if ((sbuf = getenv((char *)lognam)))
229 {
230 lengte = strlen(sbuf) + 1;
231 cp = (char_u *)malloc((size_t)lengte);
232 if (cp)
233 strcpy((char *)cp, sbuf);
234 return cp;
235 }
236 else
237 return(NULL);
238}
239
240/*
241 * mch_setenv VMS version of setenv()
242 */
243 int
244mch_setenv(char *var, char *value, int x)
245{
246 int res, dum;
247 long attrib = 0L;
248 char acmode = PSL$C_SUPER; /* needs SYSNAM privilege */
249 DESC tabnam, lognam;
250 ITMLST1 itmlst;
251
252 vul_desc(&tabnam, "LNM$JOB");
253 vul_desc(&lognam, var);
254 vul_item(&itmlst.equ, value ? strlen(value) : 0, value ? LNM$_STRING : 0,
255 value, &dum);
256 itmlst.nul = 0;
257 res = sys$crelnm(&attrib, &tabnam, &lognam, &acmode, &itmlst);
258 return((res == 1) ? 0 : -1);
259}
260
261 int
262vms_sys(char *cmd, char *out, char *inp)
263{
264 DESC cdsc, odsc, idsc;
265 long status;
266
267 if (cmd)
268 vul_desc(&cdsc, cmd);
269 if (out)
270 vul_desc(&odsc, out);
271 if (inp)
272 vul_desc(&idsc, inp);
273
274 lib$spawn(cmd ? &cdsc : NULL, /* command string */
275 inp ? &idsc : NULL, /* input file */
276 out ? &odsc : NULL, /* output file */
277 0, 0, 0, &status, 0, 0, 0, 0, 0, 0);
278 return status;
279}
280
281/*
282 * Convert VMS system() or lib$spawn() return code to Unix-like exit value.
283 */
284 int
285vms_sys_status(int status)
286{
287 if (status != SS$_NORMAL && (status & STS$M_SUCCESS) == 0)
288 return status; /* Command failed. */
289 return 0;
290}
291
292/*
293 * vms_read()
294 * function for low level char input
295 *
296 * Returns: input length
297 */
298 int
299vms_read(char *inbuf, size_t nbytes)
300{
301 int status, function, len;
302 TT_MODE tt_mode;
Bram Moolenaarea424162005-06-16 21:51:00 +0000303 ITEM itmlst[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 static long trm_mask[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
305
306 /* whatever happened earlier we need an iochan here */
307 if (!iochan)
308 tt_mode = get_tty();
309
310 vul_item(&itmlst[0], 0, TRM$_MODIFIERS,
Bram Moolenaarea424162005-06-16 21:51:00 +0000311 (char *)( TRM$M_TM_ESCAPE | TRM$M_TM_TIMED | TRM$M_TM_NOECHO |
312 TRM$M_TM_NOEDIT | TRM$M_TM_NOFILTR |
313 TRM$M_TM_NORECALL| TRM$M_TM_TRMNOECHO), 0);
314 vul_item(&itmlst[1], 0, TRM$_TIMEOUT, (char *) 1, 0 );
315 vul_item(&itmlst[2], sizeof(trm_mask), TRM$_TERM, (char *)&trm_mask, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
317 function = (IO$_READLBLK | IO$M_EXTEND);
318 memset(inbuf, 0, nbytes);
319
320 while (1)
321 {
322 status = sys$qiow(0, iochan, function, &iosb, 0, 0, inbuf, nbytes - 1,
323 0, 0, &itmlst, sizeof(itmlst));
324 len = strlen(inbuf);
325 if (len > 0)
326 break;
327 }
328 return len;
329}
330
331/*
332 * vms_wproc() is called for each matching filename by decc$to_vms().
333 * We want to save each match for later retrieval.
334 *
335 * Returns: 1 - continue finding matches
336 * 0 - stop trying to find any further mathces
337 */
338 static int
339vms_wproc(char *name, int val)
340{
341 int i;
342 int nlen;
343 static int vms_match_alloced = 0;
344
345 if (val != DECC$K_FILE) /* Directories and foreing non VMS files are not counting */
346 return 1;
347
348 if (vms_match_num == 0) {
349 /* first time through, setup some things */
350 if (NULL == vms_fmatch) {
351 vms_fmatch = (char_u **)alloc(EXPL_ALLOC_INC * sizeof(char *));
352 if (!vms_fmatch)
353 return 0;
354 vms_match_alloced = EXPL_ALLOC_INC;
355 vms_match_free = EXPL_ALLOC_INC;
356 }
357 else {
358 /* re-use existing space */
359 vms_match_free = vms_match_alloced;
360 }
361 }
362
363 vms_remove_version(name);
364
365 /* convert filename to lowercase */
366 nlen = strlen(name);
367 for (i = 0; i < nlen; i++)
368 name[i] = TOLOWER_ASC(name[i]);
369
370 /* if name already exists, don't add it */
371 for (i = 0; i<vms_match_num; i++) {
372 if (0 == STRCMP((char_u *)name,vms_fmatch[i]))
373 return 1;
374 }
375 if (--vms_match_free == 0) {
376 /* add more space to store matches */
377 vms_match_alloced += EXPL_ALLOC_INC;
378 vms_fmatch = (char_u **)realloc(vms_fmatch,
379 sizeof(char **) * vms_match_alloced);
380 if (!vms_fmatch)
381 return 0;
382 vms_match_free = EXPL_ALLOC_INC;
383 }
384 vms_fmatch[vms_match_num] = vim_strsave((char_u *)name);
385
386 ++vms_match_num;
387 return 1;
388}
389
390/*
391 * mch_expand_wildcards this code does wild-card pattern
392 * matching NOT using the shell
393 *
394 * return OK for success, FAIL for error (you may loose some
395 * memory) and put an error message in *file.
396 *
397 * num_pat number of input patterns
398 * pat array of pointers to input patterns
399 * num_file pointer to number of matched file names
400 * file pointer to array of pointers to matched file names
401 *
402 */
403 int
404mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags)
405{
406 int i, j = 0, cnt = 0;
407 char *cp;
408 char_u buf[MAXPATHL];
409 int dir;
410 int files_alloced, files_free;
411
412 *num_file = 0; /* default: no files found */
413 files_alloced = EXPL_ALLOC_INC;
414 files_free = EXPL_ALLOC_INC;
415 *file = (char_u **) alloc(sizeof(char_u **) * files_alloced);
416 if (*file == NULL)
417 {
418 *num_file = 0;
419 return FAIL;
420 }
421 for (i = 0; i < num_pat; i++)
422 {
423 /* expand environment var or home dir */
424 if (vim_strchr(pat[i],'$') || vim_strchr(pat[i],'~'))
425 expand_env(pat[i],buf,MAXPATHL);
426 else
427 STRCPY(buf,pat[i]);
428
429 vms_match_num = 0; /* reset collection counter */
430 cnt = decc$to_vms(decc$translate_vms(vms_fixfilename(buf)), vms_wproc, 1, 0);
431 /* allow wild, no dir */
432 if (cnt > 0)
433 cnt = vms_match_num;
434
435 if (cnt < 1)
436 continue;
437
438 for (i = 0; i < cnt; i++)
439 {
440 /* files should exist if expanding interactively */
441 if (!(flags & EW_NOTFOUND) && mch_getperm(vms_fmatch[i]) < 0)
442 continue;
443 /* do not include directories */
444 dir = (mch_isdir(vms_fmatch[i]));
445 if (( dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
446 continue;
447 /* allocate memory for pointers */
448 if (--files_free < 1)
449 {
450 files_alloced += EXPL_ALLOC_INC;
451 *file = (char_u **)realloc(*file,
452 sizeof(char_u **) * files_alloced);
453 if (*file == NULL)
454 {
455 *file = (char_u **)"";
456 *num_file = 0;
457 return(FAIL);
458 }
459 files_free = EXPL_ALLOC_INC;
460 }
461
462 (*file)[*num_file++] = vms_fmatch[i];
463 }
464 }
465 return OK;
466}
467
468 int
469mch_expandpath(garray_T *gap, char_u *path, int flags)
470{
471 int i,cnt = 0;
472 char *cp;
473 vms_match_num = 0;
474
475 cnt = decc$to_vms(decc$translate_vms(vms_fixfilename(path)), vms_wproc, 1, 0);
476 /* allow wild, no dir */
477 if (cnt > 0)
478 cnt = vms_match_num;
479 for (i = 0; i < cnt; i++)
480 {
481 if (mch_getperm(vms_fmatch[i]) >= 0) /* add existing file */
482 addfile(gap, vms_fmatch[i], flags);
483 }
484 return cnt;
485}
486
487/*
488 * attempt to translate a mixed unix-vms file specification to pure vms
489 */
490 static void
491vms_unix_mixed_filespec(char *in, char *out)
492{
493 char *lastcolon;
494 char *end_of_dir;
495 char ch;
496 int len;
497
498 /* copy vms filename portion up to last colon
499 * (node and/or disk)
500 */
501 lastcolon = strrchr(in, ':'); /* find last colon */
502 if (lastcolon != NULL) {
503 len = lastcolon - in + 1;
504 strncpy(out, in, len);
505 out += len;
506 in += len;
507 }
508
509 end_of_dir = NULL; /* default: no directory */
510
511 /* start of directory portion */
512 ch = *in;
513 if ((ch == '[') || (ch == '/') || (ch == '<') ) { /* start of directory(s) ? */
514 ch = '[';
515 SKIP_FOLLOWING_SLASHES(in);
516 } else if (EQN(in, "../", 3)) { /* Unix parent directory? */
517 *out++ = '[';
518 *out++ = '-';
519 end_of_dir = out;
520 ch = '.';
521 in += 2;
522 SKIP_FOLLOWING_SLASHES(in);
523 } else { /* not a special character */
524 while (EQN(in, "./", 2)) { /* Ignore Unix "current dir" */
525 in += 2;
526 SKIP_FOLLOWING_SLASHES(in);
527 }
528 if (strchr(in, '/') == NULL) { /* any more Unix directories ? */
529 strcpy(out, in); /* No - get rest of the spec */
530 return;
531 } else {
532 *out++ = '['; /* Yes, denote a Vms subdirectory */
533 ch = '.';
534 --in;
535 }
536 }
537
538 /* if we get here, there is a directory part of the filename */
539
540 /* initialize output file spec */
541 *out++ = ch;
542 ++in;
543
544 while (*in != '\0') {
545 ch = *in;
546 if ((ch == ']') || (ch == '/') || (ch == '>') ) { /* end of (sub)directory ? */
547 end_of_dir = out;
548 ch = '.';
549 SKIP_FOLLOWING_SLASHES(in);
550 }
551 else if (EQN(in, "../", 3)) { /* Unix parent directory? */
552 *out++ = '-';
553 end_of_dir = out;
554 ch = '.';
555 in += 2;
556 SKIP_FOLLOWING_SLASHES(in);
557 }
558 else {
559 while (EQN(in, "./", 2)) { /* Ignore Unix "current dir" */
560 end_of_dir = out;
561 in += 2;
562 SKIP_FOLLOWING_SLASHES(in);
563 ch = *in;
564 }
565 }
566
567 /* Place next character into output file spec */
568 *out++ = ch;
569 ++in;
570 }
571
572 *out = '\0'; /* Terminate output file spec */
573
574 if (end_of_dir != NULL) /* Terminate directory portion */
575 *end_of_dir = ']';
576}
577
578
579/*
580 * for decc$to_vms in vms_fixfilename
581 */
582 static int
583vms_fspec_proc(char *fil, int val)
584{
585 strcpy(Fspec_Rms,fil);
586 return(1);
587}
588
589/*
590 * change unix and mixed filenames to VMS
591 */
592 void *
593vms_fixfilename(void *instring)
594{
595 static char *buf = NULL;
596 static size_t buflen = 0;
597 size_t len;
598
599 /* get a big-enough buffer */
600 len = strlen(instring) + 1;
601 if (len > buflen)
602 {
603 buflen = len + 128;
604 if (buf)
605 buf = (char *)realloc(buf, buflen);
606 else
607 buf = (char *)calloc(buflen, sizeof(char));
608 }
609
610#ifdef DEBUG
611 char *tmpbuf = NULL;
612 tmpbuf = (char *)calloc(buflen, sizeof(char));
613 strcpy(tmpbuf, instring);
614#endif
615
616 Fspec_Rms = buf; /* for decc$to_vms */
617
618 if ( strchr(instring,'/') == NULL )
619 /* It is already a VMS file spec */
620 strcpy(buf, instring);
621 else if ( strchr(instring,'"') == NULL ){ /* password in the path ? */
622 /* Seems it is a regular file, let guess that it is pure Unix fspec */
623 if ( decc$to_vms(instring, vms_fspec_proc, 0, 0) <= 0 )
624 /* No... it must be mixed */
625 vms_unix_mixed_filespec(instring, buf);
626 }
627 else
628 /* we have a password in the path */
629 /* decc$ functions can not handle */
630 /* this is our only hope to resolv */
631 vms_unix_mixed_filespec(instring, buf);
632
633 return buf;
634}
635/*
636 * Remove version number from file name
637 * we need it in some special cases as:
638 * creating swap file name and writing new file
639 */
640 void
641vms_remove_version(void * fname)
642{
643 char_u *cp;
644 char_u *fp;
645
646 if ((cp = vim_strchr( fname, ';')) != NULL) /* remove version */
647 *cp = '\0';
648 else if ((cp = vim_strrchr( fname, '.')) != NULL )
649 {
650 if ((fp = vim_strrchr( fname, ']')) != NULL ) {;}
651 else if ((fp = vim_strrchr( fname, '>')) != NULL ) {;}
652 else fp = fname;
653
654 while ( *fp != '\0' && fp < cp )
655 if ( *fp++ == '.' )
656 *cp = '\0';
657 }
658 return ;
659}