1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1986-2009 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
19 ***********************************************************************/
25 * preprocessor library trace and debug support
34 * convert token string to printable form
38 pptokstr(register char* s
, register int c
)
46 while (*t
== ' ' || *t
== '\t') t
++;
63 return("`vertical-tab'");
67 if (iscntrl(c
) || !isprint(c
)) sfsprintf(buf
, sizeof(buf
), "`%03o'", c
);
68 else if (s
) return(s
);
69 else sfsprintf(buf
, sizeof(buf
), "%c", c
);
74 #if DEBUG & TRACE_debug
79 * return input stream name given index
83 ppinstr(register struct ppinstk
* p
)
89 for (i
= 0; i
< elementsof(ppinmap
); i
++)
90 if (p
->type
== ppinmap
[i
].val
)
100 sfsprintf(buf
, sizeof(buf
), "%s=%s", ppinmap
[i
].nam
, p
->symbol
->name
);
105 return(ppinmap
[i
].nam
);
107 sfsprintf(buf
, sizeof(buf
), "UNKNOWN[%d]", p
->type
);
112 * return string given fsm lex state
116 pplexstr(register int lex
)
122 if (lex
< 0) lex
&= ~lex
;
123 splice
= (lex
& SPLICE
);
125 for (i
= 0; i
< (elementsof(pplexmap
) - 1) && (lex
> pplexmap
[i
].val
|| lex
== pplexmap
[i
+1].val
); i
++);
126 if (lex
!= pplexmap
[i
].val
)
128 if (pplexmap
[i
].val
< 0) sfsprintf(buf
, sizeof(buf
), "%s|0x%04x%s", pplexmap
[i
].nam
, lex
, splice
? "|SPLICE" : "");
129 else sfsprintf(buf
, sizeof(buf
), "%s+%d", pplexmap
[i
-1].nam
, lex
- pplexmap
[i
-1].val
, splice
? "|SPLICE" : "");
134 sfsprintf(buf
, sizeof(buf
), "%s|SPLICE", pplexmap
[i
].nam
);
137 return(pplexmap
[i
].nam
);
141 * return string given map p of size n and flags
145 ppflagstr(register struct map
* p
, int n
, register long flags
)
151 static char buf
[128];
154 for (i
= 0; i
< n
; i
++)
155 if (flags
& p
[i
].val
)
157 k
= strlen(p
[i
].nam
);
158 if ((elementsof(buf
) - 2 - (s
- buf
)) > k
)
160 if (s
> buf
) *s
++ = '|';
170 * return string given pp.mode
174 ppmodestr(register long mode
)
176 return(ppflagstr(ppmodemap
, elementsof(ppmodemap
), mode
));
180 * return string given pp.option
184 ppoptionstr(register long option
)
186 return(ppflagstr(ppoptionmap
, elementsof(ppoptionmap
), option
));
190 * return string given pp.state
194 ppstatestr(register long state
)
196 return(ppflagstr(ppstatemap
, elementsof(ppstatemap
), state
));
202 * io stream stack trace
203 * sig==0 registers the handler
211 register struct ppinstk
* p
;
217 signal(SIGBUS
, pptrace
);
220 signal(SIGSEGV
, pptrace
);
223 signal(SIGILL
, pptrace
);
225 signal(SIGQUIT
, pptrace
);
231 sfprintf(sfstderr
, "\n%s during io stack trace\n", s
);
232 signal(handling
, SIG_DFL
);
233 sigunblock(handling
);
234 kill(getpid(), handling
);
236 error(PANIC
, "signal not redelivered");
239 sfprintf(sfstderr
, "\n%s - io stack trace\n", s
);
240 for (p
= pp
.in
; p
->prev
; p
= p
->prev
)
242 sfprintf(sfstderr
, "\n[%s]\n", ppinstr(p
));
243 if ((s
= pp
.in
->nextchr
) && *s
)
245 if (*s
!= '\n') sfputc(sfstderr
, '\t');
249 sfputc(sfstderr
, *s
);
250 if (*s
++ == '\n' && *s
&& *s
!= '\n') sfputc(sfstderr
, '\t');
252 if (*s
) sfprintf(sfstderr
, " ...");
255 sfprintf(sfstderr
, "\n");
257 signal(sig
, SIG_DFL
);
261 error(PANIC
, "signal not redelivered");