Changeset View
Changeset View
Standalone View
Standalone View
head/bin/sh/parser.c
Show All 34 Lines | |||||
#ifndef lint | #ifndef lint | ||||
#if 0 | #if 0 | ||||
static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; | static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; | ||||
#endif | #endif | ||||
#endif /* not lint */ | #endif /* not lint */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | |||||
#include <pwd.h> | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include "shell.h" | #include "shell.h" | ||||
#include "parser.h" | #include "parser.h" | ||||
#include "nodes.h" | #include "nodes.h" | ||||
#include "expand.h" /* defines rmescapes() */ | #include "expand.h" /* defines rmescapes() */ | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | |||||
static int xxreadtoken(void); | static int xxreadtoken(void); | ||||
static int readtoken1(int, const char *, const char *, int); | static int readtoken1(int, const char *, const char *, int); | ||||
static int noexpand(char *); | static int noexpand(char *); | ||||
static void consumetoken(int); | static void consumetoken(int); | ||||
static void synexpect(int) __dead2; | static void synexpect(int) __dead2; | ||||
static void synerror(const char *) __dead2; | static void synerror(const char *) __dead2; | ||||
static void setprompt(int); | static void setprompt(int); | ||||
static int pgetc_linecont(void); | static int pgetc_linecont(void); | ||||
static void getusername(char *, size_t); | |||||
static void * | static void * | ||||
parser_temp_alloc(size_t len) | parser_temp_alloc(size_t len) | ||||
{ | { | ||||
struct parser_temp *t; | struct parser_temp *t; | ||||
INTOFF; | INTOFF; | ||||
▲ Show 20 Lines • Show All 1,823 Lines • ▼ Show 20 Lines | if (c == '\n') { | ||||
/* Allow the backslash to be pushed back. */ | /* Allow the backslash to be pushed back. */ | ||||
pushstring("\\", 1, NULL); | pushstring("\\", 1, NULL); | ||||
return (pgetc()); | return (pgetc()); | ||||
} | } | ||||
} | } | ||||
return (c); | return (c); | ||||
} | } | ||||
static struct passwd * | |||||
getpwlogin(void) | |||||
{ | |||||
const char *login; | |||||
login = getlogin(); | |||||
if (login == NULL) | |||||
return (NULL); | |||||
return (getpwnam(login)); | |||||
} | |||||
static void | |||||
getusername(char *name, size_t namelen) | |||||
{ | |||||
static char cached_name[MAXLOGNAME]; | |||||
struct passwd *pw; | |||||
uid_t euid; | |||||
if (cached_name[0] == '\0') { | |||||
euid = geteuid(); | |||||
/* | /* | ||||
* Handle the case when there is more than one | |||||
* login with the same UID, or when the login | |||||
* returned by getlogin(2) does no longer match | |||||
* the current UID. | |||||
*/ | |||||
pw = getpwlogin(); | |||||
if (pw == NULL || pw->pw_uid != euid) | |||||
pw = getpwuid(euid); | |||||
if (pw != NULL) { | |||||
strlcpy(cached_name, pw->pw_name, | |||||
sizeof(cached_name)); | |||||
} else { | |||||
snprintf(cached_name, sizeof(cached_name), | |||||
"%u", euid); | |||||
} | |||||
} | |||||
strlcpy(name, cached_name, namelen); | |||||
} | |||||
/* | |||||
* called by editline -- any expansions to the prompt | * called by editline -- any expansions to the prompt | ||||
* should be added here. | * should be added here. | ||||
*/ | */ | ||||
char * | char * | ||||
getprompt(void *unused __unused) | getprompt(void *unused __unused) | ||||
{ | { | ||||
static char ps[PROMPTLEN]; | static char ps[PROMPTLEN]; | ||||
const char *fmt; | const char *fmt; | ||||
Show All 38 Lines | if (*fmt == '\\') | ||||
ps[i] = '\0'; | ps[i] = '\0'; | ||||
gethostname(&ps[i], PROMPTLEN - i - 1); | gethostname(&ps[i], PROMPTLEN - i - 1); | ||||
ps[PROMPTLEN - 1] = '\0'; | ps[PROMPTLEN - 1] = '\0'; | ||||
/* Skip to end of hostname. */ | /* Skip to end of hostname. */ | ||||
trim = (*fmt == 'h') ? '.' : '\0'; | trim = (*fmt == 'h') ? '.' : '\0'; | ||||
while ((ps[i] != '\0') && (ps[i] != trim)) | while ((ps[i] != '\0') && (ps[i] != trim)) | ||||
i++; | i++; | ||||
--i; | --i; | ||||
break; | |||||
/* | |||||
* User name. | |||||
*/ | |||||
case 'u': | |||||
ps[i] = '\0'; | |||||
getusername(&ps[i], PROMPTLEN - i); | |||||
/* Skip to end of username. */ | |||||
while (ps[i + 1] != '\0') | |||||
i++; | |||||
break; | break; | ||||
/* | /* | ||||
* Working directory. | * Working directory. | ||||
* | * | ||||
* \W specifies just the final component, | * \W specifies just the final component, | ||||
* \w specifies the entire path. | * \w specifies the entire path. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 106 Lines • Show Last 20 Lines |