xref: /minix3/external/bsd/less/dist/lesskey.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: lesskey.c,v 1.4 2013/09/04 19:44:21 tron Exp $	*/
2f7cf2976SLionel Sambuc 
3f7cf2976SLionel Sambuc /*
4*84d9c625SLionel Sambuc  * Copyright (C) 1984-2012  Mark Nudelman
5f7cf2976SLionel Sambuc  *
6f7cf2976SLionel Sambuc  * You may distribute under the terms of either the GNU General Public
7f7cf2976SLionel Sambuc  * License or the Less License, as specified in the README file.
8f7cf2976SLionel Sambuc  *
9*84d9c625SLionel Sambuc  * For more information, see the README file.
10f7cf2976SLionel Sambuc  */
11f7cf2976SLionel Sambuc 
12f7cf2976SLionel Sambuc 
13f7cf2976SLionel Sambuc /*
14f7cf2976SLionel Sambuc  *	lesskey [-o output] [input]
15f7cf2976SLionel Sambuc  *
16f7cf2976SLionel Sambuc  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
17f7cf2976SLionel Sambuc  *
18f7cf2976SLionel Sambuc  *	Make a .less file.
19f7cf2976SLionel Sambuc  *	If no input file is specified, standard input is used.
20f7cf2976SLionel Sambuc  *	If no output file is specified, $HOME/.less is used.
21f7cf2976SLionel Sambuc  *
22f7cf2976SLionel Sambuc  *	The .less file is used to specify (to "less") user-defined
23f7cf2976SLionel Sambuc  *	key bindings.  Basically any sequence of 1 to MAX_CMDLEN
24f7cf2976SLionel Sambuc  *	keystrokes may be bound to an existing less function.
25f7cf2976SLionel Sambuc  *
26f7cf2976SLionel Sambuc  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
27f7cf2976SLionel Sambuc  *
28f7cf2976SLionel Sambuc  *	The input file is an ascii file consisting of a
29f7cf2976SLionel Sambuc  *	sequence of lines of the form:
30f7cf2976SLionel Sambuc  *		string <whitespace> action [chars] <newline>
31f7cf2976SLionel Sambuc  *
32f7cf2976SLionel Sambuc  *	"string" is a sequence of command characters which form
33f7cf2976SLionel Sambuc  *		the new user-defined command.  The command
34f7cf2976SLionel Sambuc  *		characters may be:
35f7cf2976SLionel Sambuc  *		1. The actual character itself.
36f7cf2976SLionel Sambuc  *		2. A character preceded by ^ to specify a
37f7cf2976SLionel Sambuc  *		   control character (e.g. ^X means control-X).
38f7cf2976SLionel Sambuc  *		3. A backslash followed by one to three octal digits
39f7cf2976SLionel Sambuc  *		   to specify a character by its octal value.
40f7cf2976SLionel Sambuc  *		4. A backslash followed by b, e, n, r or t
41f7cf2976SLionel Sambuc  *		   to specify \b, ESC, \n, \r or \t, respectively.
42f7cf2976SLionel Sambuc  *		5. Any character (other than those mentioned above) preceded
43f7cf2976SLionel Sambuc  *		   by a \ to specify the character itself (characters which
44f7cf2976SLionel Sambuc  *		   must be preceded by \ include ^, \, and whitespace.
45f7cf2976SLionel Sambuc  *	"action" is the name of a "less" action, from the table below.
46f7cf2976SLionel Sambuc  *	"chars" is an optional sequence of characters which is treated
47f7cf2976SLionel Sambuc  *		as keyboard input after the command is executed.
48f7cf2976SLionel Sambuc  *
49f7cf2976SLionel Sambuc  *	Blank lines and lines which start with # are ignored,
50f7cf2976SLionel Sambuc  *	except for the special control lines:
51f7cf2976SLionel Sambuc  *		#command	Signals the beginning of the command
52f7cf2976SLionel Sambuc  *				keys section.
53f7cf2976SLionel Sambuc  *		#line-edit	Signals the beginning of the line-editing
54f7cf2976SLionel Sambuc  *				keys section.
55f7cf2976SLionel Sambuc  *		#env		Signals the beginning of the environment
56f7cf2976SLionel Sambuc  *				variable section.
57f7cf2976SLionel Sambuc  *		#stop		Stops command parsing in less;
58f7cf2976SLionel Sambuc  *				causes all default keys to be disabled.
59f7cf2976SLionel Sambuc  *
60f7cf2976SLionel Sambuc  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
61f7cf2976SLionel Sambuc  *
62f7cf2976SLionel Sambuc  *	The output file is a non-ascii file, consisting of a header,
63f7cf2976SLionel Sambuc  *	one or more sections, and a trailer.
64f7cf2976SLionel Sambuc  *	Each section begins with a section header, a section length word
65f7cf2976SLionel Sambuc  *	and the section data.  Normally there are three sections:
66f7cf2976SLionel Sambuc  *		CMD_SECTION	Definition of command keys.
67f7cf2976SLionel Sambuc  *		EDIT_SECTION	Definition of editing keys.
68f7cf2976SLionel Sambuc  *		END_SECTION	A special section header, with no
69f7cf2976SLionel Sambuc  *				length word or section data.
70f7cf2976SLionel Sambuc  *
71f7cf2976SLionel Sambuc  *	Section data consists of zero or more byte sequences of the form:
72f7cf2976SLionel Sambuc  *		string <0> <action>
73f7cf2976SLionel Sambuc  *	or
74f7cf2976SLionel Sambuc  *		string <0> <action|A_EXTRA> chars <0>
75f7cf2976SLionel Sambuc  *
76f7cf2976SLionel Sambuc  *	"string" is the command string.
77f7cf2976SLionel Sambuc  *	"<0>" is one null byte.
78f7cf2976SLionel Sambuc  *	"<action>" is one byte containing the action code (the A_xxx value).
79f7cf2976SLionel Sambuc  *	If action is ORed with A_EXTRA, the action byte is followed
80f7cf2976SLionel Sambuc  *		by the null-terminated "chars" string.
81f7cf2976SLionel Sambuc  *
82f7cf2976SLionel Sambuc  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
83f7cf2976SLionel Sambuc  */
84f7cf2976SLionel Sambuc 
85f7cf2976SLionel Sambuc #include "less.h"
86f7cf2976SLionel Sambuc #include "lesskey.h"
87f7cf2976SLionel Sambuc #include "cmd.h"
88f7cf2976SLionel Sambuc 
89f7cf2976SLionel Sambuc struct cmdname
90f7cf2976SLionel Sambuc {
91f7cf2976SLionel Sambuc 	char *cn_name;
92f7cf2976SLionel Sambuc 	int cn_action;
93f7cf2976SLionel Sambuc };
94f7cf2976SLionel Sambuc 
95f7cf2976SLionel Sambuc struct cmdname cmdnames[] =
96f7cf2976SLionel Sambuc {
97f7cf2976SLionel Sambuc 	{ "back-bracket",         A_B_BRACKET },
98f7cf2976SLionel Sambuc 	{ "back-line",            A_B_LINE },
99f7cf2976SLionel Sambuc 	{ "back-line-force",      A_BF_LINE },
100f7cf2976SLionel Sambuc 	{ "back-screen",          A_B_SCREEN },
101f7cf2976SLionel Sambuc 	{ "back-scroll",          A_B_SCROLL },
102f7cf2976SLionel Sambuc 	{ "back-search",          A_B_SEARCH },
103f7cf2976SLionel Sambuc 	{ "back-window",          A_B_WINDOW },
104f7cf2976SLionel Sambuc 	{ "debug",                A_DEBUG },
105f7cf2976SLionel Sambuc 	{ "digit",                A_DIGIT },
106f7cf2976SLionel Sambuc 	{ "display-flag",         A_DISP_OPTION },
107f7cf2976SLionel Sambuc 	{ "display-option",       A_DISP_OPTION },
108f7cf2976SLionel Sambuc 	{ "end",                  A_GOEND },
109f7cf2976SLionel Sambuc 	{ "examine",              A_EXAMINE },
110f7cf2976SLionel Sambuc 	{ "filter",               A_FILTER },
111f7cf2976SLionel Sambuc 	{ "first-cmd",            A_FIRSTCMD },
112f7cf2976SLionel Sambuc 	{ "firstcmd",             A_FIRSTCMD },
113f7cf2976SLionel Sambuc 	{ "flush-repaint",        A_FREPAINT },
114f7cf2976SLionel Sambuc 	{ "forw-bracket",         A_F_BRACKET },
115f7cf2976SLionel Sambuc 	{ "forw-forever",         A_F_FOREVER },
116*84d9c625SLionel Sambuc 	{ "forw-until-hilite",    A_F_UNTIL_HILITE },
117f7cf2976SLionel Sambuc 	{ "forw-line",            A_F_LINE },
118f7cf2976SLionel Sambuc 	{ "forw-line-force",      A_FF_LINE },
119f7cf2976SLionel Sambuc 	{ "forw-screen",          A_F_SCREEN },
120f7cf2976SLionel Sambuc 	{ "forw-screen-force",    A_FF_SCREEN },
121f7cf2976SLionel Sambuc 	{ "forw-scroll",          A_F_SCROLL },
122f7cf2976SLionel Sambuc 	{ "forw-search",          A_F_SEARCH },
123f7cf2976SLionel Sambuc 	{ "forw-window",          A_F_WINDOW },
124f7cf2976SLionel Sambuc 	{ "goto-end",             A_GOEND },
125f7cf2976SLionel Sambuc 	{ "goto-line",            A_GOLINE },
126f7cf2976SLionel Sambuc 	{ "goto-mark",            A_GOMARK },
127f7cf2976SLionel Sambuc 	{ "help",                 A_HELP },
128f7cf2976SLionel Sambuc 	{ "index-file",           A_INDEX_FILE },
129f7cf2976SLionel Sambuc 	{ "invalid",              A_UINVALID },
130f7cf2976SLionel Sambuc 	{ "left-scroll",          A_LSHIFT },
131f7cf2976SLionel Sambuc 	{ "next-file",            A_NEXT_FILE },
132f7cf2976SLionel Sambuc 	{ "next-tag",             A_NEXT_TAG },
133f7cf2976SLionel Sambuc 	{ "noaction",             A_NOACTION },
134f7cf2976SLionel Sambuc 	{ "percent",              A_PERCENT },
135f7cf2976SLionel Sambuc 	{ "pipe",                 A_PIPE },
136f7cf2976SLionel Sambuc 	{ "prev-file",            A_PREV_FILE },
137f7cf2976SLionel Sambuc 	{ "prev-tag",             A_PREV_TAG },
138f7cf2976SLionel Sambuc 	{ "quit",                 A_QUIT },
139f7cf2976SLionel Sambuc 	{ "remove-file",          A_REMOVE_FILE },
140f7cf2976SLionel Sambuc 	{ "repaint",              A_REPAINT },
141f7cf2976SLionel Sambuc 	{ "repaint-flush",        A_FREPAINT },
142f7cf2976SLionel Sambuc 	{ "repeat-search",        A_AGAIN_SEARCH },
143f7cf2976SLionel Sambuc 	{ "repeat-search-all",    A_T_AGAIN_SEARCH },
144f7cf2976SLionel Sambuc 	{ "reverse-search",       A_REVERSE_SEARCH },
145f7cf2976SLionel Sambuc 	{ "reverse-search-all",   A_T_REVERSE_SEARCH },
146f7cf2976SLionel Sambuc 	{ "right-scroll",         A_RSHIFT },
147f7cf2976SLionel Sambuc 	{ "set-mark",             A_SETMARK },
148f7cf2976SLionel Sambuc 	{ "shell",                A_SHELL },
149f7cf2976SLionel Sambuc 	{ "status",               A_STAT },
150f7cf2976SLionel Sambuc 	{ "toggle-flag",          A_OPT_TOGGLE },
151f7cf2976SLionel Sambuc 	{ "toggle-option",        A_OPT_TOGGLE },
152f7cf2976SLionel Sambuc 	{ "undo-hilite",          A_UNDO_SEARCH },
153f7cf2976SLionel Sambuc 	{ "version",              A_VERSION },
154f7cf2976SLionel Sambuc 	{ "visual",               A_VISUAL },
155f7cf2976SLionel Sambuc 	{ NULL,   0 }
156f7cf2976SLionel Sambuc };
157f7cf2976SLionel Sambuc 
158f7cf2976SLionel Sambuc struct cmdname editnames[] =
159f7cf2976SLionel Sambuc {
160f7cf2976SLionel Sambuc 	{ "back-complete",	EC_B_COMPLETE },
161f7cf2976SLionel Sambuc 	{ "backspace",		EC_BACKSPACE },
162f7cf2976SLionel Sambuc 	{ "delete",		EC_DELETE },
163f7cf2976SLionel Sambuc 	{ "down",		EC_DOWN },
164f7cf2976SLionel Sambuc 	{ "end",		EC_END },
165f7cf2976SLionel Sambuc 	{ "expand",		EC_EXPAND },
166f7cf2976SLionel Sambuc 	{ "forw-complete",	EC_F_COMPLETE },
167f7cf2976SLionel Sambuc 	{ "home",		EC_HOME },
168f7cf2976SLionel Sambuc 	{ "insert",		EC_INSERT },
169f7cf2976SLionel Sambuc 	{ "invalid",		EC_UINVALID },
170f7cf2976SLionel Sambuc 	{ "kill-line",		EC_LINEKILL },
171f7cf2976SLionel Sambuc 	{ "abort",		EC_ABORT },
172f7cf2976SLionel Sambuc 	{ "left",		EC_LEFT },
173f7cf2976SLionel Sambuc 	{ "literal",		EC_LITERAL },
174f7cf2976SLionel Sambuc 	{ "right",		EC_RIGHT },
175f7cf2976SLionel Sambuc 	{ "up",			EC_UP },
176f7cf2976SLionel Sambuc 	{ "word-backspace",	EC_W_BACKSPACE },
177f7cf2976SLionel Sambuc 	{ "word-delete",	EC_W_DELETE },
178f7cf2976SLionel Sambuc 	{ "word-left",		EC_W_LEFT },
179f7cf2976SLionel Sambuc 	{ "word-right",		EC_W_RIGHT },
180f7cf2976SLionel Sambuc 	{ NULL, 0 }
181f7cf2976SLionel Sambuc };
182f7cf2976SLionel Sambuc 
183f7cf2976SLionel Sambuc struct table
184f7cf2976SLionel Sambuc {
185f7cf2976SLionel Sambuc 	struct cmdname *names;
186f7cf2976SLionel Sambuc 	char *pbuffer;
187f7cf2976SLionel Sambuc 	char buffer[MAX_USERCMD];
188f7cf2976SLionel Sambuc };
189f7cf2976SLionel Sambuc 
190f7cf2976SLionel Sambuc struct table cmdtable;
191f7cf2976SLionel Sambuc struct table edittable;
192f7cf2976SLionel Sambuc struct table vartable;
193f7cf2976SLionel Sambuc struct table *currtable = &cmdtable;
194f7cf2976SLionel Sambuc 
195f7cf2976SLionel Sambuc char fileheader[] = {
196f7cf2976SLionel Sambuc 	C0_LESSKEY_MAGIC,
197f7cf2976SLionel Sambuc 	C1_LESSKEY_MAGIC,
198f7cf2976SLionel Sambuc 	C2_LESSKEY_MAGIC,
199f7cf2976SLionel Sambuc 	C3_LESSKEY_MAGIC
200f7cf2976SLionel Sambuc };
201f7cf2976SLionel Sambuc char filetrailer[] = {
202f7cf2976SLionel Sambuc 	C0_END_LESSKEY_MAGIC,
203f7cf2976SLionel Sambuc 	C1_END_LESSKEY_MAGIC,
204f7cf2976SLionel Sambuc 	C2_END_LESSKEY_MAGIC
205f7cf2976SLionel Sambuc };
206f7cf2976SLionel Sambuc char cmdsection[1] =	{ CMD_SECTION };
207f7cf2976SLionel Sambuc char editsection[1] =	{ EDIT_SECTION };
208f7cf2976SLionel Sambuc char varsection[1] =	{ VAR_SECTION };
209f7cf2976SLionel Sambuc char endsection[1] =	{ END_SECTION };
210f7cf2976SLionel Sambuc 
211f7cf2976SLionel Sambuc char *infile = NULL;
212f7cf2976SLionel Sambuc char *outfile = NULL ;
213f7cf2976SLionel Sambuc 
214f7cf2976SLionel Sambuc int linenum;
215f7cf2976SLionel Sambuc int errors;
216f7cf2976SLionel Sambuc 
217f7cf2976SLionel Sambuc void terror(char *);
218f7cf2976SLionel Sambuc 
219f7cf2976SLionel Sambuc extern char version[];
220f7cf2976SLionel Sambuc 
221f7cf2976SLionel Sambuc 	void
usage()222f7cf2976SLionel Sambuc usage()
223f7cf2976SLionel Sambuc {
224f7cf2976SLionel Sambuc 	fprintf(stderr, "usage: lesskey [-o output] [input]\n");
225f7cf2976SLionel Sambuc 	exit(1);
226f7cf2976SLionel Sambuc }
227f7cf2976SLionel Sambuc 
228f7cf2976SLionel Sambuc 	char *
mkpathname(dirname,filename)229f7cf2976SLionel Sambuc mkpathname(dirname, filename)
230f7cf2976SLionel Sambuc 	char *dirname;
231f7cf2976SLionel Sambuc 	char *filename;
232f7cf2976SLionel Sambuc {
233f7cf2976SLionel Sambuc 	char *pathname;
234f7cf2976SLionel Sambuc 
235f7cf2976SLionel Sambuc 	pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char));
236f7cf2976SLionel Sambuc 	strcpy(pathname, dirname);
237f7cf2976SLionel Sambuc 	strcat(pathname, PATHNAME_SEP);
238f7cf2976SLionel Sambuc 	strcat(pathname, filename);
239f7cf2976SLionel Sambuc 	return (pathname);
240f7cf2976SLionel Sambuc }
241f7cf2976SLionel Sambuc 
242f7cf2976SLionel Sambuc /*
243f7cf2976SLionel Sambuc  * Figure out the name of a default file (in the user's HOME directory).
244f7cf2976SLionel Sambuc  */
245f7cf2976SLionel Sambuc 	char *
homefile(filename)246f7cf2976SLionel Sambuc homefile(filename)
247f7cf2976SLionel Sambuc 	char *filename;
248f7cf2976SLionel Sambuc {
249f7cf2976SLionel Sambuc 	char *p;
250f7cf2976SLionel Sambuc 	char *pathname;
251f7cf2976SLionel Sambuc 
252f7cf2976SLionel Sambuc 	if ((p = getenv("HOME")) != NULL && *p != '\0')
253f7cf2976SLionel Sambuc 		pathname = mkpathname(p, filename);
254f7cf2976SLionel Sambuc #if OS2
255f7cf2976SLionel Sambuc 	else if ((p = getenv("INIT")) != NULL && *p != '\0')
256f7cf2976SLionel Sambuc 		pathname = mkpathname(p, filename);
257f7cf2976SLionel Sambuc #endif
258f7cf2976SLionel Sambuc 	else
259f7cf2976SLionel Sambuc 	{
260f7cf2976SLionel Sambuc 		fprintf(stderr, "cannot find $HOME - using current directory\n");
261f7cf2976SLionel Sambuc 		pathname = mkpathname(".", filename);
262f7cf2976SLionel Sambuc 	}
263f7cf2976SLionel Sambuc 	return (pathname);
264f7cf2976SLionel Sambuc }
265f7cf2976SLionel Sambuc 
266f7cf2976SLionel Sambuc /*
267f7cf2976SLionel Sambuc  * Parse command line arguments.
268f7cf2976SLionel Sambuc  */
269f7cf2976SLionel Sambuc 	void
parse_args(argc,argv)270f7cf2976SLionel Sambuc parse_args(argc, argv)
271f7cf2976SLionel Sambuc 	int argc;
272f7cf2976SLionel Sambuc 	char **argv;
273f7cf2976SLionel Sambuc {
274f7cf2976SLionel Sambuc 	char *arg;
275f7cf2976SLionel Sambuc 
276f7cf2976SLionel Sambuc 	outfile = NULL;
277f7cf2976SLionel Sambuc 	while (--argc > 0)
278f7cf2976SLionel Sambuc 	{
279f7cf2976SLionel Sambuc 		arg = *++argv;
280f7cf2976SLionel Sambuc 		if (arg[0] != '-')
281f7cf2976SLionel Sambuc 			/* Arg does not start with "-"; it's not an option. */
282f7cf2976SLionel Sambuc 			break;
283f7cf2976SLionel Sambuc 		if (arg[1] == '\0')
284f7cf2976SLionel Sambuc 			/* "-" means standard input. */
285f7cf2976SLionel Sambuc 			break;
286f7cf2976SLionel Sambuc 		if (arg[1] == '-' && arg[2] == '\0')
287f7cf2976SLionel Sambuc 		{
288f7cf2976SLionel Sambuc 			/* "--" means end of options. */
289f7cf2976SLionel Sambuc 			argc--;
290f7cf2976SLionel Sambuc 			argv++;
291f7cf2976SLionel Sambuc 			break;
292f7cf2976SLionel Sambuc 		}
293f7cf2976SLionel Sambuc 		switch (arg[1])
294f7cf2976SLionel Sambuc 		{
295f7cf2976SLionel Sambuc 		case '-':
296f7cf2976SLionel Sambuc 			if (strncmp(arg, "--output", 8) == 0)
297f7cf2976SLionel Sambuc 			{
298f7cf2976SLionel Sambuc 				if (arg[8] == '\0')
299f7cf2976SLionel Sambuc 					outfile = &arg[8];
300f7cf2976SLionel Sambuc 				else if (arg[8] == '=')
301f7cf2976SLionel Sambuc 					outfile = &arg[9];
302f7cf2976SLionel Sambuc 				else
303f7cf2976SLionel Sambuc 					usage();
304f7cf2976SLionel Sambuc 				goto opt_o;
305f7cf2976SLionel Sambuc 			}
306f7cf2976SLionel Sambuc 			if (strcmp(arg, "--version") == 0)
307f7cf2976SLionel Sambuc 			{
308f7cf2976SLionel Sambuc 				goto opt_V;
309f7cf2976SLionel Sambuc 			}
310f7cf2976SLionel Sambuc 			usage();
311f7cf2976SLionel Sambuc 			break;
312f7cf2976SLionel Sambuc 		case 'o':
313f7cf2976SLionel Sambuc 			outfile = &argv[0][2];
314f7cf2976SLionel Sambuc 		opt_o:
315f7cf2976SLionel Sambuc 			if (*outfile == '\0')
316f7cf2976SLionel Sambuc 			{
317f7cf2976SLionel Sambuc 				if (--argc <= 0)
318f7cf2976SLionel Sambuc 					usage();
319f7cf2976SLionel Sambuc 				outfile = *(++argv);
320f7cf2976SLionel Sambuc 			}
321f7cf2976SLionel Sambuc 			break;
322f7cf2976SLionel Sambuc 		case 'V':
323f7cf2976SLionel Sambuc 		opt_V:
324f7cf2976SLionel Sambuc 			printf("lesskey  version %s\n", version);
325f7cf2976SLionel Sambuc 			exit(0);
326f7cf2976SLionel Sambuc 		default:
327f7cf2976SLionel Sambuc 			usage();
328f7cf2976SLionel Sambuc 		}
329f7cf2976SLionel Sambuc 	}
330f7cf2976SLionel Sambuc 	if (argc > 1)
331f7cf2976SLionel Sambuc 		usage();
332f7cf2976SLionel Sambuc 	/*
333f7cf2976SLionel Sambuc 	 * Open the input file, or use DEF_LESSKEYINFILE if none specified.
334f7cf2976SLionel Sambuc 	 */
335f7cf2976SLionel Sambuc 	if (argc > 0)
336f7cf2976SLionel Sambuc 		infile = *argv;
337f7cf2976SLionel Sambuc 	else
338f7cf2976SLionel Sambuc 		infile = homefile(DEF_LESSKEYINFILE);
339f7cf2976SLionel Sambuc }
340f7cf2976SLionel Sambuc 
341f7cf2976SLionel Sambuc /*
342f7cf2976SLionel Sambuc  * Initialize data structures.
343f7cf2976SLionel Sambuc  */
344f7cf2976SLionel Sambuc 	void
init_tables()345f7cf2976SLionel Sambuc init_tables()
346f7cf2976SLionel Sambuc {
347f7cf2976SLionel Sambuc 	cmdtable.names = cmdnames;
348f7cf2976SLionel Sambuc 	cmdtable.pbuffer = cmdtable.buffer;
349f7cf2976SLionel Sambuc 
350f7cf2976SLionel Sambuc 	edittable.names = editnames;
351f7cf2976SLionel Sambuc 	edittable.pbuffer = edittable.buffer;
352f7cf2976SLionel Sambuc 
353f7cf2976SLionel Sambuc 	vartable.names = NULL;
354f7cf2976SLionel Sambuc 	vartable.pbuffer = vartable.buffer;
355f7cf2976SLionel Sambuc }
356f7cf2976SLionel Sambuc 
357f7cf2976SLionel Sambuc /*
358f7cf2976SLionel Sambuc  * Parse one character of a string.
359f7cf2976SLionel Sambuc  */
360f7cf2976SLionel Sambuc 	char *
tstr(pp,xlate)361f7cf2976SLionel Sambuc tstr(pp, xlate)
362f7cf2976SLionel Sambuc 	char **pp;
363f7cf2976SLionel Sambuc 	int xlate;
364f7cf2976SLionel Sambuc {
365f7cf2976SLionel Sambuc 	register char *p;
366f7cf2976SLionel Sambuc 	register char ch;
367f7cf2976SLionel Sambuc 	register int i;
368f7cf2976SLionel Sambuc 	static char buf[10];
369f7cf2976SLionel Sambuc 	static char tstr_control_k[] =
370f7cf2976SLionel Sambuc 		{ SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
371f7cf2976SLionel Sambuc 
372f7cf2976SLionel Sambuc 	p = *pp;
373f7cf2976SLionel Sambuc 	switch (*p)
374f7cf2976SLionel Sambuc 	{
375f7cf2976SLionel Sambuc 	case '\\':
376f7cf2976SLionel Sambuc 		++p;
377f7cf2976SLionel Sambuc 		switch (*p)
378f7cf2976SLionel Sambuc 		{
379f7cf2976SLionel Sambuc 		case '0': case '1': case '2': case '3':
380f7cf2976SLionel Sambuc 		case '4': case '5': case '6': case '7':
381f7cf2976SLionel Sambuc 			/*
382f7cf2976SLionel Sambuc 			 * Parse an octal number.
383f7cf2976SLionel Sambuc 			 */
384f7cf2976SLionel Sambuc 			ch = 0;
385f7cf2976SLionel Sambuc 			i = 0;
386f7cf2976SLionel Sambuc 			do
387f7cf2976SLionel Sambuc 				ch = 8*ch + (*p - '0');
388f7cf2976SLionel Sambuc 			while (*++p >= '0' && *p <= '7' && ++i < 3);
389f7cf2976SLionel Sambuc 			*pp = p;
390f7cf2976SLionel Sambuc 			if (xlate && ch == CONTROL('K'))
391f7cf2976SLionel Sambuc 				return tstr_control_k;
392f7cf2976SLionel Sambuc 			buf[0] = ch;
393f7cf2976SLionel Sambuc 			buf[1] = '\0';
394f7cf2976SLionel Sambuc 			return (buf);
395f7cf2976SLionel Sambuc 		case 'b':
396f7cf2976SLionel Sambuc 			*pp = p+1;
397f7cf2976SLionel Sambuc 			return ("\b");
398f7cf2976SLionel Sambuc 		case 'e':
399f7cf2976SLionel Sambuc 			*pp = p+1;
400f7cf2976SLionel Sambuc 			buf[0] = ESC;
401f7cf2976SLionel Sambuc 			buf[1] = '\0';
402f7cf2976SLionel Sambuc 			return (buf);
403f7cf2976SLionel Sambuc 		case 'n':
404f7cf2976SLionel Sambuc 			*pp = p+1;
405f7cf2976SLionel Sambuc 			return ("\n");
406f7cf2976SLionel Sambuc 		case 'r':
407f7cf2976SLionel Sambuc 			*pp = p+1;
408f7cf2976SLionel Sambuc 			return ("\r");
409f7cf2976SLionel Sambuc 		case 't':
410f7cf2976SLionel Sambuc 			*pp = p+1;
411f7cf2976SLionel Sambuc 			return ("\t");
412f7cf2976SLionel Sambuc 		case 'k':
413f7cf2976SLionel Sambuc 			if (xlate)
414f7cf2976SLionel Sambuc 			{
415f7cf2976SLionel Sambuc 				switch (*++p)
416f7cf2976SLionel Sambuc 				{
417f7cf2976SLionel Sambuc 				case 'u': ch = SK_UP_ARROW; break;
418f7cf2976SLionel Sambuc 				case 'd': ch = SK_DOWN_ARROW; break;
419f7cf2976SLionel Sambuc 				case 'r': ch = SK_RIGHT_ARROW; break;
420f7cf2976SLionel Sambuc 				case 'l': ch = SK_LEFT_ARROW; break;
421f7cf2976SLionel Sambuc 				case 'U': ch = SK_PAGE_UP; break;
422f7cf2976SLionel Sambuc 				case 'D': ch = SK_PAGE_DOWN; break;
423f7cf2976SLionel Sambuc 				case 'h': ch = SK_HOME; break;
424f7cf2976SLionel Sambuc 				case 'e': ch = SK_END; break;
425f7cf2976SLionel Sambuc 				case 'x': ch = SK_DELETE; break;
426f7cf2976SLionel Sambuc 				default:
427f7cf2976SLionel Sambuc 					terror("illegal char after \\k");
428f7cf2976SLionel Sambuc 					*pp = p+1;
429f7cf2976SLionel Sambuc 					return ("");
430f7cf2976SLionel Sambuc 				}
431f7cf2976SLionel Sambuc 				*pp = p+1;
432f7cf2976SLionel Sambuc 				buf[0] = SK_SPECIAL_KEY;
433f7cf2976SLionel Sambuc 				buf[1] = ch;
434f7cf2976SLionel Sambuc 				buf[2] = 6;
435f7cf2976SLionel Sambuc 				buf[3] = 1;
436f7cf2976SLionel Sambuc 				buf[4] = 1;
437f7cf2976SLionel Sambuc 				buf[5] = 1;
438f7cf2976SLionel Sambuc 				buf[6] = '\0';
439f7cf2976SLionel Sambuc 				return (buf);
440f7cf2976SLionel Sambuc 			}
441f7cf2976SLionel Sambuc 			/* FALLTHRU */
442f7cf2976SLionel Sambuc 		default:
443f7cf2976SLionel Sambuc 			/*
444f7cf2976SLionel Sambuc 			 * Backslash followed by any other char
445f7cf2976SLionel Sambuc 			 * just means that char.
446f7cf2976SLionel Sambuc 			 */
447f7cf2976SLionel Sambuc 			*pp = p+1;
448f7cf2976SLionel Sambuc 			buf[0] = *p;
449f7cf2976SLionel Sambuc 			buf[1] = '\0';
450f7cf2976SLionel Sambuc 			if (xlate && buf[0] == CONTROL('K'))
451f7cf2976SLionel Sambuc 				return tstr_control_k;
452f7cf2976SLionel Sambuc 			return (buf);
453f7cf2976SLionel Sambuc 		}
454f7cf2976SLionel Sambuc 	case '^':
455f7cf2976SLionel Sambuc 		/*
456*84d9c625SLionel Sambuc 		 * Caret means CONTROL.
457f7cf2976SLionel Sambuc 		 */
458f7cf2976SLionel Sambuc 		*pp = p+2;
459f7cf2976SLionel Sambuc 		buf[0] = CONTROL(p[1]);
460f7cf2976SLionel Sambuc 		buf[1] = '\0';
461f7cf2976SLionel Sambuc 		if (buf[0] == CONTROL('K'))
462f7cf2976SLionel Sambuc 			return tstr_control_k;
463f7cf2976SLionel Sambuc 		return (buf);
464f7cf2976SLionel Sambuc 	}
465f7cf2976SLionel Sambuc 	*pp = p+1;
466f7cf2976SLionel Sambuc 	buf[0] = *p;
467f7cf2976SLionel Sambuc 	buf[1] = '\0';
468f7cf2976SLionel Sambuc 	if (xlate && buf[0] == CONTROL('K'))
469f7cf2976SLionel Sambuc 		return tstr_control_k;
470f7cf2976SLionel Sambuc 	return (buf);
471f7cf2976SLionel Sambuc }
472f7cf2976SLionel Sambuc 
473f7cf2976SLionel Sambuc /*
474f7cf2976SLionel Sambuc  * Skip leading spaces in a string.
475f7cf2976SLionel Sambuc  */
476f7cf2976SLionel Sambuc 	public char *
skipsp(s)477f7cf2976SLionel Sambuc skipsp(s)
478f7cf2976SLionel Sambuc 	register char *s;
479f7cf2976SLionel Sambuc {
480f7cf2976SLionel Sambuc 	while (*s == ' ' || *s == '\t')
481f7cf2976SLionel Sambuc 		s++;
482f7cf2976SLionel Sambuc 	return (s);
483f7cf2976SLionel Sambuc }
484f7cf2976SLionel Sambuc 
485f7cf2976SLionel Sambuc /*
486f7cf2976SLionel Sambuc  * Skip non-space characters in a string.
487f7cf2976SLionel Sambuc  */
488f7cf2976SLionel Sambuc 	public char *
skipnsp(s)489f7cf2976SLionel Sambuc skipnsp(s)
490f7cf2976SLionel Sambuc 	register char *s;
491f7cf2976SLionel Sambuc {
492f7cf2976SLionel Sambuc 	while (*s != '\0' && *s != ' ' && *s != '\t')
493f7cf2976SLionel Sambuc 		s++;
494f7cf2976SLionel Sambuc 	return (s);
495f7cf2976SLionel Sambuc }
496f7cf2976SLionel Sambuc 
497f7cf2976SLionel Sambuc /*
498f7cf2976SLionel Sambuc  * Clean up an input line:
499f7cf2976SLionel Sambuc  * strip off the trailing newline & any trailing # comment.
500f7cf2976SLionel Sambuc  */
501f7cf2976SLionel Sambuc 	char *
clean_line(s)502f7cf2976SLionel Sambuc clean_line(s)
503f7cf2976SLionel Sambuc 	char *s;
504f7cf2976SLionel Sambuc {
505f7cf2976SLionel Sambuc 	register int i;
506f7cf2976SLionel Sambuc 
507f7cf2976SLionel Sambuc 	s = skipsp(s);
508f7cf2976SLionel Sambuc 	for (i = 0;  s[i] != '\n' && s[i] != '\r' && s[i] != '\0';  i++)
509f7cf2976SLionel Sambuc 		if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
510f7cf2976SLionel Sambuc 			break;
511f7cf2976SLionel Sambuc 	s[i] = '\0';
512f7cf2976SLionel Sambuc 	return (s);
513f7cf2976SLionel Sambuc }
514f7cf2976SLionel Sambuc 
515f7cf2976SLionel Sambuc /*
516f7cf2976SLionel Sambuc  * Add a byte to the output command table.
517f7cf2976SLionel Sambuc  */
518f7cf2976SLionel Sambuc 	void
add_cmd_char(c)519f7cf2976SLionel Sambuc add_cmd_char(c)
520f7cf2976SLionel Sambuc 	int c;
521f7cf2976SLionel Sambuc {
522f7cf2976SLionel Sambuc 	if (currtable->pbuffer >= currtable->buffer + MAX_USERCMD)
523f7cf2976SLionel Sambuc 	{
524f7cf2976SLionel Sambuc 		terror("too many commands");
525f7cf2976SLionel Sambuc 		exit(1);
526f7cf2976SLionel Sambuc 	}
527f7cf2976SLionel Sambuc 	*(currtable->pbuffer)++ = c;
528f7cf2976SLionel Sambuc }
529f7cf2976SLionel Sambuc 
530f7cf2976SLionel Sambuc /*
531f7cf2976SLionel Sambuc  * Add a string to the output command table.
532f7cf2976SLionel Sambuc  */
533f7cf2976SLionel Sambuc 	void
add_cmd_str(s)534f7cf2976SLionel Sambuc add_cmd_str(s)
535f7cf2976SLionel Sambuc 	char *s;
536f7cf2976SLionel Sambuc {
537f7cf2976SLionel Sambuc 	for ( ;  *s != '\0';  s++)
538f7cf2976SLionel Sambuc 		add_cmd_char(*s);
539f7cf2976SLionel Sambuc }
540f7cf2976SLionel Sambuc 
541f7cf2976SLionel Sambuc /*
542f7cf2976SLionel Sambuc  * See if we have a special "control" line.
543f7cf2976SLionel Sambuc  */
544f7cf2976SLionel Sambuc 	int
control_line(s)545f7cf2976SLionel Sambuc control_line(s)
546f7cf2976SLionel Sambuc 	char *s;
547f7cf2976SLionel Sambuc {
548f7cf2976SLionel Sambuc #define	PREFIX(str,pat)	(strncmp(str,pat,strlen(pat)) == 0)
549f7cf2976SLionel Sambuc 
550f7cf2976SLionel Sambuc 	if (PREFIX(s, "#line-edit"))
551f7cf2976SLionel Sambuc 	{
552f7cf2976SLionel Sambuc 		currtable = &edittable;
553f7cf2976SLionel Sambuc 		return (1);
554f7cf2976SLionel Sambuc 	}
555f7cf2976SLionel Sambuc 	if (PREFIX(s, "#command"))
556f7cf2976SLionel Sambuc 	{
557f7cf2976SLionel Sambuc 		currtable = &cmdtable;
558f7cf2976SLionel Sambuc 		return (1);
559f7cf2976SLionel Sambuc 	}
560f7cf2976SLionel Sambuc 	if (PREFIX(s, "#env"))
561f7cf2976SLionel Sambuc 	{
562f7cf2976SLionel Sambuc 		currtable = &vartable;
563f7cf2976SLionel Sambuc 		return (1);
564f7cf2976SLionel Sambuc 	}
565f7cf2976SLionel Sambuc 	if (PREFIX(s, "#stop"))
566f7cf2976SLionel Sambuc 	{
567f7cf2976SLionel Sambuc 		add_cmd_char('\0');
568f7cf2976SLionel Sambuc 		add_cmd_char(A_END_LIST);
569f7cf2976SLionel Sambuc 		return (1);
570f7cf2976SLionel Sambuc 	}
571f7cf2976SLionel Sambuc 	return (0);
572f7cf2976SLionel Sambuc }
573f7cf2976SLionel Sambuc 
574f7cf2976SLionel Sambuc /*
575f7cf2976SLionel Sambuc  * Output some bytes.
576f7cf2976SLionel Sambuc  */
577f7cf2976SLionel Sambuc 	void
fputbytes(fd,buf,len)578f7cf2976SLionel Sambuc fputbytes(fd, buf, len)
579f7cf2976SLionel Sambuc 	FILE *fd;
580f7cf2976SLionel Sambuc 	char *buf;
581f7cf2976SLionel Sambuc 	int len;
582f7cf2976SLionel Sambuc {
583f7cf2976SLionel Sambuc 	while (len-- > 0)
584f7cf2976SLionel Sambuc 	{
585f7cf2976SLionel Sambuc 		fwrite(buf, sizeof(char), 1, fd);
586f7cf2976SLionel Sambuc 		buf++;
587f7cf2976SLionel Sambuc 	}
588f7cf2976SLionel Sambuc }
589f7cf2976SLionel Sambuc 
590f7cf2976SLionel Sambuc /*
591f7cf2976SLionel Sambuc  * Output an integer, in special KRADIX form.
592f7cf2976SLionel Sambuc  */
593f7cf2976SLionel Sambuc 	void
fputint(fd,val)594f7cf2976SLionel Sambuc fputint(fd, val)
595f7cf2976SLionel Sambuc 	FILE *fd;
596f7cf2976SLionel Sambuc 	unsigned int val;
597f7cf2976SLionel Sambuc {
598f7cf2976SLionel Sambuc 	char c;
599f7cf2976SLionel Sambuc 
600f7cf2976SLionel Sambuc 	if (val >= KRADIX*KRADIX)
601f7cf2976SLionel Sambuc 	{
602f7cf2976SLionel Sambuc 		fprintf(stderr, "error: integer too big (%d > %d)\n",
603f7cf2976SLionel Sambuc 			val, KRADIX*KRADIX);
604f7cf2976SLionel Sambuc 		exit(1);
605f7cf2976SLionel Sambuc 	}
606f7cf2976SLionel Sambuc 	c = val % KRADIX;
607f7cf2976SLionel Sambuc 	fwrite(&c, sizeof(char), 1, fd);
608f7cf2976SLionel Sambuc 	c = val / KRADIX;
609f7cf2976SLionel Sambuc 	fwrite(&c, sizeof(char), 1, fd);
610f7cf2976SLionel Sambuc }
611f7cf2976SLionel Sambuc 
612f7cf2976SLionel Sambuc /*
613f7cf2976SLionel Sambuc  * Find an action, given the name of the action.
614f7cf2976SLionel Sambuc  */
615f7cf2976SLionel Sambuc 	int
findaction(actname)616f7cf2976SLionel Sambuc findaction(actname)
617f7cf2976SLionel Sambuc 	char *actname;
618f7cf2976SLionel Sambuc {
619f7cf2976SLionel Sambuc 	int i;
620f7cf2976SLionel Sambuc 
621f7cf2976SLionel Sambuc 	for (i = 0;  currtable->names[i].cn_name != NULL;  i++)
622f7cf2976SLionel Sambuc 		if (strcmp(currtable->names[i].cn_name, actname) == 0)
623f7cf2976SLionel Sambuc 			return (currtable->names[i].cn_action);
624f7cf2976SLionel Sambuc 	terror("unknown action");
625f7cf2976SLionel Sambuc 	return (A_INVALID);
626f7cf2976SLionel Sambuc }
627f7cf2976SLionel Sambuc 
628f7cf2976SLionel Sambuc 	void
terror(s)629f7cf2976SLionel Sambuc terror(s)
630f7cf2976SLionel Sambuc 	char *s;
631f7cf2976SLionel Sambuc {
632f7cf2976SLionel Sambuc 	fprintf(stderr, "line %d: %s\n", linenum, s);
633f7cf2976SLionel Sambuc 	errors++;
634f7cf2976SLionel Sambuc }
635f7cf2976SLionel Sambuc 
636f7cf2976SLionel Sambuc 
637f7cf2976SLionel Sambuc 	void
parse_cmdline(p)638f7cf2976SLionel Sambuc parse_cmdline(p)
639f7cf2976SLionel Sambuc 	char *p;
640f7cf2976SLionel Sambuc {
641f7cf2976SLionel Sambuc 	int cmdlen;
642f7cf2976SLionel Sambuc 	char *actname;
643f7cf2976SLionel Sambuc 	int action;
644f7cf2976SLionel Sambuc 	char *s;
645f7cf2976SLionel Sambuc 	char c;
646f7cf2976SLionel Sambuc 
647f7cf2976SLionel Sambuc 	/*
648f7cf2976SLionel Sambuc 	 * Parse the command string and store it in the current table.
649f7cf2976SLionel Sambuc 	 */
650f7cf2976SLionel Sambuc 	cmdlen = 0;
651f7cf2976SLionel Sambuc 	do
652f7cf2976SLionel Sambuc 	{
653f7cf2976SLionel Sambuc 		s = tstr(&p, 1);
654f7cf2976SLionel Sambuc 		cmdlen += strlen(s);
655f7cf2976SLionel Sambuc 		if (cmdlen > MAX_CMDLEN)
656f7cf2976SLionel Sambuc 			terror("command too long");
657f7cf2976SLionel Sambuc 		else
658f7cf2976SLionel Sambuc 			add_cmd_str(s);
659f7cf2976SLionel Sambuc 	} while (*p != ' ' && *p != '\t' && *p != '\0');
660f7cf2976SLionel Sambuc 	/*
661f7cf2976SLionel Sambuc 	 * Terminate the command string with a null byte.
662f7cf2976SLionel Sambuc 	 */
663f7cf2976SLionel Sambuc 	add_cmd_char('\0');
664f7cf2976SLionel Sambuc 
665f7cf2976SLionel Sambuc 	/*
666f7cf2976SLionel Sambuc 	 * Skip white space between the command string
667f7cf2976SLionel Sambuc 	 * and the action name.
668f7cf2976SLionel Sambuc 	 * Terminate the action name with a null byte.
669f7cf2976SLionel Sambuc 	 */
670f7cf2976SLionel Sambuc 	p = skipsp(p);
671f7cf2976SLionel Sambuc 	if (*p == '\0')
672f7cf2976SLionel Sambuc 	{
673f7cf2976SLionel Sambuc 		terror("missing action");
674f7cf2976SLionel Sambuc 		return;
675f7cf2976SLionel Sambuc 	}
676f7cf2976SLionel Sambuc 	actname = p;
677f7cf2976SLionel Sambuc 	p = skipnsp(p);
678f7cf2976SLionel Sambuc 	c = *p;
679f7cf2976SLionel Sambuc 	*p = '\0';
680f7cf2976SLionel Sambuc 
681f7cf2976SLionel Sambuc 	/*
682f7cf2976SLionel Sambuc 	 * Parse the action name and store it in the current table.
683f7cf2976SLionel Sambuc 	 */
684f7cf2976SLionel Sambuc 	action = findaction(actname);
685f7cf2976SLionel Sambuc 
686f7cf2976SLionel Sambuc 	/*
687f7cf2976SLionel Sambuc 	 * See if an extra string follows the action name.
688f7cf2976SLionel Sambuc 	 */
689f7cf2976SLionel Sambuc 	*p = c;
690f7cf2976SLionel Sambuc 	p = skipsp(p);
691f7cf2976SLionel Sambuc 	if (*p == '\0')
692f7cf2976SLionel Sambuc 	{
693f7cf2976SLionel Sambuc 		add_cmd_char(action);
694f7cf2976SLionel Sambuc 	} else
695f7cf2976SLionel Sambuc 	{
696f7cf2976SLionel Sambuc 		/*
697f7cf2976SLionel Sambuc 		 * OR the special value A_EXTRA into the action byte.
698f7cf2976SLionel Sambuc 		 * Put the extra string after the action byte.
699f7cf2976SLionel Sambuc 		 */
700f7cf2976SLionel Sambuc 		add_cmd_char(action | A_EXTRA);
701f7cf2976SLionel Sambuc 		while (*p != '\0')
702f7cf2976SLionel Sambuc 			add_cmd_str(tstr(&p, 0));
703f7cf2976SLionel Sambuc 		add_cmd_char('\0');
704f7cf2976SLionel Sambuc 	}
705f7cf2976SLionel Sambuc }
706f7cf2976SLionel Sambuc 
707f7cf2976SLionel Sambuc 	void
parse_varline(p)708f7cf2976SLionel Sambuc parse_varline(p)
709f7cf2976SLionel Sambuc 	char *p;
710f7cf2976SLionel Sambuc {
711f7cf2976SLionel Sambuc 	char *s;
712f7cf2976SLionel Sambuc 
713f7cf2976SLionel Sambuc 	do
714f7cf2976SLionel Sambuc 	{
715f7cf2976SLionel Sambuc 		s = tstr(&p, 0);
716f7cf2976SLionel Sambuc 		add_cmd_str(s);
717f7cf2976SLionel Sambuc 	} while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
718f7cf2976SLionel Sambuc 	/*
719f7cf2976SLionel Sambuc 	 * Terminate the variable name with a null byte.
720f7cf2976SLionel Sambuc 	 */
721f7cf2976SLionel Sambuc 	add_cmd_char('\0');
722f7cf2976SLionel Sambuc 
723f7cf2976SLionel Sambuc 	p = skipsp(p);
724f7cf2976SLionel Sambuc 	if (*p++ != '=')
725f7cf2976SLionel Sambuc 	{
726f7cf2976SLionel Sambuc 		terror("missing =");
727f7cf2976SLionel Sambuc 		return;
728f7cf2976SLionel Sambuc 	}
729f7cf2976SLionel Sambuc 
730f7cf2976SLionel Sambuc 	add_cmd_char(EV_OK|A_EXTRA);
731f7cf2976SLionel Sambuc 
732f7cf2976SLionel Sambuc 	p = skipsp(p);
733f7cf2976SLionel Sambuc 	while (*p != '\0')
734f7cf2976SLionel Sambuc 	{
735f7cf2976SLionel Sambuc 		s = tstr(&p, 0);
736f7cf2976SLionel Sambuc 		add_cmd_str(s);
737f7cf2976SLionel Sambuc 	}
738f7cf2976SLionel Sambuc 	add_cmd_char('\0');
739f7cf2976SLionel Sambuc }
740f7cf2976SLionel Sambuc 
741f7cf2976SLionel Sambuc /*
742f7cf2976SLionel Sambuc  * Parse a line from the lesskey file.
743f7cf2976SLionel Sambuc  */
744f7cf2976SLionel Sambuc 	void
parse_line(line)745f7cf2976SLionel Sambuc parse_line(line)
746f7cf2976SLionel Sambuc 	char *line;
747f7cf2976SLionel Sambuc {
748f7cf2976SLionel Sambuc 	char *p;
749f7cf2976SLionel Sambuc 
750f7cf2976SLionel Sambuc 	/*
751f7cf2976SLionel Sambuc 	 * See if it is a control line.
752f7cf2976SLionel Sambuc 	 */
753f7cf2976SLionel Sambuc 	if (control_line(line))
754f7cf2976SLionel Sambuc 		return;
755f7cf2976SLionel Sambuc 	/*
756f7cf2976SLionel Sambuc 	 * Skip leading white space.
757f7cf2976SLionel Sambuc 	 * Replace the final newline with a null byte.
758f7cf2976SLionel Sambuc 	 * Ignore blank lines and comments.
759f7cf2976SLionel Sambuc 	 */
760f7cf2976SLionel Sambuc 	p = clean_line(line);
761f7cf2976SLionel Sambuc 	if (*p == '\0')
762f7cf2976SLionel Sambuc 		return;
763f7cf2976SLionel Sambuc 
764f7cf2976SLionel Sambuc 	if (currtable == &vartable)
765f7cf2976SLionel Sambuc 		parse_varline(p);
766f7cf2976SLionel Sambuc 	else
767f7cf2976SLionel Sambuc 		parse_cmdline(p);
768f7cf2976SLionel Sambuc }
769f7cf2976SLionel Sambuc 
770f7cf2976SLionel Sambuc 	int
main(argc,argv)771f7cf2976SLionel Sambuc main(argc, argv)
772f7cf2976SLionel Sambuc 	int argc;
773f7cf2976SLionel Sambuc 	char *argv[];
774f7cf2976SLionel Sambuc {
775f7cf2976SLionel Sambuc 	FILE *desc;
776f7cf2976SLionel Sambuc 	FILE *out;
777f7cf2976SLionel Sambuc 	char line[1024];
778f7cf2976SLionel Sambuc 
779f7cf2976SLionel Sambuc #ifdef WIN32
780f7cf2976SLionel Sambuc 	if (getenv("HOME") == NULL)
781f7cf2976SLionel Sambuc 	{
782f7cf2976SLionel Sambuc 		/*
783f7cf2976SLionel Sambuc 		 * If there is no HOME environment variable,
784f7cf2976SLionel Sambuc 		 * try the concatenation of HOMEDRIVE + HOMEPATH.
785f7cf2976SLionel Sambuc 		 */
786f7cf2976SLionel Sambuc 		char *drive = getenv("HOMEDRIVE");
787f7cf2976SLionel Sambuc 		char *path  = getenv("HOMEPATH");
788f7cf2976SLionel Sambuc 		if (drive != NULL && path != NULL)
789f7cf2976SLionel Sambuc 		{
790f7cf2976SLionel Sambuc 			char *env = (char *) calloc(strlen(drive) +
791f7cf2976SLionel Sambuc 					strlen(path) + 6, sizeof(char));
792f7cf2976SLionel Sambuc 			strcpy(env, "HOME=");
793f7cf2976SLionel Sambuc 			strcat(env, drive);
794f7cf2976SLionel Sambuc 			strcat(env, path);
795f7cf2976SLionel Sambuc 			putenv(env);
796f7cf2976SLionel Sambuc 		}
797f7cf2976SLionel Sambuc 	}
798f7cf2976SLionel Sambuc #endif /* WIN32 */
799f7cf2976SLionel Sambuc 
800f7cf2976SLionel Sambuc 	/*
801f7cf2976SLionel Sambuc 	 * Process command line arguments.
802f7cf2976SLionel Sambuc 	 */
803f7cf2976SLionel Sambuc 	parse_args(argc, argv);
804f7cf2976SLionel Sambuc 	init_tables();
805f7cf2976SLionel Sambuc 
806f7cf2976SLionel Sambuc 	/*
807f7cf2976SLionel Sambuc 	 * Open the input file.
808f7cf2976SLionel Sambuc 	 */
809f7cf2976SLionel Sambuc 	if (strcmp(infile, "-") == 0)
810f7cf2976SLionel Sambuc 		desc = stdin;
811f7cf2976SLionel Sambuc 	else if ((desc = fopen(infile, "r")) == NULL)
812f7cf2976SLionel Sambuc 	{
813f7cf2976SLionel Sambuc #if HAVE_PERROR
814f7cf2976SLionel Sambuc 		perror(infile);
815f7cf2976SLionel Sambuc #else
816f7cf2976SLionel Sambuc 		fprintf(stderr, "Cannot open %s\n", infile);
817f7cf2976SLionel Sambuc #endif
818f7cf2976SLionel Sambuc 		usage();
819f7cf2976SLionel Sambuc 	}
820f7cf2976SLionel Sambuc 
821f7cf2976SLionel Sambuc 	/*
822f7cf2976SLionel Sambuc 	 * Read and parse the input file, one line at a time.
823f7cf2976SLionel Sambuc 	 */
824f7cf2976SLionel Sambuc 	errors = 0;
825f7cf2976SLionel Sambuc 	linenum = 0;
826f7cf2976SLionel Sambuc 	while (fgets(line, sizeof(line), desc) != NULL)
827f7cf2976SLionel Sambuc 	{
828f7cf2976SLionel Sambuc 		++linenum;
829f7cf2976SLionel Sambuc 		parse_line(line);
830f7cf2976SLionel Sambuc 	}
831f7cf2976SLionel Sambuc 
832f7cf2976SLionel Sambuc 	/*
833f7cf2976SLionel Sambuc 	 * Write the output file.
834f7cf2976SLionel Sambuc 	 * If no output file was specified, use "$HOME/.less"
835f7cf2976SLionel Sambuc 	 */
836f7cf2976SLionel Sambuc 	if (errors > 0)
837f7cf2976SLionel Sambuc 	{
838f7cf2976SLionel Sambuc 		fprintf(stderr, "%d errors; no output produced\n", errors);
839f7cf2976SLionel Sambuc 		exit(1);
840f7cf2976SLionel Sambuc 	}
841f7cf2976SLionel Sambuc 
842f7cf2976SLionel Sambuc 	if (outfile == NULL)
843f7cf2976SLionel Sambuc 		outfile = getenv("LESSKEY");
844f7cf2976SLionel Sambuc 	if (outfile == NULL)
845f7cf2976SLionel Sambuc 		outfile = homefile(LESSKEYFILE);
846f7cf2976SLionel Sambuc 	if ((out = fopen(outfile, "wb")) == NULL)
847f7cf2976SLionel Sambuc 	{
848f7cf2976SLionel Sambuc #if HAVE_PERROR
849f7cf2976SLionel Sambuc 		perror(outfile);
850f7cf2976SLionel Sambuc #else
851f7cf2976SLionel Sambuc 		fprintf(stderr, "Cannot open %s\n", outfile);
852f7cf2976SLionel Sambuc #endif
853f7cf2976SLionel Sambuc 		exit(1);
854f7cf2976SLionel Sambuc 	}
855f7cf2976SLionel Sambuc 
856f7cf2976SLionel Sambuc 	/* File header */
857f7cf2976SLionel Sambuc 	fputbytes(out, fileheader, sizeof(fileheader));
858f7cf2976SLionel Sambuc 
859f7cf2976SLionel Sambuc 	/* Command key section */
860f7cf2976SLionel Sambuc 	fputbytes(out, cmdsection, sizeof(cmdsection));
861f7cf2976SLionel Sambuc 	fputint(out, cmdtable.pbuffer - cmdtable.buffer);
862f7cf2976SLionel Sambuc 	fputbytes(out, (char *)cmdtable.buffer, cmdtable.pbuffer-cmdtable.buffer);
863f7cf2976SLionel Sambuc 	/* Edit key section */
864f7cf2976SLionel Sambuc 	fputbytes(out, editsection, sizeof(editsection));
865f7cf2976SLionel Sambuc 	fputint(out, edittable.pbuffer - edittable.buffer);
866f7cf2976SLionel Sambuc 	fputbytes(out, (char *)edittable.buffer, edittable.pbuffer-edittable.buffer);
867f7cf2976SLionel Sambuc 
868f7cf2976SLionel Sambuc 	/* Environment variable section */
869f7cf2976SLionel Sambuc 	fputbytes(out, varsection, sizeof(varsection));
870f7cf2976SLionel Sambuc 	fputint(out, vartable.pbuffer - vartable.buffer);
871f7cf2976SLionel Sambuc 	fputbytes(out, (char *)vartable.buffer, vartable.pbuffer-vartable.buffer);
872f7cf2976SLionel Sambuc 
873f7cf2976SLionel Sambuc 	/* File trailer */
874f7cf2976SLionel Sambuc 	fputbytes(out, endsection, sizeof(endsection));
875f7cf2976SLionel Sambuc 	fputbytes(out, filetrailer, sizeof(filetrailer));
876f7cf2976SLionel Sambuc 	return (0);
877f7cf2976SLionel Sambuc }
878