2 * Copyright (C) 1984-2000 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * lesskey [-o output] [input]
15 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
18 * If no input file is specified, standard input is used.
19 * If no output file is specified, $HOME/.less is used.
21 * The .less file is used to specify (to "less") user-defined
22 * key bindings. Basically any sequence of 1 to MAX_CMDLEN
23 * keystrokes may be bound to an existing less function.
25 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
27 * The input file is an ascii file consisting of a
28 * sequence of lines of the form:
29 * string <whitespace> action [chars] <newline>
31 * "string" is a sequence of command characters which form
32 * the new user-defined command. The command
34 * 1. The actual character itself.
35 * 2. A character preceded by ^ to specify a
36 * control character (e.g. ^X means control-X).
37 * 3. A backslash followed by one to three octal digits
38 * to specify a character by its octal value.
39 * 4. A backslash followed by b, e, n, r or t
40 * to specify \b, ESC, \n, \r or \t, respectively.
41 * 5. Any character (other than those mentioned above) preceded
42 * by a \ to specify the character itself (characters which
43 * must be preceded by \ include ^, \, and whitespace.
44 * "action" is the name of a "less" action, from the table below.
45 * "chars" is an optional sequence of characters which is treated
46 * as keyboard input after the command is executed.
48 * Blank lines and lines which start with # are ignored,
49 * except for the special control lines:
50 * #command Signals the beginning of the command
52 * #line-edit Signals the beginning of the line-editing
54 * #env Signals the beginning of the environment
56 * #stop Stops command parsing in less;
57 * causes all default keys to be disabled.
59 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
61 * The output file is a non-ascii file, consisting of a header,
62 * one or more sections, and a trailer.
63 * Each section begins with a section header, a section length word
64 * and the section data. Normally there are three sections:
65 * CMD_SECTION Definition of command keys.
66 * EDIT_SECTION Definition of editing keys.
67 * END_SECTION A special section header, with no
68 * length word or section data.
70 * Section data consists of zero or more byte sequences of the form:
73 * string <0> <action|A_EXTRA> chars <0>
75 * "string" is the command string.
76 * "<0>" is one null byte.
77 * "<action>" is one byte containing the action code (the A_xxx value).
78 * If action is ORed with A_EXTRA, the action byte is followed
79 * by the null-terminated "chars" string.
81 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
94 struct cmdname cmdnames[] =
96 "back-bracket", A_B_BRACKET,
97 "back-line", A_B_LINE,
98 "back-line-force", A_BF_LINE,
99 "back-screen", A_B_SCREEN,
100 "back-scroll", A_B_SCROLL,
101 "back-search", A_B_SEARCH,
102 "back-window", A_B_WINDOW,
105 "display-flag", A_DISP_OPTION,
106 "display-option", A_DISP_OPTION,
108 "examine", A_EXAMINE,
109 "first-cmd", A_FIRSTCMD,
110 "firstcmd", A_FIRSTCMD,
111 "flush-repaint", A_FREPAINT,
112 "forw-bracket", A_F_BRACKET,
113 "forw-forever", A_F_FOREVER,
114 "forw-line", A_F_LINE,
115 "forw-line-force", A_FF_LINE,
116 "forw-screen", A_F_SCREEN,
117 "forw-screen-force", A_FF_SCREEN,
118 "forw-scroll", A_F_SCROLL,
119 "forw-search", A_F_SEARCH,
120 "forw-window", A_F_WINDOW,
122 "goto-line", A_GOLINE,
123 "goto-mark", A_GOMARK,
125 "index-file", A_INDEX_FILE,
126 "invalid", A_UINVALID,
127 "left-scroll", A_LSHIFT,
128 "next-file", A_NEXT_FILE,
129 "next-tag", A_NEXT_TAG,
130 "noaction", A_NOACTION,
131 "percent", A_PERCENT,
133 "prev-file", A_PREV_FILE,
134 "prev-tag", A_PREV_TAG,
136 "remove-file", A_REMOVE_FILE,
137 "repaint", A_REPAINT,
138 "repaint-flush", A_FREPAINT,
139 "repeat-search", A_AGAIN_SEARCH,
140 "repeat-search-all", A_T_AGAIN_SEARCH,
141 "reverse-search", A_REVERSE_SEARCH,
142 "reverse-search-all", A_T_REVERSE_SEARCH,
143 "right-scroll", A_RSHIFT,
144 "set-mark", A_SETMARK,
147 "toggle-flag", A_OPT_TOGGLE,
148 "toggle-option", A_OPT_TOGGLE,
149 "undo-hilite", A_UNDO_SEARCH,
150 "version", A_VERSION,
155 struct cmdname editnames[] =
157 "back-complete", EC_B_COMPLETE,
158 "backspace", EC_BACKSPACE,
163 "forw-complete", EC_F_COMPLETE,
166 "invalid", EC_UINVALID,
167 "kill-line", EC_LINEKILL,
169 "literal", EC_LITERAL,
172 "word-backspace", EC_W_BACKSPACE,
173 "word-delete", EC_W_DELETE,
174 "word-left", EC_W_LEFT,
175 "word-right", EC_W_RIGHT,
181 struct cmdname *names;
183 char buffer[MAX_USERCMD];
186 struct table cmdtable;
187 struct table edittable;
188 struct table vartable;
189 struct table *currtable = &cmdtable;
191 char fileheader[] = {
197 char filetrailer[] = {
198 C0_END_LESSKEY_MAGIC,
199 C1_END_LESSKEY_MAGIC,
202 char cmdsection[1] = { CMD_SECTION };
203 char editsection[1] = { EDIT_SECTION };
204 char varsection[1] = { VAR_SECTION };
205 char endsection[1] = { END_SECTION };
208 char *outfile = NULL ;
213 extern char version[];
218 fprintf(stderr, "usage: lesskey [-o output] [input]\n");
223 mkpathname(dirname, filename)
229 pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char));
230 strcpy(pathname, dirname);
231 strcat(pathname, PATHNAME_SEP);
232 strcat(pathname, filename);
237 * Figure out the name of a default file (in the user's HOME directory).
246 if ((p = getenv("HOME")) != NULL && *p != '\0')
247 pathname = mkpathname(p, filename);
249 else if ((p = getenv("INIT")) != NULL && *p != '\0')
250 pathname = mkpathname(p, filename);
254 fprintf(stderr, "cannot find $HOME - using current directory\n");
255 pathname = mkpathname(".", filename);
261 * Parse command line arguments.
264 parse_args(argc, argv)
275 /* Arg does not start with "-"; it's not an option. */
278 /* "-" means standard input. */
280 if (arg[1] == '-' && arg[2] == '\0')
282 /* "--" means end of options. */
290 if (strncmp(arg, "--output", 8) == 0)
294 else if (arg[8] == '=')
300 if (strcmp(arg, "--version") == 0)
307 outfile = &argv[0][2];
309 if (*outfile == '\0')
318 printf("lesskey version %s\n", version);
327 * Open the input file, or use DEF_LESSKEYINFILE if none specified.
332 infile = homefile(DEF_LESSKEYINFILE);
336 * Initialize data structures.
341 cmdtable.names = cmdnames;
342 cmdtable.pbuffer = cmdtable.buffer;
344 edittable.names = editnames;
345 edittable.pbuffer = edittable.buffer;
347 vartable.names = NULL;
348 vartable.pbuffer = vartable.buffer;
352 * Parse one character of a string.
362 static char tstr_control_k[] =
363 { SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
372 case '0': case '1': case '2': case '3':
373 case '4': case '5': case '6': case '7':
375 * Parse an octal number.
380 ch = 8*ch + (*p - '0');
381 while (*++p >= '0' && *p <= '7' && ++i < 3);
383 if (ch == CONTROL('K'))
384 return tstr_control_k;
408 case 'u': ch = SK_UP_ARROW; break;
409 case 'd': ch = SK_DOWN_ARROW; break;
410 case 'r': ch = SK_RIGHT_ARROW; break;
411 case 'l': ch = SK_LEFT_ARROW; break;
412 case 'U': ch = SK_PAGE_UP; break;
413 case 'D': ch = SK_PAGE_DOWN; break;
414 case 'h': ch = SK_HOME; break;
415 case 'e': ch = SK_END; break;
416 case 'x': ch = SK_DELETE; break;
418 error("illegal char after \\k");
423 buf[0] = SK_SPECIAL_KEY;
433 * Backslash followed by any other char
434 * just means that char.
439 if (buf[0] == CONTROL('K'))
440 return tstr_control_k;
445 * Carat means CONTROL.
448 buf[0] = CONTROL(p[1]);
450 if (buf[0] == CONTROL('K'))
451 return tstr_control_k;
457 if (buf[0] == CONTROL('K'))
458 return tstr_control_k;
463 * Skip leading spaces in a string.
469 while (*s == ' ' || *s == '\t')
475 * Skip non-space characters in a string.
481 while (*s != '\0' && *s != ' ' && *s != '\t')
487 * Clean up an input line:
488 * strip off the trailing newline & any trailing # comment.
497 for (i = 0; s[i] != '\n' && s[i] != '\r' && s[i] != '\0'; i++)
498 if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
505 * Add a byte to the output command table.
511 if (currtable->pbuffer >= currtable->buffer + MAX_USERCMD)
513 error("too many commands");
516 *(currtable->pbuffer)++ = c;
520 * Add a string to the output command table.
526 for ( ; *s != '\0'; s++)
531 * See if we have a special "control" line.
537 #define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)-1) == 0)
539 if (PREFIX(s, "#line-edit"))
541 currtable = &edittable;
544 if (PREFIX(s, "#command"))
546 currtable = &cmdtable;
549 if (PREFIX(s, "#env"))
551 currtable = &vartable;
554 if (PREFIX(s, "#stop"))
557 add_cmd_char(A_END_LIST);
567 fputbytes(fd, buf, len)
574 fwrite(buf, sizeof(char), 1, fd);
580 * Output an integer, in special KRADIX form.
589 if (val >= KRADIX*KRADIX)
591 fprintf(stderr, "error: integer too big (%d > %d)\n",
596 fwrite(&c, sizeof(char), 1, fd);
598 fwrite(&c, sizeof(char), 1, fd);
602 * Find an action, given the name of the action.
610 for (i = 0; currtable->names[i].cn_name != NULL; i++)
611 if (strcmp(currtable->names[i].cn_name, actname) == 0)
612 return (currtable->names[i].cn_action);
613 error("unknown action");
621 fprintf(stderr, "line %d: %s\n", linenum, s);
637 * Parse the command string and store it in the current table.
644 if (cmdlen > MAX_CMDLEN)
645 error("command too long");
648 } while (*p != ' ' && *p != '\t' && *p != '\0');
650 * Terminate the command string with a null byte.
655 * Skip white space between the command string
656 * and the action name.
657 * Terminate the action name with a null byte.
662 error("missing action");
671 * Parse the action name and store it in the current table.
673 action = findaction(actname);
676 * See if an extra string follows the action name.
682 add_cmd_char(action);
686 * OR the special value A_EXTRA into the action byte.
687 * Put the extra string after the action byte.
689 add_cmd_char(action | A_EXTRA);
691 add_cmd_str(tstr(&p));
706 } while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
708 * Terminate the variable name with a null byte.
719 add_cmd_char(EV_OK|A_EXTRA);
731 * Parse a line from the lesskey file.
740 * See if it is a control line.
742 if (control_line(line))
745 * Skip leading white space.
746 * Replace the final newline with a null byte.
747 * Ignore blank lines and comments.
749 p = clean_line(line);
753 if (currtable == &vartable)
769 if (getenv("HOME") == NULL)
772 * If there is no HOME environment variable,
773 * try the concatenation of HOMEDRIVE + HOMEPATH.
775 char *drive = getenv("HOMEDRIVE");
776 char *path = getenv("HOMEPATH");
777 if (drive != NULL && path != NULL)
779 char *env = (char *) calloc(strlen(drive) +
780 strlen(path) + 6, sizeof(char));
781 strcpy(env, "HOME=");
790 * Process command line arguments.
792 parse_args(argc, argv);
796 * Open the input file.
798 if (strcmp(infile, "-") == 0)
800 else if ((desc = fopen(infile, "r")) == NULL)
805 fprintf(stderr, "Cannot open %s\n", infile);
811 * Read and parse the input file, one line at a time.
815 while (fgets(line, sizeof(line), desc) != NULL)
822 * Write the output file.
823 * If no output file was specified, use "$HOME/.less"
827 fprintf(stderr, "%d errors; no output produced\n", errors);
832 outfile = getenv("LESSKEY");
834 outfile = homefile(LESSKEYFILE);
835 if ((out = fopen(outfile, "wb")) == NULL)
840 fprintf(stderr, "Cannot open %s\n", outfile);
846 fputbytes(out, fileheader, sizeof(fileheader));
848 /* Command key section */
849 fputbytes(out, cmdsection, sizeof(cmdsection));
850 fputint(out, cmdtable.pbuffer - cmdtable.buffer);
851 fputbytes(out, (char *)cmdtable.buffer, cmdtable.pbuffer-cmdtable.buffer);
852 /* Edit key section */
853 fputbytes(out, editsection, sizeof(editsection));
854 fputint(out, edittable.pbuffer - edittable.buffer);
855 fputbytes(out, (char *)edittable.buffer, edittable.pbuffer-edittable.buffer);
857 /* Environment variable section */
858 fputbytes(out, varsection, sizeof(varsection));
859 fputint(out, vartable.pbuffer - vartable.buffer);
860 fputbytes(out, (char *)vartable.buffer, vartable.pbuffer-vartable.buffer);
863 fputbytes(out, endsection, sizeof(endsection));
864 fputbytes(out, filetrailer, sizeof(filetrailer));