-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelp.c
More file actions
260 lines (224 loc) · 5.7 KB
/
Copy pathhelp.c
File metadata and controls
260 lines (224 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Larn — Copyright © 1986 Noah Morgan
* Copyright © 2014-2026 Gibbon
*
* This file is part of Larn and is distributed under
* The Noah Licence, Version 1.0.
*
* You may use, modify, and redistribute this code for
* non‑commercial purposes, provided that:
* - this notice is preserved,
* - The Noah Licence accompanies all redistributions, and
* - no profit is made from Larn or derivative works
* without explicit permission from the copyright holder.
*
* Larn is provided “AS IS”, without warranty of any kind.
*
* See the 'LICENSE.txt' file in the 'docs' folder.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "larncons.h"
#include "larndata.h"
#include "larnfunc.h"
#include "display.h"
#include "help.h"
#include "io.h"
#include "nap.h"
/* I coded a nice line-reader for this. Unfortunately it's very hard to get a good
* cursor and text position when reading a file from inside a curses program.
* so I did this instead, I actually prefer it. -Gibbon
*
* 2026.05.13: So I decided to add back how it originally was, but converted the file to ANSI C and added a new
* function to interpret ANSI escapes. This allows the help file to be formatted with
* ANSI codes, and for it to be displayed properly in the curses interface. -Gibbon
*/
/* Interpret ANSI escapes and print using curses */
static void
ansihelp(const char *s)
{
const char *p;
char params[32];
char buf[32];
char *tok;
int pi;
char cmd;
p = s;
while (*p) {
if (*p == '\033') { /* ESC */
p++;
if (*p == '[') { /* CSI */
p++;
pi = 0;
while (*p && pi < 30 &&
((*p >= '0' && *p <= '9') || *p == ';'))
{
params[pi++] = *p++;
}
params[pi] = '\0';
cmd = *p++;
if (cmd == 'm') {
strcpy(buf, params);
tok = strtok(buf, ";");
while (tok) {
switch (atoi(tok)) {
case 0:
standend();
break;
case 1:
attron(A_BOLD);
break;
case 4:
attron(A_UNDERLINE);
break;
case 7:
standout();
break;
default:
break;
}
tok = strtok(NULL, ";");
}
}
continue;
}
continue;
}
lprc(*p++);
}
}
/*
* help function to display the help info
*
* format of the larn.help file
*
* 1st character of file: # of pages of help available (ascii digit)
* page (23 lines) for the introductory message (not counted in above)
* pages of help text (23 lines per page)
*/
int
help(void)
{
FILE *fp;
char line[512];
int pages;
int i;
int getchkey;
pages = openhelp(&fp);
if (pages < 0)
return 0;
for (i = 0; i < 23; i++)
{
char *tmp = fgets(line, sizeof(line), fp);
if (!tmp)
break;
}
while (pages > 0)
{
screen_clear();
cursor(1, 1);
for (i = 0; i < 23; i++) {
if (!fgets(line, sizeof(line), fp))
break;
line[strcspn(line, "\r\n")] = '\0';
ansihelp(line);
standend();
attrset(A_NORMAL);
lprc('\n');
}
if (pages > 1) {
lprcat(" ---- Press ");
lstandout("return");
lprcat(" to exit, ");
lstandout("space");
lprcat(" for more help ---- ");
getchkey = ttgetch();
while (getchkey != ' ' && getchkey != '\n' && getchkey != '\033')
getchkey = ttgetch();
if (getchkey == '\n' || getchkey == '\033') {
fclose(fp);
setscroll();
drawscreen();
return 0;
}
}
pages--;
}
fclose(fp);
retcont();
drawscreen();
return 0;
}
/*
* function to display the welcome message and background
*/
int
welcome(void)
{
FILE *fp;
char line[512];
int pages;
int i;
pages = openhelp(&fp);
if (pages < 0)
return 0;
screen_clear();
cursor(1, 1);
for (i = 0; i < 23; i++) {
if (!fgets(line, sizeof(line), fp))
break;
line[strcspn(line, "\r\n")] = '\0';
ansihelp(line);
standend();
attrset(A_NORMAL);
lprc('\n');
}
fclose(fp);
retcont();
return 0;
}
/*
* function to say press return to continue and reset scroll when done
*/
int
retcont(void)
{
cursor(1, 24);
lprcat("Press ");
lstandout("return");
lprcat(" to continue: ");
while (ttgetch() != '\n')
;
setscroll();
return 0;
}
/*
* routine to open the help file and return the first character - '0'
*/
int
openhelp(FILE** fp_out)
{
FILE* fp = fopen("larn.help", "r");
int pages;
if (!fp) {
cursors();
lprintf("Can't open help file \"%s\"\n", "larn.help");
lflush();
nap(4000);
drawscreen();
setscroll();
return -1;
}
/* read first character # of pages */
pages = fgetc(fp);
if (pages == EOF) {
fclose(fp);
return -1;
}
/* consume newline after the number */
fgetc(fp);
resetscroll();
*fp_out = fp;
return pages - '0';
}