LLVM 23.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(int FD, int CCSID, bool IsText) {
98 assert((!IsText || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
99 "FT_UNTAGGED and FT_BINARY are not allowed for text files");
100 struct file_tag Tag;
101 Tag.ft_ccsid = CCSID;
102 Tag.ft_txtflag = IsText;
103 Tag.ft_deferred = 0;
104 Tag.ft_rsvflags = 0;
105
106 if (fcntl(FD, F_SETTAG, &Tag) == -1) {
107 if (errno == ENOSYS)
108 // Some file systems do not support filetags.
109 // Ignore ENOSYS error to allow compilation.
110 errno = 0;
111 else
112 return errnoAsErrorCode();
113 }
114 return std::error_code();
115}
116
117ErrorOr<__ccsid_t> llvm::getzOSFileTag(const Twine &FileName, const int FD) {
118 // If we have a file descriptor, use it to find out file tagging. Otherwise we
119 // need to use stat() with the file path.
120 if (FD != -1) {
121 struct f_cnvrt Query = {
122 QUERYCVT, // cvtcmd
123 0, // pccsid
124 0, // fccsid
125 };
126 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
127 return std::error_code(errno, std::generic_category());
128 return Query.fccsid;
129 }
130 struct stat Attr;
131 if (stat(FileName.str().c_str(), &Attr) == -1)
132 return std::error_code(errno, std::generic_category());
133 return Attr.st_tag.ft_ccsid;
134}
135
136ErrorOr<bool> llvm::needzOSConversion(const Twine &FileName, const int FD) {
137 ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD);
138 if (std::error_code EC = Ccsid.getError())
139 return EC;
140 // We don't need conversion for UTF-8 tagged files or binary files.
141 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
142 // problems related to UTF-8 tagged source files.
143 switch (*Ccsid) {
144 case CCSID_UTF_8:
145 case CCSID_ISO8859_1:
146 case FT_BINARY:
147 return false;
148 default:
149 return true;
150 }
151}
152
153std::error_code llvm::copyFileTagAttributes(const std::string &Source,
154 const int DestinationFD) {
155 struct stat SourceAttributes;
156 if (stat(Source.c_str(), &SourceAttributes) == -1)
157 return std::error_code(errno, std::generic_category());
158
159 if (SourceAttributes.st_tag.ft_txtflag)
160 if (enablezOSAutoConversionCcsid(DestinationFD,
161 SourceAttributes.st_tag.ft_ccsid) == -1)
162 return errnoAsErrorCode();
163
164 return setzOSFileTag(DestinationFD, SourceAttributes.st_tag.ft_ccsid,
165 SourceAttributes.st_tag.ft_txtflag);
166}
167
168#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:28
#define CCSID_IBM_1047
Definition AutoConvert.h:26
int disablezOSAutoConversion(int FD)
#define CCSID_UTF_8
Definition AutoConvert.h:27
#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