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