1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
/* o_messages.cpp
*
* Copyright (c) 2008, eFTE SF Group (see AUTHORS file)
* Copyright (c) 1994-1996, Marko Macek
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
#include "fte.h"
#include "s_files.h"
#include "c_commands.h"
#define MAXREGEXP 32
EMessages *CompilerMsgs = 0;
static int NCRegexp = 0;
static struct {
int RefFile;
int RefLine;
int RefMsg;
RxNode *rx;
} CRegexp[MAXREGEXP];
int AddCRegexp(int file, int line, int msg, const char *regexp) {
if (NCRegexp >= MAXREGEXP) return 0;
CRegexp[NCRegexp].RefFile = file;
CRegexp[NCRegexp].RefLine = line;
CRegexp[NCRegexp].RefMsg = msg;
if ((CRegexp[NCRegexp].rx = RxCompile(regexp)) == NULL) {
return 0;
}
NCRegexp++;
return 1;
}
void FreeCRegexp() {
while (NCRegexp--) {
RxFree(CRegexp[NCRegexp].rx);
}
}
EMessages::EMessages(int createFlags, EModel **ARoot, char *ADir, char *ACommand): EList(createFlags, ARoot, "Messages") {
CompilerMsgs = this;
ErrCount = 0;
ErrList = 0;
Running = 0;
BufLen = 0;
BufPos = 0;
Command = 0;
Directory = 0;
MatchCount = 0;
ReturnCode = -1;
Running = 1;
curr_dir = 0;
RunPipe(ADir, ACommand);
}
EMessages::~EMessages() {
gui->ClosePipe(PipeId);
FreeErrors();
free(Command);
free(Directory);
CompilerMsgs = 0;
freeDirStack();
}
void EMessages::freeDirStack() {
while (curr_dir != 0) {
aDir *a = curr_dir;
curr_dir = curr_dir->next;
free(a->name);
delete a;
}
}
void EMessages::NotifyDelete(EModel *Deleting) {
for (int i = 0; i < ErrCount; i++) {
if (ErrList[i]->Buf == Deleting) {
/* NOT NEEDED!
char bk[16];
sprintf(bk, "_MSG.%d", i);
((EBuffer *)Deleting)->RemoveBookmark(bk);
*/
ErrList[i]->Buf = 0;
}
}
}
void EMessages::FindErrorFiles() {
for (int i = 0; i < ErrCount; i++)
if (ErrList[i]->Buf == 0 && ErrList[i]->file != 0)
FindErrorFile(i);
}
void EMessages::FindErrorFile(int err) {
assert(err >= 0 && err < ErrCount);
if (ErrList[err]->file == 0)
return ;
EBuffer *B;
ErrList[err]->Buf = 0;
B = FindFile(ErrList[err]->file);
if (B == 0)
return ;
if (B->Loaded == 0)
return;
AddFileError(B, err);
}
void EMessages::AddFileError(EBuffer *B, int err) {
char bk[16];
EPoint P;
assert(err >= 0 && err < ErrCount);
sprintf(bk, "_MSG.%d", err);
P.Col = 0;
P.Row = ErrList[err]->line - 1; // offset 0
if (P.Row >= B->RCount)
P.Row = B->RCount - 1;
if (P.Row < 0)
P.Row = 0;
if (B->PlaceBookmark(bk, P) == 1)
ErrList[err]->Buf = B;
}
void EMessages::FindFileErrors(EBuffer *B) {
for (int i = 0; i < ErrCount; i++)
if (ErrList[i]->Buf == 0 && ErrList[i]->file != 0) {
if (filecmp(B->FileName, ErrList[i]->file) == 0) {
AddFileError(B, i);
}
}
}
int EMessages::RunPipe(char *ADir, char *ACommand) {
if (!KeepMessages)
FreeErrors();
free(Command);
free(Directory);
Command = strdup(ACommand);
Directory = strdup(ADir);
MatchCount = 0;
ReturnCode = -1;
Running = 1;
BufLen = BufPos = 0;
Row = ErrCount - 1;
{
char s[2 * MAXPATH * 4];
sprintf(s, "[running '%s' in '%s']", Command, Directory);
AddError(0, -1, 0, s);
}
{
char s[MAXPATH * 2];
sprintf(s, "Messages [%s]: %s", Directory, Command);
SetTitle(s);
}
ChangeDir(Directory);
PipeId = gui->OpenPipe(Command, this);
return 0;
}
EEventMap *EMessages::GetEventMap() {
return FindEventMap("MESSAGES");
}
int EMessages::ExecCommand(int Command, ExState &State) {
switch (Command) {
case ExChildClose:
if (Running == 0 || PipeId == -1)
break;
ReturnCode = gui->ClosePipe(PipeId);
PipeId = -1;
Running = 0;
{
char s[30];
sprintf(s, "[aborted, status=%d]", ReturnCode);
AddError(0, -1, 0, s);
}
return ErOK;
case ExActivateInOtherWindow:
ShowError(View->Next, Row);
return ErOK;
}
return EList::ExecCommand(Command, State);
}
void EMessages::AddError(Error *p) {
ErrCount++;
ErrList = (Error **) realloc(ErrList, sizeof(void *) * ErrCount);
ErrList[ErrCount - 1] = p;
ErrList[ErrCount - 1]->Buf = 0;
FindErrorFile(ErrCount - 1);
if (ErrCount > Count)
if (Row >= Count - 1) {
//if (ErrCount > 1 && !ErrList[TopRow]->file)
Row = ErrCount - 1;
}
UpdateList();
}
void EMessages::AddError(char *file, int line, char *msg, const char *text, int hilit) {
Error *pe;
pe = (Error *) malloc(sizeof(Error));
if (pe == 0)
return ;
pe->file = file ? strdup(file) : 0;
pe->line = line;
pe->msg = msg ? strdup(msg) : 0;
pe->text = text ? strdup(text) : 0;
pe->hilit = hilit;
AddError(pe);
}
void EMessages::FreeErrors() {
if (ErrList) {
for (int i = 0; i < ErrCount; i++) {
if (ErrList[i]->Buf != 0) {
char bk[16];
sprintf(bk, "_MSG.%d", i);
((EBuffer *)(ErrList[i]->Buf))->RemoveBookmark(bk);
}
free(ErrList[i]->msg);
free(ErrList[i]->text);
free(ErrList[i]->file);
free(ErrList[i]);
}
free(ErrList);
}
ErrCount = 0;
ErrList = 0;
BufLen = BufPos = 0;
}
int EMessages::GetLine(char *Line, int maxim) {
int rc;
char *p;
int l;
//fprintf(stderr, "GetLine: %d\n", Running);
*Line = 0;
if (Running && PipeId != -1) {
rc = gui->ReadPipe(PipeId, MsgBuf + BufLen, sizeof(MsgBuf) - BufLen);
//fprintf(stderr, "GetLine: ReadPipe rc = %d\n", rc);
if (rc == -1) {
ReturnCode = gui->ClosePipe(PipeId);
PipeId = -1;
Running = 0;
}
if (rc > 0)
BufLen += rc;
}
l = maxim - 1;
if (BufLen - BufPos < l)
l = BufLen - BufPos;
//fprintf(stderr, "GetLine: Data %d\n", l);
p = (char *)memchr(MsgBuf + BufPos, '\n', l);
if (p) {
*p = 0;
strcpy(Line, MsgBuf + BufPos);
l = strlen(Line);
if (l > 0 && Line[l - 1] == '\r')
Line[l - 1] = 0;
BufPos = p + 1 - MsgBuf;
//fprintf(stderr, "GetLine: Line %d\n", strlen(Line));
} else if (Running && sizeof(MsgBuf) != BufLen) {
memmove(MsgBuf, MsgBuf + BufPos, BufLen - BufPos);
BufLen -= BufPos;
BufPos = 0;
//fprintf(stderr, "GetLine: Line Incomplete\n");
return 0;
} else {
if (l == 0)
return 0;
memcpy(Line, MsgBuf + BufPos, l);
Line[l] = 0;
if (l > 0 && Line[l - 1] == '\r')
Line[l - 1] = 0;
BufPos += l;
//fprintf(stderr, "GetLine: Line Last %d\n", l);
}
memmove(MsgBuf, MsgBuf + BufPos, BufLen - BufPos);
BufLen -= BufPos;
BufPos = 0;
//fprintf(stderr, "GetLine: Got Line\n");
return 1;
}
static void getWord(char* dest, char*& pin) {
char *pout, *pend;
char ch, ec;
while (*pin == ' ' || *pin == '\t')
pin++;
pout = dest;
pend = dest + 256 - 1;
if (*pin == '\'' || *pin == '"' || *pin == '`') {
ec = *pin++;
if (ec == '`')
ec = '\'';
for (;;) {
ch = *pin++;
if (ch == '`')
ch = '\'';
if (ch == ec || ch == 0)
break;
if (pout < pend)
*pout++ = ch;
}
if (ch == 0)
pin--;
} else {
for (;;) {
ch = *pin++;
if (ch == ' ' || ch == '\t' || ch == 0)
break;
if (pout < pend) *pout++ = ch;
}
}
*pout = 0;
}
void EMessages::GetErrors() {
char line[4096];
RxMatchRes RM;
//int retc;
int i, n;
int didmatch = 0;
int WasRunning = Running;
char fn[256];
//fprintf(stderr, "Reading pipe\n");
while (GetLine((char *)line, sizeof(line))) {
if (strlen(line) > 0 && line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
didmatch = 0;
for (i = 0; i < NCRegexp; i++) {
if (RxExec(CRegexp[i].rx, line, strlen(line), line, &RM) == 1) {
char ln[256] = "";
char msg[256] = "";
char fn1[256] = "";
char fn2[256] = "";
char *file = 0;
*fn = 0;
memset(fn, 0, 256);
memset(ln, 0, 256);
memset(msg, 0, 256);
n = CRegexp[i].RefFile;
if (RM.Close[n] - RM.Open[n] < 256)
memcpy(fn, line + RM.Open[n], RM.Close[n] - RM.Open[n]);
n = CRegexp[i].RefLine;
if (RM.Close[n] - RM.Open[n] < 256)
memcpy(ln, line + RM.Open[n], RM.Close[n] - RM.Open[n]);
n = CRegexp[i].RefMsg;
if (RM.Close[n] - RM.Open[n] < 256)
memcpy(msg, line + RM.Open[n], RM.Close[n] - RM.Open[n]);
if (IsFullPath(fn))
file = fn;
else {
if (curr_dir == 0)
strcpy(fn1, Directory);
else
strcpy(fn1, curr_dir->name);
Slash(fn1, 1);
strcat(fn1, fn);
if (ExpandPath(fn1, fn2, sizeof(fn2)) == 0)
file = fn2;
else
file = fn1;
}
AddError(file, atoi(ln), msg[0] ? msg : 0, line, 1);
didmatch = 1;
MatchCount++;
break;
}
}
if (!didmatch) {
AddError(0, -1, 0, line);
//** Quicky: check for gnumake 'entering directory'
//** make[x]: entering directory `xxx'
//** make[x]: leaving...
static char t1[] = "entering directory";
static char t2[] = "leaving directory";
char* pin;
if ((pin = strstr(line, "]:")) != 0) {
//** It *is* some make line.. Check for 'entering'..
pin += 2;
//while(*pin != ':' && *pin != 0) pin++;
//pin++;
while (*pin == ' ')
pin++;
if (strnicmp(pin, t1, sizeof(t1) - 1) == 0) { // Entering?
//** Get the directory name from the line,
pin += sizeof(t1) - 1;
getWord(fn, pin);
//dbg("entering %s", fn);
if (*fn) {
//** Indeedy entering directory! Link in list,
aDir * a = new aDir;
assert(a != 0);
a->name = strdup(fn);
assert(a->name != 0);
a->next = curr_dir;
curr_dir = a;
}
} else if (strnicmp(pin, t2, sizeof(t2) - 1) == 0) { // Leaving?
pin += sizeof(t2) - 1;
getWord(fn, pin); // Get dirname,
//dbg("leaving %s", fn);
aDir *a;
a = curr_dir;
if (a != 0)
curr_dir = curr_dir->next; // Remove from stack,
if (a != 0 && stricmp(a->name, fn) == 0) {
//** Pop this item.
free(a->name);
delete a;
} else {
//** Mismatch filenames -> error, and revoke stack.
//dbg("mismatch on %s", fn);
AddError(0, -1, 0, "efte: mismatch in directory stack!?");
//** In this case we totally die the stack..
while (a != 0) {
free(a->name);
delete a;
a = curr_dir;
if (a != 0)
curr_dir = curr_dir->next;
}
}
}
}
}
}
//fprintf(stderr, "Reading Stopped\n");
if (!Running && WasRunning) {
char s[30];
sprintf(s, "[done, status=%d]", ReturnCode);
AddError(0, -1, 0, s);
}
//UpdateList();
//NeedsUpdate = 1;
}
int EMessages::CompilePrevError(EView *V) {
if (ErrCount > 0) {
bad:
if (Row > 0) {
Row--;
if (ErrList[Row]->line == -1 || !ErrList[Row]->file) goto bad;
ShowError(V, Row);
} else {
V->Msg(S_INFO, "No previous error.");
return 0;
}
} else {
V->Msg(S_INFO, "No errors.");
return 0;
}
return 1;
}
int EMessages::CompileNextError(EView *V) {
if (ErrCount > 0) {
bad:
if (Row < ErrCount - 1) {
Row++;
if (ErrList[Row]->line == -1 || !ErrList[Row]->file) goto bad;
ShowError(V, Row);
} else {
if (Running)
V->Msg(S_INFO, "No more errors (yet).");
else
V->Msg(S_INFO, "No more errors.");
return 0;
}
} else {
if (Running)
V->Msg(S_INFO, "No errors (yet).");
else
V->Msg(S_INFO, "No errors.");
return 0;
}
return 1;
}
int EMessages::Compile(char * /*Command*/) {
return 0;
}
void EMessages::ShowError(EView *V, int err) {
if (err >= 0 && err < ErrCount) {
if (ErrList[err]->file) {
// should check if relative path
// possibly scan for (gnumake) directory info in output
if (ErrList[err]->Buf != 0) {
char bk[16];
V->SwitchToModel(ErrList[err]->Buf);
sprintf(bk, "_MSG.%d", err);
ErrList[err]->Buf->GotoBookmark(bk);
} else {
if (FileLoad(0, ErrList[err]->file, 0, V) == 1) {
V->SwitchToModel(ActiveModel);
((EBuffer *)ActiveModel)->CenterNearPosR(0, ErrList[err]->line - 1);
}
}
if (ErrList[err]->msg != 0)
V->Msg(S_INFO, "%s", ErrList[err]->msg);
else
V->Msg(S_INFO, "%s", ErrList[err]->text);
}
}
}
void EMessages::DrawLine(PCell B, int Line, int Col, ChColor color, int Width) {
if (Line < ErrCount)
if (Col < int(strlen(ErrList[Line]->text))) {
char str[1024];
int len;
len = UnTabStr(str, sizeof(str),
ErrList[Line]->text,
strlen(ErrList[Line]->text));
if (len > Col)
MoveStr(B, 0, Width, str + Col, color, Width);
}
}
char* EMessages::FormatLine(int Line) {
char *p;
if (Line < ErrCount)
p = strdup(ErrList[Line]->text);
else
p = 0;
return p;
}
int EMessages::IsHilited(int Line) {
return (Line >= 0 && Line < ErrCount) ? ErrList[Line]->hilit : 0;
}
void EMessages::UpdateList() {
Count = ErrCount;
EList::UpdateList();
}
int EMessages::Activate(int /*No*/) {
//assert(No == Row);
//Row = No;
ShowError(View, Row);
return 1;
}
int EMessages::CanActivate(int Line) {
int ok = 0;
if (Line < ErrCount)
if (ErrList[Line]->file || ErrList[Line]->line != -1) ok = 1;
return ok;
}
void EMessages::NotifyPipe(int APipeId) {
//fprintf(stderr, "Got notified");
if (APipeId == PipeId)
GetErrors();
}
void EMessages::GetName(char *AName, int MaxLen) {
strncpy(AName, "Messages", MaxLen);
}
void EMessages::GetInfo(char *AInfo, int /*MaxLen*/) {
sprintf(AInfo,
"%2d %04d/%03d Messages: %d (%s) ",
ModelNo,
Row, Count,
MatchCount,
Command);
}
void EMessages::GetPath(char *APath, int MaxLen) {
strncpy(APath, Directory, MaxLen);
APath[MaxLen - 1] = 0;
Slash(APath, 0);
}
void EMessages::GetTitle(char *ATitle, int /*MaxLen*/, char *ASTitle, int SMaxLen) {
sprintf(ATitle, "Messages: %s", Command);
strncpy(ASTitle, "Messages", SMaxLen);
ASTitle[SMaxLen - 1] = 0;
}
// get row length for specified row, used in MoveLineEnd to get actual row length
int EMessages::GetRowLength(int ARow) {
if ((ARow >= 0) && (ARow < ErrCount)) {
return strlen(ErrList[ARow]->text);
}
return 0;
}
|