1/* te-vms.c -- Utilities for VMS.
2   Copyright 2009 Free Software Foundation, Inc.
3
4   Written by Douglas B Rupp <rupp@gnat.com>
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20#include "as.h"
21#include "te-vms.h"
22
23/* The purspose of the two alternate versions below is to have one that
24   works for native VMS and one that works on an NFS mounted filesystem
25   (Unix Server/VMS client).  The main issue being to generate the special
26   VMS file timestamps for the debug info.  */
27
28#ifdef VMS
29#define __NEW_STARLET 1
30#include <vms/starlet.h>
31#include <vms/rms.h>
32#include <vms/atrdef.h>
33#include <vms/fibdef.h>
34#include <vms/stsdef.h>
35#include <vms/iodef.h>
36#include <vms/fatdef.h>
37#include <errno.h>
38#include <vms/descrip.h>
39#include <string.h>
40#include <unixlib.h>
41
42#define MAXPATH 256
43
44/* Descrip.h doesn't have everything...  */
45typedef struct fibdef * __fibdef_ptr32 __attribute__ (( mode (SI) ));
46
47struct dsc$descriptor_fib
48{
49  unsigned int   fib$l_len;
50  __fibdef_ptr32 fib$l_addr;
51};
52
53/* I/O Status Block.  */
54struct IOSB
55{
56  unsigned short status, count;
57  unsigned int devdep;
58};
59
60static char *tryfile;
61
62/* Variable length string.  */
63struct vstring
64{
65  short length;
66  char string[NAM$C_MAXRSS+1];
67};
68
69static char filename_buff [MAXPATH];
70static char vms_filespec [MAXPATH];
71
72/* Callback function for filespec style conversion.  */
73
74static int
75translate_unix (char *name, int type ATTRIBUTE_UNUSED)
76{
77  strncpy (filename_buff, name, MAXPATH);
78  filename_buff [MAXPATH - 1] = (char) 0;
79  return 0;
80}
81
82/* Wrapper for DECC function that converts a Unix filespec
83   to VMS style filespec.  */
84
85static char *
86to_vms_file_spec (char *filespec)
87{
88  strncpy (vms_filespec, "", MAXPATH);
89  decc$to_vms (filespec, translate_unix, 1, 1);
90  strncpy (vms_filespec, filename_buff, MAXPATH);
91
92  vms_filespec [MAXPATH - 1] = (char) 0;
93
94  return vms_filespec;
95}
96
97#else /* not VMS */
98
99#define _BSD_SOURCE 1
100#include <sys/stat.h>
101#include <time.h>
102
103#define VMS_EPOCH_OFFSET        35067168000000000LL
104#define VMS_GRANULARITY_FACTOR  10000000
105
106#endif /* VMS */
107
108/* Return VMS file date, size, format, version given a name.  */
109
110static int
111vms_file_stats_name (const char *dirname,
112                     const char *filename,
113		     long long *cdt,
114		     long *siz,
115		     char *rfo,
116		     int *ver)
117{
118  char fullname[strlen (dirname) + strlen (filename) + 1];
119#ifdef VMS
120  struct FAB fab;
121  struct NAM nam;
122
123  unsigned long long create;
124  FAT recattr;
125  char ascnamebuff [256];
126
127  ATRDEF atrlst[]
128    = {
129      { ATR$S_CREDATE,  ATR$C_CREDATE,  &create },
130      { ATR$S_RECATTR,  ATR$C_RECATTR,  &recattr },
131      { ATR$S_ASCNAME,  ATR$C_ASCNAME,  &ascnamebuff },
132      { 0, 0, 0}
133    };
134
135  FIBDEF fib;
136  struct dsc$descriptor_fib fibdsc = {sizeof (fib), (void *) &fib};
137
138  struct IOSB iosb;
139
140  long status;
141  unsigned short chan;
142
143  struct vstring file;
144  struct dsc$descriptor_s filedsc
145    = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) file.string};
146  struct vstring device;
147  struct dsc$descriptor_s devicedsc
148    = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) device.string};
149  struct vstring result;
150  struct dsc$descriptor_s resultdsc
151    = {NAM$C_MAXRSS, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, (void *) result.string};
152
153  if (strcmp (filename, "<internal>") == 0
154      || strcmp (filename, "<built-in>") == 0)
155    {
156      if (cdt)
157	*cdt = 0;
158
159      if (siz)
160	*siz = 0;
161
162      if (rfo)
163	*rfo = 0;
164
165      if (ver)
166        *ver = 0;
167
168      return 0;
169    }
170
171  strcpy (fullname, dirname);
172  strcat (fullname, filename);
173
174  tryfile = to_vms_file_spec (fullname);
175
176  /* Allocate and initialize a FAB and NAM structures.  */
177  fab = cc$rms_fab;
178  nam = cc$rms_nam;
179
180  nam.nam$l_esa = file.string;
181  nam.nam$b_ess = NAM$C_MAXRSS;
182  nam.nam$l_rsa = result.string;
183  nam.nam$b_rss = NAM$C_MAXRSS;
184  fab.fab$l_fna = tryfile;
185  fab.fab$b_fns = strlen (tryfile);
186  fab.fab$l_nam = &nam;
187
188  /* Validate filespec syntax and device existence.  */
189  status = SYS$PARSE (&fab, 0, 0);
190  if ((status & 1) != 1)
191    return 1;
192
193  file.string[nam.nam$b_esl] = 0;
194
195  /* Find matching filespec.  */
196  status = SYS$SEARCH (&fab, 0, 0);
197  if ((status & 1) != 1)
198    return 1;
199
200  file.string[nam.nam$b_esl] = 0;
201  result.string[result.length=nam.nam$b_rsl] = 0;
202
203  /* Get the device name and assign an IO channel.  */
204  strncpy (device.string, nam.nam$l_dev, nam.nam$b_dev);
205  devicedsc.dsc$w_length  = nam.nam$b_dev;
206  chan = 0;
207  status = SYS$ASSIGN (&devicedsc, &chan, 0, 0, 0);
208  if ((status & 1) != 1)
209    return 1;
210
211  /* Initialize the FIB and fill in the directory id field.  */
212  memset (&fib, 0, sizeof (fib));
213  fib.fib$w_did[0]  = nam.nam$w_did[0];
214  fib.fib$w_did[1]  = nam.nam$w_did[1];
215  fib.fib$w_did[2]  = nam.nam$w_did[2];
216  fib.fib$l_acctl = 0;
217  fib.fib$l_wcc = 0;
218  strcpy (file.string, (strrchr (result.string, ']') + 1));
219  filedsc.dsc$w_length = strlen (file.string);
220  result.string[result.length = 0] = 0;
221
222  /* Open and close the file to fill in the attributes.  */
223  status
224    = SYS$QIOW (0, chan, IO$_ACCESS|IO$M_ACCESS, &iosb, 0, 0,
225		&fibdsc, &filedsc, &result.length, &resultdsc, &atrlst, 0);
226  if ((status & 1) != 1)
227    return 1;
228  if ((iosb.status & 1) != 1)
229    return 1;
230
231  result.string[result.length] = 0;
232  status = SYS$QIOW (0, chan, IO$_DEACCESS, &iosb, 0, 0, &fibdsc, 0, 0, 0,
233		     &atrlst, 0);
234  if ((status & 1) != 1)
235    return 1;
236  if ((iosb.status & 1) != 1)
237    return 1;
238
239  /* Deassign the channel and exit.  */
240  status = SYS$DASSGN (chan);
241  if ((status & 1) != 1)
242    return 1;
243
244  if (cdt) *cdt = create;
245  if (siz) *siz = (512 * 65536 * recattr.fat$w_efblkh) +
246                  (512 * (recattr.fat$w_efblkl - 1)) +
247                  recattr.fat$w_ffbyte;
248  if (rfo) *rfo = recattr.fat$v_rtype;
249  if (ver) *ver = strtol (strrchr (ascnamebuff, ';') + 1, 0, 10);
250#else /* not VMS */
251
252  struct stat buff;
253  struct tm *ts;
254  long long gmtoff, secs, nsecs;
255
256  strcpy (fullname, dirname);
257  strcat (fullname, filename);
258
259  if ((stat (fullname, &buff)) != 0)
260     return 1;
261
262  if (cdt)
263    {
264      ts = localtime (& buff.st_mtime);
265
266#ifdef HAVE_TM_GMTOFF
267	gmtoff = ts->tm_gmtoff;
268#else
269	{
270	  extern long timezone;
271
272	  if (ts->tm_isdst == 1)
273	    gmtoff = - (timezone - 3600);
274	  else
275	    gmtoff = - timezone;
276	}
277#endif
278
279#ifdef HAVE_ST_MTIM_TV_SEC
280      secs = buff.st_mtim.tv_sec;
281#else
282      secs = buff.st_mtime;
283#endif
284
285#ifdef HAVE_ST_MTIM_TV_NSEC
286      nsecs = buff.st_mtim.tv_nsec;
287#else
288      nsecs = 0;
289#endif
290
291      /* VMS timestamps are stored in local time to 100 nsec accuracy, but by
292	 experiment I found timestamps truncated to (at least) microseconds
293	 on an NFS mounted filesystem, hence the adjustment below. DBR. */
294      *cdt = ((secs + gmtoff) * VMS_GRANULARITY_FACTOR)
295	+ (nsecs / 1000 * 10) + VMS_EPOCH_OFFSET;
296    }
297
298  if (siz)
299    *siz = buff.st_size;
300
301  if (rfo)
302    *rfo = 2; /* Stream LF format.  */
303
304  /* Returning a file version of 0 is never correct for debug info, version 1
305     will be correct if file editing is done only on the Unix side.  If editing
306     is done on the VMS side, then its TBD.  */
307  if (ver)
308    *ver = 1;
309#endif /* VMS */
310
311  return 0;
312}
313
314bfd_uint64_t
315vms_dwarf2_file_time_name (const char *filename, const char *dirname)
316{
317  long long cdt;
318
319  if (vms_file_stats_name (dirname, filename, &cdt, 0, 0, 0) == 0)
320    return cdt;
321  else
322    return 0;
323}
324
325long
326vms_dwarf2_file_size_name (const char *filename, const char *dirname)
327{
328  long siz;
329
330  if (vms_file_stats_name (dirname, filename, 0, &siz, 0, 0) == 0)
331    return siz;
332  else
333    return 0;
334}
335
336/* VMS debugger needs the filename with version appended.  */
337/* Longest filename on VMS is 255 characters. Largest version is 32768.  */
338char *
339vms_dwarf2_file_name (const char *filename, const char *dirname)
340{
341  int ver;
342  static char buff [255 + 7];
343
344  vms_file_stats_name (dirname, filename, 0, 0, 0, &ver);
345  snprintf (buff, 255 + 7, "%s;%d", filename, ver);
346  return buff;
347}
348