Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/shitty/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ int ApplicationImpl::run(int argc, char* argv[]) {
// window, and setenv() must never run in a forked child of a
// multithreaded process: glibc's environ lock is not reset at fork.
configureTerminalChildEnvironment(*composer.brand, composer.opts->vt.widths);
enterHomeWhenLaunchedFromDesktop();
composer.fontSize = composer.opts->fontsize;
composer.inputRemap = InputRemap::create(composer);
if (testFd >= 0) {
Expand Down
10 changes: 9 additions & 1 deletion lib/shitty/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "toml.h"
#include "brand.h"
#include "darts.h"
#include "startup.h"
#include "terminal_colors.h"

#include <lib/vterm/num.h>
Expand All @@ -38,6 +39,7 @@
#include <wchar.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

using namespace stl;

Expand Down Expand Up @@ -1272,7 +1274,13 @@ void OptionsParser::parse() {
vt.boldColors = getBool("boldColors");
vt.kittyCtrlBaseLayout = getBool("kittyCtrlBaseLayout");
noDecorations = getBool("no-decorations");
login = getBool("login");
// A desktop launch has no shell environment to inherit, so the
// profile files that build PATH (Homebrew's lives in .zprofile) only
// run for a login shell. An explicit login setting still wins.
StringView loginOption;
OptionSource loginSource = OptionSource::NONE;
get("login", loginOption, &loginSource);
login = getBool("login") || (loginSource == OptionSource::HardDefault && launchedFromDesktop(getppid()));
maximized = getBool("maximized");
fullscreen = getBool("fullscreen");
showWraps = getBool("showWraps");
Expand Down
32 changes: 32 additions & 0 deletions lib/shitty/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <lib/vterm/fatal.h>
#include <lib/vterm/term_features.h>

#include <std/ios/sys.h>

#include <pwd.h>
#include <limits.h>
#include <stdlib.h>
Expand Down Expand Up @@ -149,3 +151,33 @@ void configureTerminalChildEnvironment(const Brand& brand, const UnicodeWidths&
}
brand.configureVersionEnvironment();
}

bool launchedFromDesktop(pid_t parent) {
#if defined(__APPLE__)
return parent == 1;
#else
(void)(parent);
return false;
#endif
}

void enterHomeWhenLaunchedFromDesktop() {
if (!launchedFromDesktop(getppid())) {
return;
}
char current[PATH_MAX];
if (getcwd(current, sizeof(current)) == nullptr || StringView(current) != StringView(u8"/")) {
return;
}
const char* home = getenv("HOME");
if (home == nullptr || home[0] != '/') {
const passwd* entry = getpwuid(getuid());
home = entry != nullptr ? entry->pw_dir : nullptr;
}
if (home == nullptr || home[0] != '/') {
return;
}
if (chdir(home) < 0) {
sysO << StringView(u8"Warning: could not enter the home directory.") << endL;
}
}
10 changes: 10 additions & 0 deletions lib/shitty/startup.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <std/lib/vector.h>
#include <std/sys/types.h>

#include <sys/types.h>

struct Brand;
class UnicodeWidths;

Expand All @@ -30,3 +32,11 @@ struct LaunchCommand {
LaunchCommand buildLaunchCommand(int argc, char* argv[], stl::StringView defaultShell, bool login);

void configureTerminalChildEnvironment(const Brand& brand, const UnicodeWidths& widths);

// True when launchd itself started this process - a Finder, Dock or open(1)
// launch of the .app on macOS - so no shell handed it an environment.
// parent is the parent process id; always false outside macOS.
bool launchedFromDesktop(pid_t parent);

// A desktop launch starts in "/", which is no place for a shell to begin.
void enterHomeWhenLaunchedFromDesktop();
10 changes: 10 additions & 0 deletions lib/shitty/startup_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ STD_TEST_SUITE(Startup) {
// The width level lands in the reported features verbatim.
STD_INSIST(StringView(getenv("TERM_FEATURES")) == StringView("T3CwLrMSc7UUw15Ts3BFGsGoSyHNoSxP"));
}

STD_TEST(OnlyLaunchdAsParentMeansADesktopLaunch) {
STD_INSIST(!launchedFromDesktop(2));
STD_INSIST(!launchedFromDesktop(4711));
#if defined(__APPLE__)
STD_INSIST(launchedFromDesktop(1));
#else
STD_INSIST(!launchedFromDesktop(1));
#endif
}
}