Newer
Older
incomment = CHAR_LITERAL;
linestate = LS_DIRTY;
cp += 1;
} else if (strncmp(cp, "\"", 1) == 0) {
incomment = STRING_LITERAL;
linestate = LS_DIRTY;
cp += 1;
} else if (strncmp(cp, "\n", 1) == 0) {
linestate = LS_START;
cp += 1;
} else if (strchr(" \r\t", *cp) != NULL) {
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
cp += 1;
} else
return (cp);
continue;
case CXX_COMMENT:
if (strncmp(cp, "\n", 1) == 0) {
incomment = NO_COMMENT;
linestate = LS_START;
}
cp += 1;
continue;
case CHAR_LITERAL:
case STRING_LITERAL:
if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
(incomment == STRING_LITERAL && cp[0] == '\"')) {
incomment = NO_COMMENT;
cp += 1;
} else if (cp[0] == '\\') {
if (cp[1] == '\0')
cp += 1;
else
cp += 2;
} else if (strncmp(cp, "\n", 1) == 0) {
if (incomment == CHAR_LITERAL)
error("unterminated char literal");
else
error("unterminated string literal");
} else
cp += 1;
continue;
case C_COMMENT:
if (strncmp(cp, "*\\\r\n", 4) == 0) {
incomment = FINISHING_COMMENT;
cp += 4;
} else if (strncmp(cp, "*\\\n", 3) == 0) {
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
incomment = FINISHING_COMMENT;
cp += 3;
} else if (strncmp(cp, "*/", 2) == 0) {
incomment = NO_COMMENT;
cp += 2;
} else
cp += 1;
continue;
case STARTING_COMMENT:
if (*cp == '*') {
incomment = C_COMMENT;
cp += 1;
} else if (*cp == '/') {
incomment = CXX_COMMENT;
cp += 1;
} else {
incomment = NO_COMMENT;
linestate = LS_DIRTY;
}
continue;
case FINISHING_COMMENT:
if (*cp == '/') {
incomment = NO_COMMENT;
cp += 1;
} else
incomment = C_COMMENT;
continue;
default:
abort(); /* bug */
}
return (cp);
}
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
/*
* Skip macro arguments.
*/
static const char *
skipargs(const char *cp)
{
const char *ocp = cp;
int level = 0;
cp = skipcomment(cp);
if (*cp != '(')
return (cp);
do {
if (*cp == '(')
level++;
if (*cp == ')')
level--;
cp = skipcomment(cp+1);
} while (level != 0 && *cp != '\0');
if (level == 0)
return (cp);
else
/* Rewind and re-detect the syntax error later. */
return (ocp);
}
/*
* Skip over an identifier.
*/
static const char *
skipsym(const char *cp)
{
while (!endsym(*cp))
++cp;
return (cp);
}
/*
* Look for the symbol in the symbol table. If it is found, we return
* the symbol table index, else we return -1.
*/
static int
findsym(const char *str)
{
const char *cp;
int symind;
cp = skipsym(str);
if (cp == str)
return (-1);
if (symlist) {
if (symdepth && firstsym)
printf("%s%3d", zerosyms ? "" : "\n", depth);
firstsym = zerosyms = false;
printf("%s%.*s%s",
symdepth ? " " : "",
(int)(cp-str), str,
symdepth ? "" : "\n");
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
/* we don't care about the value of the symbol */
return (0);
}
for (symind = 0; symind < nsyms; ++symind) {
if (strlcmp(symname[symind], str, cp-str) == 0) {
debug("findsym %s %s", symname[symind],
value[symind] ? value[symind] : "");
return (symind);
}
}
return (-1);
}
/*
* Add a symbol to the symbol table.
*/
static void
addsym(bool ignorethis, bool definethis, char *sym)
{
int symind;
char *val;
symind = findsym(sym);
if (symind < 0) {
if (nsyms >= MAXSYMS)
errx(2, "too many symbols");
symind = nsyms++;
}
symname[symind] = sym;
ignore[symind] = ignorethis;
val = sym + (skipsym(sym) - sym);
if (definethis) {
if (*val == '=') {
value[symind] = val+1;
*val = '\0';
} else if (*val == '\0')
else
usage();
} else {
if (*val != '\0')
usage();
value[symind] = NULL;
}
debug("addsym %s=%s", symname[symind],
value[symind] ? value[symind] : "undef");
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
}
/*
* Compare s with n characters of t.
* The same as strncmp() except that it checks that s[n] == '\0'.
*/
static int
strlcmp(const char *s, const char *t, size_t n)
{
while (n-- && *t != '\0')
if (*s != *t)
return ((unsigned char)*s - (unsigned char)*t);
else
++s, ++t;
return ((unsigned char)*s);
}
/*
* Diagnostics.
*/
static void
debug(const char *msg, ...)
{
va_list ap;
if (debugging) {
va_start(ap, msg);
vwarnx(msg, ap);
va_end(ap);
}
}
static void
error(const char *msg)
{
if (depth == 0)
warnx("%s: %d: %s", filename, linenum, msg);
else
warnx("%s: %d: %s (#if line %d depth %d)",
filename, linenum, msg, stifline[depth], depth);