Debian Codegraph @

vms/gawkmisc.vms

raw
1 /*
2 * gawkmisc.vms --- miscellanious gawk routines that are OS specific.
3 */
4
5 /*
6 * Copyright (C) 1986, 1988, 1989, 1991-1996, 2003, 2011, 2014, 2021, 2022,
7 * the Free Software Foundation, Inc.
8 *
9 * This file is part of GAWK, the GNU implementation of the
10 * AWK Progamming Language.
11 *
12 * GAWK is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * GAWK is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31
32 #include <descrip.h>
33 #include <dvidef.h>
34 #include <efndef.h>
35 #include <fscndef.h>
36 #include <stsdef.h>
37 #include <time.h>
38 #include <lnmdef.h>
39
40
41 #pragma member_alignment save
42 #pragma nomember_alignment longword
43 struct item_list_3 {
44 unsigned short len;
45 unsigned short code;
46 void * bufadr;
47 unsigned short * retlen;
48 };
49
50 struct filescan_itmlst_2 {
51 unsigned short length;
52 unsigned short itmcode;
53 char * component;
54 };
55
56 #pragma member_alignment
57
58 int SYS$GETDVIW(
59 unsigned long efn,
60 unsigned short chan,
61 const struct dsc$descriptor_s * devnam,
62 const struct item_list_3 * itmlst,
63 void * iosb,
64 void (* astadr)(unsigned long),
65 unsigned long astprm,
66 void * nullarg);
67
68 int SYS$FILESCAN(
69 const struct dsc$descriptor_s * srcstr,
70 struct filescan_itmlst_2 * valuelist,
71 unsigned long * fldflags,
72 struct dsc$descriptor_s *auxout,
73 unsigned short * retlen);
74
75 int SYS$TRNLNM(
76 const unsigned long * attr,
77 const struct dsc$descriptor_s * table_dsc,
78 struct dsc$descriptor_s * name_dsc,
79 const unsigned char * acmode,
80 const struct item_list_3 * item_list);
81
82 const char quote = '\'';
83 const char *defpath = DEFPATH;
84 const char *deflibpath = DEFLIBPATH;
85 const char envsep = ',';
86 #define VMS_NAME_LEN 255
87 static char vms_name[VMS_NAME_LEN+1];
88
89 /* Take all the fun out of simply looking up a logical name */
90 static int sys_trnlnm
91 (const char * logname,
92 char * value,
93 int value_len)
94 {
95 const $DESCRIPTOR(table_dsc, "LNM$FILE_DEV");
96 const unsigned long attr = LNM$M_CASE_BLIND;
97 struct dsc$descriptor_s name_dsc;
98 int status;
99 unsigned short result;
100 struct item_list_3 itlst[2];
101
102 itlst[0].len = value_len;
103 itlst[0].code = LNM$_STRING;
104 itlst[0].bufadr = value;
105 itlst[0].retlen = &result;
106
107 itlst[1].len = 0;
108 itlst[1].code = 0;
109
110 name_dsc.dsc$w_length = strlen(logname);
111 name_dsc.dsc$a_pointer = (char *)logname;
112 name_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
113 name_dsc.dsc$b_class = DSC$K_CLASS_S;
114
115 status = SYS$TRNLNM(&attr, &table_dsc, &name_dsc, 0, itlst);
116
117 if ($VMS_STATUS_SUCCESS(status)) {
118
119 /* Null terminate and return the string */
120 value[result] = '\0';
121 }
122
123 return status;
124 }
125
126
127 /* gawk_name --- pull out the "gawk" part from how the OS called us */
128
129 /* You would not think that this should be a such a problem, but
130 * VMS extended file specifications are tricky to parse, and we have
131 * to tell the difference between a CRTL generated argv[0] and a
132 * passed exec() argv[0] and handle both cases.
133 */
134
135 const char *
136 gawk_name(filespec)
137 const char *filespec;
138 {
139 int status;
140 int result;
141 char * shell;
142 int lcname = 0;
143
144 /* If the path name starts with a /, then it is an absolute path
145 * that may have been generated by the CRTL instead of the command
146 * name. If it is the device name between the slashes, then this
147 * was likely from the run command and needs to be fixed up.
148 * If the DECC$POSIX_COMPLIANT_PATHNAMES is set to 2, then it is
149 * the DISK$VOLUME that will be present, and it will still need to
150 * be fixed.
151 */
152
153 result = 0;
154 if (filespec[0] == '/') {
155 char * nextslash;
156 int length;
157 struct item_list_3 itemlist[3];
158 unsigned short dvi_iosb[4];
159 char alldevnam[64];
160 unsigned short alldevnam_len;
161 struct dsc$descriptor_s devname_dsc;
162 char diskvolnam[256];
163 unsigned short diskvolnam_len;
164
165 /* Get some information about the disk */
166 /*--------------------------------------*/
167 itemlist[0].len = (sizeof alldevnam) - 1;
168 itemlist[0].code = DVI$_ALLDEVNAM;
169 itemlist[0].bufadr = alldevnam;
170 itemlist[0].retlen = &alldevnam_len;
171 itemlist[1].len = (sizeof diskvolnam) - 1 - 5;
172 itemlist[1].code = DVI$_VOLNAM;
173 itemlist[1].bufadr = &diskvolnam[5];
174 itemlist[1].retlen = &diskvolnam_len;
175 itemlist[2].len = 0;
176 itemlist[2].code = 0;
177
178 /* Add the prefix for the volume name. */
179 /* SYS$GETDVI will append the volume name to this */
180 strcpy(diskvolnam,"DISK$");
181
182 nextslash = strchr(&filespec[1], '/');
183 if (nextslash != NULL) {
184 length = nextslash - filespec - 1;
185
186 /* DECC requires a cast here */
187 devname_dsc.dsc$a_pointer = (char *)&filespec[1];
188 devname_dsc.dsc$w_length = length;
189 devname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
190 devname_dsc.dsc$b_class = DSC$K_CLASS_S;
191
192 status = SYS$GETDVIW(
193 EFN$C_ENF,
194 0,
195 &devname_dsc,
196 itemlist,
197 dvi_iosb,
198 NULL, 0, 0);
199 if (!$VMS_STATUS_SUCCESS(status)) {
200 /* If the sys$getdviw fails, then this path
201 * was passed by an exec() program and not
202 * from DCL, so do nothing.
203 * An example is "/tmp/program" where tmp:
204 * does not exist
205 */
206 result = 0;
207 } else if (!$VMS_STATUS_SUCCESS(dvi_iosb[0])) {
208 result = 0;
209 } else {
210 char * devnam;
211 int devnam_len;
212 char argv_dev[64];
213
214 /* Null terminate the returned alldevnam */
215 alldevnam[alldevnam_len] = 0;
216 devnam = alldevnam;
217 devnam_len = alldevnam_len;
218
219 /* Need to skip past any leading underscore */
220 if (devnam[0] == '_') {
221 devnam++;
222 devnam_len--;
223 }
224
225 /* And remove the trailing colon */
226 if (devnam[devnam_len - 1] == ':') {
227 devnam_len--;
228 devnam[devnam_len] = 0;
229 }
230
231 /* Null terminate the returned volnam */
232 diskvolnam_len += 5;
233 diskvolnam[diskvolnam_len] = 0;
234
235 /* Check first for normal CRTL behavior */
236 if (devnam_len == length) {
237 strncpy(vms_name, &filespec[1], length);
238 vms_name[length] = 0;
239 result = (strcasecmp(devnam, vms_name) == 0);
240 }
241
242 /* If we have not got a match check for
243 * POSIX Compliant behavior. To be more
244 * accurate, we could also check to see
245 * if that feature is active.
246 */
247 if ((result == 0) &&
248 (diskvolnam_len == length)) {
249 int cmp;
250 strncpy(vms_name, &filespec[1], length);
251 vms_name[length] = 0;
252 cmp = strcasecmp(diskvolnam, vms_name);
253 result = (cmp == 0);
254 }
255 }
256 }
257 } else {
258 /* The path did not start with a slash, so it could be VMS
259 * format. If it is vms format, it has a volume/device in
260 * it as it must be an absolute path
261 */
262 struct dsc$descriptor_s path_desc;
263 int status;
264 unsigned long field_flags;
265 struct filescan_itmlst_2 item_list[5];
266 char * volume;
267 char * name;
268 int name_len;
269 char * ext;
270
271 /* DECC requires a cast here */
272 path_desc.dsc$a_pointer = (char *)filespec;
273 path_desc.dsc$w_length = strlen(filespec);
274 path_desc.dsc$b_dtype = DSC$K_DTYPE_T;
275 path_desc.dsc$b_class = DSC$K_CLASS_S;
276
277 /* Don't actually need to initialize anything buf itmcode */
278 /* I just do not like uninitialized input values */
279
280 /* Sanity check, this must be the same length as input */
281 item_list[0].itmcode = FSCN$_FILESPEC;
282 item_list[0].length = 0;
283 item_list[0].component = NULL;
284
285 /* If the device is present, then it if a VMS spec */
286 item_list[1].itmcode = FSCN$_DEVICE;
287 item_list[1].length = 0;
288 item_list[1].component = NULL;
289
290 /* we need the program name and type */
291 item_list[2].itmcode = FSCN$_NAME;
292 item_list[2].length = 0;
293 item_list[2].component = NULL;
294
295 item_list[3].itmcode = FSCN$_TYPE;
296 item_list[3].length = 0;
297 item_list[3].component = NULL;
298
299 /* End the list */
300 item_list[4].itmcode = 0;
301 item_list[4].length = 0;
302 item_list[4].component = NULL;
303
304 status = SYS$FILESCAN(
305 (const struct dsc$descriptor_s *)&path_desc,
306 item_list, &field_flags, NULL, NULL);
307
308 if ($VMS_STATUS_SUCCESS(status) &&
309 (item_list[0].length == path_desc.dsc$w_length) &&
310 (item_list[1].length != 0)) {
311
312 char * dollar;
313 int keep_ext;
314 int i;
315
316 /* We need the filescan to be successful,
317 * same length as input, and a volume to be present.
318 *
319 * We will assume that we only get to this path on
320 * a version of VMS that does not support the EFS
321 * character set.
322 *
323 * There may be a xxx$ prefix on the image name.
324 * Linux programs do not handle that well, so
325 * strip the prefix.
326 */
327 name = item_list[2].component;
328 name_len = item_list[2].length;
329 dollar = strrchr(name, '$');
330 if (dollar != NULL) {
331 dollar++;
332 name_len = name_len - (dollar - name);
333 name = dollar;
334 }
335
336 strncpy(vms_name, name, name_len);
337 vms_name[name_len] = 0;
338 result = 1;
339
340 /* We only keep the extension if it is not ".exe" */
341 keep_ext = 0;
342 ext = item_list[3].component;
343
344 if (item_list[3].length != 1) {
345 if (item_list[3].length != 4) {
346 keep_ext = 1;
347 } else {
348 int x;
349 x = strncmp(ext, ".exe", 4);
350 if (x != 0) {
351 keep_ext = 1;
352 }
353 }
354 }
355
356 if (keep_ext == 1) {
357 strncpy(&vms_name[name_len],
358 ext, item_list[3].length);
359 }
360 }
361 }
362
363 if (result) {
364 char * lastslash;
365 char * dollar;
366 char * dotexe;
367 char * lastdot;
368 char * extension;
369
370 /* This means it is probably the name from a DCL command
371 * Find the last slash which separates the file from the
372 * path.
373 */
374 lastslash = strrchr(filespec, '/');
375
376 if (lastslash != NULL) {
377 int i;
378
379 lastslash++;
380
381 /* There may be a xxx$ prefix on the image name. */
382 /* Linux programs do not handle that well, so */
383 /* strip the prefix */
384 dollar = strrchr(lastslash, '$');
385
386 if (dollar != NULL) {
387 dollar++;
388 lastslash = dollar;
389 }
390
391 strcpy(vms_name, lastslash);
392
393 /* In UNIX mode + EFS character set, there should
394 * not be a version present, as it is not possible
395 * when parsing to tell if it is a version or part
396 * of the UNIX filename as UNIX programs use numeric
397 * extensions for many reasons.
398 */
399
400 lastdot = strrchr(vms_name, '.');
401 if (lastdot != NULL) {
402 int i;
403
404 i = 1;
405 while (isdigit(lastdot[i])) {
406 i++;
407 }
408 if (lastdot[i] == 0) {
409 *lastdot = 0;
410 }
411 }
412
413 /* Find the .exe on the name (case insenstive)
414 * and toss it
415 */
416 dotexe = strrchr(vms_name, '.');
417 if (dotexe != NULL) {
418 if ((dotexe[1] == 'e' || dotexe[1] == 'E') &&
419 (dotexe[2] == 'x' || dotexe[2] == 'X') &&
420 (dotexe[3] == 'e' || dotexe[3] == 'E') &&
421 (dotexe[4] == 0)) {
422
423 *dotexe = 0;
424 } else {
425 /* Also need to handle a null
426 * extension because of a CRTL bug.
427 */
428 if (dotexe[1] == 0) {
429 *dotexe = 0;
430 }
431 }
432 }
433 }
434 } else {
435 /* No changes needed */
436 strncpy(vms_name, filespec, VMS_NAME_LEN);
437 vms_name[VMS_NAME_LEN] = 0;
438 }
439
440
441 /*
442 * The above fixes up the name, but for the DCL shell
443 * may leave it in upper case, which messes up the self tests.
444 * force it to lower case here.
445 */
446 shell = getenv("SHELL");
447 if (shell != NULL) {
448 if (strcmp(shell, "DCL") == 0) {
449 lcname = 1;
450 }
451 } else {
452 lcname = 1;
453 }
454 if (lcname == 1) {
455 int i = 0;
456 while (vms_name[i] != 0) {
457 vms_name[i] = tolower(vms_name[i]);
458 i++;
459 }
460 }
461 return vms_name;
462 }
463
464 /* os_arg_fixup --- fixup the command line */
465
466 void
467 os_arg_fixup(argcp, argvp)
468 int *argcp;
469 char ***argvp;
470 {
471 char *tz_rule;
472 int status;
473
474 (void) vms_arg_fixup(argcp, argvp);
475
476 /* Fix up the time zone */
477 /* For some reason it gets trashed */
478 tz_rule = malloc(1024);
479 status = sys_trnlnm("TZ", tz_rule, 1024);
480 if ($VMS_STATUS_SUCCESS(status)) {
481 setenv("TZ", tz_rule, 1);
482 } else {
483 status = sys_trnlnm("SYS$TIMEZONE_RULE", tz_rule, 1024);
484 if ($VMS_STATUS_SUCCESS(status)) {
485 setenv("TZ", tz_rule, 1);
486 }
487 }
488 free(tz_rule);
489 }
490
491 /* os_devopen --- open special per-OS devices */
492
493 int
494 os_devopen(name, flag)
495 const char *name;
496 int flag;
497 {
498 return vms_devopen(name, flag);
499 }
500
501 /* optimal_bufsize --- determine optimal buffer size */
502
503 size_t
504 optimal_bufsize(fd, stb)
505 int fd;
506 struct stat *stb;
507 {
508
509 /* force all members to zero in case OS doesn't use all of them. */
510 memset(stb, '\0', sizeof(struct stat));
511
512 /*
513 * These values correspond with the RMS multi-block count used by
514 * vms_open() in vms/vms_misc.c.
515 */
516 if (isatty(fd) > 0)
517 return BUFSIZ;
518 else if (fstat(fd, stb) < 0)
519 return 8*512; /* conservative in case of DECnet access */
520 else
521 return 32*512;
522 }
523
524 /* ispath --- return true if path has directory components */
525
526 int
527 ispath(file)
528 const char *file;
529 {
530 for (; *file; file++) {
531 switch (*file) {
532 case ':':
533 case ']':
534 case '>':
535 case '/':
536 return 1;
537 }
538 }
539 return 0;
540 }
541
542 /* isdirpunct --- return true if char is a directory separator */
543
544 int
545 isdirpunct(c)
546 int c;
547 {
548 return (strchr(":]>/", c) != NULL);
549 }
550
551 /* os_close_on_exec --- set close on exec flag, print warning if fails */
552
553 void
554 os_close_on_exec(fd, name, what, dir)
555 int fd;
556 const char *name, *what, *dir;
557 {
558 /* no-op */
559 }
560
561 /* os_isdir --- is this an fd on a directory? */
562
563 #if ! defined(S_ISDIR) && defined(S_IFDIR)
564 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
565 #endif
566
567 int
568 os_isdir(fd)
569 int fd;
570 {
571 struct stat sbuf;
572
573 return (fstat(fd, &sbuf) == 0 && S_ISDIR(sbuf.st_mode));
574 }
575
576 /* os_isreadable --- fd can be read from */
577
578 int
579 os_isreadable(const awk_input_buf_t *iobuf, bool *isdir)
580 {
581 *isdir = false;
582
583 switch (iobuf->sbuf.st_mode & S_IFMT) {
584 case S_IFREG:
585 case S_IFCHR: /* ttys, /dev/null, .. */
586 #ifdef S_IFSOCK
587 case S_IFSOCK:
588 #endif
589 #ifdef S_IFIFO
590 case S_IFIFO:
591 #endif
592 return true;
593 case S_IFDIR:
594 *isdir = true;
595 /* fall through */
596 default:
597 return false;
598 }
599 }
600
601 /* os_is_setuid --- true if running setuid root */
602
603 int
604 os_is_setuid()
605 {
606 return 0;
607 }
608
609 /* os_setbinmode --- set binary mode on file */
610
611 int
612 os_setbinmode (fd, mode)
613 int fd, mode;
614 {
615 return 0;
616 }
617
618 /* os_restore_mode --- restore the original mode of the console device */
619
620 void
621 os_restore_mode (fd)
622 int fd;
623 {
624 /* no-op */
625 return;
626 }
627
628 /* files_are_same --- deal with VMS struct stat */
629
630 int
631 files_are_same(char *newfile, SRCFILE *oldfile)
632 {
633 struct stat st, *f1, *f2;
634
635 f1 = &st;
636 if (stat(newfile, f1) != 0) return 0;
637
638 f2 = &oldfile->sbuf;
639 /* compare device string */
640 #ifdef _USE_STD_STAT
641 return (f1->st_dev == f2->st_dev
642 /* and 48-bit file id cookie */
643 && f1->st_ino == f2->st_ino);
644 #else
645 return (strcmp(f1->st_dev, f2->st_dev) == 0
646 /* and 48-bit file id cookie stored in 3 short ints */
647 && f1->st_ino[0] == f2->st_ino[0]
648 && f1->st_ino[1] == f2->st_ino[1]
649 && f1->st_ino[2] == f2->st_ino[2]);
650 #endif
651 }
652
653 int
654 os_isatty(int fd)
655 {
656 return (isatty(fd) > 0);
657 }
658
659 void
660 init_sockets(void)
661 {
662 }
663
664 void
665 os_maybe_set_errno(void)
666 {
667 }