LLVM 24.0.0git
AutoConvert.cpp
Go to the documentation of this file.
1//===- AutoConvert.cpp - Auto conversion between ASCII/EBCDIC -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains functions used for auto conversion between
10// ASCII/EBCDIC codepages specific to z/OS.
11//
12//===----------------------------------------------------------------------===//
13
14#ifdef __MVS__
15
17#include <cassert>
18#include <fcntl.h>
19#include <sys/stat.h>
20#include <unistd.h>
21
22using namespace llvm;
23
24static int savedStdHandleAutoConversionMode[3] = {-1, -1, -1};
25
26int disablezOSAutoConversion(int FD) {
27 static const struct f_cnvrt Convert = {
28 SETCVTOFF, // cvtcmd
29 0, // pccsid
30 0, // fccsid
31 };
32
33 return fcntl(FD, F_CONTROL_CVT, &Convert);
34}
35
37 assert(FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO);
38 if (savedStdHandleAutoConversionMode[FD] == -1)
39 return 0;
40 struct f_cnvrt Cvt = {
41 savedStdHandleAutoConversionMode[FD], // cvtcmd
42 0, // pccsid
43 0, // fccsid
44 };
45 return (fcntl(FD, F_CONTROL_CVT, &Cvt));
46}
47
48int enablezOSAutoConversion(int FD) {
49 struct f_cnvrt Query = {
50 QUERYCVT, // cvtcmd
51 0, // pccsid
52 0, // fccsid
53 };
54
55 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
56 return -1;
57
58 // We don't need conversion for UTF-8 tagged files.
59 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
60 // problems related to UTF-8 tagged source files.
61 // When the pccsid is not ISO8859-1, autoconversion is still needed.
62 if (Query.pccsid == CCSID_ISO8859_1 &&
63 (Query.fccsid == CCSID_UTF_8 || Query.fccsid == CCSID_ISO8859_1))
64 return 0;
65
66 // Save the state of std handles before we make changes to it.
67 if ((FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO) &&
68 savedStdHandleAutoConversionMode[FD] == -1)
69 savedStdHandleAutoConversionMode[FD] = Query.cvtcmd;
70
71 if (FD == STDOUT_FILENO || FD == STDERR_FILENO)
72 Query.cvtcmd = SETCVTON;
73 else
74 Query.cvtcmd = SETCVTALL;
75
76 Query.pccsid =
77 (FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO)
78 ? 0
80 // Assume untagged files to be IBM-1047 encoded.
81 Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
82 return fcntl(FD, F_CONTROL_CVT, &Query);
83}
84
85int enablezOSAutoConversionCcsid(int FD, int ccsid) {
86 struct f_cnvrt cvt = {
87 SETCVTALL, // cvtcmd
88 CCSID_UTF_8, // pccsid
89 0, // fccsid
90 };
91 if (ccsid == FT_UNTAGGED)
92 return -1;
93 cvt.fccsid = ccsid;
94 return fcntl(FD, F_CONTROL_CVT, &cvt);
95}
96
97std::error_code llvm::setzOSFileTag(sys::fs::file_t FD, int CCSID,
98 bool IsText) {
99 assert((!IsText || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
100 "FT_UNTAGGED and FT_BINARY are not allowed for text files");
101 struct file_tag Tag;
102 Tag.ft_ccsid = CCSID;
103 Tag.ft_txtflag = IsText;
104 Tag.ft_deferred = 0;
105 Tag.ft_rsvflags = 0;
106
107 if (fcntl(FD.get(), F_SETTAG, &Tag) == -1) {
108 if (errno == ENOSYS)
109 // Some file systems do not support filetags.
110 // Ignore ENOSYS error to allow compilation.
111 errno = 0;
112 else
113 return errnoAsErrorCode();
114 }
115 return std::error_code();
116}
117
118ErrorOr<__ccsid_t> llvm::getzOSFileTag(const Twine &FileName,
119 const sys::fs::file_t FD) {
120 // If we have a file descriptor, use it to find out file tagging. Otherwise we
121 // need to use stat() with the file path.
122 if (FD != -1) {
123 struct f_cnvrt Query = {
124 QUERYCVT, // cvtcmd
125 0, // pccsid
126 0, // fccsid
127 };
128 if (fcntl(FD.get(), F_CONTROL_CVT, &Query) == -1)
129 return std::error_code(errno, std::generic_category());
130 return Query.fccsid;
131 }
132 struct stat Attr;
133 if (stat(FileName.str().c_str(), &Attr) == -1)
134 return std::error_code(errno, std::generic_category());
135 return Attr.st_tag.ft_ccsid;
136}
137
138ErrorOr<bool> llvm::needzOSConversion(const Twine &FileName,
139 const sys::fs::file_t FD) {
140 ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD);
141 if (std::error_code EC = Ccsid.getError())
142 return EC;
143 // We don't need conversion for UTF-8 tagged files or binary files.
144 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
145 // problems related to UTF-8 tagged source files.
146 switch (*Ccsid) {
147 case CCSID_UTF_8:
148 case CCSID_ISO8859_1:
149 case FT_BINARY:
150 return false;
151 default:
152 return true;
153 }
154}
155
156std::error_code
157llvm::copyFileTagAttributes(const std::string &Source,
158 const sys::fs::file_t DestinationFD) {
159 struct stat SourceAttributes;
160 if (stat(Source.c_str(), &SourceAttributes) == -1)
161 return std::error_code(errno, std::generic_category());
162
163 if (SourceAttributes.st_tag.ft_txtflag)
164 if (enablezOSAutoConversionCcsid(DestinationFD.get(),
165 SourceAttributes.st_tag.ft_ccsid) == -1)
166 return errnoAsErrorCode();
167
168 return setzOSFileTag(DestinationFD, SourceAttributes.st_tag.ft_ccsid,
169 SourceAttributes.st_tag.ft_txtflag);
170}
171
172#endif //__MVS__
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
int restorezOSStdHandleAutoConversion(int FD)
int enablezOSAutoConversionCcsid(int FD, int ccsid)
int enablezOSAutoConversion(int FD)
#define CCSID_ISO8859_1
Definition AutoConvert.h:29
#define CCSID_IBM_1047
Definition AutoConvert.h:27
int disablezOSAutoConversion(int FD)
#define CCSID_UTF_8
Definition AutoConvert.h:28
#define STDOUT_FILENO
Definition InitLLVM.cpp:28
#define STDERR_FILENO
Definition InitLLVM.cpp:31
#define STDIN_FILENO
Definition InitLLVM.cpp:25
Represents either an error or a value T.
Definition ErrorOr.h:56
std::error_code getError() const
Definition ErrorOr.h:152
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
This is an optimization pass for GlobalISel generic memory operations.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition Error.h:1256
This class wraps the platform specific file handle/descriptor type to provide an unified representati...
Definition File.h:21
value_type get() const
Get the underlying value and return a platform specific value.
Definition File.h:46