decode.c (009e81b16465ea457c0e63fd49fe77f47cc27a5a) decode.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 */

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

224static struct tablelist *list_var_tables = NULL;
225static struct tablelist *list_sysvar_tables = NULL;
226
227
228/*
229 * Expand special key abbreviations in a command table.
230 */
231 static void
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 */

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

224static struct tablelist *list_var_tables = NULL;
225static struct tablelist *list_sysvar_tables = NULL;
226
227
228/*
229 * Expand special key abbreviations in a command table.
230 */
231 static void
232expand_special_keys(table, len)
233 char *table;
234 int len;
232expand_special_keys(char *table, int len)
235{
233{
236 register char *fm;
237 register char *to;
238 register int a;
234 char *fm;
235 char *to;
236 int a;
239 char *repl;
240 int klen;
241
242 for (fm = table; fm < table + len; )
243 {
244 /*
245 * Rewrite each command in the table with any
246 * special key abbreviations expanded.

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

285 }
286 }
287}
288
289/*
290 * Initialize the command lists.
291 */
292 public void
237 char *repl;
238 int klen;
239
240 for (fm = table; fm < table + len; )
241 {
242 /*
243 * Rewrite each command in the table with any
244 * special key abbreviations expanded.

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

283 }
284 }
285}
286
287/*
288 * Initialize the command lists.
289 */
290 public void
293init_cmds()
291init_cmds(void)
294{
295 /*
296 * Add the default command tables.
297 */
298 add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
299 add_ecmd_table((char*)edittable, sizeof(edittable));
300#if USERFILE
301 /*

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

315 add_hometable("LESSKEY", LESSKEYFILE, 0);
316#endif
317}
318
319/*
320 * Add a command table.
321 */
322 static int
292{
293 /*
294 * Add the default command tables.
295 */
296 add_fcmd_table((char*)cmdtable, sizeof(cmdtable));
297 add_ecmd_table((char*)edittable, sizeof(edittable));
298#if USERFILE
299 /*

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

313 add_hometable("LESSKEY", LESSKEYFILE, 0);
314#endif
315}
316
317/*
318 * Add a command table.
319 */
320 static int
323add_cmd_table(tlist, buf, len)
324 struct tablelist **tlist;
325 char *buf;
326 int len;
321add_cmd_table(struct tablelist **tlist, char *buf, int len)
327{
322{
328 register struct tablelist *t;
323 struct tablelist *t;
329
330 if (len == 0)
331 return (0);
332 /*
333 * Allocate a tablelist structure, initialize it,
334 * and link it into the list of tables.
335 */
336 if ((t = (struct tablelist *)

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

345 *tlist = t;
346 return (0);
347}
348
349/*
350 * Add a command table.
351 */
352 public void
324
325 if (len == 0)
326 return (0);
327 /*
328 * Allocate a tablelist structure, initialize it,
329 * and link it into the list of tables.
330 */
331 if ((t = (struct tablelist *)

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

340 *tlist = t;
341 return (0);
342}
343
344/*
345 * Add a command table.
346 */
347 public void
353add_fcmd_table(buf, len)
354 char *buf;
355 int len;
348add_fcmd_table(char *buf, int len)
356{
357 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
358 error("Warning: some commands disabled", NULL_PARG);
359}
360
361/*
362 * Add an editing command table.
363 */
364 public void
349{
350 if (add_cmd_table(&list_fcmd_tables, buf, len) < 0)
351 error("Warning: some commands disabled", NULL_PARG);
352}
353
354/*
355 * Add an editing command table.
356 */
357 public void
365add_ecmd_table(buf, len)
366 char *buf;
367 int len;
358add_ecmd_table(char *buf, int len)
368{
369 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
370 error("Warning: some edit commands disabled", NULL_PARG);
371}
372
373/*
374 * Add an environment variable table.
375 */
376 static void
359{
360 if (add_cmd_table(&list_ecmd_tables, buf, len) < 0)
361 error("Warning: some edit commands disabled", NULL_PARG);
362}
363
364/*
365 * Add an environment variable table.
366 */
367 static void
377add_var_table(tlist, buf, len)
378 struct tablelist **tlist;
379 char *buf;
380 int len;
368add_var_table(struct tablelist **tlist, char *buf, int len)
381{
382 if (add_cmd_table(tlist, buf, len) < 0)
383 error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
384}
385
386/*
387 * Search a single command table for the command string in cmd.
388 */
389 static int
369{
370 if (add_cmd_table(tlist, buf, len) < 0)
371 error("Warning: environment variables from lesskey file unavailable", NULL_PARG);
372}
373
374/*
375 * Search a single command table for the command string in cmd.
376 */
377 static int
390cmd_search(cmd, table, endtable, sp)
391 char *cmd;
392 char *table;
393 char *endtable;
394 char **sp;
378cmd_search(char *cmd, char *table, char *endtable, char **sp)
395{
379{
396 register char *p;
397 register char *q;
398 register int a;
380 char *p;
381 char *q;
382 int a;
399
400 *sp = NULL;
401 for (p = table, q = cmd; p < endtable; p++, q++)
402 {
403 if (*p == *q)
404 {
405 /*
406 * Current characters match.

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

474 return (A_INVALID);
475}
476
477/*
478 * Decode a command character and return the associated action.
479 * The "extra" string, if any, is returned in sp.
480 */
481 static int
383
384 *sp = NULL;
385 for (p = table, q = cmd; p < endtable; p++, q++)
386 {
387 if (*p == *q)
388 {
389 /*
390 * Current characters match.

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

458 return (A_INVALID);
459}
460
461/*
462 * Decode a command character and return the associated action.
463 * The "extra" string, if any, is returned in sp.
464 */
465 static int
482cmd_decode(tlist, cmd, sp)
483 struct tablelist *tlist;
484 char *cmd;
485 char **sp;
466cmd_decode(struct tablelist *tlist, char *cmd, char **sp)
486{
467{
487 register struct tablelist *t;
488 register int action = A_INVALID;
468 struct tablelist *t;
469 int action = A_INVALID;
489
490 /*
491 * Search thru all the command tables.
492 * Stop when we find an action which is not A_INVALID.
493 */
494 for (t = tlist; t != NULL; t = t->t_next)
495 {
496 action = cmd_search(cmd, t->t_start, t->t_end, sp);

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

501 action = A_INVALID;
502 return (action);
503}
504
505/*
506 * Decode a command from the cmdtables list.
507 */
508 public int
470
471 /*
472 * Search thru all the command tables.
473 * Stop when we find an action which is not A_INVALID.
474 */
475 for (t = tlist; t != NULL; t = t->t_next)
476 {
477 action = cmd_search(cmd, t->t_start, t->t_end, sp);

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

482 action = A_INVALID;
483 return (action);
484}
485
486/*
487 * Decode a command from the cmdtables list.
488 */
489 public int
509fcmd_decode(cmd, sp)
510 char *cmd;
511 char **sp;
490fcmd_decode(char *cmd, char **sp)
512{
513 return (cmd_decode(list_fcmd_tables, cmd, sp));
514}
515
516/*
517 * Decode a command from the edittables list.
518 */
519 public int
491{
492 return (cmd_decode(list_fcmd_tables, cmd, sp));
493}
494
495/*
496 * Decode a command from the edittables list.
497 */
498 public int
520ecmd_decode(cmd, sp)
521 char *cmd;
522 char **sp;
499ecmd_decode(char *cmd, char **sp)
523{
524 return (cmd_decode(list_ecmd_tables, cmd, sp));
525}
526
527/*
528 * Get the value of an environment variable.
529 * Looks first in the lesskey file, then in the real environment.
530 */
531 public char *
500{
501 return (cmd_decode(list_ecmd_tables, cmd, sp));
502}
503
504/*
505 * Get the value of an environment variable.
506 * Looks first in the lesskey file, then in the real environment.
507 */
508 public char *
532lgetenv(var)
533 char *var;
509lgetenv(char *var)
534{
535 int a;
536 char *s;
537
538 a = cmd_decode(list_var_tables, var, &s);
539 if (a == EV_OK)
540 return (s);
541 s = getenv(var);

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

549
550#if USERFILE
551/*
552 * Get an "integer" from a lesskey file.
553 * Integers are stored in a funny format:
554 * two bytes, low order first, in radix KRADIX.
555 */
556 static int
510{
511 int a;
512 char *s;
513
514 a = cmd_decode(list_var_tables, var, &s);
515 if (a == EV_OK)
516 return (s);
517 s = getenv(var);

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

525
526#if USERFILE
527/*
528 * Get an "integer" from a lesskey file.
529 * Integers are stored in a funny format:
530 * two bytes, low order first, in radix KRADIX.
531 */
532 static int
557gint(sp)
558 char **sp;
533gint(char **sp)
559{
560 int n;
561
562 n = *(*sp)++;
563 n += *(*sp)++ * KRADIX;
564 return (n);
565}
566
567/*
568 * Process an old (pre-v241) lesskey file.
569 */
570 static int
534{
535 int n;
536
537 n = *(*sp)++;
538 n += *(*sp)++ * KRADIX;
539 return (n);
540}
541
542/*
543 * Process an old (pre-v241) lesskey file.
544 */
545 static int
571old_lesskey(buf, len)
572 char *buf;
573 int len;
546old_lesskey(char *buf, int len)
574{
575 /*
576 * Old-style lesskey file.
577 * The file must end with either
578 * ...,cmd,0,action
579 * or ...,cmd,0,action|A_EXTRA,string,0
580 * So the last byte or the second to last byte must be zero.
581 */
582 if (buf[len-1] != '\0' && buf[len-2] != '\0')
583 return (-1);
584 add_fcmd_table(buf, len);
585 return (0);
586}
587
588/*
589 * Process a new (post-v241) lesskey file.
590 */
591 static int
547{
548 /*
549 * Old-style lesskey file.
550 * The file must end with either
551 * ...,cmd,0,action
552 * or ...,cmd,0,action|A_EXTRA,string,0
553 * So the last byte or the second to last byte must be zero.
554 */
555 if (buf[len-1] != '\0' && buf[len-2] != '\0')
556 return (-1);
557 add_fcmd_table(buf, len);
558 return (0);
559}
560
561/*
562 * Process a new (post-v241) lesskey file.
563 */
564 static int
592new_lesskey(buf, len, sysvar)
593 char *buf;
594 int len;
595 int sysvar;
565new_lesskey(char *buf, int len, int sysvar)
596{
597 char *p;
566{
567 char *p;
598 register int c;
599 register int n;
568 int c;
569 int n;
600
601 /*
602 * New-style lesskey file.
603 * Extract the pieces.
604 */
605 if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
606 buf[len-2] != C1_END_LESSKEY_MAGIC ||
607 buf[len-1] != C2_END_LESSKEY_MAGIC)

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

638 }
639 }
640}
641
642/*
643 * Set up a user command table, based on a "lesskey" file.
644 */
645 public int
570
571 /*
572 * New-style lesskey file.
573 * Extract the pieces.
574 */
575 if (buf[len-3] != C0_END_LESSKEY_MAGIC ||
576 buf[len-2] != C1_END_LESSKEY_MAGIC ||
577 buf[len-1] != C2_END_LESSKEY_MAGIC)

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

608 }
609 }
610}
611
612/*
613 * Set up a user command table, based on a "lesskey" file.
614 */
615 public int
646lesskey(filename, sysvar)
647 char *filename;
648 int sysvar;
616lesskey(char *filename, int sysvar)
649{
617{
650 register char *buf;
651 register POSITION len;
652 register long n;
653 register int f;
618 char *buf;
619 POSITION len;
620 long n;
621 int f;
654
655 if (secure)
656 return (1);
657 /*
658 * Try to open the lesskey file.
659 */
660 filename = shell_unquote(filename);
661 f = open(filename, OPEN_READ);

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

708 return (old_lesskey(buf, (int)len));
709 return (new_lesskey(buf, (int)len, sysvar));
710}
711
712/*
713 * Add the standard lesskey file "$HOME/.less"
714 */
715 public void
622
623 if (secure)
624 return (1);
625 /*
626 * Try to open the lesskey file.
627 */
628 filename = shell_unquote(filename);
629 f = open(filename, OPEN_READ);

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

676 return (old_lesskey(buf, (int)len));
677 return (new_lesskey(buf, (int)len, sysvar));
678}
679
680/*
681 * Add the standard lesskey file "$HOME/.less"
682 */
683 public void
716add_hometable(envname, def_filename, sysvar)
717 char *envname;
718 char *def_filename;
719 int sysvar;
684add_hometable(char *envname, char *def_filename, int sysvar)
720{
721 char *filename;
722 PARG parg;
723
724 if (envname != NULL && (filename = lgetenv(envname)) != NULL)
725 filename = save(filename);
726 else if (sysvar)
727 filename = save(def_filename);

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

737 free(filename);
738}
739#endif
740
741/*
742 * See if a char is a special line-editing command.
743 */
744 public int
685{
686 char *filename;
687 PARG parg;
688
689 if (envname != NULL && (filename = lgetenv(envname)) != NULL)
690 filename = save(filename);
691 else if (sysvar)
692 filename = save(def_filename);

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

702 free(filename);
703}
704#endif
705
706/*
707 * See if a char is a special line-editing command.
708 */
709 public int
745editchar(c, flags)
746 int c;
747 int flags;
710editchar(int c, int flags)
748{
749 int action;
750 int nch;
751 char *s;
752 char usercmd[MAX_CMDLEN+1];
753
754 /*
755 * An editing character could actually be a sequence of characters;

--- 87 unchanged lines hidden ---
711{
712 int action;
713 int nch;
714 char *s;
715 char usercmd[MAX_CMDLEN+1];
716
717 /*
718 * An editing character could actually be a sequence of characters;

--- 87 unchanged lines hidden ---