AutoConvert.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifdef __MVS__
  14. #include "llvm/Support/AutoConvert.h"
  15. #include <fcntl.h>
  16. #include <sys/stat.h>
  17. std::error_code llvm::disableAutoConversion(int FD) {
  18. static const struct f_cnvrt Convert = {
  19. SETCVTOFF, // cvtcmd
  20. 0, // pccsid
  21. (short)FT_BINARY, // fccsid
  22. };
  23. if (fcntl(FD, F_CONTROL_CVT, &Convert) == -1)
  24. return std::error_code(errno, std::generic_category());
  25. return std::error_code();
  26. }
  27. std::error_code llvm::enableAutoConversion(int FD) {
  28. struct f_cnvrt Query = {
  29. QUERYCVT, // cvtcmd
  30. 0, // pccsid
  31. 0, // fccsid
  32. };
  33. if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
  34. return std::error_code(errno, std::generic_category());
  35. Query.cvtcmd = SETCVTALL;
  36. Query.pccsid =
  37. (FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO)
  38. ? 0
  39. : CCSID_UTF_8;
  40. // Assume untagged files to be IBM-1047 encoded.
  41. Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
  42. if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
  43. return std::error_code(errno, std::generic_category());
  44. return std::error_code();
  45. }
  46. std::error_code llvm::setFileTag(int FD, int CCSID, bool Text) {
  47. assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
  48. "FT_UNTAGGED and FT_BINARY are not allowed for text files");
  49. struct file_tag Tag;
  50. Tag.ft_ccsid = CCSID;
  51. Tag.ft_txtflag = Text;
  52. Tag.ft_deferred = 0;
  53. Tag.ft_rsvflags = 0;
  54. if (fcntl(FD, F_SETTAG, &Tag) == -1)
  55. return std::error_code(errno, std::generic_category());
  56. return std::error_code();
  57. }
  58. #endif // __MVS__