14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*10898Sroland.mainz@nrubsig.org * Copyright (c) 1986-2009 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * *
194887Schin ***********************************************************************/
204887Schin #pragma prototyped
214887Schin /*
224887Schin * Glenn Fowler
234887Schin * AT&T Research
244887Schin *
254887Schin * preprocessor library trace and debug support
264887Schin */
274887Schin
284887Schin #include "pplib.h"
294887Schin #include "ppfsm.h"
304887Schin
314887Schin #include <ctype.h>
324887Schin
334887Schin /*
344887Schin * convert token string to printable form
354887Schin */
364887Schin
374887Schin char*
pptokstr(register char * s,register int c)384887Schin pptokstr(register char* s, register int c)
394887Schin {
404887Schin register char* t;
414887Schin
424887Schin static char buf[8];
434887Schin
444887Schin if (t = s)
454887Schin {
464887Schin while (*t == ' ' || *t == '\t') t++;
474887Schin c = *t ? *t : *s;
484887Schin }
494887Schin switch (c)
504887Schin {
514887Schin case 0:
524887Schin case 0400:
534887Schin return("`EOF'");
544887Schin case ' ':
554887Schin return("`space'");
564887Schin case '\f':
574887Schin return("`formfeed'");
584887Schin case '\n':
594887Schin return("`newline'");
604887Schin case '\t':
614887Schin return("`tab'");
624887Schin case '\v':
634887Schin return("`vertical-tab'");
644887Schin case T_TOKCAT:
654887Schin return("##");
664887Schin default:
674887Schin if (iscntrl(c) || !isprint(c)) sfsprintf(buf, sizeof(buf), "`%03o'", c);
684887Schin else if (s) return(s);
694887Schin else sfsprintf(buf, sizeof(buf), "%c", c);
704887Schin return(buf);
714887Schin }
724887Schin }
734887Schin
744887Schin #if DEBUG & TRACE_debug
754887Schin
764887Schin #include "ppdebug.h"
774887Schin
784887Schin /*
794887Schin * return input stream name given index
804887Schin */
814887Schin
824887Schin char*
ppinstr(register struct ppinstk * p)834887Schin ppinstr(register struct ppinstk* p)
844887Schin {
854887Schin register int i;
864887Schin
874887Schin static char buf[128];
884887Schin
894887Schin for (i = 0; i < elementsof(ppinmap); i++)
904887Schin if (p->type == ppinmap[i].val)
914887Schin {
924887Schin switch (p->type)
934887Schin {
944887Schin case IN_MACRO:
954887Schin #if MACDEF
964887Schin case IN_MULTILINE:
974887Schin #endif
984887Schin if (p->symbol)
994887Schin {
1004887Schin sfsprintf(buf, sizeof(buf), "%s=%s", ppinmap[i].nam, p->symbol->name);
1014887Schin return(buf);
1024887Schin }
1034887Schin break;
1044887Schin }
1054887Schin return(ppinmap[i].nam);
1064887Schin }
1074887Schin sfsprintf(buf, sizeof(buf), "UNKNOWN[%d]", p->type);
1084887Schin return(buf);
1094887Schin }
1104887Schin
1114887Schin /*
1124887Schin * return string given fsm lex state
1134887Schin */
1144887Schin
1154887Schin char*
pplexstr(register int lex)1164887Schin pplexstr(register int lex)
1174887Schin {
1184887Schin register int i;
1194887Schin int splice;
1204887Schin static char buf[64];
1214887Schin
1224887Schin if (lex < 0) lex &= ~lex;
1234887Schin splice = (lex & SPLICE);
1244887Schin lex &= 0x7f;
1254887Schin for (i = 0; i < (elementsof(pplexmap) - 1) && (lex > pplexmap[i].val || lex == pplexmap[i+1].val); i++);
1264887Schin if (lex != pplexmap[i].val)
1274887Schin {
1284887Schin if (pplexmap[i].val < 0) sfsprintf(buf, sizeof(buf), "%s|0x%04x%s", pplexmap[i].nam, lex, splice ? "|SPLICE" : "");
1294887Schin else sfsprintf(buf, sizeof(buf), "%s+%d", pplexmap[i-1].nam, lex - pplexmap[i-1].val, splice ? "|SPLICE" : "");
1304887Schin return(buf);
1314887Schin }
1324887Schin if (splice)
1334887Schin {
1344887Schin sfsprintf(buf, sizeof(buf), "%s|SPLICE", pplexmap[i].nam);
1354887Schin return(buf);
1364887Schin }
1374887Schin return(pplexmap[i].nam);
1384887Schin }
1394887Schin
1404887Schin /*
1414887Schin * return string given map p of size n and flags
1424887Schin */
1434887Schin
1444887Schin static char*
ppflagstr(register struct map * p,int n,register long flags)1454887Schin ppflagstr(register struct map* p, int n, register long flags)
1464887Schin {
1474887Schin register int i;
1484887Schin register int k;
1494887Schin register char* s;
1504887Schin
1514887Schin static char buf[128];
1524887Schin
1534887Schin s = buf;
1544887Schin for (i = 0; i < n; i++)
1554887Schin if (flags & p[i].val)
1564887Schin {
1574887Schin k = strlen(p[i].nam);
1584887Schin if ((elementsof(buf) - 2 - (s - buf)) > k)
1594887Schin {
1604887Schin if (s > buf) *s++ = '|';
1614887Schin strcpy(s, p[i].nam);
1624887Schin s += k;
1634887Schin }
1644887Schin }
1654887Schin *s = 0;
1664887Schin return(buf);
1674887Schin }
1684887Schin
1694887Schin /*
1704887Schin * return string given pp.mode
1714887Schin */
1724887Schin
1734887Schin char*
ppmodestr(register long mode)1744887Schin ppmodestr(register long mode)
1754887Schin {
1764887Schin return(ppflagstr(ppmodemap, elementsof(ppmodemap), mode));
1774887Schin }
1784887Schin
1794887Schin /*
1804887Schin * return string given pp.option
1814887Schin */
1824887Schin
1834887Schin char*
ppoptionstr(register long option)1844887Schin ppoptionstr(register long option)
1854887Schin {
1864887Schin return(ppflagstr(ppoptionmap, elementsof(ppoptionmap), option));
1874887Schin }
1884887Schin
1894887Schin /*
1904887Schin * return string given pp.state
1914887Schin */
1924887Schin
1934887Schin char*
ppstatestr(register long state)1944887Schin ppstatestr(register long state)
1954887Schin {
1964887Schin return(ppflagstr(ppstatemap, elementsof(ppstatemap), state));
1974887Schin }
1984887Schin
1994887Schin #include <sig.h>
2004887Schin
2014887Schin /*
2024887Schin * io stream stack trace
2034887Schin * sig==0 registers the handler
2044887Schin */
2054887Schin
2064887Schin void
pptrace(int sig)2074887Schin pptrace(int sig)
2084887Schin {
2094887Schin register char* s;
2104887Schin register char* x;
2114887Schin register struct ppinstk* p;
2124887Schin static int handling;
2134887Schin
2144887Schin if (!sig)
2154887Schin {
2164887Schin #ifdef SIGBUS
2174887Schin signal(SIGBUS, pptrace);
2184887Schin #endif
2194887Schin #ifdef SIGSEGV
2204887Schin signal(SIGSEGV, pptrace);
2214887Schin #endif
2224887Schin #ifdef SIGILL
2234887Schin signal(SIGILL, pptrace);
2244887Schin #endif
2254887Schin signal(SIGQUIT, pptrace);
2264887Schin return;
2274887Schin }
2284887Schin s = fmtsignal(sig);
2294887Schin if (handling)
2304887Schin {
2314887Schin sfprintf(sfstderr, "\n%s during io stack trace\n", s);
2324887Schin signal(handling, SIG_DFL);
2334887Schin sigunblock(handling);
2344887Schin kill(getpid(), handling);
2354887Schin pause();
2364887Schin error(PANIC, "signal not redelivered");
2374887Schin }
2384887Schin handling = sig;
2394887Schin sfprintf(sfstderr, "\n%s - io stack trace\n", s);
2404887Schin for (p = pp.in; p->prev; p = p->prev)
2414887Schin {
2424887Schin sfprintf(sfstderr, "\n[%s]\n", ppinstr(p));
2434887Schin if ((s = pp.in->nextchr) && *s)
2444887Schin {
2454887Schin if (*s != '\n') sfputc(sfstderr, '\t');
2464887Schin x = s + 256;
2474887Schin while (*s && s < x)
2484887Schin {
2494887Schin sfputc(sfstderr, *s);
2504887Schin if (*s++ == '\n' && *s && *s != '\n') sfputc(sfstderr, '\t');
2514887Schin }
2524887Schin if (*s) sfprintf(sfstderr, " ...");
2534887Schin }
2544887Schin }
2554887Schin sfprintf(sfstderr, "\n");
2564887Schin handling = 0;
2574887Schin signal(sig, SIG_DFL);
2584887Schin sigunblock(sig);
2594887Schin kill(getpid(), sig);
2604887Schin pause();
2614887Schin error(PANIC, "signal not redelivered");
2624887Schin }
2634887Schin
2644887Schin #endif
265