1 #if HAVE_UNISTD_H
2 #   include <unistd.h>
3 #endif
4 #if HAVE_FCNTL_H
5 #   include <fcntl.h>
6 #endif
7 #include "deco.h"
8 #include "scr.h"
9 #include "env.h"
10 
11 #define CS              34
12 #define HELPDIR         "/usr/lib/deco/help/"
13 #define LCLHELPDIR      "/usr/local/share/deco/help/"
14 
15 struct helptab {
16 	char row;
17 	char col;
18 	char bold;
19 	char *str;
20 };
21 
22 struct help {
23 	char *name;
24 	struct helptab *htab;
25 	char **mesg;
26 };
27 
28 static void inithelp (struct help *hp);
29 static void initnewhelp (char **q, char *p);
30 
31 static char *genmesg [] = {
32 	"The Demos Commander, Copyright (C) 1989-1997 Serge Vakulenko",
33 	" Cursor Arrows ",
34 	"Left             ^[ l",
35 	"Right            ^[ r",
36 	"Up               ^[ u",
37 	"Down             ^[ d",
38 	" Cursor Movement ",
39 	"Char left        ^S or ^A",
40 	"Char right       ^D",
41 	" Panels ",
42 	"Switch panels    ^I or <Tab>",
43 	"Panels on/off    ^O or ^P",
44 	"Swap panels      ^U",
45 	"Toggle status    ^L",
46 	"Full screen      ^F",
47 	"Double width     ^W",
48 	"The \"+\" and \"-\" keys select and unselect groups of files.",
49 	" Delete ",
50 	"Char left        ^H or <BS>",
51 	"Under cursor     ^G or <Del>",
52 	"Line             ^Y",
53 	" Other ",
54 	"History          ^B",
55 	"Home directory   ^\\",
56 	"Reread directory ^R",
57 	"Select/Unselect  ^T or <Ins>",
58 	"Previous command ^E",
59 	"Next command     ^X",
60 	"Run file or",
61 	"Enter directory  ^M or <Ret>",
62 	"Use file name    ^J or <LF>",
63 	"Function keys    ^[ + digit",
64 	0,
65 };
66 
67 static struct helptab gentab [] = {
68        {  1,      0,      0,      0 },
69     /*  2, */
70        {  3,      2,      1,      0 },
71        {  4,      6,      0,      0 },
72        {  5,      6,      0,      0 },
73        {  6,      6,      0,      0 },
74        {  7,      6,      0,      0 },
75        {  8,      2,      1,      0 },
76        {  9,      6,      0,      0 },
77        { 10,      6,      0,      0 },
78        { 11,      2,      1,      0 },
79        { 12,      6,      0,      0 },
80        { 13,      6,      0,      0 },
81        { 14,      6,      0,      0 },
82        { 15,      6,      0,      0 },
83        { 16,      6,      0,      0 },
84        { 17,      6,      0,      0 },
85     /* 18, */
86        { 19,      0,      0,      0 },
87     /*  2, */
88        {  3,  CS+ 2,      1,      0 },
89        {  4,  CS+ 6,      0,      0 },
90        {  5,  CS+ 6,      0,      0 },
91        {  6,  CS+ 6,      0,      0 },
92        {  7,  CS+ 2,      1,      0 },
93        {  8,  CS+ 6,      0,      0 },
94        {  9,  CS+ 6,      0,      0 },
95        { 10,  CS+ 6,      0,      0 },
96        { 11,  CS+ 6,      0,      0 },
97        { 12,  CS+ 6,      0,      0 },
98        { 13,  CS+ 6,      0,      0 },
99        { 14,  CS+ 6,      0,      0 },
100        { 15,  CS+ 6,      0,      0 },
101        { 16,  CS+ 6,      0,      0 },
102        { 17,  CS+ 6,      0,      0 },
103        {  0,      0,      0,      0 },
104 };
105 
106 static char *editmesg [] = {
107 	"The Demos Commander Text Editor, Copyright (C) 1990-1997 Serge Vakulenko",
108 	" Cursor Movement ",
109 	"Char Left        ^A",
110 	"Char Right       ^D",
111 	"Line Up          ^E",
112 	"Line Down        ^X",
113 	"Word Left        ^W",
114 	"Word Right       ^F",
115 	"Prev Page        ^R",
116 	"Next Page        ^B",
117 	"Begin of line    Home",
118 	"End of line      End",
119 	"Top of file      F5",
120 	"End of file      F6",
121 	"Top of screen    F8",
122 	" Search ",
123 	"Search string    F7",
124 	" Delete ",
125 	"Char left        ^H or <BS>",
126 	"Under cursor     ^G or <Del>",
127 	"Line             ^Y",
128 	"To end of line   ^K",
129 	" Other ",
130 	"Redraw screen    ^]",
131 	"Quote next char  ^V",
132 	"Save file        F2",
133 	"Exit             ^C",
134 	" Modes ",
135 	"Raw mode         F3",
136 	"Visible spaces   F9",
137 	0,
138 };
139 
140 static struct helptab edittab [] = {
141        {  1,      0,      0,      0 },
142        {  3,      2,      1,      0 },
143        {  4,      6,      0,      0 },
144        {  5,      6,      0,      0 },
145        {  6,      6,      0,      0 },
146        {  7,      6,      0,      0 },
147        {  8,      6,      0,      0 },
148        {  9,      6,      0,      0 },
149        { 10,      6,      0,      0 },
150        { 11,      6,      0,      0 },
151        { 12,      6,      0,      0 },
152        { 13,      6,      0,      0 },
153        { 14,      6,      0,      0 },
154        { 15,      6,      0,      0 },
155        { 16,      6,      0,      0 },
156        { 17,      2,      1,      0 },
157        { 18,      6,      0,      0 },
158        {  3,  CS+ 2,      1,      0 },
159        {  4,  CS+ 6,      0,      0 },
160        {  5,  CS+ 6,      0,      0 },
161        {  6,  CS+ 6,      0,      0 },
162        {  7,  CS+ 6,      0,      0 },
163        {  9,  CS+ 2,      1,      0 },
164        { 10,  CS+ 6,      0,      0 },
165        { 11,  CS+ 6,      0,      0 },
166        { 12,  CS+ 6,      0,      0 },
167        { 13,  CS+ 6,      0,      0 },
168        { 15,  CS+ 2,      1,      0 },
169        { 16,  CS+ 6,      0,      0 },
170        { 17,  CS+ 6,      0,      0 },
171        {  0,      0,      0,      0 },
172 };
173 
174 static char *viewmesg [] = {
175 	"The Demos Commander Viewer, Copyright (C) 1990-1997 Serge Vakulenko",
176 	" Cursor Movement ",
177 	"Page Left        ^A",
178 	"Page Right       ^D",
179 	"Line Up          ^E",
180 	"Line Down        ^X",
181 	"Prev Page        ^R",
182 	"Next Page        ^B",
183 	"Top of file      Home or F5",
184 	"End of file      End or F6",
185 	" Search ",
186 	"Search string    F7",
187 	" Other ",
188 	"Redraw screen    ^]",
189 	"Exit             ^C",
190 	" Modes ",
191 	"Raw mode         F3",
192 	"Visible spaces   F9",
193 	"Lines longer than 1024 bytes",
194 	"are broken and marked with \"\\\"",
195 	"Spaces are displayed as \".\"",
196 	"and tabs as \"_\"",
197 	0,
198 };
199 
200 static struct helptab viewtab [] = {
201        {  1,      0,      0,      0 },
202        {  3,      3,      1,      0 },
203        {  5,      7,      0,      0 },
204        {  6,      7,      0,      0 },
205        {  7,      7,      0,      0 },
206        {  8,      7,      0,      0 },
207        {  9,      7,      0,      0 },
208        { 10,      7,      0,      0 },
209        { 11,      7,      0,      0 },
210        { 12,      7,      0,      0 },
211        { 15,      3,      1,      0 },
212        { 17,      7,      0,      0 },
213        {  3,  CS+ 3,      1,      0 },
214        {  5,  CS+ 7,      0,      0 },
215        {  6,  CS+ 7,      0,      0 },
216        {  9,  CS+ 3,      1,      0 },
217        { 11,  CS+ 7,      0,      0 },
218        { 12,  CS+ 7,      0,      0 },
219        { 15,  CS+ 3,      0,      0 },
220        { 16,  CS+ 3,      0,      0 },
221        { 17,  CS+ 3,      0,      0 },
222        { 18,  CS+ 3,      0,      0 },
223        {  0,      0,      0,      0 },
224 };
225 
226 static char *hviewmesg [] = {
227 	"The Demos Commander Hexadecimal Viewer, Copyright (C) 1990-1997 Serge Vakulenko",
228 	" Cursor Movement ",
229 	"Page Left        ^A",
230 	"Page Right       ^D",
231 	"Line Up          ^E",
232 	"Line Down        ^X",
233 	"Prev Page        ^R",
234 	"Next Page        ^B",
235 	"Top of file      Home or F5",
236 	"End of file      End or F6",
237 	" Search ",
238 	"Search string    F7",
239 	" Other ",
240 	"Redraw screen    ^]",
241 	"Exit             ^C",
242 	" Modes ",
243 	"Raw mode         F3",
244 	"Visible spaces   F9",
245 	"Spaces are displayed as \".\"",
246 	"and tabs as \"_\"",
247 	"For hexadecimal search",
248 	"type char codes as \"\\xx\"",
249 	0,
250 };
251 
252 static struct helptab hviewtab [] = {
253        {  1,      0,      0,      0 },
254        {  3,      3,      1,      0 },
255        {  5,      7,      0,      0 },
256        {  6,      7,      0,      0 },
257        {  7,      7,      0,      0 },
258        {  8,      7,      0,      0 },
259        {  9,      7,      0,      0 },
260        { 10,      7,      0,      0 },
261        { 11,      7,      0,      0 },
262        { 12,      7,      0,      0 },
263        { 15,      3,      1,      0 },
264        { 17,      7,      0,      0 },
265        {  3,  CS+ 3,      1,      0 },
266        {  5,  CS+ 7,      0,      0 },
267        {  6,  CS+ 7,      0,      0 },
268        {  9,  CS+ 3,      1,      0 },
269        { 11,  CS+ 7,      0,      0 },
270        { 12,  CS+ 7,      0,      0 },
271        { 15,  CS+ 3,      0,      0 },
272        { 16,  CS+ 3,      0,      0 },
273        { 17,  CS+ 3,      0,      0 },
274        { 18,  CS+ 3,      0,      0 },
275        {  0,      0,      0,      0 },
276 };
277 
278 static struct help help [] = {
279 	{ "general",      gentab,         genmesg },
280 	{ "edit",         edittab,        editmesg },
281 	{ "view",         viewtab,        viewmesg },
282 	{ "hexview",      hviewtab,       hviewmesg },
283 	{ 0,              0,              0 },
284 };
285 
findhelp(char * name)286 struct help *findhelp (char *name)
287 {
288 	register struct help *p;
289 
290 	for (p=help; p->name; ++p)
291 		if (! strcmp (p->name, name))
292 			return (p);
293 	return (0);
294 }
295 
genhelp()296 void genhelp ()
297 {
298 	runhelp ("general");
299 }
300 
runhelp(char * name)301 void runhelp (char *name)
302 {
303 	register r, c, w, h;
304 	BOX *box;
305 	register struct helptab *p;
306 	struct help *hp;
307 
308 	hp = findhelp (name);
309 	if (! hp)
310 		return;
311 	p = hp->htab;
312 	if (! p->str)
313 		inithelp (hp);
314 	w = 72;
315 	h = 21;
316 	r = 2;
317 	c = 4;
318 
319 	box = VGetBox (r, c-1, h, w+2);                 /* save box */
320 	VStandOut ();
321 	VSetDim ();
322 	VFillBox (r, c-1, h, w+2, ' ');                 /* clear */
323 	VDrawBox (r, c, h, w);                          /* draw margins */
324 	VMPutString (r, c + (w-6) / 2, " Help ");       /* head */
325 
326 	for (; p->row; ++p) {
327 		if (p->row >= h)
328 			continue;
329 		if (p->bold) {
330 			VSetBold ();
331 			VStandEnd ();
332 		}
333 		if (! p->col)
334 			VMPutString (r+p->row, c + (w-strlen(p->str))/2, p->str);
335 		else
336 			VMPutString (r+p->row, c + p->col, p->str);
337 		if (p->bold) {
338 			VSetDim ();
339 			VStandOut ();
340 		}
341 	}
342 	VStandEnd ();
343 	VSetNormal ();
344 	for (;;) {
345 		hidecursor ();
346 		VSync ();
347 		switch (KeyGet ()) {
348 		default:
349 			break;
350 		case cntrl (']'):       /* redraw screen */
351 			VRedraw ();
352 			continue;
353 		}
354 		break;
355 	}
356 	VUngetBox (box);
357 	VFreeBox (box);
358 }
359 
findhelpfile(char * filename,char * name,char * dir)360 static int findhelpfile (char *filename, char *name, char *dir)
361 {
362 	register char *s;
363 
364 	s = filename;                   /* build dir/name */
365 	strcpy (s, dir);
366 	s += strlen (s);
367 	switch (lang) {
368 	case ENG: *s++ = 'e'; *s++ = '.'; break;
369 	case RUS: *s++ = 'r'; *s++ = '.'; break;
370 	case UKR: *s++ = 'u'; *s++ = '.'; break;
371 	case DEU: *s++ = 'd'; *s++ = '.'; break;
372 	case FRA: *s++ = 'f'; *s++ = '.'; break;
373 	}
374 	strcpy (s, name);
375 	return (! access (filename, 4)); /* can I read it ? */
376 }
377 
inithelp(struct help * hp)378 static void inithelp (struct help *hp)
379 {
380 	register struct helptab *p;
381 	register n, fd;
382 	register char **q;
383 	char filename [60];
384 	char buf [1024];
385 
386 	if (! findhelpfile (filename, hp->name, LCLHELPDIR))
387 		findhelpfile (filename, hp->name, HELPDIR);
388 	fd = open (filename, 0);
389 	if (fd >= 0) {
390 		n = read (fd, buf, sizeof (buf) - 1);
391 		if (n > 0) {
392 			buf [n] = 0;
393 			initnewhelp (hp->mesg, mdup (buf));
394 		}
395 		close (fd);
396 	}
397 	q = hp->mesg;
398 	for (p=hp->htab; p->row && *q; ++p, ++q)
399 		p->str = *q;
400 }
401 
initnewhelp(char ** q,char * p)402 static void initnewhelp (char **q, char *p)
403 {
404 	for (; *q; ++q) {
405 		for(*q=p; *p && *p != '\n'; ++p)
406 			if (*p == '@')
407 				*p = ' ';
408 		if (! *p)
409 			break;
410 		*p++ = 0;
411 	}
412 	*q = 0;
413 }
414