Deleted Added
sdiff udiff text old ( 95095 ) new ( 95887 )
full compact
1/* $OpenBSD: eval.c,v 1.43 2002/02/16 21:27:48 millert Exp $ */
2/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
3
4/*
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.

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

33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41__SCCSID("@(#)eval.c 8.2 (Berkeley) 4/27/95");
42__RCSID_SOURCE("$OpenBSD: eval.c,v 1.43 2002/02/16 21:27:48 millert Exp $");
43__FBSDID("$FreeBSD: head/usr.bin/m4/eval.c 95095 2002-04-20 01:49:10Z jmallett $");
44
45/*
46 * eval.c
47 * Facility: m4 macro processor
48 * by: oz
49 */
50
51#include <sys/types.h>

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

101 * A call in the form of macro-or-builtin() will result in:
102 * argv[0] = nullstr
103 * argv[1] = macro-or-builtin
104 * argv[2] = nullstr
105 *
106 * argc is 3 for macro-or-builtin() and 2 for macro-or-builtin
107 */
108void
109eval(argv, argc, td)
110 const char *argv[];
111 int argc;
112 int td;
113{
114 ssize_t mark = -1;
115
116 expansion_id++;
117 if (td & RECDEF)
118 errx(1, "%s at line %lu: expanding recursive definition for %s",
119 CURRENT_NAME, CURRENT_LINE, argv[1]);
120 if (traced_macros && is_traced(argv[1]))

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

126 if (mark != -1)
127 finish_trace(mark);
128}
129
130/*
131 * expand_builtin - evaluate built-in macros.
132 */
133void
134expand_builtin(argv, argc, td)
135 const char *argv[];
136 int argc;
137 int td;
138{
139 int c, n;
140 int ac;
141 static int sysval = 0;
142
143#ifdef DEBUG
144 printf("argc = %d\n", argc);
145 for (n = 0; n < argc; n++)

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

493 break;
494 }
495}
496
497/*
498 * expand_macro - user-defined macro expansion
499 */
500void
501expand_macro(argv, argc)
502 const char *argv[];
503 int argc;
504{
505 const char *t;
506 const char *p;
507 int n;
508 int argno;
509
510 t = argv[0]; /* defn string as a whole */
511 p = t;

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

568 if (p == t) /* do last character */
569 PUTBACK(*p);
570}
571
572/*
573 * dodefine - install definition in the table
574 */
575void
576dodefine(name, defn)
577 const char *name;
578 const char *defn;
579{
580 ndptr p;
581 int n;
582
583 if (!*name)
584 errx(1, "%s at line %lu: null definition.", CURRENT_NAME,
585 CURRENT_LINE);
586 if ((p = lookup(name)) == nil)

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

606 p->type |= RECDEF;
607}
608
609/*
610 * dodefn - push back a quoted definition of
611 * the given name.
612 */
613static void
614dodefn(name)
615 const char *name;
616{
617 ndptr p;
618 const char *real;
619
620 if ((p = lookup(name)) != nil) {
621 if (p->defn != null) {
622 pbstr(rquote);
623 pbstr(p->defn);

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

632/*
633 * dopushdef - install a definition in the hash table
634 * without removing a previous definition. Since
635 * each new entry is entered in *front* of the
636 * hash bucket, it hides a previous definition from
637 * lookup.
638 */
639static void
640dopushdef(name, defn)
641 const char *name;
642 const char *defn;
643{
644 ndptr p;
645
646 if (!*name)
647 errx(1, "%s at line %lu: null definition", CURRENT_NAME,
648 CURRENT_LINE);
649 p = addent(name);
650 if (!*defn)

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

655 if (STREQ(name, defn))
656 p->type |= RECDEF;
657}
658
659/*
660 * dump_one_def - dump the specified definition.
661 */
662static void
663dump_one_def(p)
664 ndptr p;
665{
666 const char *real;
667
668 if (mimic_gnu) {
669 if ((p->type & TYPEMASK) == MACRTYPE)
670 fprintf(traceout, "%s:\t%s\n", p->name, p->defn);
671 else {
672 real = builtin_realname(p->type);

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

679}
680
681/*
682 * dodumpdef - dump the specified definitions in the hash
683 * table to stderr. If nothing is specified, the entire
684 * hash table is dumped.
685 */
686static void
687dodump(argv, argc)
688 const char *argv[];
689 int argc;
690{
691 int n;
692 ndptr p;
693
694 if (argc > 2) {
695 for (n = 2; n < argc; n++)
696 if ((p = lookup(argv[n])) != nil)
697 dump_one_def(p);
698 } else {
699 for (n = 0; n < HASHSIZE; n++)
700 for (p = hashtab[n]; p != nil; p = p->nxtptr)
701 dump_one_def(p);
702 }
703}
704
705/*
706 * dotrace - mark some macros as traced/untraced depending upon on.
707 */
708static void
709dotrace(argv, argc, on)
710 const char *argv[];
711 int argc;
712 int on;
713{
714 int n;
715
716 if (argc > 2) {
717 for (n = 2; n < argc; n++)
718 mark_traced(argv[n], on);
719 } else
720 mark_traced(NULL, on);
721}
722
723/*
724 * doifelse - select one of two alternatives - loop.
725 */
726static void
727doifelse(argv, argc)
728 const char *argv[];
729 int argc;
730{
731 cycle {
732 if (STREQ(argv[2], argv[3]))
733 pbstr(argv[4]);
734 else if (argc == 6)
735 pbstr(argv[5]);
736 else if (argc > 6) {
737 argv += 3;
738 argc -= 3;
739 continue;
740 }
741 break;
742 }
743}
744
745/*
746 * doinclude - include a given file.
747 */
748static int
749doincl(ifile)
750 const char *ifile;
751{
752 if (ilevel + 1 == MAXINP)
753 errx(1, "%s at line %lu: too many include files.",
754 CURRENT_NAME, CURRENT_LINE);
755 if (fopen_trypath(infile+ilevel+1, ifile) != NULL) {
756 ilevel++;
757 if ((inname[ilevel] = strdup(ifile)) == NULL)
758 err(1, NULL);

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

765}
766
767#ifdef EXTENDED
768/*
769 * dopaste - include a given file without any
770 * macro processing.
771 */
772static int
773dopaste(pfile)
774 const char *pfile;
775{
776 FILE *pf;
777 int c;
778
779 if ((pf = fopen(pfile, "r")) != NULL) {
780 fprintf(active, "#line 1 \"%s\"\n", pfile);
781 while ((c = getc(pf)) != EOF)
782 putc(c, active);
783 (void) fclose(pf);
784 emitline();
785 return (1);
786 } else
787 return (0);
788}
789#endif
790
791static void
792gnu_dochq(argv, ac)
793 const char *argv[];
794 int ac;
795{
796 /* In gnu-m4 mode, the only way to restore quotes is to have no
797 * arguments at all. */
798 if (ac == 2) {
799 lquote[0] = LQUOTE, lquote[1] = EOS;
800 rquote[0] = RQUOTE, rquote[1] = EOS;
801 } else {
802 strlcpy(lquote, argv[2], sizeof(lquote));
803 if(ac > 3)
804 strlcpy(rquote, argv[3], sizeof(rquote));
805 else
806 rquote[0] = EOS;
807 }
808}
809
810/*
811 * dochq - change quote characters
812 */
813static void
814dochq(argv, argc)
815 const char *argv[];
816 int argc;
817{
818 if (argc > 2) {
819 if (*argv[2])
820 strlcpy(lquote, argv[2], sizeof(lquote));
821 else {
822 lquote[0] = LQUOTE;
823 lquote[1] = EOS;
824 }

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

829 strcpy(rquote, lquote);
830 } else {
831 lquote[0] = LQUOTE, lquote[1] = EOS;
832 rquote[0] = RQUOTE, rquote[1] = EOS;
833 }
834}
835
836static void
837gnu_dochc(argv, ac)
838 const char *argv[];
839 int ac;
840{
841 /* In gnu-m4 mode, no arguments mean no comment
842 * arguments at all. */
843 if (ac == 2) {
844 scommt[0] = EOS;
845 ecommt[0] = EOS;
846 } else {
847 if (*argv[2])

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

853 else
854 ecommt[0] = ECOMMT, ecommt[1] = EOS;
855 }
856}
857/*
858 * dochc - change comment characters
859 */
860static void
861dochc(argv, argc)
862 const char *argv[];
863 int argc;
864{
865 if (argc > 2) {
866 if (*argv[2])
867 strlcpy(scommt, argv[2], sizeof(scommt));
868 if (argc > 3) {
869 if (*argv[3])
870 strlcpy(ecommt, argv[3], sizeof(ecommt));
871 }

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

877 ecommt[0] = ECOMMT, ecommt[1] = EOS;
878 }
879}
880
881/*
882 * dodivert - divert the output to a temporary file
883 */
884static void
885dodiv(n)
886 int n;
887{
888 int fd;
889
890 oindex = n;
891 if (n >= maxout) {
892 if (mimic_gnu)
893 resizedivs(n + 10);
894 else

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

909 active = outfile[n];
910}
911
912/*
913 * doundivert - undivert a specified output, or all
914 * other outputs, in numerical order.
915 */
916static void
917doundiv(argv, argc)
918 const char *argv[];
919 int argc;
920{
921 int ind;
922 int n;
923
924 if (argc > 2) {
925 for (ind = 2; ind < argc; ind++) {
926 n = atoi(argv[ind]);
927 if (n > 0 && n < maxout && outfile[n] != NULL)

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

934 if (outfile[n] != NULL)
935 getdiv(n);
936}
937
938/*
939 * dosub - select substring
940 */
941static void
942dosub(argv, argc)
943 const char *argv[];
944 int argc;
945{
946 const char *ap, *fc, *k;
947 int nc;
948
949 ap = argv[2]; /* target string */
950#ifdef EXPR
951 fc = ap + expr(argv[3]); /* first char */
952#else

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

985 * mapvec[n] == n). Even if the entry in the mapvec is null for an ordinary
986 * character, it will stabilize, since mapvec[0] == 0 at all times. At the
987 * end, we restore mapvec* back to normal where mapvec[n] == n for
988 * 0 <= n <= 127. This strategy, along with the restoration of mapvec, is
989 * about 5 times faster than any algorithm that makes multiple passes over
990 * destination string.
991 */
992static void
993map(dest, src, from, to)
994 char *dest;
995 const char *src;
996 const char *from;
997 const char *to;
998{
999 const char *tmp;
1000 unsigned char sch, dch;
1001 static char frombis[257];
1002 static char tobis[257];
1003 static unsigned char mapvec[256] = {
1004 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1005 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,

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

1061
1062
1063/*
1064 * handledash:
1065 * use buffer to copy the src string, expanding character ranges
1066 * on the way.
1067 */
1068static const char *
1069handledash(buffer, end, src)
1070 char *buffer;
1071 char *end;
1072 const char *src;
1073{
1074 char *p;
1075
1076 p = buffer;
1077 while(*src) {
1078 if (src[1] == '-' && src[2]) {
1079 unsigned char i;
1080 for (i = (unsigned char)src[0];

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

1089 } else
1090 *p++ = *src++;
1091 if (p == end)
1092 break;
1093 }
1094 *p = '\0';
1095 return buffer;
1096}
1097