#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "hack.h"
extern int CO, LI;
extern char *CD;
extern char quitchars[];
static void page_more(FILE *, int);
int
dowhatis(void)
{
FILE *fp;
char bufr[BUFSZ+6];
char *buf = &bufr[6], q;
size_t len;
extern char readchar();
if (!(fp = fopen(DATAFILE, "r")))
pline("Cannot open data file!");
else {
pline("Specify what? ");
q = readchar();
if (q != '\t')
while (fgets(buf,BUFSZ,fp))
if (*buf == q) {
len = strcspn(buf, "\n");
if (len == 0)
continue;
buf[len] = '\0';
if (buf[1] == '\t'){
buf = bufr;
buf[0] = q;
(void) strncpy(buf+1, " ", 7);
len = strlen(buf);
}
pline("%s", buf);
if (buf[len - 1] == ';') {
pline("More info? ");
if (readchar() == 'y') {
page_more(fp,1);
return(0);
}
}
(void) fclose(fp);
return(0);
}
pline("I've never heard of such things.");
(void) fclose(fp);
}
return(0);
}
static int got_intrup;
void
intruph(int notused)
{
got_intrup++;
}
static void
page_more(FILE *fp, int strip)
{
char *bufr;
sig_t prevsig = signal(SIGINT, intruph);
set_pager(0);
bufr = (char *) alloc((unsigned) CO);
while (fgets(bufr, CO, fp) && (!strip || *bufr == '\t') &&
!got_intrup) {
bufr[strcspn(bufr, "\n")] = '\0';
if (page_line(bufr+strip)) {
set_pager(2);
goto ret;
}
}
set_pager(1);
ret:
free(bufr);
(void) fclose(fp);
(void) signal(SIGINT, prevsig);
got_intrup = 0;
}
static boolean whole_screen = TRUE;
#define PAGMIN 12
void
set_whole_screen(void)
{
whole_screen = (LI-ROWNO-2 <= PAGMIN || !CD);
}
#ifdef NEWS
int
readnews(void)
{
int ret;
whole_screen = TRUE;
ret = page_file(NEWS, TRUE);
set_whole_screen();
return(ret);
}
#endif
void
set_pager(int mode)
{
static boolean so;
if(mode == 0) {
if(!whole_screen) {
clrlin();
curs(1, ROWNO+4);
} else {
cls();
}
so = flags.standout;
flags.standout = 1;
} else {
if(mode == 1) {
curs(1, LI);
more();
}
flags.standout = so;
if(whole_screen)
docrt();
else {
curs(1, ROWNO+4);
cl_eos();
}
}
}
int
page_line(char *s)
{
extern char morc;
if(cury == LI-1) {
if(!*s)
return(0);
putchar('\n');
cury++;
cmore("q\033");
if(morc) {
morc = 0;
return(1);
}
if(whole_screen)
cls();
else {
curs(1, ROWNO+4);
cl_eos();
}
}
puts(s);
cury++;
return(0);
}
void
cornline(int mode, char *text)
{
static struct line {
struct line *next_line;
char *line_text;
} *texthead, *texttail;
static int maxlen;
static int linect;
struct line *tl;
if(mode == 0) {
texthead = 0;
maxlen = 0;
linect = 0;
if(text) {
cornline(1, text);
cornline(1, "");
}
return;
}
if(mode == 1) {
int len;
if(!text) return;
linect++;
len = strlen(text);
if(len > maxlen)
maxlen = len;
tl = (struct line *)
alloc((unsigned)(len + sizeof(struct line) + 1));
tl->next_line = 0;
tl->line_text = (char *)(tl + 1);
(void) strlcpy(tl->line_text, text, len + 1);
if(!texthead)
texthead = tl;
else
texttail->next_line = tl;
texttail = tl;
return;
}
if(mode == 2 && linect == 1)
pline("%s", texthead->line_text);
else
if(mode == 2) {
int curline, lth;
if(flags.toplin == 1) more();
remember_topl();
lth = CO - maxlen - 2;
if (linect < LI && lth >= 10) {
home();
cl_end();
flags.toplin = 0;
curline = 1;
for (tl = texthead; tl; tl = tl->next_line) {
curs(lth, curline);
if(curline > 1)
cl_end();
putsym(' ');
putstr (tl->line_text);
curline++;
}
curs(lth, curline);
cl_end();
cmore(text);
home();
cl_end();
docorner(lth, curline-1);
} else {
set_pager(0);
for (tl = texthead; tl; tl = tl->next_line) {
if (page_line (tl->line_text)) {
set_pager(2);
goto cleanup;
}
}
if(text) {
cgetret(text);
set_pager(2);
} else
set_pager(1);
}
}
cleanup:
while ((tl = texthead)) {
texthead = tl->next_line;
free(tl);
}
}
int
dohelp(void)
{
char c;
pline ("Long or short help? ");
while (((c = readchar ()) != 'l') && (c != 's') && !strchr(quitchars,c))
hackbell ();
if (!strchr(quitchars, c))
(void) page_file((c == 'l') ? HELP : SHELP, FALSE);
return(0);
}
int
page_file(char *fnam, boolean silent)
{
#ifdef DEF_PAGER
{
int fd = open(fnam, O_RDONLY);
if(fd == -1) {
if(!silent) pline("Cannot open %s.", fnam);
return(0);
}
if(child(1)){
extern char *catmore;
(void) close(0);
if(dup(fd)) {
if(!silent) printf("Cannot open %s as stdin.\n", fnam);
} else {
execlp(catmore, basename(catmore), (char *)NULL);
if(!silent) printf("Cannot exec %s.\n", catmore);
}
exit(1);
}
(void) close(fd);
}
#else
{
FILE *f;
if ((f = fopen (fnam, "r")) == (FILE *) 0) {
if(!silent) {
home(); perror (fnam); flags.toplin = 1;
pline ("Cannot open %s.", fnam);
}
return(0);
}
page_more(f, 0);
}
#endif
return(1);
}
#ifdef UNIX
#ifdef SHELL
int
dosh(void)
{
char *str;
if(child(0)) {
if ((str = getenv("SHELL")))
execlp(str, str, (char *)NULL);
else
execl("/bin/sh", "sh", (char *)NULL);
pline("sh: cannot execute.");
exit(1);
}
return(0);
}
#endif
#include <sys/wait.h>
int
child(int wt)
{
int status;
int f;
char *home;
gid_t gid;
f = fork();
if(f == 0){
settty(NULL);
gid = getgid();
setresgid(gid, gid, gid);
#ifdef CHDIR
home = getenv("HOME");
if (home == NULL || *home == '\0')
home = "/";
(void) chdir(home);
#endif
return(1);
}
if(f == -1) {
pline("Fork failed. Try again.");
return(0);
}
(void) signal(SIGINT,SIG_IGN);
(void) signal(SIGQUIT,SIG_IGN);
(void) wait(&status);
gettty();
setftty();
(void) signal(SIGINT,done1);
#ifdef WIZARD
if(wizard) (void) signal(SIGQUIT,SIG_DFL);
#endif
if(wt) getret();
docrt();
return(0);
}
#endif