1 //===- AutoConvert.cpp - Auto conversion between ASCII/EBCDIC -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains functions used for auto conversion between
10 // ASCII/EBCDIC codepages specific to z/OS.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/AutoConvert.h"
17 #include "llvm/Support/Error.h"
25 static int savedStdHandleAutoConversionMode
[3] = {-1, -1, -1};
27 int disablezOSAutoConversion(int FD
) {
28 static const struct f_cnvrt Convert
= {
34 return fcntl(FD
, F_CONTROL_CVT
, &Convert
);
37 int restorezOSStdHandleAutoConversion(int FD
) {
38 assert(FD
== STDIN_FILENO
|| FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
);
39 if (savedStdHandleAutoConversionMode
[FD
] == -1)
41 struct f_cnvrt Cvt
= {
42 savedStdHandleAutoConversionMode
[FD
], // cvtcmd
46 return (fcntl(FD
, F_CONTROL_CVT
, &Cvt
));
49 int enablezOSAutoConversion(int FD
) {
50 struct f_cnvrt Query
= {
56 if (fcntl(FD
, F_CONTROL_CVT
, &Query
) == -1)
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
))
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
;
72 if (FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
)
73 Query
.cvtcmd
= SETCVTON
;
75 Query
.cvtcmd
= SETCVTALL
;
78 (FD
== STDIN_FILENO
|| FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
)
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
);
86 std::error_code
llvm::disablezOSAutoConversion(int FD
) {
87 if (::disablezOSAutoConversion(FD
) == -1)
88 return errnoAsErrorCode();
90 return std::error_code();
93 std::error_code
llvm::enablezOSAutoConversion(int FD
) {
94 if (::enablezOSAutoConversion(FD
) == -1)
95 return errnoAsErrorCode();
97 return std::error_code();
100 std::error_code
llvm::restorezOSStdHandleAutoConversion(int FD
) {
101 if (::restorezOSStdHandleAutoConversion(FD
) == -1)
102 return errnoAsErrorCode();
104 return std::error_code();
107 std::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");
111 Tag
.ft_ccsid
= CCSID
;
112 Tag
.ft_txtflag
= Text
;
116 if (fcntl(FD
, F_SETTAG
, &Tag
) == -1)
117 return errnoAsErrorCode();
118 return std::error_code();
121 ErrorOr
<__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.
125 struct f_cnvrt Query
= {
130 if (fcntl(FD
, F_CONTROL_CVT
, &Query
) == -1)
131 return std::error_code(errno
, std::generic_category());
135 if (stat(FileName
, &Attr
) == -1)
136 return std::error_code(errno
, std::generic_category());
137 return Attr
.st_tag
.ft_ccsid
;
140 ErrorOr
<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())
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.
149 case CCSID_ISO8859_1
: