2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, 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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
95 static void lkerr(char *);
97 struct cmdname cmdnames
[] = {
98 { "back-bracket", A_B_BRACKET
},
99 { "back-line", A_B_LINE
},
100 { "back-line-force", A_BF_LINE
},
101 { "back-screen", A_B_SCREEN
},
102 { "back-scroll", A_B_SCROLL
},
103 { "back-search", A_B_SEARCH
},
104 { "back-window", A_B_WINDOW
},
105 { "debug", A_DEBUG
},
106 { "digit", A_DIGIT
},
107 { "display-flag", A_DISP_OPTION
},
108 { "display-option", A_DISP_OPTION
},
110 { "examine", A_EXAMINE
},
111 { "filter", A_FILTER
},
112 { "first-cmd", A_FIRSTCMD
},
113 { "firstcmd", A_FIRSTCMD
},
114 { "flush-repaint", A_FREPAINT
},
115 { "forw-bracket", A_F_BRACKET
},
116 { "forw-forever", A_F_FOREVER
},
117 { "forw-until-hilite", A_F_UNTIL_HILITE
},
118 { "forw-line", A_F_LINE
},
119 { "forw-line-force", A_FF_LINE
},
120 { "forw-screen", A_F_SCREEN
},
121 { "forw-screen-force", A_FF_SCREEN
},
122 { "forw-scroll", A_F_SCROLL
},
123 { "forw-search", A_F_SEARCH
},
124 { "forw-skip", A_F_SKIP
},
125 { "forw-window", A_F_WINDOW
},
126 { "goto-end", A_GOEND
},
127 { "goto-line", A_GOLINE
},
128 { "goto-mark", A_GOMARK
},
130 { "index-file", A_INDEX_FILE
},
131 { "invalid", A_UINVALID
},
132 { "left-scroll", A_LSHIFT
},
133 { "next-file", A_NEXT_FILE
},
134 { "next-tag", A_NEXT_TAG
},
135 { "noaction", A_NOACTION
},
136 { "percent", A_PERCENT
},
138 { "prev-file", A_PREV_FILE
},
139 { "prev-tag", A_PREV_TAG
},
141 { "remove-file", A_REMOVE_FILE
},
142 { "repaint", A_REPAINT
},
143 { "repaint-flush", A_FREPAINT
},
144 { "repeat-search", A_AGAIN_SEARCH
},
145 { "repeat-search-all", A_T_AGAIN_SEARCH
},
146 { "reverse-search", A_REVERSE_SEARCH
},
147 { "reverse-search-all", A_T_REVERSE_SEARCH
},
148 { "right-scroll", A_RSHIFT
},
149 { "set-mark", A_SETMARK
},
150 { "status", A_STAT
},
151 { "toggle-flag", A_OPT_TOGGLE
},
152 { "toggle-option", A_OPT_TOGGLE
},
153 { "undo-hilite", A_UNDO_SEARCH
},
154 { "version", A_VERSION
},
155 { "visual", A_VISUAL
},
159 struct cmdname editnames
[] = {
160 { "back-complete", EC_B_COMPLETE
},
161 { "backspace", EC_BACKSPACE
},
162 { "delete", EC_DELETE
},
165 { "expand", EC_EXPAND
},
166 { "forw-complete", EC_F_COMPLETE
},
168 { "insert", EC_INSERT
},
169 { "invalid", EC_UINVALID
},
170 { "kill-line", EC_LINEKILL
},
171 { "abort", EC_ABORT
},
173 { "literal", EC_LITERAL
},
174 { "right", EC_RIGHT
},
176 { "word-backspace", EC_W_BACKSPACE
},
177 { "word-delete", EC_W_DELETE
},
178 { "word-left", EC_W_LEFT
},
179 { "word-right", EC_W_RIGHT
},
184 struct cmdname
*names
;
186 char buffer
[MAX_USERCMD
];
189 struct table cmdtable
;
190 struct table edittable
;
191 struct table vartable
;
192 struct table
*currtable
= &cmdtable
;
194 char fileheader
[] = {
200 char filetrailer
[] = {
201 C0_END_LESSKEY_MAGIC
,
202 C1_END_LESSKEY_MAGIC
,
205 char cmdsection
[1] = { CMD_SECTION
};
206 char editsection
[1] = { EDIT_SECTION
};
207 char varsection
[1] = { VAR_SECTION
};
208 char endsection
[1] = { END_SECTION
};
211 char *outfile
= NULL
;
216 extern char version
[];
221 (void) fprintf(stderr
, "usage: lesskey [-o output] [input]\n");
226 mkpathname(char *dirname
, char *filename
)
231 len
= strlen(dirname
) + strlen(filename
) + 2;
232 pathname
= calloc(1, len
);
233 if (pathname
== NULL
) {
234 fprintf(stderr
, "mkpathname: out of memory\n");
237 (void) snprintf(pathname
, len
, "%s/%s", dirname
, filename
);
242 * Figure out the name of a default file (in the user's HOME directory).
245 homefile(char *filename
)
250 if ((p
= getenv("HOME")) != NULL
&& *p
!= '\0') {
251 pathname
= mkpathname(p
, filename
);
253 (void) fprintf(stderr
, "cannot find $HOME - "
254 "using current directory\n");
255 pathname
= mkpathname(".", filename
);
261 * Parse command line arguments.
264 parse_args(int argc
, char **argv
)
272 /* Arg does not start with "-"; it's not an option. */
275 /* "-" means standard input. */
277 if (arg
[1] == '-' && arg
[2] == '\0') {
278 /* "--" means end of options. */
285 if (strncmp(arg
, "--output", 8) == 0) {
288 else if (arg
[8] == '=')
294 if (strcmp(arg
, "--version") == 0) {
300 outfile
= &argv
[0][2];
302 if (*outfile
== '\0') {
310 (void) printf("lesskey version %s\n", version
);
319 * Open the input file, or use DEF_LESSKEYINFILE if none specified.
324 infile
= homefile(DEF_LESSKEYINFILE
);
328 * Initialize data structures.
333 cmdtable
.names
= cmdnames
;
334 cmdtable
.pbuffer
= cmdtable
.buffer
;
336 edittable
.names
= editnames
;
337 edittable
.pbuffer
= edittable
.buffer
;
339 vartable
.names
= NULL
;
340 vartable
.pbuffer
= vartable
.buffer
;
344 * Parse one character of a string.
347 tstr(char **pp
, int xlate
)
353 static char tstr_control_k
[] =
354 { SK_SPECIAL_KEY
, SK_CONTROL_K
, 6, 1, 1, 1, '\0' };
361 case '0': case '1': case '2': case '3':
362 case '4': case '5': case '6': case '7':
364 * Parse an octal number.
369 ch
= 8*ch
+ (*p
- '0');
370 while (*++p
>= '0' && *p
<= '7' && ++i
< 3)
373 if (xlate
&& ch
== CONTROL('K'))
374 return (tstr_control_k
);
398 case 'u': ch
= SK_UP_ARROW
; break;
399 case 'd': ch
= SK_DOWN_ARROW
; break;
400 case 'r': ch
= SK_RIGHT_ARROW
; break;
401 case 'l': ch
= SK_LEFT_ARROW
; break;
402 case 'U': ch
= SK_PAGE_UP
; break;
403 case 'D': ch
= SK_PAGE_DOWN
; break;
404 case 'h': ch
= SK_HOME
; break;
405 case 'e': ch
= SK_END
; break;
406 case 'x': ch
= SK_DELETE
; break;
408 lkerr("illegal char after \\k");
413 buf
[0] = SK_SPECIAL_KEY
;
425 * Backslash followed by any other char
426 * just means that char.
431 if (xlate
&& buf
[0] == CONTROL('K'))
432 return (tstr_control_k
);
437 * Caret means CONTROL.
440 buf
[0] = CONTROL(p
[1]);
442 if (buf
[0] == CONTROL('K'))
443 return (tstr_control_k
);
449 if (xlate
&& buf
[0] == CONTROL('K'))
450 return (tstr_control_k
);
455 * Skip leading spaces in a string.
460 while (*s
== ' ' || *s
== '\t')
466 * Skip non-space characters in a string.
471 while (*s
!= '\0' && *s
!= ' ' && *s
!= '\t')
477 * Clean up an input line:
478 * strip off the trailing newline & any trailing # comment.
486 for (i
= 0; s
[i
] != '\n' && s
[i
] != '\r' && s
[i
] != '\0'; i
++)
487 if (s
[i
] == '#' && (i
== 0 || s
[i
-1] != '\\'))
494 * Add a byte to the output command table.
499 if (currtable
->pbuffer
>= currtable
->buffer
+ MAX_USERCMD
) {
500 lkerr("too many commands");
503 *(currtable
->pbuffer
)++ = (char)c
;
507 * Add a string to the output command table.
512 for (; *s
!= '\0'; s
++)
517 * See if we have a special "control" line.
520 control_line(char *s
)
522 #define PREFIX(str, pat) (strncmp(str, pat, strlen(pat)) == 0)
524 if (PREFIX(s
, "#line-edit")) {
525 currtable
= &edittable
;
528 if (PREFIX(s
, "#command")) {
529 currtable
= &cmdtable
;
532 if (PREFIX(s
, "#env")) {
533 currtable
= &vartable
;
536 if (PREFIX(s
, "#stop")) {
538 add_cmd_char(A_END_LIST
);
548 fputbytes(FILE *fd
, char *buf
, int len
)
551 (void) fwrite(buf
, sizeof (char), 1, fd
);
557 * Output an integer, in special KRADIX form.
560 fputint(FILE *fd
, unsigned int val
)
564 if (val
>= KRADIX
*KRADIX
) {
565 (void) fprintf(stderr
, "error: integer too big (%d > %d)\n",
570 (void) fwrite(&c
, sizeof (char), 1, fd
);
572 (void) fwrite(&c
, sizeof (char), 1, fd
);
576 * Find an action, given the name of the action.
579 findaction(char *actname
)
583 for (i
= 0; currtable
->names
[i
].cn_name
!= NULL
; i
++)
584 if (strcmp(currtable
->names
[i
].cn_name
, actname
) == 0)
585 return (currtable
->names
[i
].cn_action
);
586 lkerr("unknown action");
593 (void) fprintf(stderr
, "line %d: %s\n", linenum
, s
);
599 parse_cmdline(char *p
)
608 * Parse the command string and store it in the current table.
614 if (cmdlen
> MAX_CMDLEN
)
615 lkerr("command too long");
618 } while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0');
620 * Terminate the command string with a null byte.
625 * Skip white space between the command string
626 * and the action name.
627 * Terminate the action name with a null byte.
631 lkerr("missing action");
640 * Parse the action name and store it in the current table.
642 action
= findaction(actname
);
645 * See if an extra string follows the action name.
650 add_cmd_char(action
);
653 * OR the special value A_EXTRA into the action byte.
654 * Put the extra string after the action byte.
656 add_cmd_char(action
| A_EXTRA
);
658 add_cmd_str(tstr(&p
, 0));
664 parse_varline(char *p
)
671 } while (*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '\0');
673 * Terminate the variable name with a null byte.
683 add_cmd_char(EV_OK
|A_EXTRA
);
694 * Parse a line from the lesskey file.
697 parse_line(char *line
)
702 * See if it is a control line.
704 if (control_line(line
))
707 * Skip leading white space.
708 * Replace the final newline with a null byte.
709 * Ignore blank lines and comments.
711 p
= clean_line(line
);
715 if (currtable
== &vartable
)
722 main(int argc
, char **argv
)
728 if (pledge("stdio rpath wpath cpath", NULL
) == -1)
732 * Process command line arguments.
734 parse_args(argc
, argv
);
738 * Open the input file.
740 if (strcmp(infile
, "-") == 0)
742 else if ((desc
= fopen(infile
, "r")) == NULL
) {
748 * Read and parse the input file, one line at a time.
752 while (fgets(line
, sizeof (line
), desc
) != NULL
) {
759 * Write the output file.
760 * If no output file was specified, use "$HOME/.less"
763 (void) fprintf(stderr
, "%d errors; no output produced\n",
769 outfile
= getenv("LESSKEY");
771 outfile
= homefile(LESSKEYFILE
);
772 if ((out
= fopen(outfile
, "wb")) == NULL
) {
778 fputbytes(out
, fileheader
, sizeof (fileheader
));
780 /* Command key section */
781 fputbytes(out
, cmdsection
, sizeof (cmdsection
));
782 fputint(out
, cmdtable
.pbuffer
- cmdtable
.buffer
);
783 fputbytes(out
, (char *)cmdtable
.buffer
,
784 cmdtable
.pbuffer
-cmdtable
.buffer
);
786 /* Edit key section */
787 fputbytes(out
, editsection
, sizeof (editsection
));
788 fputint(out
, edittable
.pbuffer
- edittable
.buffer
);
789 fputbytes(out
, (char *)edittable
.buffer
,
790 edittable
.pbuffer
-edittable
.buffer
);
792 /* Environment variable section */
793 fputbytes(out
, varsection
, sizeof (varsection
));
794 fputint(out
, vartable
.pbuffer
- vartable
.buffer
);
795 fputbytes(out
, (char *)vartable
.buffer
,
796 vartable
.pbuffer
-vartable
.buffer
);
799 fputbytes(out
, endsection
, sizeof (endsection
));
800 fputbytes(out
, filetrailer
, sizeof (filetrailer
));