Skip to content

Commit

Permalink
Bug fixes (redirect to file starting with ~, multiple interpreters moved
Browse files Browse the repository at this point in the history
in function)
  • Loading branch information
N-Holzschuch committed May 21, 2022
1 parent 0631648 commit 21d4f6e
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 189 deletions.
6 changes: 5 additions & 1 deletion ios_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern "C" {
#define fputc ios_fputc
#define putw ios_putw
#define fflush ios_fflush
#define abort() ios_exit(1)
#endif

// Thread-local input and output streams
Expand All @@ -49,7 +50,6 @@ extern __thread FILE* thread_stdout;
extern __thread FILE* thread_stderr;

#define exit ios_exit
#define abort() ios_exit(1)
#define _exit ios_exit
#define kill ios_killpid
#define _kill ios_killpid
Expand Down Expand Up @@ -109,6 +109,10 @@ extern int ios_opentty(void);
extern void ios_closetty(void);
extern void ios_stopInteractive(void);
extern void ios_startInteractive(void);
// Communication between dash and ios_system:
extern const char* ios_expandtilde(const char *login);
extern void ios_activateChildStreams(FILE** old_stdin, FILE** old_stdout, FILE ** old_stderr);
extern const char* ios_getBookmarkedVersion(const char* p);

#ifdef __cplusplus
}
Expand Down
658 changes: 486 additions & 172 deletions ios_system.m

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions ios_system/ios_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

//! Project version number for ios_system.
FOUNDATION_EXPORT double ios_systemVersionNumber;
Expand Down Expand Up @@ -76,5 +77,7 @@ extern void makeGlobal(void);
extern void makeLocal(void);
extern void replaceCommand(NSString* commandName, NSString* functionName, bool allOccurences);
extern NSError* addCommandList(NSString* fileLocation);
extern NSArray* backgroundCommandList;
extern int numPythonInterpreters;
extern int cd_main(int argc, char** argv);
extern const char* ios_getBookmarkedVersion(const char* p);
20 changes: 17 additions & 3 deletions libc_replacement.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ void makeLocal(void) {

inline pthread_t ios_getThreadId(pid_t pid) {
// return ios_getLastThreadId(); // previous behaviour
if (pid >= IOS_MAX_THREADS) { return -1; }
return thread_ids[pid];
}

Expand Down Expand Up @@ -224,8 +225,9 @@ inline void ios_storeThreadId(pthread_t thread) {
char* libc_getenv(const char* variableName) {
if (environment[current_pid] != NULL) {
if (variableName == NULL) { return NULL; }
// fprintf(stderr, "libc_getenv: %s\n", variableName); fflush(stderr);
char** envp = environment[current_pid];
int varNameLen = strlen(variableName);
unsigned long varNameLen = strlen(variableName);
if (varNameLen == 0) { return NULL; }
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
if (envp[i] == NULL) { continue; }
Expand Down Expand Up @@ -268,7 +270,7 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
return -1;
}
char** envp = environment[current_pid];
int varNameLen = strlen(variableName);
unsigned long varNameLen = strlen(variableName);
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
if (envp[i] == NULL) { continue; }
if (strncmp(variableName, envp[i], varNameLen) == 0) {
Expand Down Expand Up @@ -352,7 +354,7 @@ int ios_unsetenv(const char* variableName) {
return -1;
}
char** envp = environment[current_pid];
int varNameLen = strlen(variableName);
unsigned long varNameLen = strlen(variableName);
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
if (envp[i] == NULL) { continue; }
if (strncmp(variableName, envp[i], varNameLen) == 0) {
Expand Down Expand Up @@ -454,6 +456,18 @@ void ios_releaseThread(pthread_t thread) {
// fprintf(stderr, "Not found\n");
}

void ios_releaseBackgroundThread(pthread_t thread) {
// Same as ios_releaseThread, but do not reset the directory.
for (int p = 0; p < IOS_MAX_THREADS; p++) {
if (thread_ids[p] == thread) {
// fprintf(stderr, "Found Id %d\n", p);
current_pid = previousPid[p];
thread_ids[p] = NULL;
return;
}
}
// fprintf(stderr, "Not found\n");
}

void ios_releaseThreadId(pid_t pid) {
// Don't reset the environment; sometimes, commands try to change the environment while it is being erased.
Expand Down
4 changes: 2 additions & 2 deletions shell_cmds/pwd/pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ __FBSDID("$FreeBSD: src/bin/pwd/pwd.c,v 1.25 2005/02/09 17:37:38 ru Exp $");

static char *getcwd_logical(void);
static void usage(void);
extern void ios_printBookmarkedVersion(char* p);

int
pwd_main(int argc, char *argv[])
Expand Down Expand Up @@ -98,7 +97,8 @@ pwd_main(int argc, char *argv[])
if (physical) {
fprintf(thread_stdout, "%s\n", p);
} else {
ios_printBookmarkedVersion(p);
fprintf(thread_stdout, ios_getBookmarkedVersion(p));
fprintf(thread_stdout, "\n");
}
} else {
err(1, ".");
Expand Down
18 changes: 9 additions & 9 deletions text_cmds/grep/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ __FBSDID("$FreeBSD: src/usr.bin/grep/file.c,v 1.7 2011/10/11 22:27:23 gabor Exp
#define MAXBUFSIZ (32 * 1024)
#define LNBUFBUMP 80

static gzFile gzbufdesc;
static __thread gzFile gzbufdesc;
#ifndef WITHOUT_LZMA
static lzma_stream lstrm = LZMA_STREAM_INIT;
static __thread lzma_stream lstrm = LZMA_STREAM_INIT;
#endif
#ifndef WITHOUT_BZIP2
static BZFILE* bzbufdesc;
static __thread BZFILE* bzbufdesc;
#endif

static unsigned char *buffer;
static unsigned char *bufpos;
static size_t bufrem;
static size_t fsiz;
static __thread unsigned char *buffer;
static __thread unsigned char *bufpos;
static __thread size_t bufrem;
static __thread size_t fsiz;

static unsigned char *lnbuf;
static size_t lnbuflen;
static __thread unsigned char *lnbuf;
static __thread size_t lnbuflen;

static inline int
grep_refill(struct file *f)
Expand Down
4 changes: 3 additions & 1 deletion text_cmds/grep/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ struct qentry {
struct str data;
};

// queue is the only item in grep that is not a thread-local variable.
// it could create issues, but at the same time the macros are not thread-friendly.
static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue);
static unsigned long long count;
static __thread unsigned long long count;

static struct qentry *dequeue(void);

Expand Down
2 changes: 1 addition & 1 deletion text_cmds/grep/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ __FBSDID("$FreeBSD: src/usr.bin/grep/util.c,v 1.19 2011/12/07 12:25:28 gabor Exp
#include "grep.h"
#include "ios_error.h"

static int linesqueued;
static __thread int linesqueued;
static int procline(struct str *l, int);

bool
Expand Down

0 comments on commit 21d4f6e

Please sign in to comment.