/*
* State machine utility functions
*/
static void
ignoreoff(void)
{
if (depth == 0)
abort(); /* bug */
ignoring[depth] = ignoring[depth-1];
}
static void
ignoreon(void)
{
ignoring[depth] = true;
}
static void
keywordedit(const char *replacement)
{
snprintf(keyword, tline + sizeof(tline) – keyword,
“%s%s”, replacement, newline);
print();
}
static void
nest(void)
{
if (depth > MAXDEPTH-1)
abort(); /* bug */
if (depth == MAXDEPTH-1)
error(“Too many levels of nesting”);
depth += 1;
stifline[depth] = linenum;
}
static void
unnest(void)
{
if (depth == 0)
abort(); /* bug */
depth -= 1;
}
static void
state(Ifstate is)
{
ifstate[depth] = is;
}
/*
* Write a line to the output or not, according to command line options.
*/
static void
flushline(bool keep)
{
if (symlist)
return;
if (keep ^ complement) {
bool blankline = tline[strspn(tline, " \t\r\n")] == ‘\0′;
if (blankline && compblank && blankcount != blankmax) {
delcount += 1;
blankcount += 1;
} else {
if (lnnum && delcount > 0)
printf(“#line %d%s”, linenum, newline);
fputs(tline, output);
delcount = 0;
blankmax = blankcount = blankline ? blankcount + 1 : 0;
}
} else {
if (lnblank)
fputs(newline, output);
exitstat = 1;
delcount += 1;
blankcount = 0;
}
if (debugging)
fflush(output);
}
/*
* The driver for the state machine.
*/
static void
process(void)
{
/* When compressing blank lines, act as if the file
is preceded by a large number of blank lines. */
blankmax = blankcount = 1000;
for (;;) {
Linetype lineval = parseline();
trans_table[ifstate[depth]][lineval]();
debug(“process line %d %s -> %s depth %d”,
linenum, linetype_name[lineval],
ifstate_name[ifstate[depth]], depth);
}
}
/*
* Flush the output and handle errors.
*/
static void
closeout(void)
{
if (symdepth && !zerosyms)
printf(“\n”);
if (fclose(output) == EOF) {
warn(“couldn’t write to %s”, ofilename);
if (overwriting) {
unlink(tempname);
errx(2, “%s unchanged”, filename);
} else {
exit(2);
}
}
}
/*
* Clean up and exit.
*/
static void
done(void)
{
if (incomment)
error(“EOF in comment”);
closeout();
if (overwriting && rename(tempname, ofilename) == -1) {
warn(“couldn’t rename temporary file”);
unlink(tempname);
errx(2, “%s unchanged”, ofilename);
}
exit(exitstat);
}