lesskey.c (009e81b16465ea457c0e63fd49fe77f47cc27a5a) lesskey.c (1ea316270f1f75922ac53976d5d8808a41442f46)
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */

--- 199 unchanged lines hidden (view full) ---

208char endsection[1] = { END_SECTION };
209
210char *infile = NULL;
211char *outfile = NULL ;
212
213int linenum;
214int errors;
215
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */

--- 199 unchanged lines hidden (view full) ---

208char endsection[1] = { END_SECTION };
209
210char *infile = NULL;
211char *outfile = NULL ;
212
213int linenum;
214int errors;
215
216static void lk_error(char *s);
217
216extern char version[];
217
218 void
218extern char version[];
219
220 void
219usage()
221usage(void)
220{
221 fprintf(stderr, "usage: lesskey [-o output] [input]\n");
222 exit(1);
223}
224
225 char *
222{
223 fprintf(stderr, "usage: lesskey [-o output] [input]\n");
224 exit(1);
225}
226
227 char *
226mkpathname(dirname, filename)
227 char *dirname;
228 char *filename;
228mkpathname(char *dirname, char *filename)
229{
230 char *pathname;
231
232 pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char));
233 strcpy(pathname, dirname);
234 strcat(pathname, PATHNAME_SEP);
235 strcat(pathname, filename);
236 return (pathname);
237}
238
239/*
240 * Figure out the name of a default file (in the user's HOME directory).
241 */
242 char *
229{
230 char *pathname;
231
232 pathname = calloc(strlen(dirname) + strlen(filename) + 2, sizeof(char));
233 strcpy(pathname, dirname);
234 strcat(pathname, PATHNAME_SEP);
235 strcat(pathname, filename);
236 return (pathname);
237}
238
239/*
240 * Figure out the name of a default file (in the user's HOME directory).
241 */
242 char *
243homefile(filename)
244 char *filename;
243homefile(char *filename)
245{
246 char *p;
247 char *pathname;
248
249 if ((p = getenv("HOME")) != NULL && *p != '\0')
250 pathname = mkpathname(p, filename);
251#if OS2
252 else if ((p = getenv("INIT")) != NULL && *p != '\0')

--- 6 unchanged lines hidden (view full) ---

259 }
260 return (pathname);
261}
262
263/*
264 * Parse command line arguments.
265 */
266 void
244{
245 char *p;
246 char *pathname;
247
248 if ((p = getenv("HOME")) != NULL && *p != '\0')
249 pathname = mkpathname(p, filename);
250#if OS2
251 else if ((p = getenv("INIT")) != NULL && *p != '\0')

--- 6 unchanged lines hidden (view full) ---

258 }
259 return (pathname);
260}
261
262/*
263 * Parse command line arguments.
264 */
265 void
267parse_args(argc, argv)
268 int argc;
269 char **argv;
266parse_args(int argc, char **argv)
270{
271 char *arg;
272
273 outfile = NULL;
274 while (--argc > 0)
275 {
276 arg = *++argv;
277 if (arg[0] != '-')

--- 56 unchanged lines hidden (view full) ---

334 else
335 infile = homefile(DEF_LESSKEYINFILE);
336}
337
338/*
339 * Initialize data structures.
340 */
341 void
267{
268 char *arg;
269
270 outfile = NULL;
271 while (--argc > 0)
272 {
273 arg = *++argv;
274 if (arg[0] != '-')

--- 56 unchanged lines hidden (view full) ---

331 else
332 infile = homefile(DEF_LESSKEYINFILE);
333}
334
335/*
336 * Initialize data structures.
337 */
338 void
342init_tables()
339init_tables(void)
343{
344 cmdtable.names = cmdnames;
345 cmdtable.pbuffer = cmdtable.buffer;
346
347 edittable.names = editnames;
348 edittable.pbuffer = edittable.buffer;
349
350 vartable.names = NULL;
351 vartable.pbuffer = vartable.buffer;
352}
353
354/*
355 * Parse one character of a string.
356 */
357 char *
340{
341 cmdtable.names = cmdnames;
342 cmdtable.pbuffer = cmdtable.buffer;
343
344 edittable.names = editnames;
345 edittable.pbuffer = edittable.buffer;
346
347 vartable.names = NULL;
348 vartable.pbuffer = vartable.buffer;
349}
350
351/*
352 * Parse one character of a string.
353 */
354 char *
358tstr(pp, xlate)
359 char **pp;
360 int xlate;
355tstr(char **pp, int xlate)
361{
356{
362 register char *p;
363 register char ch;
364 register int i;
357 char *p;
358 char ch;
359 int i;
365 static char buf[10];
366 static char tstr_control_k[] =
367 { SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
368
369 p = *pp;
370 switch (*p)
371 {
372 case '\\':

--- 43 unchanged lines hidden (view full) ---

416 case 'r': ch = SK_RIGHT_ARROW; break;
417 case 'l': ch = SK_LEFT_ARROW; break;
418 case 'U': ch = SK_PAGE_UP; break;
419 case 'D': ch = SK_PAGE_DOWN; break;
420 case 'h': ch = SK_HOME; break;
421 case 'e': ch = SK_END; break;
422 case 'x': ch = SK_DELETE; break;
423 default:
360 static char buf[10];
361 static char tstr_control_k[] =
362 { SK_SPECIAL_KEY, SK_CONTROL_K, 6, 1, 1, 1, '\0' };
363
364 p = *pp;
365 switch (*p)
366 {
367 case '\\':

--- 43 unchanged lines hidden (view full) ---

411 case 'r': ch = SK_RIGHT_ARROW; break;
412 case 'l': ch = SK_LEFT_ARROW; break;
413 case 'U': ch = SK_PAGE_UP; break;
414 case 'D': ch = SK_PAGE_DOWN; break;
415 case 'h': ch = SK_HOME; break;
416 case 'e': ch = SK_END; break;
417 case 'x': ch = SK_DELETE; break;
418 default:
424 error("illegal char after \\k");
419 lk_error("illegal char after \\k");
425 *pp = p+1;
426 return ("");
427 }
428 *pp = p+1;
429 buf[0] = SK_SPECIAL_KEY;
430 buf[1] = ch;
431 buf[2] = 6;
432 buf[3] = 1;

--- 33 unchanged lines hidden (view full) ---

466 return tstr_control_k;
467 return (buf);
468}
469
470/*
471 * Skip leading spaces in a string.
472 */
473 public char *
420 *pp = p+1;
421 return ("");
422 }
423 *pp = p+1;
424 buf[0] = SK_SPECIAL_KEY;
425 buf[1] = ch;
426 buf[2] = 6;
427 buf[3] = 1;

--- 33 unchanged lines hidden (view full) ---

461 return tstr_control_k;
462 return (buf);
463}
464
465/*
466 * Skip leading spaces in a string.
467 */
468 public char *
474skipsp(s)
475 register char *s;
469skipsp(char *s)
476{
477 while (*s == ' ' || *s == '\t')
478 s++;
479 return (s);
480}
481
482/*
483 * Skip non-space characters in a string.
484 */
485 public char *
470{
471 while (*s == ' ' || *s == '\t')
472 s++;
473 return (s);
474}
475
476/*
477 * Skip non-space characters in a string.
478 */
479 public char *
486skipnsp(s)
487 register char *s;
480skipnsp(char *s)
488{
489 while (*s != '\0' && *s != ' ' && *s != '\t')
490 s++;
491 return (s);
492}
493
494/*
495 * Clean up an input line:
496 * strip off the trailing newline & any trailing # comment.
497 */
498 char *
481{
482 while (*s != '\0' && *s != ' ' && *s != '\t')
483 s++;
484 return (s);
485}
486
487/*
488 * Clean up an input line:
489 * strip off the trailing newline & any trailing # comment.
490 */
491 char *
499clean_line(s)
500 char *s;
492clean_line(char *s)
501{
493{
502 register int i;
494 int i;
503
504 s = skipsp(s);
505 for (i = 0; s[i] != '\n' && s[i] != '\r' && s[i] != '\0'; i++)
506 if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
507 break;
508 s[i] = '\0';
509 return (s);
510}
511
512/*
513 * Add a byte to the output command table.
514 */
515 void
495
496 s = skipsp(s);
497 for (i = 0; s[i] != '\n' && s[i] != '\r' && s[i] != '\0'; i++)
498 if (s[i] == '#' && (i == 0 || s[i-1] != '\\'))
499 break;
500 s[i] = '\0';
501 return (s);
502}
503
504/*
505 * Add a byte to the output command table.
506 */
507 void
516add_cmd_char(c)
517 int c;
508add_cmd_char(int c)
518{
519 if (currtable->pbuffer >= currtable->buffer + MAX_USERCMD)
520 {
509{
510 if (currtable->pbuffer >= currtable->buffer + MAX_USERCMD)
511 {
521 error("too many commands");
512 lk_error("too many commands");
522 exit(1);
523 }
524 *(currtable->pbuffer)++ = c;
525}
526
527/*
528 * Add a string to the output command table.
529 */
530 void
513 exit(1);
514 }
515 *(currtable->pbuffer)++ = c;
516}
517
518/*
519 * Add a string to the output command table.
520 */
521 void
531add_cmd_str(s)
532 char *s;
522add_cmd_str(char *s)
533{
534 for ( ; *s != '\0'; s++)
535 add_cmd_char(*s);
536}
537
538/*
539 * See if we have a special "control" line.
540 */
541 int
523{
524 for ( ; *s != '\0'; s++)
525 add_cmd_char(*s);
526}
527
528/*
529 * See if we have a special "control" line.
530 */
531 int
542control_line(s)
543 char *s;
532control_line(char *s)
544{
545#define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
546
547 if (PREFIX(s, "#line-edit"))
548 {
549 currtable = &edittable;
550 return (1);
551 }

--- 15 unchanged lines hidden (view full) ---

567 }
568 return (0);
569}
570
571/*
572 * Output some bytes.
573 */
574 void
533{
534#define PREFIX(str,pat) (strncmp(str,pat,strlen(pat)) == 0)
535
536 if (PREFIX(s, "#line-edit"))
537 {
538 currtable = &edittable;
539 return (1);
540 }

--- 15 unchanged lines hidden (view full) ---

556 }
557 return (0);
558}
559
560/*
561 * Output some bytes.
562 */
563 void
575fputbytes(fd, buf, len)
576 FILE *fd;
577 char *buf;
578 int len;
564fputbytes(FILE *fd, char *buf, int len)
579{
580 while (len-- > 0)
581 {
582 fwrite(buf, sizeof(char), 1, fd);
583 buf++;
584 }
585}
586
587/*
588 * Output an integer, in special KRADIX form.
589 */
590 void
565{
566 while (len-- > 0)
567 {
568 fwrite(buf, sizeof(char), 1, fd);
569 buf++;
570 }
571}
572
573/*
574 * Output an integer, in special KRADIX form.
575 */
576 void
591fputint(fd, val)
592 FILE *fd;
593 unsigned int val;
577fputint(FILE *fd, unsigned int val)
594{
595 char c;
596
597 if (val >= KRADIX*KRADIX)
598 {
599 fprintf(stderr, "error: integer too big (%d > %d)\n",
600 val, KRADIX*KRADIX);
601 exit(1);
602 }
603 c = val % KRADIX;
604 fwrite(&c, sizeof(char), 1, fd);
605 c = val / KRADIX;
606 fwrite(&c, sizeof(char), 1, fd);
607}
608
609/*
610 * Find an action, given the name of the action.
611 */
612 int
578{
579 char c;
580
581 if (val >= KRADIX*KRADIX)
582 {
583 fprintf(stderr, "error: integer too big (%d > %d)\n",
584 val, KRADIX*KRADIX);
585 exit(1);
586 }
587 c = val % KRADIX;
588 fwrite(&c, sizeof(char), 1, fd);
589 c = val / KRADIX;
590 fwrite(&c, sizeof(char), 1, fd);
591}
592
593/*
594 * Find an action, given the name of the action.
595 */
596 int
613findaction(actname)
614 char *actname;
597findaction(char *actname)
615{
616 int i;
617
618 for (i = 0; currtable->names[i].cn_name != NULL; i++)
619 if (strcmp(currtable->names[i].cn_name, actname) == 0)
620 return (currtable->names[i].cn_action);
598{
599 int i;
600
601 for (i = 0; currtable->names[i].cn_name != NULL; i++)
602 if (strcmp(currtable->names[i].cn_name, actname) == 0)
603 return (currtable->names[i].cn_action);
621 error("unknown action");
604 lk_error("unknown action");
622 return (A_INVALID);
623}
624
605 return (A_INVALID);
606}
607
625 void
626error(s)
627 char *s;
608 static void
609lk_error(char *s)
628{
629 fprintf(stderr, "line %d: %s\n", linenum, s);
630 errors++;
631}
632
633
634 void
610{
611 fprintf(stderr, "line %d: %s\n", linenum, s);
612 errors++;
613}
614
615
616 void
635parse_cmdline(p)
636 char *p;
617parse_cmdline(char *p)
637{
638 int cmdlen;
639 char *actname;
640 int action;
641 char *s;
642 char c;
643
644 /*
645 * Parse the command string and store it in the current table.
646 */
647 cmdlen = 0;
648 do
649 {
650 s = tstr(&p, 1);
651 cmdlen += (int) strlen(s);
652 if (cmdlen > MAX_CMDLEN)
618{
619 int cmdlen;
620 char *actname;
621 int action;
622 char *s;
623 char c;
624
625 /*
626 * Parse the command string and store it in the current table.
627 */
628 cmdlen = 0;
629 do
630 {
631 s = tstr(&p, 1);
632 cmdlen += (int) strlen(s);
633 if (cmdlen > MAX_CMDLEN)
653 error("command too long");
634 lk_error("command too long");
654 else
655 add_cmd_str(s);
656 } while (*p != ' ' && *p != '\t' && *p != '\0');
657 /*
658 * Terminate the command string with a null byte.
659 */
660 add_cmd_char('\0');
661
662 /*
663 * Skip white space between the command string
664 * and the action name.
665 * Terminate the action name with a null byte.
666 */
667 p = skipsp(p);
668 if (*p == '\0')
669 {
635 else
636 add_cmd_str(s);
637 } while (*p != ' ' && *p != '\t' && *p != '\0');
638 /*
639 * Terminate the command string with a null byte.
640 */
641 add_cmd_char('\0');
642
643 /*
644 * Skip white space between the command string
645 * and the action name.
646 * Terminate the action name with a null byte.
647 */
648 p = skipsp(p);
649 if (*p == '\0')
650 {
670 error("missing action");
651 lk_error("missing action");
671 return;
672 }
673 actname = p;
674 p = skipnsp(p);
675 c = *p;
676 *p = '\0';
677
678 /*

--- 18 unchanged lines hidden (view full) ---

697 add_cmd_char(action | A_EXTRA);
698 while (*p != '\0')
699 add_cmd_str(tstr(&p, 0));
700 add_cmd_char('\0');
701 }
702}
703
704 void
652 return;
653 }
654 actname = p;
655 p = skipnsp(p);
656 c = *p;
657 *p = '\0';
658
659 /*

--- 18 unchanged lines hidden (view full) ---

678 add_cmd_char(action | A_EXTRA);
679 while (*p != '\0')
680 add_cmd_str(tstr(&p, 0));
681 add_cmd_char('\0');
682 }
683}
684
685 void
705parse_varline(p)
706 char *p;
686parse_varline(char *p)
707{
708 char *s;
709
710 do
711 {
712 s = tstr(&p, 0);
713 add_cmd_str(s);
714 } while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
715 /*
716 * Terminate the variable name with a null byte.
717 */
718 add_cmd_char('\0');
719
720 p = skipsp(p);
721 if (*p++ != '=')
722 {
687{
688 char *s;
689
690 do
691 {
692 s = tstr(&p, 0);
693 add_cmd_str(s);
694 } while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0');
695 /*
696 * Terminate the variable name with a null byte.
697 */
698 add_cmd_char('\0');
699
700 p = skipsp(p);
701 if (*p++ != '=')
702 {
723 error("missing =");
703 lk_error("missing =");
724 return;
725 }
726
727 add_cmd_char(EV_OK|A_EXTRA);
728
729 p = skipsp(p);
730 while (*p != '\0')
731 {
732 s = tstr(&p, 0);
733 add_cmd_str(s);
734 }
735 add_cmd_char('\0');
736}
737
738/*
739 * Parse a line from the lesskey file.
740 */
741 void
704 return;
705 }
706
707 add_cmd_char(EV_OK|A_EXTRA);
708
709 p = skipsp(p);
710 while (*p != '\0')
711 {
712 s = tstr(&p, 0);
713 add_cmd_str(s);
714 }
715 add_cmd_char('\0');
716}
717
718/*
719 * Parse a line from the lesskey file.
720 */
721 void
742parse_line(line)
743 char *line;
722parse_line(char *line)
744{
745 char *p;
746
747 /*
748 * See if it is a control line.
749 */
750 if (control_line(line))
751 return;

--- 8 unchanged lines hidden (view full) ---

760
761 if (currtable == &vartable)
762 parse_varline(p);
763 else
764 parse_cmdline(p);
765}
766
767 int
723{
724 char *p;
725
726 /*
727 * See if it is a control line.
728 */
729 if (control_line(line))
730 return;

--- 8 unchanged lines hidden (view full) ---

739
740 if (currtable == &vartable)
741 parse_varline(p);
742 else
743 parse_cmdline(p);
744}
745
746 int
768main(argc, argv)
769 int argc;
770 char *argv[];
747main(int argc, char *argv[])
771{
772 FILE *desc;
773 FILE *out;
774 char line[1024];
775
776#ifdef WIN32
777 if (getenv("HOME") == NULL)
778 {

--- 96 unchanged lines hidden ---
748{
749 FILE *desc;
750 FILE *out;
751 char line[1024];
752
753#ifdef WIN32
754 if (getenv("HOME") == NULL)
755 {

--- 96 unchanged lines hidden ---