GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
grsisort.cxx
Go to the documentation of this file.
1#include <cstdio>
2#include <string>
3#include <sys/stat.h>
4#include <netdb.h>
5#include <iostream>
6#include <stdexcept>
7#include <pwd.h>
8
9#include "TPluginManager.h"
10#include "TGRSIint.h"
11
12#include "GVersion.h"
13#include "Globals.h"
14#include "TThread.h"
15
16#ifdef __APPLE__
17#define HAVE_UTMPX_H
18#define UTMP_NO_ADDR
19#ifndef ut_user
20#define ut_user ut_name
21#endif
22#ifndef UTMP_FILE
23#define UTMP_FILE "/etc/utmp"
24#endif
25#endif
26
27#ifdef HAVE_UTMPX_H
28#include <utmpx.h>
29#define STRUCT_UTMP struct utmpx
30#else
31#if defined(__linux) && defined(__powerpc) && (__GNUC__ == 2) && (__GNUC_MINOR__ < 90)
32extern "C" {
33#endif
34#include <utmp.h>
35#define STRUCT_UTMP struct utmp
36#endif
37
39
40void SetGRSIEnv();
42static int ReadUtmp();
43static STRUCT_UTMP* SearchEntry(int /*n*/, const char* /*tty*/);
44static void SetDisplay();
45
46TStopwatch* gStopwatch;
47
49{
50 // Be polite when you leave.
51 double realTime = gStopwatch->RealTime();
52 int hour = static_cast<int>(realTime / 3600);
53 realTime -= hour * 3600;
54 int min = static_cast<int>(realTime / 60);
55 realTime -= min * 60;
56 std::cout << DMAGENTA << std::endl
57 << "bye,bye\t" << DCYAN << getpwuid(getuid())->pw_name << RESET_COLOR << " after " << hour << ":"
58 << std::setfill('0') << std::setw(2) << min << ":" << std::setprecision(3) << std::fixed << realTime
59 << " h:m:s" << std::endl;
60}
61
62int main(int argc, char** argv)
63{
64 gStopwatch = new TStopwatch;
65 std::atexit(atexitHandler);
66
67 try {
68 TThread::Initialize();
69 TObject::SetObjectStat(false);
70
71 SetDisplay();
74 TGRSIint* input = nullptr;
75
76 // Create an instance of the grsi interpreter so that we can run root-like interpretive mode
77 input = TGRSIint::instance(argc, argv);
78 input->SetReturnFromRun(true);
79 // Run the code!
80 input->Run(true);
81 } catch(grsi::exit_exception& e) {
82 std::cerr << e.message << std::endl;
83 // Close files and clean up properly here
84 } catch(std::runtime_error& e) {
85 std::cerr << e.what() << std::endl;
86 std::cout << "Don't know how to handle this error, exiting " << argv[0] << "!" << std::endl;
87 }
88
89 return 0;
90}
91
93{
94 // gPluginMgr->AddHandler("GRootCanvas","grsi","GRootCanvas"
95 gPluginMgr->AddHandler("TGuiFactory", "root", "GROOTGuiFactory", "Gui", "GROOTGuiFactory()");
96 gPluginMgr->AddHandler("TBrowserImp", "GRootBrowser", "GRootBrowser", "Gui",
97 "NewBrowser(TBrowser *,const char *,Int_t,Int_t,UInt_t,UInt_t");
98 gPluginMgr->AddHandler("TBrowserImp", "GRootBrowser", "GRootBrowser", "Gui",
99 "NewBrowser(TBrowser *,const char *,Int_t,Int_t");
100}
101
102static int ReadUtmp()
103{
104 FILE* utmp = nullptr;
105 struct stat file_stats{};
106 size_t n_read = 0;
107 size_t size = 0;
108
109 gUtmpContents = nullptr;
110
111 utmp = fopen(UTMP_FILE, "r");
112 if(utmp == nullptr) {
113 return 0;
114 }
115
116 fstat(fileno(utmp), &file_stats);
117 size = file_stats.st_size;
118 if(size <= 0) {
119 fclose(utmp);
120 return 0;
121 }
122
123 gUtmpContents = new STRUCT_UTMP; //static_cast<STRUCT_UTMP*>(malloc(size));
124 if(gUtmpContents == nullptr) {
125 fclose(utmp);
126 return 0;
127 }
128
129 n_read = fread(gUtmpContents, 1, size, utmp);
130 if(ferror(utmp) == 0) {
131 if(fclose(utmp) != EOF && n_read == size) {
132 return static_cast<int>(size / sizeof(STRUCT_UTMP));
133 }
134 } else {
135 fclose(utmp);
136 }
137
138 delete gUtmpContents;
139 gUtmpContents = nullptr;
140 return 0;
141}
142
143static STRUCT_UTMP* SearchEntry(int n, const char* tty)
144{
146 while((n--) != 0) {
147 if((ue->ut_name[0] != 0) && (strncmp(tty, ue->ut_line, sizeof(ue->ut_line)) == 0)) { // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
148 return ue;
149 }
150 ue++;
151 }
152 return nullptr;
153}
154
155static void SetDisplay()
156{
157 // Set DISPLAY environment variable.
158
159 if(getenv("DISPLAY") == nullptr) {
160 char* tty = ttyname(0); // device user is logged in on
161 if(tty != nullptr) {
162 tty += 5; // remove "/dev/"
163 STRUCT_UTMP* utmp_entry = SearchEntry(ReadUtmp(), tty);
164 if(utmp_entry != nullptr) {
165 size_t length = sizeof(utmp_entry->ut_host);
166 std::string display;
167 auto* host = new char[length + 1];
168 // instead of using size of utmp_entry->ut_host to prevent warning from gcc 9.1
169 strncpy(host, utmp_entry->ut_host, length); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
170 host[sizeof(utmp_entry->ut_host)] = 0;
171 if(host[0] != 0) {
172 display = host;
173 if(strchr(host, ':') != nullptr) {
174 fprintf(stderr, "*** DISPLAY not set, setting it to %s\n", host);
175 } else {
176 fprintf(stderr, "*** DISPLAY not set, setting it to %s:0.0\n", host);
177 display += ":0.0";
178 }
179 setenv("DISPLAY", display.c_str(), 0);
180#ifndef UTMP_NO_ADDR
181 } else if(utmp_entry->ut_addr != 0) {
182 struct hostent* he = gethostbyaddr(reinterpret_cast<const char*>(&utmp_entry->ut_addr), sizeof(utmp_entry->ut_addr), AF_INET);
183 if(he != nullptr) {
184 fprintf(stderr, "*** DISPLAY not set, setting it to %s:0.0\n", he->h_name);
185 display = he->h_name;
186 display += ":0.0";
187 setenv("DISPLAY", display.c_str(), 0);
188 }
189#endif
190 }
191 delete[] host;
192 }
193 delete gUtmpContents;
194 }
195 }
196}
#define DCYAN
Definition Globals.h:21
#define RESET_COLOR
Definition Globals.h:5
#define DMAGENTA
Definition Globals.h:20
static TGRSIint * instance(int argc=0, char **argv=nullptr, void *options=nullptr, int numOptions=-1, bool noLogo=false, const char *appClassName="grsisort")
Definition TGRSIint.cxx:53
void atexitHandler()
Definition grsisort.cxx:48
static STRUCT_UTMP * SearchEntry(int, const char *)
Definition grsisort.cxx:143
TStopwatch * gStopwatch
Definition grsisort.cxx:46
int main(int argc, char **argv)
Definition grsisort.cxx:62
static STRUCT_UTMP * gUtmpContents
Definition grsisort.cxx:38
#define STRUCT_UTMP
Definition grsisort.cxx:35
void SetGRSIEnv()
static int ReadUtmp()
Definition grsisort.cxx:102
static void SetDisplay()
Definition grsisort.cxx:155
void SetGRSIPluginHandlers()
Definition grsisort.cxx:92
void SetGRSIEnv()
Definition Globals.cxx:3
const char * message
Definition Globals.h:83