2 * Copyright (C) 1984-2012 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, see the README file.
12 * lesskey [-o output] [input]
14 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
17 * If no input file is specified, standard input is used.
18 * If no output file is specified, $HOME/.less is used.
20 * The .less file is used to specify (to "less") user-defined
21 * key bindings. Basically any sequence of 1 to MAX_CMDLEN
22 * keystrokes may be bound to an existing less function.
24 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
26 * The input file is an ascii file consisting of a
27 * sequence of lines of the form:
28 * string <whitespace> action [chars] <newline>
30 * "string" is a sequence of command characters which form
31 * the new user-defined command. The command
33 * 1. The actual character itself.
34 * 2. A character preceded by ^ to specify a
35 * control character (e.g. ^X means control-X).
36 * 3. A backslash followed by one to three octal digits
37 * to specify a character by its octal value.
38 * 4. A backslash followed by b, e, n, r or t
39 * to specify \b, ESC, \n, \r or \t, respectively.
40 * 5. Any character (other than those mentioned above) preceded
41 * by a \ to specify the character itself (characters which
42 * must be preceded by \ include ^, \, and whitespace.
43 * "action" is the name of a "less" action, from the table below.
44 * "chars" is an optional sequence of characters which is treated
45 * as keyboard input after the command is executed.
47 * Blank lines and lines which start with # are ignored,
48 * except for the special control lines:
49 * #command Signals the beginning of the command
51 * #line-edit Signals the beginning of the line-editing
53 * #env Signals the beginning of the environment
55 * #stop Stops command parsing in less;
56 * causes all default keys to be disabled.
58 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
60 * The output file is a non-ascii file, consisting of a header,
61 * one or more sections, and a trailer.
62 * Each section begins with a section header, a section length word
63 * and the section data. Normally there are three sections:
64 * CMD_SECTION Definition of command keys.
65 * EDIT_SECTION Definition of editing keys.
66 * END_SECTION A special section header, with no
67 * length word or section data.
69 * Section data consists of zero or more byte sequences of the form:
72 * string <0> <action|A_EXTRA> chars <0>
74 * "string" is the command string.
75 * "<0>" is one null byte.
76 * "<action>" is one byte containing the action code (the A_xxx value).
77 * If action is ORed with A_EXTRA, the action byte is followed
78 * by the null-terminated "chars" string.
80 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
93 struct cmdname cmdnames
[] =
95 { "back-bracket", A_B_BRACKET
},
96 { "back-line", A_B_LINE
},
97 { "back-line-force", A_BF_LINE
},
98 { "back-screen", A_B_SCREEN
},
99 { "back-scroll", A_B_SCROLL
},
100 { "back-search", A_B_SEARCH
},
101 { "back-window", A_B_WINDOW
},
102 { "debug", A_DEBUG
},
103 { "digit", A_DIGIT
},
104 { "display-flag", A_DISP_OPTION
},
105 { "display-option", A_DISP_OPTION
},
107 { "examine", A_EXAMINE
},
108 { "filter", A_FILTER
},
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-until-hilite", A_F_UNTIL_HILITE
},
115 { "forw-line", A_F_LINE
},
116 { "forw-line-force", A_FF_LINE
},
117 { "forw-screen", A_F_SCREEN
},
118 { "forw-screen-force", A_FF_SCREEN
},
119 { "forw-scroll", A_F_SCROLL
},
120 { "forw-search", A_F_SEARCH
},
121 { "forw-window", A_F_WINDOW
},
122 { "goto-end", A_GOEND
},
123 { "goto-line", A_GOLINE
},
124 { "goto-mark", A_GOMARK
},
126 { "index-file", A_INDEX_FILE
},
127 { "invalid", A_UINVALID
},
128 { "left-scroll", A_LSHIFT
},
129 { "next-file", A_NEXT_FILE
},
130 { "next-tag", A_NEXT_TAG
},
131 { "noaction", A_NOACTION
},
132 { "percent", A_PERCENT
},
134 { "prev-file", A_PREV_FILE
},
135 { "prev-tag", A_PREV_TAG
},
137 { "remove-file", A_REMOVE_FILE
},
138 { "repaint", A_REPAINT
},
139 { "repaint-flush", A_FREPAINT
},
140 { "repeat-search", A_AGAIN_SEARCH
},
141 { "repeat-search-all", A_T_AGAIN_SEARCH
},
142 { "reverse-search", A_REVERSE_SEARCH
},
143 { "reverse-search-all", A_T_REVERSE_SEARCH
},
144 { "right-scroll", A_RSHIFT
},
145 { "set-mark", A_SETMARK
},
146 { "shell", A_SHELL
},
147 { "status", A_STAT
},
148 { "toggle-flag", A_OPT_TOGGLE
},
149 { "toggle-option", A_OPT_TOGGLE
},
150 { "undo-hilite", A_UNDO_SEARCH
},
151 { "version", A_VERSION
},
152 { "visual", A_VISUAL
},
156 struct cmdname editnames
[] =
158 { "back-complete", EC_B_COMPLETE
},
159 { "backspace", EC_BACKSPACE
},
160 { "delete", EC_DELETE
},
163 { "expand", EC_EXPAND
},
164 { "forw-complete", EC_F_COMPLETE
},
166 { "insert", EC_INSERT
},
167 { "invalid", EC_UINVALID
},
168 { "kill-line", EC_LINEKILL
},
169 { "abort", EC_ABORT
},
171 { "literal", EC_LITERAL
},
172 { "right", EC_RIGHT
},
174 { "word-backspace", EC_W_BACKSPACE
},
175 { "word-delete", EC_W_DELETE
},
176 { "word-left", EC_W_LEFT
},
177 { "word-right", EC_W_RIGHT
},
183 struct cmdname
*names
;
185 char buffer
[MAX_USERCMD
];
188 struct table cmdtable
;
189 struct table edittable
;
190 struct table vartable
;
191 struct table
*currtable
= &cmdtable
;
193 char fileheader
[] = {
199 char filetrailer
[] = {
200 C0_END_LESSKEY_MAGIC
,
201 C1_END_LESSKEY_MAGIC
,
204 char cmdsection
[1] = { CMD_SECTION
};
205 char editsection
[1] = { EDIT_SECTION
};
206 char varsection
[1] = { VAR_SECTION
};
207 char endsection
[1] = { END_SECTION
};
210 char *outfile
= NULL
;
215 extern char version
[];
220 fprintf(stderr
, "usage: lesskey [-o output] [input]\n");
225 mkpathname(dirname
, filename
)
231 pathname
= calloc(strlen(dirname
) + strlen(filename
) + 2, sizeof(char));
232 strcpy(pathname
, dirname
);
233 strcat(pathname
, PATHNAME_SEP
);
234 strcat(pathname
, filename
);
239 * Figure out the name of a default file (in the user's HOME directory).
248 if ((p
= getenv("HOME")) != NULL
&& *p
!= '\0')
249 pathname
= mkpathname(p
, filename
);
251 else if ((p
= getenv("INIT")) != NULL
&& *p
!= '\0')
252 pathname
= mkpathname(p
, filename
);
256 fprintf(stderr
, "cannot find $HOME - using current directory\n");
257 pathname
= mkpathname(".", filename
);
263 * Parse command line arguments.
266 parse_args(argc
, argv
)
277 /* Arg does not start with "-"; it's not an option. */
280 /* "-" means standard input. */
282 if (arg
[1] == '-' && arg
[2] == '\0')
284 /* "--" means end of options. */
292 if (strncmp(arg
, "--output", 8) == 0)
296 else if (arg
[8] == '=')
302 if (strcmp(arg
, "--version") == 0)
309 outfile
= &argv
[0][2];
311 if (*outfile
== '\0')
320 printf("lesskey version %s\n", version
);
329 * Open the input file, or use DEF_LESSKEYINFILE if none specified.
334 infile
= homefile(DEF_LESSKEYINFILE
);
338 * Initialize data structures.
343 cmdtable
.names
= cmdnames
;
344 cmdtable
.pbuffer
= cmdtable
.buffer
;
346 edittable
.names
= editnames
;
347 edittable
.pbuffer
= edittable
.buffer
;
349 vartable
.names
= NULL
;
350 vartable
.pbuffer
= vartable
.buffer
;
354 * Parse one character of a string.
365 static char tstr_control_k
[] =
366 { SK_SPECIAL_KEY
, SK_CONTROL_K
, 6, 1, 1, 1, '\0' };
375 case '0': case '1': case '2': case '3':
376 case '4': case '5': case '6': case '7':
378 * Parse an octal number.
383 ch
= 8*ch
+ (*p
- '0');
384 while (*++p
>= '0' && *p
<= '7' && ++i
< 3);
386 if (xlate
&& ch
== CONTROL('K'))
387 return tstr_control_k
;
413 case 'u': ch
= SK_UP_ARROW
; break;
414 case 'd': ch
= SK_DOWN_ARROW
; break;
415 case 'r': ch
= SK_RIGHT_ARROW
; break;
416 case 'l': ch
= SK_LEFT_ARROW
; break;
417 case 'U': ch
= SK_PAGE_UP
; break;
418 case 'D': ch
= SK_PAGE_DOWN
; break;
419 case 'h': ch
= SK_HOME
; break;
420 case 'e': ch
= SK_END
; break;
421 case 'x': ch
= SK_DELETE
; break;
423 error("illegal char after \\k");
428 buf
[0] = SK_SPECIAL_KEY
;
440 * Backslash followed by any other char
441 * just means that char.
446 if (xlate
&& buf
[0] == CONTROL('K'))
447 return tstr_control_k
;
452 * Caret means CONTROL.
455 buf
[0] = CONTROL(p
[1]);
457 if (buf
[0] == CONTROL('K'))
458 return tstr_control_k
;
464 if (xlate
&& buf
[0] == CONTROL('K'))
465 return tstr_control_k
;
470 * Skip leading spaces in a string.
476 while (*s
== ' ' || *s
== '\t')
482 * Skip non-space characters in a string.
488 while (*s
!= '\0' && *s
!= ' ' && *s
!= '\t')
494 * Clean up an input line:
495 * strip off the trailing newline & any trailing # comment.
504 for (i
= 0; s
[i
] != '\n' && s
[i
] != '\r' && s
[i
] != '\0'; i
++)
505 if (s
[i
] == '#' && (i
== 0 || s
[i
-1] != '\\'))
512 * Add a byte to the output command table.
518 if (currtable
->pbuffer
>= currtable
->buffer
+ MAX_USERCMD
)
520 error("too many commands");
523 *(currtable
->pbuffer
)++ = c
;
527 * Add a string to the output command table.
533 for ( ; *s
!= '\0'; s
++)
538 * See if we have a special "control" line.
544 #define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
546 if (PREFIX(s
, "#line-edit"))
548 currtable
= &edittable
;
551 if (PREFIX(s
, "#command"))
553 currtable
= &cmdtable
;
556 if (PREFIX(s
, "#env"))
558 currtable
= &vartable
;
561 if (PREFIX(s
, "#stop"))
564 add_cmd_char(A_END_LIST
);
574 fputbytes(fd
, buf
, len
)
581 fwrite(buf
, sizeof(char), 1, fd
);
587 * Output an integer, in special KRADIX form.
596 if (val
>= KRADIX
*KRADIX
)
598 fprintf(stderr
, "error: integer too big (%d > %d)\n",
603 fwrite(&c
, sizeof(char), 1, fd
);
605 fwrite(&c
, sizeof(char), 1, fd
);
609 * Find an action, given the name of the action.
617 for (i
= 0; currtable
->names
[i
].cn_name
!= NULL
; i
++)
618 if (strcmp(currtable
->names
[i
].cn_name
, actname
) == 0)
619 return (currtable
->names
[i
].cn_action
);
620 error("unknown action");
628 fprintf(stderr
, "line %d: %s\n", linenum
, s
);
644 * Parse the command string and store it in the current table.
651 if (cmdlen
> MAX_CMDLEN
)
652 error("command too long");
655 } while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0');
657 * Terminate the command string with a null byte.
662 * Skip white space between the command string
663 * and the action name.
664 * Terminate the action name with a null byte.
669 error("missing action");
678 * Parse the action name and store it in the current table.
680 action
= findaction(actname
);
683 * See if an extra string follows the action name.
689 add_cmd_char(action
);
693 * OR the special value A_EXTRA into the action byte.
694 * Put the extra string after the action byte.
696 add_cmd_char(action
| A_EXTRA
);
698 add_cmd_str(tstr(&p
, 0));
713 } while (*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '\0');
715 * Terminate the variable name with a null byte.
726 add_cmd_char(EV_OK
|A_EXTRA
);
738 * Parse a line from the lesskey file.
747 * See if it is a control line.
749 if (control_line(line
))
752 * Skip leading white space.
753 * Replace the final newline with a null byte.
754 * Ignore blank lines and comments.
756 p
= clean_line(line
);
760 if (currtable
== &vartable
)
776 if (getenv("HOME") == NULL
)
779 * If there is no HOME environment variable,
780 * try the concatenation of HOMEDRIVE + HOMEPATH.
782 char *drive
= getenv("HOMEDRIVE");
783 char *path
= getenv("HOMEPATH");
784 if (drive
!= NULL
&& path
!= NULL
)
786 char *env
= (char *) calloc(strlen(drive
) +
787 strlen(path
) + 6, sizeof(char));
788 strcpy(env
, "HOME=");
797 * Process command line arguments.
799 parse_args(argc
, argv
);
803 * Open the input file.
805 if (strcmp(infile
, "-") == 0)
807 else if ((desc
= fopen(infile
, "r")) == NULL
)
812 fprintf(stderr
, "Cannot open %s\n", infile
);
818 * Read and parse the input file, one line at a time.
822 while (fgets(line
, sizeof(line
), desc
) != NULL
)
829 * Write the output file.
830 * If no output file was specified, use "$HOME/.less"
834 fprintf(stderr
, "%d errors; no output produced\n", errors
);
839 outfile
= getenv("LESSKEY");
841 outfile
= homefile(LESSKEYFILE
);
842 if ((out
= fopen(outfile
, "wb")) == NULL
)
847 fprintf(stderr
, "Cannot open %s\n", outfile
);
853 fputbytes(out
, fileheader
, sizeof(fileheader
));
855 /* Command key section */
856 fputbytes(out
, cmdsection
, sizeof(cmdsection
));
857 fputint(out
, cmdtable
.pbuffer
- cmdtable
.buffer
);
858 fputbytes(out
, (char *)cmdtable
.buffer
, cmdtable
.pbuffer
-cmdtable
.buffer
);
859 /* Edit key section */
860 fputbytes(out
, editsection
, sizeof(editsection
));
861 fputint(out
, edittable
.pbuffer
- edittable
.buffer
);
862 fputbytes(out
, (char *)edittable
.buffer
, edittable
.pbuffer
-edittable
.buffer
);
864 /* Environment variable section */
865 fputbytes(out
, varsection
, sizeof(varsection
));
866 fputint(out
, vartable
.pbuffer
- vartable
.buffer
);
867 fputbytes(out
, (char *)vartable
.buffer
, vartable
.pbuffer
-vartable
.buffer
);
870 fputbytes(out
, endsection
, sizeof(endsection
));
871 fputbytes(out
, filetrailer
, sizeof(filetrailer
));