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"
22 static int savedStdHandleAutoConversionMode
[3] = {-1, -1, -1};
24 int disableAutoConversion(int FD
) {
25 static const struct f_cnvrt Convert
= {
31 return fcntl(FD
, F_CONTROL_CVT
, &Convert
);
34 int restoreStdHandleAutoConversion(int FD
) {
35 assert(FD
== STDIN_FILENO
|| FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
);
36 if (savedStdHandleAutoConversionMode
[FD
] == -1)
38 struct f_cnvrt Cvt
= {
39 savedStdHandleAutoConversionMode
[FD
], // cvtcmd
43 return (fcntl(FD
, F_CONTROL_CVT
, &Cvt
));
46 int enableAutoConversion(int FD
) {
47 struct f_cnvrt Query
= {
53 if (fcntl(FD
, F_CONTROL_CVT
, &Query
) == -1)
56 // We don't need conversion for UTF-8 tagged files.
57 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
58 // problems related to UTF-8 tagged source files.
59 // When the pccsid is not ISO8859-1, autoconversion is still needed.
60 if (Query
.pccsid
== CCSID_ISO8859_1
&&
61 (Query
.fccsid
== CCSID_UTF_8
|| Query
.fccsid
== CCSID_ISO8859_1
))
64 // Save the state of std handles before we make changes to it.
65 if ((FD
== STDIN_FILENO
|| FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
) &&
66 savedStdHandleAutoConversionMode
[FD
] == -1)
67 savedStdHandleAutoConversionMode
[FD
] = Query
.cvtcmd
;
69 if (FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
)
70 Query
.cvtcmd
= SETCVTON
;
72 Query
.cvtcmd
= SETCVTALL
;
75 (FD
== STDIN_FILENO
|| FD
== STDOUT_FILENO
|| FD
== STDERR_FILENO
)
78 // Assume untagged files to be IBM-1047 encoded.
79 Query
.fccsid
= (Query
.fccsid
== FT_UNTAGGED
) ? CCSID_IBM_1047
: Query
.fccsid
;
80 return fcntl(FD
, F_CONTROL_CVT
, &Query
);
83 std::error_code
llvm::disableAutoConversion(int FD
) {
84 if (::disableAutoConversion(FD
) == -1)
85 return std::error_code(errno
, std::generic_category());
87 return std::error_code();
90 std::error_code
llvm::enableAutoConversion(int FD
) {
91 if (::enableAutoConversion(FD
) == -1)
92 return std::error_code(errno
, std::generic_category());
94 return std::error_code();
97 std::error_code
llvm::restoreStdHandleAutoConversion(int FD
) {
98 if (::restoreStdHandleAutoConversion(FD
) == -1)
99 return std::error_code(errno
, std::generic_category());
101 return std::error_code();
104 std::error_code
llvm::setFileTag(int FD
, int CCSID
, bool Text
) {
105 assert((!Text
|| (CCSID
!= FT_UNTAGGED
&& CCSID
!= FT_BINARY
)) &&
106 "FT_UNTAGGED and FT_BINARY are not allowed for text files");
108 Tag
.ft_ccsid
= CCSID
;
109 Tag
.ft_txtflag
= Text
;
113 if (fcntl(FD
, F_SETTAG
, &Tag
) == -1)
114 return std::error_code(errno
, std::generic_category());
115 return std::error_code();