changeset 119:458df24e973d

srogue: use functions from mdport.c. Shell escape, passwd entries, terminal settings, and most signal handling is now done with the more portable md_* functions.
author John "Elwin" Edwards
date Wed, 30 Apr 2014 14:46:30 -0700
parents 8d1dfc5a912c
children d6b7c3fb37ea
files srogue/command.c srogue/configure.ac srogue/io.c srogue/main.c srogue/mdport.c srogue/options.c srogue/rip.c srogue/save.c srogue/wizard.c srogue/xcrypt.c
diffstat 10 files changed, 34 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
--- a/srogue/command.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/command.c	Wed Apr 30 14:46:30 2014 -0700
@@ -369,8 +369,10 @@
 	/*
 	 * Reset the signal in case we got here via an interrupt
 	 */
+#ifdef SIGINT
 	if (signal(SIGINT, quit) != quit)
 		mpos = 0;
+#endif
 	msg("Really quit? [y/n/s]");
 /*	ch = tolower(readchar());*/
 	ch = readchar();
@@ -395,7 +397,9 @@
 		}
 	}
 	else {
+#ifdef SIGINT
 		signal(SIGINT, quit);
+#endif
 		wmove(cw, 0, 0);
 		wclrtoeol(cw);
 		draw(cw);
@@ -637,46 +641,15 @@
 	/*
 	 * Fork and do a shell
 	 */
-#ifndef __DJGPP__
-	while((pid = fork()) < 0)
-		sleep(1);
-	if (pid == 0) {
-		setuid(playuid);	/* Set back to original user */
-		setgid(playgid);
-		execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", 0);
-		perror("No shelly");
-		byebye(-1);
-	}
-	else {
-		signal(SIGINT, SIG_IGN);
-		signal(SIGQUIT, SIG_IGN);
-		while (wait(&ret_status) != pid)
-			continue;
-		signal(SIGINT, quit);
-		signal(SIGQUIT, endit);
-
-#else
-	{
-		char shell[PATH_MAX];
-
-		if (sh && *sh)
-			strncpy(shell,sh,PATH_MAX);
-		else
-			sprintf(shell, "%s\\bin\\sh.exe", getenv("DJDIR"));
-
-		if (spawnl(P_WAIT,shell, "shell", "-i", 0) == -1)
-			msg("No shelly: %s", shell);
-
-#endif
-		printf("\n%s", retstr);
-		fflush(stdout);
-		nonl();
-		noecho();
-		crmode();
-		in_shell = FALSE;
-		wait_for(cw, '\n');
-		restscr(cw);
-	}
+	md_shellescape();
+	printf("\n%s", retstr);
+	fflush(stdout);
+	nonl();
+	noecho();
+	crmode();
+	in_shell = FALSE;
+	wait_for(cw, '\n');
+	restscr(cw);
 }
 
 
--- a/srogue/configure.ac	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/configure.ac	Wed Apr 30 14:46:30 2014 -0700
@@ -15,6 +15,7 @@
 AC_CHECK_HEADERS([arpa/inet.h pwd.h errno.h fcntl.h limits.h nlist.h stdlib.h string.h sys/ioctl.h termios.h unistd.h term.h ncurses/term.h process.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
+AC_TYPE_UID_T
 AC_TYPE_SIZE_T
 AC_STRUCT_TM
 # Checks for library functions.
--- a/srogue/io.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/io.c	Wed Apr 30 14:46:30 2014 -0700
@@ -263,7 +263,6 @@
 
 #ifdef NEED_GETTIME
 #include <stdio.h>
-#include <pwd.h>
 
 /*
  * gettime:
--- a/srogue/main.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/main.c	Wed Apr 30 14:46:30 2014 -0700
@@ -18,11 +18,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include <termios.h>
 #include <fcntl.h>
 #include <stdio.h>
-#include <signal.h>
-#include <pwd.h>
 #include <limits.h>
 #include <sys/stat.h>
 #include "rogue.h"
@@ -39,8 +36,6 @@
 
 #include "rogue.ext"
 
-struct termios terminal;
-
 main(argc, argv, envp)
 char **argv;
 char **envp;
@@ -48,8 +43,6 @@
 	register char *env;
 	register struct linked_list *item;
 	register struct object *obj;
-	struct passwd *pw;
-	struct passwd *getpwuid();
 	char alldone, wpt;
 	char *getpass(), *xcrypt(), *strrchr();
 	int lowtime;
@@ -132,10 +125,11 @@
 
 	if ((env = getenv("HOME")) != NULL)
 		strcpy(home, env);
-	else if ((pw = getpwuid(playuid)) != NULL)
-		strcpy(home, pw->pw_dir);
-	else
-		home[0] = '\0';
+	else {
+		strncpy(home, md_gethomedir(), LINLEN);
+		if (home[LINLEN-1] != '\0')
+			home[0] = '\0';
+	}
 
         if (strcmp(home,"/") == 0)
 		home[0] = '\0';
@@ -153,13 +147,7 @@
 
 	if (!use_savedir && (env == NULL || whoami[0] == '\0'))
 	{
-		if((pw = getpwuid(playuid)) == NULL)
-		{
-			printf("Say, who are you?\n");
-			exit(1);
-		}
-		else
-			strucpy(whoami, pw->pw_name, strlen(pw->pw_name));
+		strucpy(whoami, md_getusername(), strlen(md_getusername()));
 	}
 
 	if (env == NULL || fruit[0] == '\0')
@@ -191,8 +179,7 @@
 	seed = dnum;
 	srand48(seed);			/* init rnd number gen */
 
-	signal(SIGINT, byebye);		/* just in case */
-	signal(SIGQUIT ,byebye);
+	md_onsignal_exit();		/* just in case */
 
 	init_everything();
 
@@ -397,27 +384,7 @@
 */
 setup()
 {
-	signal(SIGHUP, auto_save);
-	signal(SIGINT, auto_save);
-	signal(SIGQUIT,  byebye);
-	signal(SIGILL, game_err);
-	signal(SIGTRAP, game_err);
-#ifdef SIGIOT
-	signal(SIGIOT, game_err);
-#endif
-#ifdef SIGEMT
-	signal(SIGEMT, game_err);
-#endif
-	signal(SIGFPE, game_err);
-#ifdef SIGBUS
-	signal(SIGBUS, game_err);
-#endif
-	signal(SIGSEGV, game_err);
-#ifdef SIGSYS
-	signal(SIGSYS, game_err);
-#endif
-	signal(SIGPIPE, game_err);
-	signal(SIGTERM, game_err);
+	md_onsignal_autosave();
 
 	nonl();
 	cbreak();
@@ -433,9 +400,6 @@
 {
 	reg char *opts;
 
-	tcgetattr(0,&terminal);
-
-
 	/* parse environment declaration of options */
 
 	if ((opts = getenv("ROGUEOPTS")) != NULL)
--- a/srogue/mdport.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/mdport.c	Wed Apr 30 14:46:30 2014 -0700
@@ -210,6 +210,9 @@
 #ifdef SIGTERM
     signal(SIGTERM, exit);
 #endif
+#ifdef SIGINT
+    signal(SIGINT, exit);
+#endif
 }
 
 extern void auto_save(int sig);
--- a/srogue/options.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/options.c	Wed Apr 30 14:46:30 2014 -0700
@@ -14,14 +14,11 @@
  * See the file LICENSE.TXT for full copyright and licensing information.
  */
 
-#include <termios.h>
 #include <ctype.h>
 #include <string.h>
 #include "rogue.h"
 #include "rogue.ext"
 
-extern struct termios terminal;
-
 /*
  * description of an option and what to do with it
  */
@@ -137,7 +134,7 @@
 		}
 		if (c == -1)
 			continue;
-		else if(c == terminal.c_cc[VERASE])	{	/* process erase char */
+		else if(c == md_erasechar())	{	/* process erase char */
 			if (sp > buf) {
 				reg int i;
 	
@@ -147,7 +144,7 @@
 			}
 			continue;
 		}
-		else if (c == terminal.c_cc[VKILL]) {   /* process kill character */
+		else if (c == md_killchar()) {   /* process kill character */
 			sp = buf;
 			wmove(awin, oy, ox);
 			continue;
--- a/srogue/rip.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/rip.c	Wed Apr 30 14:46:30 2014 -0700
@@ -14,11 +14,9 @@
  * See the file LICENSE.TXT for full copyright and licensing information.
  */
 
-#include <signal.h>
 #include <ctype.h>
 #include <string.h>
 #include <sys/types.h>
-#include <pwd.h>
 #include <fcntl.h>
 #include "rogue.h"
 #include "rogue.ext"
@@ -127,8 +125,7 @@
 	reg FILE *outf;
 	char *packend;
 
-	signal(SIGINT, byebye);
-	signal(SIGQUIT, byebye);
+	md_onsignal_exit();
 	if (aflag != WINNER) {
 		if (aflag == CHICKEN)
 			packend = "when you chickened out";
@@ -213,8 +210,7 @@
             encwrite((char *) scoreline, 100, outf);
         }
 	fclose(outf);
-	signal(SIGINT, byebye);
-	signal(SIGQUIT, byebye);
+	md_onsignal_exit();
 	clear();
 	refresh();
 	endwin();
@@ -300,12 +296,7 @@
 			}
 			printf(" [Exp: %d/%ld]",scp->sc_explvl,scp->sc_exppts);
 			if (showname) {
-				struct passwd *pp, *getpwuid();
-
-				if ((pp = getpwuid(scp->sc_uid)) == NULL)
-					printf(" (%d)\n", scp->sc_uid);
-				else
-					printf(" (%s)\n", pp->pw_name);
+				printf(" (%s)\n", md_getrealname(scp->sc_uid));
 			}
 			else
 				printf("\n");
--- a/srogue/save.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/save.c	Wed Apr 30 14:46:30 2014 -0700
@@ -42,10 +42,7 @@
  */
 ignore()
 {
-	int i;
-
-	for (i = 0; i < NSIG; i++)
-		signal(i, SIG_IGN);
+	md_ignoreallsignals();
 }
 
 /*
@@ -191,8 +188,7 @@
 	msg("");
 	rs_save_file(savef);
 	close(fnum);
-	signal(SIGINT, byebye);
-	signal(SIGQUIT, byebye);
+	md_onsignal_exit();
 	wclear(cw);
 	draw(cw);
 }
--- a/srogue/wizard.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/wizard.c	Wed Apr 30 14:46:30 2014 -0700
@@ -15,14 +15,10 @@
  */
 
 #include <stdlib.h>
-#include <termios.h>
 #include <ctype.h>
 #include "rogue.h"
-#include <pwd.h>
 #include "rogue.ext"
 
-extern struct termios terminal;
-
 /*
  * whatis:
  *	What a certain object is
@@ -359,9 +355,9 @@
 	mpos = 0;
 	sp = buf;
 	while ((c = getchar()) != '\n' && c != '\r' && c != ESCAPE)
-	if (c == terminal.c_cc[VKILL])
+	if (c == md_killchar())
 		sp = buf;
-	else if (c == terminal.c_cc[VERASE] && sp > buf)
+	else if (c == md_erasechar() && sp > buf)
 		sp--;
 	else
 		*sp++ = c;
--- a/srogue/xcrypt.c	Sun Apr 27 08:29:14 2014 -0700
+++ b/srogue/xcrypt.c	Wed Apr 30 14:46:30 2014 -0700
@@ -51,7 +51,6 @@
 
 #include <sys/types.h>
 #include <sys/param.h>
-#include <pwd.h>
 #include <string.h>
 
 #ifdef DEBUG