1 /* $NetBSD: lesskey.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
14 * lesskey [-o output] [input]
16 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
19 * If no input file is specified, standard input is used.
20 * If no output file is specified, $HOME/.less is used.
22 * The .less file is used to specify (to "less") user-defined
23 * key bindings. Basically any sequence of 1 to MAX_CMDLEN
24 * keystrokes may be bound to an existing less function.
26 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
28 * The input file is an ascii file consisting of a
29 * sequence of lines of the form:
30 * string <whitespace> action [chars] <newline>
32 * "string" is a sequence of command characters which form
33 * the new user-defined command. The command
35 * 1. The actual character itself.
36 * 2. A character preceded by ^ to specify a
37 * control character (e.g. ^X means control-X).
38 * 3. A backslash followed by one to three octal digits
39 * to specify a character by its octal value.
40 * 4. A backslash followed by b, e, n, r or t
41 * to specify \b, ESC, \n, \r or \t, respectively.
42 * 5. Any character (other than those mentioned above) preceded
43 * by a \ to specify the character itself (characters which
44 * must be preceded by \ include ^, \, and whitespace.
45 * "action" is the name of a "less" action, from the table below.
46 * "chars" is an optional sequence of characters which is treated
47 * as keyboard input after the command is executed.
49 * Blank lines and lines which start with # are ignored,
50 * except for the special control lines:
51 * #command Signals the beginning of the command
53 * #line-edit Signals the beginning of the line-editing
55 * #env Signals the beginning of the environment
57 * #stop Stops command parsing in less;
58 * causes all default keys to be disabled.
60 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
62 * The output file is a non-ascii file, consisting of a header,
63 * one or more sections, and a trailer.
64 * Each section begins with a section header, a section length word
65 * and the section data. Normally there are three sections:
66 * CMD_SECTION Definition of command keys.
67 * EDIT_SECTION Definition of editing keys.
68 * END_SECTION A special section header, with no
69 * length word or section data.
71 * Section data consists of zero or more byte sequences of the form:
74 * string <0> <action|A_EXTRA> chars <0>
76 * "string" is the command string.
77 * "<0>" is one null byte.
78 * "<action>" is one byte containing the action code (the A_xxx value).
79 * If action is ORed with A_EXTRA, the action byte is followed
80 * by the null-terminated "chars" string.
82 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
95 struct cmdname cmdnames
[] =
97 { "back-bracket", A_B_BRACKET
},
98 { "back-line", A_B_LINE
},
99 { "back-line-force", A_BF_LINE
},
100 { "back-screen", A_B_SCREEN
},
101 { "back-scroll", A_B_SCROLL
},
102 { "back-search", A_B_SEARCH
},
103 { "back-window", A_B_WINDOW
},
104 { "debug", A_DEBUG
},
105 { "digit", A_DIGIT
},
106 { "display-flag", A_DISP_OPTION
},
107 { "display-option", A_DISP_OPTION
},
109 { "examine", A_EXAMINE
},
110 { "filter", A_FILTER
},
111 { "first-cmd", A_FIRSTCMD
},
112 { "firstcmd", A_FIRSTCMD
},
113 { "flush-repaint", A_FREPAINT
},
114 { "forw-bracket", A_F_BRACKET
},
115 { "forw-forever", A_F_FOREVER
},
116 { "forw-until-hilite", A_F_UNTIL_HILITE
},
117 { "forw-line", A_F_LINE
},
118 { "forw-line-force", A_FF_LINE
},
119 { "forw-screen", A_F_SCREEN
},
120 { "forw-screen-force", A_FF_SCREEN
},
121 { "forw-scroll", A_F_SCROLL
},
122 { "forw-search", A_F_SEARCH
},
123 { "forw-window", A_F_WINDOW
},
124 { "goto-end", A_GOEND
},
125 { "goto-line", A_GOLINE
},
126 { "goto-mark", A_GOMARK
},
128 { "index-file", A_INDEX_FILE
},
129 { "invalid", A_UINVALID
},
130 { "left-scroll", A_LSHIFT
},
131 { "next-file", A_NEXT_FILE
},
132 { "next-tag", A_NEXT_TAG
},
133 { "noaction", A_NOACTION
},
134 { "percent", A_PERCENT
},
136 { "prev-file", A_PREV_FILE
},
137 { "prev-tag", A_PREV_TAG
},
139 { "remove-file", A_REMOVE_FILE
},
140 { "repaint", A_REPAINT
},
141 { "repaint-flush", A_FREPAINT
},
142 { "repeat-search", A_AGAIN_SEARCH
},
143 { "repeat-search-all", A_T_AGAIN_SEARCH
},
144 { "reverse-search", A_REVERSE_SEARCH
},
145 { "reverse-search-all", A_T_REVERSE_SEARCH
},
146 { "right-scroll", A_RSHIFT
},
147 { "set-mark", A_SETMARK
},
148 { "shell", A_SHELL
},
149 { "status", A_STAT
},
150 { "toggle-flag", A_OPT_TOGGLE
},
151 { "toggle-option", A_OPT_TOGGLE
},
152 { "undo-hilite", A_UNDO_SEARCH
},
153 { "version", A_VERSION
},
154 { "visual", A_VISUAL
},
158 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
},
185 struct cmdname
*names
;
187 char buffer
[MAX_USERCMD
];
190 struct table cmdtable
;
191 struct table edittable
;
192 struct table vartable
;
193 struct table
*currtable
= &cmdtable
;
195 char fileheader
[] = {
201 char filetrailer
[] = {
202 C0_END_LESSKEY_MAGIC
,
203 C1_END_LESSKEY_MAGIC
,
206 char cmdsection
[1] = { CMD_SECTION
};
207 char editsection
[1] = { EDIT_SECTION
};
208 char varsection
[1] = { VAR_SECTION
};
209 char endsection
[1] = { END_SECTION
};
212 char *outfile
= NULL
;
219 extern char version
[];
224 fprintf(stderr
, "usage: lesskey [-o output] [input]\n");
229 mkpathname(dirname
, filename
)
235 pathname
= calloc(strlen(dirname
) + strlen(filename
) + 2, sizeof(char));
236 strcpy(pathname
, dirname
);
237 strcat(pathname
, PATHNAME_SEP
);
238 strcat(pathname
, filename
);
243 * Figure out the name of a default file (in the user's HOME directory).
252 if ((p
= getenv("HOME")) != NULL
&& *p
!= '\0')
253 pathname
= mkpathname(p
, filename
);
255 else if ((p
= getenv("INIT")) != NULL
&& *p
!= '\0')
256 pathname
= mkpathname(p
, filename
);
260 fprintf(stderr
, "cannot find $HOME - using current directory\n");
261 pathname
= mkpathname(".", filename
);
267 * Parse command line arguments.
270 parse_args(argc
, argv
)
281 /* Arg does not start with "-"; it's not an option. */
284 /* "-" means standard input. */
286 if (arg
[1] == '-' && arg
[2] == '\0')
288 /* "--" means end of options. */
296 if (strncmp(arg
, "--output", 8) == 0)
300 else if (arg
[8] == '=')
306 if (strcmp(arg
, "--version") == 0)
313 outfile
= &argv
[0][2];
315 if (*outfile
== '\0')
324 printf("lesskey version %s\n", version
);
333 * Open the input file, or use DEF_LESSKEYINFILE if none specified.
338 infile
= homefile(DEF_LESSKEYINFILE
);
342 * Initialize data structures.
347 cmdtable
.names
= cmdnames
;
348 cmdtable
.pbuffer
= cmdtable
.buffer
;
350 edittable
.names
= editnames
;
351 edittable
.pbuffer
= edittable
.buffer
;
353 vartable
.names
= NULL
;
354 vartable
.pbuffer
= vartable
.buffer
;
358 * Parse one character of a string.
369 static char tstr_control_k
[] =
370 { SK_SPECIAL_KEY
, SK_CONTROL_K
, 6, 1, 1, 1, '\0' };
379 case '0': case '1': case '2': case '3':
380 case '4': case '5': case '6': case '7':
382 * Parse an octal number.
387 ch
= 8*ch
+ (*p
- '0');
388 while (*++p
>= '0' && *p
<= '7' && ++i
< 3);
390 if (xlate
&& ch
== CONTROL('K'))
391 return tstr_control_k
;
417 case 'u': ch
= SK_UP_ARROW
; break;
418 case 'd': ch
= SK_DOWN_ARROW
; break;
419 case 'r': ch
= SK_RIGHT_ARROW
; break;
420 case 'l': ch
= SK_LEFT_ARROW
; break;
421 case 'U': ch
= SK_PAGE_UP
; break;
422 case 'D': ch
= SK_PAGE_DOWN
; break;
423 case 'h': ch
= SK_HOME
; break;
424 case 'e': ch
= SK_END
; break;
425 case 'x': ch
= SK_DELETE
; break;
427 terror("illegal char after \\k");
432 buf
[0] = SK_SPECIAL_KEY
;
444 * Backslash followed by any other char
445 * just means that char.
450 if (xlate
&& buf
[0] == CONTROL('K'))
451 return tstr_control_k
;
456 * Caret means CONTROL.
459 buf
[0] = CONTROL(p
[1]);
461 if (buf
[0] == CONTROL('K'))
462 return tstr_control_k
;
468 if (xlate
&& buf
[0] == CONTROL('K'))
469 return tstr_control_k
;
474 * Skip leading spaces in a string.
480 while (*s
== ' ' || *s
== '\t')
486 * Skip non-space characters in a string.
492 while (*s
!= '\0' && *s
!= ' ' && *s
!= '\t')
498 * Clean up an input line:
499 * strip off the trailing newline & any trailing # comment.
508 for (i
= 0; s
[i
] != '\n' && s
[i
] != '\r' && s
[i
] != '\0'; i
++)
509 if (s
[i
] == '#' && (i
== 0 || s
[i
-1] != '\\'))
516 * Add a byte to the output command table.
522 if (currtable
->pbuffer
>= currtable
->buffer
+ MAX_USERCMD
)
524 terror("too many commands");
527 *(currtable
->pbuffer
)++ = c
;
531 * Add a string to the output command table.
537 for ( ; *s
!= '\0'; s
++)
542 * See if we have a special "control" line.
548 #define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
550 if (PREFIX(s
, "#line-edit"))
552 currtable
= &edittable
;
555 if (PREFIX(s
, "#command"))
557 currtable
= &cmdtable
;
560 if (PREFIX(s
, "#env"))
562 currtable
= &vartable
;
565 if (PREFIX(s
, "#stop"))
568 add_cmd_char(A_END_LIST
);
578 fputbytes(fd
, buf
, len
)
585 fwrite(buf
, sizeof(char), 1, fd
);
591 * Output an integer, in special KRADIX form.
600 if (val
>= KRADIX
*KRADIX
)
602 fprintf(stderr
, "error: integer too big (%d > %d)\n",
607 fwrite(&c
, sizeof(char), 1, fd
);
609 fwrite(&c
, sizeof(char), 1, fd
);
613 * Find an action, given the name of the action.
621 for (i
= 0; currtable
->names
[i
].cn_name
!= NULL
; i
++)
622 if (strcmp(currtable
->names
[i
].cn_name
, actname
) == 0)
623 return (currtable
->names
[i
].cn_action
);
624 terror("unknown action");
632 fprintf(stderr
, "line %d: %s\n", linenum
, s
);
648 * Parse the command string and store it in the current table.
655 if (cmdlen
> MAX_CMDLEN
)
656 terror("command too long");
659 } while (*p
!= ' ' && *p
!= '\t' && *p
!= '\0');
661 * Terminate the command string with a null byte.
666 * Skip white space between the command string
667 * and the action name.
668 * Terminate the action name with a null byte.
673 terror("missing action");
682 * Parse the action name and store it in the current table.
684 action
= findaction(actname
);
687 * See if an extra string follows the action name.
693 add_cmd_char(action
);
697 * OR the special value A_EXTRA into the action byte.
698 * Put the extra string after the action byte.
700 add_cmd_char(action
| A_EXTRA
);
702 add_cmd_str(tstr(&p
, 0));
717 } while (*p
!= ' ' && *p
!= '\t' && *p
!= '=' && *p
!= '\0');
719 * Terminate the variable name with a null byte.
730 add_cmd_char(EV_OK
|A_EXTRA
);
742 * Parse a line from the lesskey file.
751 * See if it is a control line.
753 if (control_line(line
))
756 * Skip leading white space.
757 * Replace the final newline with a null byte.
758 * Ignore blank lines and comments.
760 p
= clean_line(line
);
764 if (currtable
== &vartable
)
780 if (getenv("HOME") == NULL
)
783 * If there is no HOME environment variable,
784 * try the concatenation of HOMEDRIVE + HOMEPATH.
786 char *drive
= getenv("HOMEDRIVE");
787 char *path
= getenv("HOMEPATH");
788 if (drive
!= NULL
&& path
!= NULL
)
790 char *env
= (char *) calloc(strlen(drive
) +
791 strlen(path
) + 6, sizeof(char));
792 strcpy(env
, "HOME=");
801 * Process command line arguments.
803 parse_args(argc
, argv
);
807 * Open the input file.
809 if (strcmp(infile
, "-") == 0)
811 else if ((desc
= fopen(infile
, "r")) == NULL
)
816 fprintf(stderr
, "Cannot open %s\n", infile
);
822 * Read and parse the input file, one line at a time.
826 while (fgets(line
, sizeof(line
), desc
) != NULL
)
833 * Write the output file.
834 * If no output file was specified, use "$HOME/.less"
838 fprintf(stderr
, "%d errors; no output produced\n", errors
);
843 outfile
= getenv("LESSKEY");
845 outfile
= homefile(LESSKEYFILE
);
846 if ((out
= fopen(outfile
, "wb")) == NULL
)
851 fprintf(stderr
, "Cannot open %s\n", outfile
);
857 fputbytes(out
, fileheader
, sizeof(fileheader
));
859 /* Command key section */
860 fputbytes(out
, cmdsection
, sizeof(cmdsection
));
861 fputint(out
, cmdtable
.pbuffer
- cmdtable
.buffer
);
862 fputbytes(out
, (char *)cmdtable
.buffer
, cmdtable
.pbuffer
-cmdtable
.buffer
);
863 /* Edit key section */
864 fputbytes(out
, editsection
, sizeof(editsection
));
865 fputint(out
, edittable
.pbuffer
- edittable
.buffer
);
866 fputbytes(out
, (char *)edittable
.buffer
, edittable
.pbuffer
-edittable
.buffer
);
868 /* Environment variable section */
869 fputbytes(out
, varsection
, sizeof(varsection
));
870 fputint(out
, vartable
.pbuffer
- vartable
.buffer
);
871 fputbytes(out
, (char *)vartable
.buffer
, vartable
.pbuffer
-vartable
.buffer
);
874 fputbytes(out
, endsection
, sizeof(endsection
));
875 fputbytes(out
, filetrailer
, sizeof(filetrailer
));