Annotation of src/usr.bin/less/lesskey.c, Revision 1.10
1.1 etheisen 1: /*
1.7 shadchin 2: * Copyright (C) 1984-2012 Mark Nudelman
1.9 nicm 3: * Modified for use with illumos by Garrett D'Amore.
4: * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.5 millert 5: *
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.
1.1 etheisen 8: *
1.7 shadchin 9: * For more information, see the README file.
1.8 nicm 10: */
1.1 etheisen 11:
12: /*
13: * lesskey [-o output] [input]
14: *
1.8 nicm 15: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1 etheisen 16: *
17: * Make a .less file.
18: * If no input file is specified, standard input is used.
19: * If no output file is specified, $HOME/.less is used.
20: *
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.
24: *
1.8 nicm 25: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1 etheisen 26: *
1.8 nicm 27: * The input file is an ascii file consisting of a
1.1 etheisen 28: * sequence of lines of the form:
29: * string <whitespace> action [chars] <newline>
30: *
31: * "string" is a sequence of command characters which form
32: * the new user-defined command. The command
33: * characters may be:
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.
1.8 nicm 41: * 5. Any character (other than those mentioned above) preceded
1.1 etheisen 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.
47: *
1.8 nicm 48: * Blank lines and lines which start with # are ignored,
1.1 etheisen 49: * except for the special control lines:
1.5 millert 50: * #command Signals the beginning of the command
51: * keys section.
1.1 etheisen 52: * #line-edit Signals the beginning of the line-editing
53: * keys section.
1.5 millert 54: * #env Signals the beginning of the environment
55: * variable section.
1.1 etheisen 56: * #stop Stops command parsing in less;
57: * causes all default keys to be disabled.
58: *
1.8 nicm 59: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1 etheisen 60: *
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.
1.8 nicm 67: * END_SECTION A special section header, with no
1.1 etheisen 68: * length word or section data.
69: *
70: * Section data consists of zero or more byte sequences of the form:
71: * string <0> <action>
72: * or
73: * string <0> <action|A_EXTRA> chars <0>
74: *
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.
80: *
1.8 nicm 81: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1.1 etheisen 82: */
83:
84: #include "less.h"
85: #include "lesskey.h"
86: #include "cmd.h"
87:
88: struct cmdname
89: {
90: char *cn_name;
91: int cn_action;
92: };
93:
1.8 nicm 94: static void lkerr(char *s);
95:
96: struct cmdname cmdnames[] =
1.1 etheisen 97: {
1.8 nicm 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 },
109: { "end", A_GOEND },
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 },
129: { "help", A_HELP },
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 },
137: { "pipe", A_PIPE },
138: { "prev-file", A_PREV_FILE },
139: { "prev-tag", A_PREV_TAG },
140: { "quit", A_QUIT },
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 },
156: { NULL, 0 }
1.1 etheisen 157: };
158:
1.8 nicm 159: struct cmdname editnames[] = {
1.5 millert 160: { "back-complete", EC_B_COMPLETE },
161: { "backspace", EC_BACKSPACE },
162: { "delete", EC_DELETE },
163: { "down", EC_DOWN },
164: { "end", EC_END },
165: { "expand", EC_EXPAND },
166: { "forw-complete", EC_F_COMPLETE },
167: { "home", EC_HOME },
168: { "insert", EC_INSERT },
169: { "invalid", EC_UINVALID },
170: { "kill-line", EC_LINEKILL },
1.6 shadchin 171: { "abort", EC_ABORT },
1.5 millert 172: { "left", EC_LEFT },
173: { "literal", EC_LITERAL },
174: { "right", EC_RIGHT },
175: { "up", EC_UP },
176: { "word-backspace", EC_W_BACKSPACE },
177: { "word-delete", EC_W_DELETE },
178: { "word-left", EC_W_LEFT },
179: { "word-right", EC_W_RIGHT },
180: { NULL, 0 }
1.1 etheisen 181: };
182:
183: struct table
184: {
185: struct cmdname *names;
186: char *pbuffer;
187: char buffer[MAX_USERCMD];
188: };
189:
190: struct table cmdtable;
191: struct table edittable;
1.5 millert 192: struct table vartable;
1.1 etheisen 193: struct table *currtable = &cmdtable;
194:
195: char fileheader[] = {
1.8 nicm 196: C0_LESSKEY_MAGIC,
197: C1_LESSKEY_MAGIC,
198: C2_LESSKEY_MAGIC,
1.1 etheisen 199: C3_LESSKEY_MAGIC
200: };
201: char filetrailer[] = {
1.8 nicm 202: C0_END_LESSKEY_MAGIC,
203: C1_END_LESSKEY_MAGIC,
1.1 etheisen 204: C2_END_LESSKEY_MAGIC
205: };
206: char cmdsection[1] = { CMD_SECTION };
207: char editsection[1] = { EDIT_SECTION };
1.5 millert 208: char varsection[1] = { VAR_SECTION };
1.1 etheisen 209: char endsection[1] = { END_SECTION };
210:
211: char *infile = NULL;
1.8 nicm 212: char *outfile = NULL;
1.1 etheisen 213:
214: int linenum;
215: int errors;
216:
217: extern char version[];
218:
1.8 nicm 219: void
220: usage(void)
1.5 millert 221: {
1.8 nicm 222: (void) fprintf(stderr, "usage: lesskey [-o output] [input]\n");
1.5 millert 223: exit(1);
224: }
225:
1.8 nicm 226: char *
227: mkpathname(char *dirname, char *filename)
1.1 etheisen 228: {
229: char *pathname;
1.4 deraadt 230: size_t len;
1.1 etheisen 231:
1.4 deraadt 232: len = strlen(dirname) + strlen(filename) + 2;
1.8 nicm 233: pathname = calloc(1, len);
234: if (pathname == NULL) {
235: fprintf(stderr, "mkpathname: out of memory\n");
236: exit(1);
237: }
238: (void) snprintf(pathname, len, "%s/%s", dirname, filename);
1.1 etheisen 239: return (pathname);
240: }
241:
242: /*
243: * Figure out the name of a default file (in the user's HOME directory).
244: */
1.8 nicm 245: char *
246: homefile(char *filename)
1.1 etheisen 247: {
248: char *p;
249: char *pathname;
250:
1.8 nicm 251: if ((p = getenv("HOME")) != NULL && *p != '\0') {
1.1 etheisen 252: pathname = mkpathname(p, filename);
1.8 nicm 253: } else {
254: (void) fprintf(stderr, "cannot find $HOME - "
255: "using current directory\n");
1.1 etheisen 256: pathname = mkpathname(".", filename);
257: }
258: return (pathname);
259: }
260:
261: /*
262: * Parse command line arguments.
263: */
1.8 nicm 264: void
265: parse_args(int argc, char **argv)
1.1 etheisen 266: {
1.5 millert 267: char *arg;
268:
1.1 etheisen 269: outfile = NULL;
1.8 nicm 270: while (--argc > 0) {
1.5 millert 271: arg = *++argv;
272: if (arg[0] != '-')
273: /* Arg does not start with "-"; it's not an option. */
274: break;
275: if (arg[1] == '\0')
276: /* "-" means standard input. */
277: break;
1.8 nicm 278: if (arg[1] == '-' && arg[2] == '\0') {
1.5 millert 279: /* "--" means end of options. */
280: argc--;
281: argv++;
282: break;
283: }
1.8 nicm 284: switch (arg[1]) {
1.5 millert 285: case '-':
1.8 nicm 286: if (strncmp(arg, "--output", 8) == 0) {
1.5 millert 287: if (arg[8] == '\0')
288: outfile = &arg[8];
289: else if (arg[8] == '=')
290: outfile = &arg[9];
291: else
292: usage();
293: goto opt_o;
294: }
1.8 nicm 295: if (strcmp(arg, "--version") == 0) {
1.5 millert 296: goto opt_V;
297: }
298: usage();
299: break;
1.1 etheisen 300: case 'o':
301: outfile = &argv[0][2];
1.5 millert 302: opt_o:
1.8 nicm 303: if (*outfile == '\0') {
1.1 etheisen 304: if (--argc <= 0)
305: usage();
306: outfile = *(++argv);
307: }
308: break;
309: case 'V':
1.5 millert 310: opt_V:
1.8 nicm 311: (void) printf("lesskey version %s\n", version);
1.1 etheisen 312: exit(0);
313: default:
314: usage();
315: }
316: }
317: if (argc > 1)
318: usage();
319: /*
320: * Open the input file, or use DEF_LESSKEYINFILE if none specified.
321: */
322: if (argc > 0)
323: infile = *argv;
324: else
325: infile = homefile(DEF_LESSKEYINFILE);
326: }
327:
328: /*
329: * Initialize data structures.
330: */
1.8 nicm 331: void
332: init_tables(void)
1.1 etheisen 333: {
334: cmdtable.names = cmdnames;
335: cmdtable.pbuffer = cmdtable.buffer;
336:
337: edittable.names = editnames;
338: edittable.pbuffer = edittable.buffer;
1.5 millert 339:
340: vartable.names = NULL;
341: vartable.pbuffer = vartable.buffer;
1.1 etheisen 342: }
343:
344: /*
345: * Parse one character of a string.
346: */
1.8 nicm 347: char *
348: tstr(char **pp, int xlate)
349: {
350: char *p;
351: char ch;
352: int i;
1.5 millert 353: static char buf[10];
354: static char tstr_control_k[] =
355: { SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
1.1 etheisen 356:
357: p = *pp;
1.8 nicm 358: switch (*p) {
1.1 etheisen 359: case '\\':
360: ++p;
1.8 nicm 361: switch (*p) {
1.1 etheisen 362: case '0': case '1': case '2': case '3':
363: case '4': case '5': case '6': case '7':
364: /*
365: * Parse an octal number.
366: */
367: ch = 0;
368: i = 0;
369: do
370: ch = 8*ch + (*p - '0');
1.8 nicm 371: while (*++p >= '0' && *p <= '7' && ++i < 3)
372: ;
1.1 etheisen 373: *pp = p;
1.5 millert 374: if (xlate && ch == CONTROL('K'))
1.8 nicm 375: return (tstr_control_k);
1.5 millert 376: buf[0] = ch;
377: buf[1] = '\0';
378: return (buf);
1.1 etheisen 379: case 'b':
380: *pp = p+1;
1.5 millert 381: return ("\b");
1.1 etheisen 382: case 'e':
383: *pp = p+1;
1.5 millert 384: buf[0] = ESC;
385: buf[1] = '\0';
386: return (buf);
1.1 etheisen 387: case 'n':
388: *pp = p+1;
1.5 millert 389: return ("\n");
1.1 etheisen 390: case 'r':
391: *pp = p+1;
1.5 millert 392: return ("\r");
1.1 etheisen 393: case 't':
394: *pp = p+1;
1.5 millert 395: return ("\t");
396: case 'k':
1.8 nicm 397: if (xlate) {
398: switch (*++p) {
1.5 millert 399: case 'u': ch = SK_UP_ARROW; break;
400: case 'd': ch = SK_DOWN_ARROW; break;
401: case 'r': ch = SK_RIGHT_ARROW; break;
402: case 'l': ch = SK_LEFT_ARROW; break;
403: case 'U': ch = SK_PAGE_UP; break;
404: case 'D': ch = SK_PAGE_DOWN; break;
405: case 'h': ch = SK_HOME; break;
406: case 'e': ch = SK_END; break;
407: case 'x': ch = SK_DELETE; break;
408: default:
1.8 nicm 409: lkerr("illegal char after \\k");
1.5 millert 410: *pp = p+1;
411: return ("");
412: }
413: *pp = p+1;
414: buf[0] = SK_SPECIAL_KEY;
415: buf[1] = ch;
416: buf[2] = 6;
417: buf[3] = 1;
418: buf[4] = 1;
419: buf[5] = 1;
420: buf[6] = '\0';
421: return (buf);
422: }
423: /* FALLTHRU */
1.1 etheisen 424: default:
425: /*
1.8 nicm 426: * Backslash followed by any other char
1.1 etheisen 427: * just means that char.
428: */
429: *pp = p+1;
1.5 millert 430: buf[0] = *p;
431: buf[1] = '\0';
432: if (xlate && buf[0] == CONTROL('K'))
1.8 nicm 433: return (tstr_control_k);
1.5 millert 434: return (buf);
1.1 etheisen 435: }
436: case '^':
437: /*
1.7 shadchin 438: * Caret means CONTROL.
1.1 etheisen 439: */
440: *pp = p+2;
1.5 millert 441: buf[0] = CONTROL(p[1]);
442: buf[1] = '\0';
443: if (buf[0] == CONTROL('K'))
1.8 nicm 444: return (tstr_control_k);
1.5 millert 445: return (buf);
1.1 etheisen 446: }
447: *pp = p+1;
1.5 millert 448: buf[0] = *p;
449: buf[1] = '\0';
450: if (xlate && buf[0] == CONTROL('K'))
1.8 nicm 451: return (tstr_control_k);
1.5 millert 452: return (buf);
1.1 etheisen 453: }
454:
455: /*
456: * Skip leading spaces in a string.
457: */
1.8 nicm 458: char *
459: skipsp(char *s)
1.1 etheisen 460: {
1.8 nicm 461: while (*s == ' ' || *s == '\t')
1.1 etheisen 462: s++;
463: return (s);
464: }
465:
466: /*
467: * Skip non-space characters in a string.
468: */
1.8 nicm 469: char *
470: skipnsp(char *s)
1.1 etheisen 471: {
472: while (*s != '\0' && *s != ' ' && *s != '\t')
473: s++;
474: return (s);
475: }
476:
477: /*
478: * Clean up an input line:
479: * strip off the trailing newline & any trailing # comment.
480: */
1.8 nicm 481: char *
482: clean_line(char *s)
1.1 etheisen 483: {
1.8 nicm 484: int i;
1.1 etheisen 485:
486: s = skipsp(s);
1.5 millert 487: for (i = 0; s[i] != '\n' && s[i] != '\r' && s[i] != '\0'; i++)
1.1 etheisen 488: if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
489: break;
490: s[i] = '\0';
491: return (s);
492: }
493:
494: /*
495: * Add a byte to the output command table.
496: */
1.8 nicm 497: void
498: add_cmd_char(int c)
499: {
500: if (currtable->pbuffer >= currtable->buffer + MAX_USERCMD) {
501: lkerr("too many commands");
1.1 etheisen 502: exit(1);
503: }
1.8 nicm 504: *(currtable->pbuffer)++ = (char)c;
1.1 etheisen 505: }
506:
507: /*
1.5 millert 508: * Add a string to the output command table.
509: */
1.8 nicm 510: void
511: add_cmd_str(char *s)
1.5 millert 512: {
1.8 nicm 513: for (; *s != '\0'; s++)
1.5 millert 514: add_cmd_char(*s);
515: }
516:
517: /*
1.1 etheisen 518: * See if we have a special "control" line.
519: */
1.8 nicm 520: int
521: control_line(char *s)
1.1 etheisen 522: {
1.8 nicm 523: #define PREFIX(str, pat) (strncmp(str, pat, strlen(pat)) == 0)
1.1 etheisen 524:
1.8 nicm 525: if (PREFIX(s, "#line-edit")) {
1.1 etheisen 526: currtable = &edittable;
527: return (1);
528: }
1.8 nicm 529: if (PREFIX(s, "#command")) {
1.1 etheisen 530: currtable = &cmdtable;
531: return (1);
532: }
1.8 nicm 533: if (PREFIX(s, "#env")) {
1.5 millert 534: currtable = &vartable;
535: return (1);
536: }
1.8 nicm 537: if (PREFIX(s, "#stop")) {
1.1 etheisen 538: add_cmd_char('\0');
539: add_cmd_char(A_END_LIST);
540: return (1);
541: }
542: return (0);
543: }
544:
545: /*
546: * Output some bytes.
547: */
1.8 nicm 548: void
549: fputbytes(FILE *fd, char *buf, int len)
550: {
551: while (len-- > 0) {
552: (void) fwrite(buf, sizeof (char), 1, fd);
1.1 etheisen 553: buf++;
554: }
555: }
556:
557: /*
558: * Output an integer, in special KRADIX form.
559: */
1.8 nicm 560: void
561: fputint(FILE *fd, unsigned int val)
1.1 etheisen 562: {
563: char c;
564:
1.8 nicm 565: if (val >= KRADIX*KRADIX) {
566: (void) fprintf(stderr, "error: integer too big (%d > %d)\n",
567: val, KRADIX*KRADIX);
1.1 etheisen 568: exit(1);
569: }
570: c = val % KRADIX;
1.8 nicm 571: (void) fwrite(&c, sizeof (char), 1, fd);
1.1 etheisen 572: c = val / KRADIX;
1.8 nicm 573: (void) fwrite(&c, sizeof (char), 1, fd);
1.1 etheisen 574: }
575:
576: /*
577: * Find an action, given the name of the action.
578: */
1.8 nicm 579: int
580: findaction(char *actname)
1.1 etheisen 581: {
582: int i;
583:
584: for (i = 0; currtable->names[i].cn_name != NULL; i++)
585: if (strcmp(currtable->names[i].cn_name, actname) == 0)
586: return (currtable->names[i].cn_action);
1.8 nicm 587: lkerr("unknown action");
1.1 etheisen 588: return (A_INVALID);
589: }
590:
1.8 nicm 591: void
592: lkerr(char *s)
1.1 etheisen 593: {
1.8 nicm 594: (void) fprintf(stderr, "line %d: %s\n", linenum, s);
1.1 etheisen 595: errors++;
596: }
597:
598:
1.8 nicm 599: void
600: parse_cmdline(char *p)
1.1 etheisen 601: {
602: int cmdlen;
603: char *actname;
604: int action;
1.5 millert 605: char *s;
606: char c;
1.1 etheisen 607:
608: /*
609: * Parse the command string and store it in the current table.
610: */
611: cmdlen = 0;
1.8 nicm 612: do {
1.5 millert 613: s = tstr(&p, 1);
614: cmdlen += strlen(s);
615: if (cmdlen > MAX_CMDLEN)
1.8 nicm 616: lkerr("command too long");
1.1 etheisen 617: else
1.5 millert 618: add_cmd_str(s);
1.1 etheisen 619: } while (*p != ' ' && *p != '\t' && *p != '\0');
620: /*
621: * Terminate the command string with a null byte.
622: */
623: add_cmd_char('\0');
624:
625: /*
626: * Skip white space between the command string
627: * and the action name.
628: * Terminate the action name with a null byte.
629: */
630: p = skipsp(p);
1.8 nicm 631: if (*p == '\0') {
632: lkerr("missing action");
1.1 etheisen 633: return;
634: }
635: actname = p;
636: p = skipnsp(p);
637: c = *p;
638: *p = '\0';
639:
640: /*
641: * Parse the action name and store it in the current table.
642: */
643: action = findaction(actname);
644:
645: /*
646: * See if an extra string follows the action name.
647: */
648: *p = c;
649: p = skipsp(p);
1.8 nicm 650: if (*p == '\0') {
1.1 etheisen 651: add_cmd_char(action);
1.8 nicm 652: } else {
1.1 etheisen 653: /*
654: * OR the special value A_EXTRA into the action byte.
655: * Put the extra string after the action byte.
656: */
657: add_cmd_char(action | A_EXTRA);
658: while (*p != '\0')
1.5 millert 659: add_cmd_str(tstr(&p, 0));
1.1 etheisen 660: add_cmd_char('\0');
661: }
662: }
663:
1.8 nicm 664: void
665: parse_varline(char *p)
1.5 millert 666: {
667: char *s;
668:
1.8 nicm 669: do {
1.5 millert 670: s = tstr(&p, 0);
671: add_cmd_str(s);
672: } while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
673: /*
674: * Terminate the variable name with a null byte.
675: */
676: add_cmd_char('\0');
677:
678: p = skipsp(p);
1.8 nicm 679: if (*p++ != '=') {
680: lkerr("missing =");
1.5 millert 681: return;
682: }
683:
684: add_cmd_char(EV_OK|A_EXTRA);
685:
686: p = skipsp(p);
1.8 nicm 687: while (*p != '\0') {
1.5 millert 688: s = tstr(&p, 0);
689: add_cmd_str(s);
690: }
691: add_cmd_char('\0');
692: }
693:
694: /*
695: * Parse a line from the lesskey file.
696: */
1.8 nicm 697: void
698: parse_line(char *line)
1.5 millert 699: {
700: char *p;
701:
702: /*
703: * See if it is a control line.
704: */
705: if (control_line(line))
706: return;
707: /*
708: * Skip leading white space.
709: * Replace the final newline with a null byte.
710: * Ignore blank lines and comments.
711: */
712: p = clean_line(line);
713: if (*p == '\0')
714: return;
715:
716: if (currtable == &vartable)
717: parse_varline(p);
718: else
719: parse_cmdline(p);
720: }
721:
1.8 nicm 722: int
723: main(int argc, char **argv)
1.1 etheisen 724: {
725: FILE *desc;
726: FILE *out;
1.5 millert 727: char line[1024];
728:
1.1 etheisen 729: /*
730: * Process command line arguments.
731: */
732: parse_args(argc, argv);
733: init_tables();
734:
735: /*
736: * Open the input file.
737: */
738: if (strcmp(infile, "-") == 0)
739: desc = stdin;
1.8 nicm 740: else if ((desc = fopen(infile, "r")) == NULL) {
1.1 etheisen 741: perror(infile);
1.5 millert 742: usage();
1.1 etheisen 743: }
744:
745: /*
746: * Read and parse the input file, one line at a time.
747: */
748: errors = 0;
749: linenum = 0;
1.8 nicm 750: while (fgets(line, sizeof (line), desc) != NULL) {
1.1 etheisen 751: ++linenum;
752: parse_line(line);
753: }
754:
755: /*
756: * Write the output file.
757: * If no output file was specified, use "$HOME/.less"
758: */
1.8 nicm 759: if (errors > 0) {
760: (void) fprintf(stderr, "%d errors; no output produced\n",
761: errors);
1.1 etheisen 762: exit(1);
763: }
764:
765: if (outfile == NULL)
1.5 millert 766: outfile = getenv("LESSKEY");
767: if (outfile == NULL)
1.1 etheisen 768: outfile = homefile(LESSKEYFILE);
1.8 nicm 769: if ((out = fopen(outfile, "wb")) == NULL) {
1.1 etheisen 770: perror(outfile);
771: exit(1);
772: }
773:
774: /* File header */
1.8 nicm 775: fputbytes(out, fileheader, sizeof (fileheader));
1.1 etheisen 776:
777: /* Command key section */
1.8 nicm 778: fputbytes(out, cmdsection, sizeof (cmdsection));
1.1 etheisen 779: fputint(out, cmdtable.pbuffer - cmdtable.buffer);
1.8 nicm 780: fputbytes(out, (char *)cmdtable.buffer,
781: cmdtable.pbuffer-cmdtable.buffer);
782:
1.1 etheisen 783: /* Edit key section */
1.8 nicm 784: fputbytes(out, editsection, sizeof (editsection));
1.1 etheisen 785: fputint(out, edittable.pbuffer - edittable.buffer);
1.8 nicm 786: fputbytes(out, (char *)edittable.buffer,
787: edittable.pbuffer-edittable.buffer);
1.1 etheisen 788:
1.5 millert 789: /* Environment variable section */
1.8 nicm 790: fputbytes(out, varsection, sizeof (varsection));
1.5 millert 791: fputint(out, vartable.pbuffer - vartable.buffer);
1.8 nicm 792: fputbytes(out, (char *)vartable.buffer,
793: vartable.pbuffer-vartable.buffer);
1.5 millert 794:
1.1 etheisen 795: /* File trailer */
1.8 nicm 796: fputbytes(out, endsection, sizeof (endsection));
797: fputbytes(out, filetrailer, sizeof (filetrailer));
1.5 millert 798: return (0);
1.1 etheisen 799: }