summaryrefslogtreecommitdiff
path: root/vmsfunctions.c
blob: e422d488bb1521a5e5da9bc16ea648123ccdb055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* VMS functions
Copyright (C) 1996-2016 Free Software Foundation, Inc.
This file is part of GNU Make.

GNU Make is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "makeint.h"
#include "debug.h"
#include "job.h"

#include <ctype.h>
#include <string.h>

#ifdef __DECC
#include <starlet.h>
#endif

#include <rms.h>
#include "vmsdir.h"

#ifdef HAVE_VMSDIR_H

DIR *
opendir (char *dspec)
{
  struct DIR *dir  = xcalloc (sizeof (struct DIR));
  struct NAM *dnam = xmalloc (sizeof (struct NAM));
  struct FAB *dfab = &dir->fab;
  char *searchspec = xmalloc (MAXNAMLEN + 1);

  *dfab = cc$rms_fab;
  *dnam = cc$rms_nam;
  sprintf (searchspec, "%s*.*;", dspec);

  dfab->fab$l_fna = searchspec;
  dfab->fab$b_fns = strlen (searchspec);
  dfab->fab$l_nam = dnam;

  *dnam = cc$rms_nam;
  dnam->nam$l_esa = searchspec;
  dnam->nam$b_ess = MAXNAMLEN;

  if (! (sys$parse (dfab) & 1))
    {
      free (dir);
      free (dnam);
      free (searchspec);
      return (NULL);
    }

  return dir;
}

#define uppercasify(str) \
  do \
    { \
      char *tmp; \
      for (tmp = (str); *tmp != '\0'; tmp++) \
        if (islower ((unsigned char)*tmp)) \
          *tmp = toupper ((unsigned char)*tmp); \
    } \
  while (0)

struct direct *
readdir (DIR *dir)
{
  struct FAB *dfab = &dir->fab;
  struct NAM *dnam = (struct NAM *)(dfab->fab$l_nam);
  struct direct *dentry = &dir->dir;
  int i;

  memset (dentry, 0, sizeof *dentry);

  dnam->nam$l_rsa = dir->d_result;
  dnam->nam$b_rss = MAXNAMLEN;

  DB (DB_VERBOSE, ("."));

  if (!((i = sys$search (dfab)) & 1))
    {
      DB (DB_VERBOSE, (_("sys$search() failed with %d\n"), i));
      return (NULL);
    }

  dentry->d_off = 0;
  if (dnam->nam$w_fid == 0)
    dentry->d_fileno = 1;
  else
    dentry->d_fileno = dnam->nam$w_fid[0] + (dnam->nam$w_fid[1] << 16);

  dentry->d_reclen = sizeof (struct direct);
  dentry->d_namlen = dnam->nam$b_name + dnam->nam$b_type;
  strncpy (dentry->d_name, dnam->nam$l_name, dentry->d_namlen);
  dentry->d_name[dentry->d_namlen] = '\0';

#ifdef HAVE_CASE_INSENSITIVE_FS
  uppercasify (dentry->d_name);
#endif

  return (dentry);
}

int
closedir (DIR *dir)
{
  if (dir != NULL)
    {
      struct FAB *dfab = &dir->fab;
      struct NAM *dnam = (struct NAM *)(dfab->fab$l_nam);
      if (dnam != NULL)
        free (dnam->nam$l_esa);
      free (dnam);
      free (dir);
    }

  return 0;
}
#endif /* compiled for OpenVMS prior to V7.x */

/* Argv0 will be a full vms file specification, like
   node$dka100:[utils.gnumake]make.exe;47
   prefix it with "mcr " to make it a vms command, executable for DCL. */
const char *
vms_command(const char* argv0)
{
  size_t l = strlen(argv0) + 1;
  char* s = xmalloc(l + 4);
  memcpy(s, "mcr ", 4);
  memcpy(s+4, argv0, l);
  return s;
}

/* Argv0 aka argv[0] will be a full vms file specification, like
   node$dka100:[utils.gnumake]make.exe;47, set up by the CRTL.
   The vms progname should be ^^^^, the file name without
   file type .exe and ;version.
   Use sys$parse to get the name part of the file specification. That is
   in the above example, pick up "make" and return a copy of that string.
   If something goes wrong in sys$parse (unlikely, this is a VMS/CRTL supplied
   file specification) or if there is an empty name part (not easy to produce,
   but it is possible) just return "make".
   Somes notes ...
   NAM[L]$M_SYNCHK requests a syntax check, only.
   NAM is for ODS2 names (shorter parts, output usually converted to UPPERCASE).
   NAML is for ODS2/ODS5 names (longer parts, output unchanged).
   NAM$M_NO_SHORT_UPCASE may not be available for older versions of VMS.
   NAML is not available on older versions of VMS (NAML$C_BID not defined).
   argv[0] on older versions of VMS (no extended parse style and no
   CRTL feature DECC$ARGV_PARSE_STYLE) is always in lowercase. */
const char *
vms_progname(const char* argv0)
{
  int status;
  static struct FAB fab;
  char *progname;
  const char *fallback = "make";

#ifdef NAML$C_BID
  static char esa[NAML$C_MAXRSS];
  static struct NAML naml;
#else
  static char esa[NAM$C_MAXRSS];
  static struct NAM nam;
#endif

  fab = cc$rms_fab;
  fab.fab$l_fna = (char*)argv0;
  fab.fab$b_fns = strlen(argv0);

#ifdef NAML$C_BID
  fab.fab$l_naml = &naml;
  naml = cc$rms_naml;
  naml.naml$l_long_expand = esa;
  naml.naml$l_long_expand_alloc = NAML$C_MAXRSS;
  naml.naml$b_nop = NAML$M_SYNCHK;
  naml.naml$l_input_flags = NAML$M_NO_SHORT_OUTPUT;
#else
  fab.fab$l_nam = &nam;
  nam = cc$rms_nam;
  nam.nam$l_esa = esa;
  nam.nam$b_ess = NAM$C_MAXRSS;
# ifdef NAM$M_NO_SHORT_UPCASE
  nam.nam$b_nop = NAM$M_SYNCHK | NAM$M_NO_SHORT_UPCASE;
# else
  nam.nam$b_nop = NAM$M_SYNCHK;
# endif
#endif

  status = sys$parse(&fab);
  if (!(status & 1))
    return fallback;

#ifdef NAML$C_BID
  if (naml.naml$l_long_name_size == 0)
    return fallback;
  progname = xmalloc(naml.naml$l_long_name_size + 1);
  memcpy(progname, naml.naml$l_long_name, naml.naml$l_long_name_size);
  progname[naml.naml$l_long_name_size] = '\0';
#else
  if (nam.nam$b_name == 0)
    return fallback;
  progname = xmalloc(nam.nam$b_name + 1);
# ifdef NAM$M_NO_SHORT_UPCASE
  memcpy(progname, nam.nam$l_name, nam.nam$b_name);
# else
    {
      int i;
      for (i = 0; i < nam.nam$b_name; i++)
        progname[i] = tolower(nam.nam$l_name[i]);
    }
# endif
  progname[nam.nam$b_name] = '\0';
#endif

  return progname;
}