LLVM 20.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 "llvm/Support/Error.h"
18#include <cassert>
19#include <fcntl.h>
20#include <sys/stat.h>
21#include <unistd.h>
22
23using namespace llvm;
24
25static int savedStdHandleAutoConversionMode[3] = {-1, -1, -1};
26
27int disablezOSAutoConversion(int FD) {
28 static const struct f_cnvrt Convert = {
29 SETCVTOFF, // cvtcmd
30 0, // pccsid
31 0, // fccsid
32 };
33
34 return fcntl(FD, F_CONTROL_CVT, &Convert);
35}
36
37int restorezOSStdHandleAutoConversion(int FD) {
38 assert(FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO);
39 if (savedStdHandleAutoConversionMode[FD] == -1)
40 return 0;
41 struct f_cnvrt Cvt = {
42 savedStdHandleAutoConversionMode[FD], // cvtcmd
43 0, // pccsid
44 0, // fccsid
45 };
46 return (fcntl(FD, F_CONTROL_CVT, &Cvt));
47}
48
49int enablezOSAutoConversion(int FD) {
50 struct f_cnvrt Query = {
51 QUERYCVT, // cvtcmd
52 0, // pccsid
53 0, // fccsid
54 };
55
56 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
57 return -1;
58
59 // We don't need conversion for UTF-8 tagged files.
60 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
61 // problems related to UTF-8 tagged source files.
62 // When the pccsid is not ISO8859-1, autoconversion is still needed.
63 if (Query.pccsid == CCSID_ISO8859_1 &&
64 (Query.fccsid == CCSID_UTF_8 || Query.fccsid == CCSID_ISO8859_1))
65 return 0;
66
67 // Save the state of std handles before we make changes to it.
68 if ((FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO) &&
69 savedStdHandleAutoConversionMode[FD] == -1)
70 savedStdHandleAutoConversionMode[FD] = Query.cvtcmd;
71
72 if (FD == STDOUT_FILENO || FD == STDERR_FILENO)
73 Query.cvtcmd = SETCVTON;
74 else
75 Query.cvtcmd = SETCVTALL;
76
77 Query.pccsid =
78 (FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO)
79 ? 0
80 : CCSID_UTF_8;
81 // Assume untagged files to be IBM-1047 encoded.
82 Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
83 return fcntl(FD, F_CONTROL_CVT, &Query);
84}
85
86std::error_code llvm::disablezOSAutoConversion(int FD) {
87 if (::disablezOSAutoConversion(FD) == -1)
88 return errnoAsErrorCode();
89
90 return std::error_code();
91}
92
93std::error_code llvm::enablezOSAutoConversion(int FD) {
94 if (::enablezOSAutoConversion(FD) == -1)
95 return errnoAsErrorCode();
96
97 return std::error_code();
98}
99
100std::error_code llvm::restorezOSStdHandleAutoConversion(int FD) {
101 if (::restorezOSStdHandleAutoConversion(FD) == -1)
102 return errnoAsErrorCode();
103
104 return std::error_code();
105}
106
107std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
108 assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
109 "FT_UNTAGGED and FT_BINARY are not allowed for text files");
110 struct file_tag Tag;
111 Tag.ft_ccsid = CCSID;
112 Tag.ft_txtflag = Text;
113 Tag.ft_deferred = 0;
114 Tag.ft_rsvflags = 0;
115
116 if (fcntl(FD, F_SETTAG, &Tag) == -1)
117 return errnoAsErrorCode();
118 return std::error_code();
119}
120
121ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
122 // If we have a file descriptor, use it to find out file tagging. Otherwise we
123 // need to use stat() with the file path.
124 if (FD != -1) {
125 struct f_cnvrt Query = {
126 QUERYCVT, // cvtcmd
127 0, // pccsid
128 0, // fccsid
129 };
130 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
131 return std::error_code(errno, std::generic_category());
132 return Query.fccsid;
133 }
134 struct stat Attr;
135 if (stat(FileName, &Attr) == -1)
136 return std::error_code(errno, std::generic_category());
137 return Attr.st_tag.ft_ccsid;
138}
139
140ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) {
141 ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD);
142 if (std::error_code EC = Ccsid.getError())
143 return EC;
144 // We don't need conversion for UTF-8 tagged files or binary files.
145 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
146 // problems related to UTF-8 tagged source files.
147 switch (*Ccsid) {
148 case CCSID_UTF_8:
149 case CCSID_ISO8859_1:
150 case FT_BINARY:
151 return false;
152 default:
153 return true;
154 }
155}
156
157#endif //__MVS__
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represents either an error or a value T.
Definition: ErrorOr.h:56
std::error_code getError() const
Definition: ErrorOr.h:152
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition: Error.h:1226