1218885Sdim//===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10276479Sdim//  This file implements the operating system Process concept.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14276479Sdim#include "llvm/ADT/StringExtras.h"
15249423Sdim#include "llvm/Config/config.h"
16276479Sdim#include "llvm/Support/FileSystem.h"
17288943Sdim#include "llvm/Support/Path.h"
18218885Sdim#include "llvm/Support/Process.h"
19276479Sdim#include "llvm/Support/Program.h"
20218885Sdim
21249423Sdimusing namespace llvm;
22218885Sdimusing namespace sys;
23218885Sdim
24218885Sdim//===----------------------------------------------------------------------===//
25218885Sdim//=== WARNING: Implementation here must contain only TRULY operating system
26218885Sdim//===          independent code.
27218885Sdim//===----------------------------------------------------------------------===//
28218885Sdim
29276479SdimOptional<std::string> Process::FindInEnvPath(const std::string& EnvName,
30276479Sdim                                             const std::string& FileName)
31276479Sdim{
32288943Sdim  assert(!path::is_absolute(FileName));
33276479Sdim  Optional<std::string> FoundPath;
34276479Sdim  Optional<std::string> OptPath = Process::GetEnv(EnvName);
35276479Sdim  if (!OptPath.hasValue())
36276479Sdim    return FoundPath;
37249423Sdim
38276479Sdim  const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
39276479Sdim  SmallVector<StringRef, 8> Dirs;
40276479Sdim  SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
41249423Sdim
42276479Sdim  for (const auto &Dir : Dirs) {
43276479Sdim    if (Dir.empty())
44276479Sdim      continue;
45249423Sdim
46276479Sdim    SmallString<128> FilePath(Dir);
47276479Sdim    path::append(FilePath, FileName);
48276479Sdim    if (fs::exists(Twine(FilePath))) {
49276479Sdim      FoundPath = FilePath.str();
50276479Sdim      break;
51276479Sdim    }
52276479Sdim  }
53276479Sdim
54276479Sdim  return FoundPath;
55276479Sdim}
56276479Sdim
57276479Sdim
58261991Sdim#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
59261991Sdim
60261991Sdim#define ALLCOLORS(FGBG,BOLD) {\
61261991Sdim    COLOR(FGBG, "0", BOLD),\
62261991Sdim    COLOR(FGBG, "1", BOLD),\
63261991Sdim    COLOR(FGBG, "2", BOLD),\
64261991Sdim    COLOR(FGBG, "3", BOLD),\
65261991Sdim    COLOR(FGBG, "4", BOLD),\
66261991Sdim    COLOR(FGBG, "5", BOLD),\
67261991Sdim    COLOR(FGBG, "6", BOLD),\
68261991Sdim    COLOR(FGBG, "7", BOLD)\
69261991Sdim  }
70261991Sdim
71261991Sdimstatic const char colorcodes[2][2][8][10] = {
72261991Sdim { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
73261991Sdim { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
74261991Sdim};
75261991Sdim
76218885Sdim// Include the platform-specific parts of this class.
77218885Sdim#ifdef LLVM_ON_UNIX
78218885Sdim#include "Unix/Process.inc"
79218885Sdim#endif
80218885Sdim#ifdef LLVM_ON_WIN32
81218885Sdim#include "Windows/Process.inc"
82218885Sdim#endif
83