main prompt.c
  1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
  2
  3#include "in_all.h"
  4#include "prompt.h"
  5#include "term.h"
  6#include "output.h"
  7#include "options.h"
  8#include "display.h"
  9#include "process.h"
 10#include "getline.h"
 11#include "main.h"
 12#include "getcomm.h"
 13#include "machine.h"
 14#include "keys.h"
 15#include "assert.h"
 16#include "commands.h"
 17
 18static char *errorgiven; /* Set to error message, if there is one */
 19
 20static char *display_basename(char *path, char *scratch, size_t scratch_size);
 21
 22char *
 23copy(char *p, const char *ep, char *s)
 24{
 25	while (p < ep && *s) {
 26		*p++ = *s++;
 27	}
 28	return p;
 29}
 30
 31/*
 32 * display the prompt and refresh the screen.
 33 */
 34
 35void
 36give_prompt()
 37{
 38
 39	char **name;
 40	struct scr_info *p = &scr_info;
 41	char buf[256];
 42	char filebuf[256];
 43	char *pb = buf;
 44
 45	if (startcomm)
 46		return;
 47	flush();
 48	if (window()) {
 49		redraw(0);
 50		flush();
 51	}
 52	if (!stupid) {
 53		/*
 54		 * fancy prompt
 55		 */
 56		clrbline();
 57		standout();
 58		pb = copy(pb, &buf[255],
 59		          display_basename(currentfile, filebuf, sizeof(filebuf)));
 60		if (stdf >= 0) {
 61			pb = copy(pb, &buf[255], ", ");
 62			pb = copy(pb, &buf[255], getnum(p->firstline));
 63			pb = copy(pb, &buf[255], "-");
 64			pb = copy(pb, &buf[255], getnum(p->lastline));
 65		}
 66	} else {
 67		*pb++ = '\007'; /* Stupid terminal, stupid prompt */
 68	}
 69	if (errorgiven) {
 70		/*
 71		 * display error message
 72		 */
 73		pb = copy(pb, &buf[255], " ");
 74		pb = copy(pb, &buf[255], errorgiven);
 75		if (stupid) {
 76			pb = copy(pb, &buf[255], "\r\n");
 77		}
 78		errorgiven = 0;
 79	} else if (!stupid && (status || maxpos)) {
 80		pb = copy(pb, &buf[255], " (");
 81		name = &filenames[filecount];
 82		if (status) {
 83			/*
 84			 * indicate top and/or bottom
 85			 */
 86			if (status & START) {
 87				if (!*(name - 1)) {
 88					pb = copy(pb, &buf[255], "Top");
 89				} else {
 90					pb = copy(pb, &buf[255], "Previous: ");
 91					pb = copy(pb, &buf[255], display_basename(*(name - 1),
 92					                                         filebuf,
 93					                                         sizeof(filebuf)));
 94				}
 95				if (status & EOFILE) {
 96					pb = copy(pb, &buf[255], ", ");
 97				}
 98			}
 99			if (status & EOFILE) {
100				if (!*(name + 1)) {
101					pb = copy(pb, &buf[255], "Bottom");
102				} else {
103					pb = copy(pb, &buf[255], "Next: ");
104					pb = copy(pb, &buf[255], display_basename(*(name + 1),
105					                                         filebuf,
106					                                         sizeof(filebuf)));
107				}
108			}
109		} else { /* display percentage */
110			pb = copy(pb, &buf[255], getnum((100 * getpos(p->lastline)) / maxpos));
111			pb = copy(pb, &buf[255], "%");
112		}
113		pb = copy(pb, &buf[255], ")");
114	}
115	*pb = '\0';
116	if (!stupid) {
117		buf[COLS - 1] = 0;
118		putline(buf);
119		standend();
120	} else
121		putline(buf);
122}
123
124static char *
125display_basename(char *path, char *scratch, size_t scratch_size)
126{
127	char *base;
128
129	if (!path || scratch_size == 0) {
130		return "";
131	}
132	(void)strncpy(scratch, path, scratch_size - 1);
133	scratch[scratch_size - 1] = '\0';
134	base = basename(scratch);
135	return base ? base : scratch;
136}
137
138/*
139 * Remember error message
140 */
141
142void
143error(char *str)
144{
145
146	errorgiven = str;
147}
148
149void
150ret_to_continue()
151{ /* Obvious */
152	int c;
153	static char buf[2];
154
155	for (;;) {
156		clrbline();
157		standout();
158		if (errorgiven) {
159			putline(errorgiven);
160			putline(" ");
161			errorgiven = 0;
162		}
163		putline("[Type anything to continue]");
164		standend();
165		if (is_escape(c = getch())) {
166			buf[0] = c;
167			(void)match(buf, &c, currmap->k_mach);
168			assert(c > 0);
169			do_comm(c, -1L);
170		} else
171			break;
172	}
173	clrbline();
174}