source: trunk/dll/mle.c@ 1335

Last change on this file since 1335 was 1335, checked in by Steven Levine, 17 years ago

Ticket 26: Add exception handlers to all threads using xbeginthread

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.0 KB
RevLine 
[2]1
[123]2/***********************************************************************
3
4 $Id: mle.c 1335 2008-12-12 23:49:02Z stevenhl $
5
6 MLE text editor/viewer
7
8 Copyright (c) 1993-97 M. Kimes
[1335]9 Copyright (c) 2004, 2008 Steven H.Levine
[123]10
[301]11 01 Aug 04 SHL Rework lstrip/rstrip usage
12 16 Apr 06 SHL MLEexportfile: rework to avoid wrap problems
[350]13 14 Jul 06 SHL Use Runtime_Error
[528]14 03 Nov 06 SHL Count thread usage
[574]15 22 Mar 07 GKY Use QWL_USER
[775]16 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
[793]17 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[814]18 26 Aug 07 GKY DosSleep(1) in loops changed to (0)
[888]19 17 Dec 07 GKY Make WPURLDEFAULTSETTINGS the fall back for ftp/httprun
[985]20 29 Feb 08 GKY Refactor global command line variables to notebook.h
[1029]21 22 Jun 08 GKY Fixed memory buffer access after it had been freed
[1042]22 06 Jul 08 GKY Rework LoadThread logic with Steven's help
[1335]23 10 Dec 08 SHL Integrate exception handler support
[123]24
25***********************************************************************/
26
[2]27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#include <share.h>
[1335]31// #include <process.h> // _beginthread
[350]32
[907]33#define INCL_DOS
34#define INCL_WIN
35#define INCL_LONGLONG
36
[1185]37#include "fm3dll.h"
[1225]38#include "fm3dll2.h" // #define's for UM_*, control id's, etc.
39#include "mle.h"
[1210]40#include "init.h" // Data declaration(s)
41#include "mainwnd.h" // Data declaration(s)
42#include "newview.h" // Data declarations
[2]43#include "fm3dlg.h"
44#include "fm3str.h"
[907]45#include "errutil.h" // Dos_Error...
46#include "strutil.h" // GetPString
[985]47#include "notebook.h" // httprun etc
[1161]48#include "autoview.h" // CreateHexDump
49#include "saveclip.h" // SaveToClip
[1185]50#include "common.h" // DecrThreadUsage, IncrThreadUsage
51#include "chklist.h" // PosOverOkay
52#include "valid.h" // TestBinary
53#include "strips.h" // bstrip
54#include "systemf.h" // runemf2
55#include "wrappers.h" // xfopen
56#include "misc.h" // PostMsg
[1029]57#include "fortify.h"
[1335]58#include "excputil.h" // xbeginthread
[2]59
[350]60static PSZ pszSrcFile = __FILE__;
61
[2]62#define FAKEROT 1
[578]63#define DOROT13(c) (!isalpha((c)))?(c):((((c) >= (char) 'A') && \
[1335]64 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
65 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
66 ((c) - (char) 0xd):(c)
[578]67
68/*((FAKEROT==0)?(c):(FAKEROT==1)?(!isalpha((c)))?(c):((((c) >= (char) 'A') && \
[1335]69 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
70 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
71 ((c) - (char) 0xd):(c):((c) >= (char) '!') ? ((((c) + (char) 47) > (char) '~') ? ((c) - (char) 47) :\
72 ((c) + (char) 47)) : (c))*/
[2]73
[551]74LONG MLEgetlinetext(HWND h, LONG l, CHAR * buf, INT maxlen)
[301]75{
[2]76 /* get text of line l from MLE */
77
[551]78 IPT s, e;
[2]79
[551]80 s = MLEstartofline(h, l);
81 e = MLElinelenleft(h, s);
82 return MLEtextatpos(h, s, buf, min((INT) e, maxlen));
[2]83}
84
[551]85LONG MLEdeleteline(HWND h, LONG l)
[301]86{
[2]87 /* delete line l from MLE */
88
[551]89 IPT s, e;
[2]90
[551]91 s = MLEstartofline(h, l);
92 e = MLElinelenleft(h, s);
93 return MLEdelete(h, s, e);
[2]94}
95
[551]96LONG MLEdeletecurline(HWND h)
[301]97{
[2]98 /* delete current line from MLE */
99
100 LONG l;
101
102 l = MLEcurline(h);
[551]103 return MLEdeleteline(h, l);
[2]104}
105
[551]106LONG MLEdeletetoeol(HWND h)
[301]107{
[2]108 /* delete from cursor pos to end of line */
109
[551]110 IPT s, e;
[2]111
112 s = MLEcurpos(h);
113 e = MLEcurlenleft(h);
[551]114 return MLEdelete(h, s, e);
[2]115}
116
[551]117VOID MLEclearall(HWND h)
[301]118{
[2]119 /* remove all text from MLE */
120 LONG len;
121
122 len = MLEgetlen(h);
[551]123 if (len)
124 MLEdelete(h, 0, len);
[2]125}
126
[551]127LONG MLEtextatcursor(HWND h, CHAR * buffer, INT buflen)
[301]128{
[2]129 /* place up to buflen chars of text from cursor pos into buffer
130 * return # of chars imported
131 */
132
[551]133 IPT i;
[2]134
135 i = MLEcurpos(h);
[551]136 return MLEtextatpos(h, i, buffer, buflen);
[2]137}
138
[551]139LONG MLEtextatpos(HWND h, IPT i, CHAR * buffer, INT buflen)
[301]140{
[2]141 /* place up to buflen chars of text from pos i in buffer
142 * return # of chars imported
143 */
144
[551]145 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer),
146 MPFROMLONG((LONG) buflen));
147 return (LONG) WinSendMsg(h, MLM_EXPORT,
148 MPFROMP(&i), MPFROMLONG((PLONG) & buflen));
[2]149}
150
[551]151LONG MLEsizeofsel(HWND h)
[301]152{
[2]153 /* return length of selected text */
154
[551]155 IPT cursor, anchor, test;
[2]156
157 cursor = MLEcurpos(h);
158 anchor = MLEancpos(h);
[551]159 test = min(cursor, anchor);
[2]160 /* MLE fakes us out; get real length in bytes */
[551]161 return (LONG) WinSendMsg(h, MLM_QUERYFORMATTEXTLENGTH,
162 MPFROMLONG(test),
163 MPFROMLONG((LONG) ((cursor < anchor) ?
164 (anchor - cursor) :
165 (cursor - anchor))));
[2]166}
167
[551]168VOID MLEinternet(HWND h, BOOL ftp)
[301]169{
[2]170 CHAR *temp = NULL;
[551]171 IPT ancpos, curpos, here;
172 LONG len, oldlen;
[350]173 APIRET rc;
[892]174 ULONG size;
[2]175
176 len = MLEsizeofsel(h);
[551]177 len = min(2048, len);
[2]178 oldlen = len;
[350]179 if (len) {
[2]180 len++;
[551]181 rc = DosAllocMem((PVOID) & temp, 4096,
182 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]183 if (rc || !temp)
[551]184 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
185 GetPString(IDS_OUTOFMEMORY));
[350]186 else {
[2]187 ancpos = MLEancpos(h);
188 curpos = MLEcurpos(h);
[551]189 here = min(curpos, ancpos);
190 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(len));
191 len = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&len));
[350]192 if (len <= 1)
[551]193 Runtime_Error(pszSrcFile, __LINE__, "len <= 1");
[350]194 else {
[551]195 if (len > oldlen)
196 len--;
197 temp[len] = 0;
198 bstripcr(temp);
199 if (*temp) {
[1335]200 if (ftp) {
201 if (fFtpRunWPSDefault) {
202 CHAR WPSDefaultFtpRun[CCHMAXPATH], WPSDefaultFtpRunDir[CCHMAXPATH];
[892]203
[1335]204 size = sizeof(WPSDefaultFtpRun);
205 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
206 "DefaultBrowserExe", WPSDefaultFtpRun, &size);
207 size = sizeof(WPSDefaultFtpRunDir);
208 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
209 "DefaultWorkingDir", WPSDefaultFtpRunDir, &size);
210 runemf2(SEPARATE | WINDOWED,
211 h, pszSrcFile, __LINE__,
212 WPSDefaultFtpRunDir,
213 fLibPathStrictFtpRun ? "SET LIBPATHSTRICT=TRUE" : NULL,
214 "%s %s", WPSDefaultFtpRun, temp);
215 }
216 else
217 runemf2(SEPARATE | WINDOWED,
218 h, pszSrcFile, __LINE__,
219 ftprundir, NULL, "%s %s", ftprun, temp);
220 }
221 else
222 if (fHttpRunWPSDefault) {
223 CHAR WPSDefaultHttpRun[CCHMAXPATH], WPSDefaultHttpRunDir[CCHMAXPATH];
[892]224
[1335]225 size = sizeof(WPSDefaultHttpRun);
226 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
227 "DefaultBrowserExe", WPSDefaultHttpRun, &size);
228 size = sizeof(WPSDefaultHttpRunDir);
229 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
230 "DefaultWorkingDir", WPSDefaultHttpRunDir, &size);
231 runemf2(SEPARATE | WINDOWED,
232 h, pszSrcFile, __LINE__,
233 WPSDefaultHttpRunDir,
234 fLibPathStrictHttpRun ? "SET LIBPATHSTRICT=TRUE" : NULL,
235 "%s %s", WPSDefaultHttpRun, temp);
236 }
237 else
238 runemf2(SEPARATE | WINDOWED,
239 h, pszSrcFile, __LINE__,
240 httprundir, NULL, "%s %s", httprun, temp);
[551]241 }
[2]242 }
243 DosFreeMem(temp);
244 }
245 }
246}
247
[551]248BOOL MLEdoblock(HWND h, INT action, CHAR * filename)
[301]249{
[2]250 /* perform action on text in selection */
251
252 register CHAR *p;
[551]253 CHAR *sel, *temp = NULL;
254 IPT ancpos, curpos, here;
255 LONG sellen, oldlen;
[350]256 APIRET rc;
[2]257
258 oldlen = MLEsizeofsel(h);
[551]259 if (!oldlen)
[2]260 return TRUE;
[551]261 sel = xmallocz((size_t) (oldlen + 2), pszSrcFile, __LINE__);
[350]262 if (!sel)
[2]263 return FALSE;
[551]264 rc = DosAllocMem((PVOID) & temp, 32768L,
265 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]266 if (rc || !temp) {
[551]267 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
268 GetPString(IDS_OUTOFMEMORY));
[1039]269 free(sel);
[1063]270# ifdef FORTIFY
271 Fortify_LeaveScope();
272# endif
[2]273 DosPostEventSem(CompactSem);
274 return FALSE;
275 }
276
277 ancpos = MLEancpos(h);
278 curpos = MLEcurpos(h);
[551]279 here = min(curpos, ancpos);
[2]280 p = sel;
281 MLEdisable(h);
[551]282 while (oldlen > 0) {
283 sellen = min(oldlen + 1, 32701);
284 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
285 sellen =
286 (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&sellen));
[350]287 if (sellen < 1) {
288 Runtime_Error(pszSrcFile, __LINE__, "len < 1");
[1039]289 free(sel);
[1063]290# ifdef FORTIFY
291 Fortify_LeaveScope();
292# endif
[2]293 DosPostEventSem(CompactSem);
294 return FALSE;
295 }
[551]296 if (sellen > min(oldlen, 32700))
[2]297 sellen--;
[551]298 memcpy(p, temp, sellen);
[2]299 p += sellen;
300 oldlen -= sellen;
301 }
[551]302 switch (action) {
303 case APPENDCLIP:
304 SaveToClip(h, sel, TRUE);
305 DosFreeMem(temp);
[1039]306 free(sel);
[1063]307# ifdef FORTIFY
308 Fortify_LeaveScope();
309# endif
[551]310 MLEenable(h);
311 DosPostEventSem(CompactSem);
312 return TRUE;
313
314 case WRITE:
315 {
316 FILE *fp;
317
318 fp = fopen(filename, "a+");
319 if (!fp)
320 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
321 if (fp) {
322 fseek(fp, 0L, SEEK_END);
323 fwrite(sel, 1, strlen(sel), fp);
324 fclose(fp);
325 }
326#ifdef __DEBUG_ALLOC__
327 _heap_check();
328#endif
[2]329 DosFreeMem(temp);
[1039]330 free(sel);
[1063]331# ifdef FORTIFY
332 Fortify_LeaveScope();
333# endif
[2]334 MLEenable(h);
335 DosPostEventSem(CompactSem);
336 return TRUE;
[551]337 }
[2]338
[551]339 case UPPERCASE:
340 p = sel;
341 while (*p) {
342 if (isalpha(*p))
343 *p = toupper(*p);
344 p++;
345 }
346 break;
[2]347
[551]348 case LOWERCASE:
349 p = sel;
350 while (*p) {
351 if (isalpha(*p))
352 *p = tolower(*p);
353 p++;
354 }
355 break;
[2]356
[551]357 case TOGGLECASE:
358 p = sel;
359 while (*p) {
360 if (isalpha(*p)) {
361 if (islower(*p))
362 *p = toupper(*p);
363 else
364 *p = tolower(*p);
[2]365 }
[551]366 p++;
367 }
368 break;
[2]369
[551]370 case ROT13:
371 p = sel;
372 while (*p) {
373 *p = DOROT13(*p); // fixme condition both true and false?
374 p++;
375 }
376 break;
[2]377
[551]378 case XOR:
379 p = sel;
380 while (*p) {
381 *p = (~*p);
382 p++;
383 }
384 break;
[2]385
[551]386 case FORMAT:
387 p = sel;
388 while (*p) {
389 if (*p == '\r') {
390 memmove(p, p + 1, strlen(p));
391 continue;
[2]392 }
[551]393 if (*p == '\n') {
394 *p = ' ';
395 continue;
[2]396 }
[551]397 p++;
398 }
399 break;
[2]400
[551]401 default: /* unknown action */
[2]402#ifdef __DEBUG_ALLOC__
403 _heap_check();
404#endif
[551]405 DosFreeMem(temp);
[1039]406 free(sel);
[1063]407# ifdef FORTIFY
408 Fortify_LeaveScope();
409# endif
[551]410 DosPostEventSem(CompactSem);
411 MLEenable(h);
412 return FALSE;
[2]413 }
414
415 /* replace selection with altered text */
416 p = sel;
[551]417 here = min(curpos, ancpos);
418 MLEclear(h); /* delete current selection */
419 sellen = oldlen = strlen(sel); /* actual number of bytes */
420 while (oldlen > 0) {
421 sellen = min(oldlen, 32700);
422 memcpy(temp, p, sellen);
423 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(sellen));
424 sellen = (LONG) WinSendMsg(h,
425 MLM_IMPORT,
426 MPFROMP(&here), MPFROMLONG(sellen));
[350]427 if (!sellen) {
428 Runtime_Error(pszSrcFile, __LINE__, "sellen 0");
[2]429 break;
430 }
431 p += sellen;
432 oldlen -= sellen;
[551]433 if (oldlen && *p == '\n' /* && *(p - 1) == '\r' */ )
[2]434 p--;
[551]435 } // while
436 WinSendMsg(h, MLM_SETSEL, MPFROMLONG(ancpos), MPFROMLONG(curpos));
[2]437 MLEenable(h);
438#ifdef __DEBUG_ALLOC__
[551]439 _heap_check();
[2]440#endif
441 DosFreeMem(temp);
[1039]442 free(sel);
[1029]443# ifdef FORTIFY
444 Fortify_LeaveScope();
[1063]445# endif
[2]446 DosPostEventSem(CompactSem);
447 return TRUE;
448}
449
[551]450BOOL MLEquotepara(HWND h, CHAR * initials, BOOL fQuoteOld)
[301]451{
[2]452 LONG num;
[551]453 CHAR lineend[2], line[8], *p;
[2]454
[551]455 if (!initials || !*initials)
[2]456 initials = " > ";
457 num = MLEcurline(h);
[551]458 while (MLElinelen(h, num) < 3L && MLEnumlines(h) >= num)
[2]459 num++;
[551]460 while (MLElinelen(h, num) > 2L && MLEnumlines(h) >= num) {
461 memset(line, 0, 8);
462 MLEgetlinetext(h, num, line, 7L);
[2]463 line[7] = 0;
[551]464 if ((p = strchr(line, '>')) == NULL) {
465 MLEsetcurpos(h, MLEstartofline(h, num));
466 MLEinsert(h, initials);
467 MLEsetcurpos(h, (MLEstartofline(h, num) + MLElinelen(h, num)) - 1L);
468 MLEtextatcursor(h, lineend, 2L);
469 if (*lineend != '\r' && *lineend != '\n')
470 MLEinsert(h, "\n");
[2]471 }
[551]472 else if (fQuoteOld) {
473 while (isspace(line[strlen(line) - 1]))
474 line[strlen(line) - 1] = 0;
475 MLEsetcurpos(h, MLEstartofline(h, num) + (p - line));
476 MLEinsert(h, ">");
[2]477 }
478 num++;
479 }
[551]480 MLEsetcurpos(h, MLEstartofline(h, num));
[2]481 return TRUE;
482}
483
[551]484BOOL MLEAutoLoad(HWND h, CHAR * filename)
[301]485{
[2]486 XMLEWNDPTR *vw;
487
[574]488 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
[551]489 if (vw && vw->size != sizeof(XMLEWNDPTR))
[2]490 vw = NULL;
[551]491 if (TestBinary(filename)) {
492 if (vw)
[2]493 vw->hex = 1;
[551]494 return MLEHexLoad(h, filename);
[2]495 }
[551]496 if (vw)
[2]497 vw->hex = 2;
[551]498 return MLEloadfile(h, filename);
[2]499}
500
[551]501BOOL MLEHexLoad(HWND h, CHAR * filename)
[301]502{
[2]503 /* insert a file into the current position in the MLE */
504
[551]505 HAB hab;
506 CHAR *buffer = NULL, *hexbuff = NULL;
[841]507 IPT iptOffset = -1;
508 ULONG numread, howmuch, numimport, action, len, left = 0;
[551]509 BOOL ret = TRUE, first = TRUE;
510 CHAR titletext[512];
511 HWND grandpa;
[2]512 XMLEWNDPTR *vw;
[551]513 HFILE handle;
514 APIRET rc;
[2]515
516 *titletext = 0;
517 hab = WinQueryAnchorBlock(h);
[574]518 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
[551]519 if (vw && vw->size != sizeof(XMLEWNDPTR))
[2]520 vw = NULL;
521 grandpa = GrandparentOf(h);
522 *titletext = 0;
[551]523 WinQueryWindowText(grandpa, 512, titletext);
[844]524 rc = DosOpen(filename, &handle, &action, 0, 0,
525 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
526 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT |
527 OPEN_FLAGS_SEQUENTIAL | OPEN_SHARE_DENYNONE |
528 OPEN_ACCESS_READONLY, 0);
[350]529 if (rc) {
530 ret = FALSE;
531 }
532 else {
[841]533 DosChgFilePtr(handle, 0, FILE_END, &len);
534 DosChgFilePtr(handle, 0, FILE_BEGIN, &action);
[350]535 if (len) {
[841]536 rc = DosAllocMem((PVOID) & hexbuff, 50001,
[551]537 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]538 if (rc || !hexbuff) {
[551]539 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
540 GetPString(IDS_OUTOFMEMORY));
541 ret = FALSE;
[350]542 }
543 else {
[551]544 buffer = xmalloc(10000, pszSrcFile, __LINE__);
545 if (!buffer)
546 ret = FALSE;
[350]547 else {
[551]548 MLEclearall(h);
549 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(hexbuff),
550 MPFROMLONG(50000L));
551 if (!DosRead(handle, buffer, min(10000, len), &numread) && numread) {
[2]552
[551]553 CHAR s[81];
[2]554
[551]555 MLEsetwrap(h, FALSE);
556 WinSetSysValue(HWND_DESKTOP, SV_INSERTMODE, FALSE);
557 *hexbuff = 0;
558 numimport = CreateHexDump(buffer,
559 numread, hexbuff, 50000, left, TRUE);
560 while (len && numimport) { /* import entire file */
561 left += numread;
562 len -= numread;
563 if (!WinIsWindow(hab, h) || (vw && vw->killme))
564 break;
565 howmuch = (INT) WinSendMsg(h,
566 MLM_IMPORT,
567 MPFROMP(&iptOffset),
568 MPFROMLONG(numimport));
569 if (first && len) {
570 MLEdisable(h);
571 WinEnableWindowUpdate(h, FALSE);
572 first = FALSE;
573 }
[2]574// fprintf(stderr,"%d bytes of %d imported\n",howmuch,numimport);
[551]575 if (howmuch < 1) {
576 numimport = 0;
577 break;
578 }
579 while (howmuch < numimport) {
580 numimport -= howmuch;
581 memmove(hexbuff, hexbuff + howmuch, numimport);
[814]582 DosSleep(0); //26 Aug 07 GKY 1
[551]583 if (!WinIsWindow(hab, h) || (vw && vw->killme))
584 break;
585 howmuch = (INT) WinSendMsg(h,
586 MLM_IMPORT,
587 MPFROMP(&iptOffset),
588 MPFROMLONG((LONG) numimport));
589 if (howmuch < 1)
590 break;
591 }
592 if (DosRead(handle, buffer, min(10000, len), &numread)
593 || !numread) {
594 numimport = 0;
595 break;
596 }
597 *hexbuff = 0;
598 numimport =
599 CreateHexDump(buffer, numread, hexbuff, 50000, left, TRUE);
600 if (numimport < 1 || !WinIsWindow(hab, h) || (vw && vw->killme)) {
601 numimport = 0;
602 break;
603 }
604 sprintf(s, GetPString(IDS_LOADINGMLETEXT), len);
605 WinSetWindowText(grandpa, s);
606 }
[771]607 DosSleep(1);
[551]608 }
609 else
610 ret = FALSE;
[1039]611 free(buffer);
[1063]612# ifdef FORTIFY
[1335]613 Fortify_LeaveScope();
[1063]614# endif
[551]615 }
616 DosFreeMem(hexbuff);
[2]617 }
618 }
[551]619 if (WinIsWindow(hab, h))
620 WinSetWindowText(grandpa, titletext);
[2]621 DosClose(handle);
622 }
[551]623 if (!first) {
624 WinEnableWindowUpdate(h, TRUE);
[2]625 MLEenable(h);
626 }
[551]627 MLEsetchanged(h, FALSE);
[2]628 return ret;
629}
630
[350]631//== MLEinsertfile() insert a file into the current position in the MLE ==
632
[551]633BOOL MLEinsertfile(HWND h, CHAR * filename)
[301]634{
[2]635
[551]636 HAB hab;
[2]637 FILE *fp;
[551]638 CHAR *buffer = NULL;
639 INT len;
640 IPT iptOffset = -1L;
641 INT numread, howmuch, tempnum, x;
642 BOOL ret = TRUE, first = TRUE, once = FALSE, dont = FALSE;
643 CHAR titletext[512];
644 HWND grandpa;
[2]645 XMLEWNDPTR *vw;
646 APIRET rc;
647
648 *titletext = 0;
649 hab = WinQueryAnchorBlock(h);
[574]650 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
[551]651 if (vw && vw->size != sizeof(XMLEWNDPTR))
[2]652 vw = NULL;
653 grandpa = GrandparentOf(h);
654 *titletext = 0;
[551]655 WinQueryWindowText(grandpa, 512, titletext);
656 fp = _fsopen(filename, "r", SH_DENYNO);
[350]657 if (!fp)
658 ret = FALSE;
659 else {
[551]660 setvbuf(fp, NULL, _IONBF, 0);
661 fseek(fp, 0L, SEEK_END);
662 len = (INT) ftell(fp);
663 fseek(fp, 0L, SEEK_SET);
664 if (len && len != -1) {
665 rc = DosAllocMem((PVOID) & buffer,
666 50000L, PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]667 if (rc || !buffer) {
[551]668 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
669 GetPString(IDS_OUTOFMEMORY));
670 ret = FALSE;
[350]671 }
672 else {
[551]673 WinSendMsg(h,
674 MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(50000L));
675 numread = fread(buffer, 1, min(50000, len), fp);
676 if (numread < 1)
677 ret = FALSE;
678 while (len && numread > 0) { /* here we go... */
[2]679
[551]680 CHAR s[81];
[2]681
[551]682 while (numread > 0) { /* import entire file */
683 if (!WinIsWindow(hab, h) || (vw && vw->killme))
684 break;
685 if (strlen(buffer) < numread) {
686 if (!once && !dont)
687 rc = saymsg(MB_YESNOCANCEL,
688 HWND_DESKTOP,
689 GetPString(IDS_WARNINGTEXT),
690 GetPString(IDS_TEXTNULSTEXT));
691 else if (once)
692 rc = MBID_YES;
693 else if (dont)
694 rc = MBID_NO;
695 if (rc == MBID_YES) {
696 once = FALSE;
697 for (x = 0; x < numread; x++) {
698 if (!buffer[x])
699 buffer[x] = ' ';
700 }
701 }
702 else if (rc == MBID_CANCEL) {
703 len = 0;
704 numread = 0;
705 saymsg(MB_ENTER,
706 HWND_DESKTOP,
707 GetPString(IDS_OBEYTEXT),
708 GetPString(IDS_LOADCANCELLEDTEXT));
709 break;
710 }
711 else if (rc == MBID_NO)
712 dont = TRUE;
713 }
714 howmuch = (INT) WinSendMsg(h,
715 MLM_IMPORT,
716 MPFROMP(&iptOffset),
717 MPFROMLONG((LONG) numread));
718 if (first && !feof(fp)) {
719 MLEdisable(h);
720 WinEnableWindowUpdate(h, FALSE);
721 first = FALSE;
722 }
[2]723// fprintf(stderr,"%d bytes of %d imported\n",howmuch,numread);
[551]724 if (howmuch < 1) {
725 numread = 0;
726 break;
727 }
728 len -= howmuch;
729 if (howmuch < numread) {
730 numread -= howmuch;
731 memmove(buffer, buffer + howmuch, numread);
732 if (numread && len) {
733 tempnum = numread;
734 numread = fread(buffer + tempnum,
735 1, min(50000 - tempnum, len), fp);
736 if (numread > 1)
737 numread += tempnum;
738 else
739 numread = tempnum;
740 }
[814]741 DosSleep(0); //26 Aug 07 GKY 1
[551]742 }
743 else
744 numread = fread(buffer, 1, min(50000, len), fp);
745 if (numread < 1 || !WinIsWindow(hab, h) || (vw && vw->killme)) {
746 numread = 0;
747 break;
748 }
749 sprintf(s, GetPString(IDS_LOADINGMLETEXT), len);
750 WinSetWindowText(grandpa, s);
751 }
[814]752 DosSleep(0); //26 Aug 07 GKY 1
[551]753 }
754 DosFreeMem(buffer);
[2]755 }
756 }
[551]757 if (WinIsWindow(hab, h))
758 WinSetWindowText(grandpa, titletext);
[2]759 fclose(fp);
760 }
[551]761 if (!first) {
762 WinEnableWindowUpdate(h, TRUE);
[2]763 MLEenable(h);
764 }
765 return ret;
766}
767
[551]768typedef struct
769{
[2]770 USHORT size;
771 USHORT hex;
[551]772 HWND h;
773 CHAR filename[CCHMAXPATH];
774 HWND hwndReport;
775 HWND msg;
776}
777BKGLOAD;
[2]778
[551]779VOID LoadThread(VOID * arg)
[301]780{
[2]781 BKGLOAD *bkg;
[551]782 BOOL fSuccess;
783 HAB thab;
784 HMQ thmq;
[2]785
786 DosError(FERR_DISABLEHARDERR);
787
[1042]788# ifdef FORTIFY
789 Fortify_EnterScope();
[1063]790# endif
[1042]791
[551]792 bkg = (BKGLOAD *) arg;
793 if (bkg) {
[2]794 thab = WinInitialize(0);
[551]795 if (thab) {
796 thmq = WinCreateMsgQueue(thab, 0);
797 if (thmq) {
798 WinCancelShutdown(thmq, TRUE);
799 IncrThreadUsage();
800 priority_normal();
801 if (bkg->hex == 1)
802 fSuccess = MLEHexLoad(bkg->h, bkg->filename);
803 else if (bkg->hex == 2)
804 fSuccess = MLEloadfile(bkg->h, bkg->filename);
805 else
806 fSuccess = MLEAutoLoad(bkg->h, bkg->filename);
807 priority_bumped();
808 if (bkg->hwndReport && WinIsWindow(thab, bkg->hwndReport))
809 PostMsg(bkg->hwndReport, bkg->msg, MPFROMLONG(fSuccess),
810 MPFROMP(bkg->filename));
[2]811#ifdef __DEBUG_ALLOC__
[551]812 _heap_check();
[2]813#endif
[551]814 WinDestroyMsgQueue(thmq);
[2]815 }
[1039]816 else
[1335]817 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
[528]818 DecrThreadUsage();
[2]819 WinTerminate(thab);
820 }
[1039]821 else {
[1042]822 // fixme to be gone? - can not PostMsg without HAB
[1029]823 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
[1042]824 }
825 free(bkg);
826 } // if bkg
[1029]827# ifdef FORTIFY
828 Fortify_LeaveScope();
[1063]829# endif
[1335]830 // _endthread(); // 10 Dec 08 SHL
[2]831}
832
[551]833INT MLEbackgroundload(HWND hwndReport, ULONG msg, HWND h, CHAR * filename,
834 INT hex)
[350]835{
836 /* load a file into the MLE in the background (via a separate thread)
837 * return _beginthread status
838 */
[2]839
840 BKGLOAD *bkg;
841
[551]842 bkg = xmallocz(sizeof(BKGLOAD), pszSrcFile, __LINE__);
[350]843 if (!bkg)
844 return -1;
845 bkg->size = sizeof(BKGLOAD);
[551]846 bkg->hex = (USHORT) hex;
[350]847 bkg->hwndReport = hwndReport;
848 bkg->msg = msg;
849 bkg->h = h;
[551]850 strcpy(bkg->filename, filename);
[1335]851 return xbeginthread(LoadThread,
852 65536,
853 bkg,
854 pszSrcFile,
855 __LINE__);
[2]856}
857
[551]858BOOL MLEloadfile(HWND h, CHAR * filename)
[301]859{
[2]860 /* load a file into the MLE, getting rid of whatever was already
861 * there. Note this returns without erasing existing text if the
862 * file to load does not exist
863 */
864
[843]865 FILESTATUS3 fsa;
[551]866 BOOL ret;
[2]867
[843]868 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, sizeof(fsa)) &&
869 ~fsa.attrFile & FILE_DIRECTORY) {
[2]870 MLEclearall(h);
[551]871 ret = MLEinsertfile(h, filename);
872 MLEsetchanged(h, FALSE);
[2]873 return ret;
874 }
875 return FALSE;
876}
877
[551]878BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
879 BOOL striptraillines, BOOL striptrailspaces)
[301]880{
[2]881 /* save the MLE contents as a file. Format the output so that
882 * the file is CR/LF terminated as presented in the MLE.
883 */
884
[301]885 FILE *fp = NULL;
886 CHAR *buffer = NULL;
887 CHAR *p;
888 BOOL ok = TRUE;
[551]889 INT blanklines = 0;
[301]890 BOOL fWrap = MLEgetwrap(h);
[350]891 APIRET rc;
[2]892
[301]893 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
[551]894 if (!MLEgetlen(h)) /* nothing to save; forget it */
[2]895 return TRUE;
896
[551]897 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
[301]898
[551]899 if (striptraillines) {
[2]900
901 register LONG x;
[551]902 LONG numlines;
[2]903
904 numlines = MLEnumlines(h);
[551]905 for (x = numlines; x; x--) {
906 if (MLElinelen(h, x - 1L) < 2)
907 MLEdeleteline(h, x - 1L);
[2]908 else
[551]909 break;
[2]910 }
[551]911 if (!MLEgetlen(h)) {
[301]912 /* nothing to save; forget it */
913 MLEsetwrap(h, fWrap); // Restore
[2]914 return TRUE;
[301]915 }
[2]916 }
917
[551]918 rc = DosAllocMem((PVOID) & buffer, 4096L,
919 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]920 if (rc || !buffer) {
[551]921 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
922 GetPString(IDS_OUTOFMEMORY));
[350]923 ok = FALSE;
924 }
[551]925 else {
926 fp = fopen(filename, "a+");
[350]927 if (!fp)
[551]928 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
[350]929 if (!fp)
930 ok = FALSE;
931 else {
[551]932 LONG numlines, ret, len, wl, temp;
933 IPT s;
[2]934
[551]935 fseek(fp, 0L, SEEK_END);
[2]936 numlines = MLEnumlines(h);
937
[551]938 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(4095L));
939 for (temp = 0; temp < numlines; temp++) {
940 s = MLEstartofline(h, temp);
941 wl = len = (LONG) MLElinelenleft(h, s);
942 ret = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&s), MPFROMP(&len));
943 if (ret < 0)
944 break;
945 wl = min(wl, ret);
946 buffer[wl] = 0;
947 if (*buffer) {
948 p = buffer + strlen(buffer) - 1;
949 while (p >= buffer && (*p == '\n' || *p == '\r')) {
950 *p = 0;
951 p--;
952 }
953 }
954 if (tabspaces) {
955 p = buffer;
956 while (*p) {
957 if (*p == '\t') {
958 *p = ' ';
959 memmove((p + tabspaces) - 1, p, strlen(p) + 1);
960 memset(p, ' ', tabspaces);
961 }
962 p++;
963 }
964 }
965 if (striptrailspaces && *buffer) {
966 p = buffer + strlen(buffer) - 1;
967 while (p >= buffer && (*p == ' ' || *p == '\t')) {
968 *p = 0;
969 p--;
970 }
971 }
972 if (striptraillines) {
973 if (!*buffer) {
974 blanklines++;
975 continue;
976 }
977 else {
978 while (blanklines) {
979 fwrite("\n", 1, 1, fp);
980 blanklines--;
981 }
982 }
983 }
984 strcat(buffer, "\n");
985 // buffer = translate_out(buffer,4095,h,filename);
986 if (fwrite(buffer, 1, strlen(buffer), fp) < 1) {
987 saymsg(MB_ENTER,
988 h, GetPString(IDS_ARGHTEXT), GetPString(IDS_WRITEERRORTEXT));
989 break;
990 }
991 } // for lines
[2]992 }
993 }
[301]994
[551]995 MLEsetwrap(h, fWrap); // Restore
[301]996
997 if (fp)
998 fclose(fp);
999 if (buffer)
1000 DosFreeMem(buffer);
1001
1002 return ok;
[2]1003}
1004
[551]1005MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[301]1006{
[2]1007 /* initiate search(/replace)s in edit mode */
1008
1009 SRCHPTR *vw;
1010
[551]1011 if (msg != WM_INITDLG)
[574]1012 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
[2]1013 else
1014 vw = NULL;
1015
[551]1016 switch (msg) {
1017 case WM_INITDLG:
1018 vw = (SRCHPTR *) mp2;
1019 if (!vw) {
1020 WinDismissDlg(hwnd, 0);
[2]1021 break;
[551]1022 }
[574]1023 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
[551]1024 WinSendDlgItemMsg(hwnd, SRCH_SEARCH, EM_SETTEXTLIMIT,
1025 MPFROM2SHORT(256, 0), MPVOID);
1026 WinSendDlgItemMsg(hwnd, SRCH_REPLACE, EM_SETTEXTLIMIT,
1027 MPFROM2SHORT(256, 0), MPVOID);
1028 if (*vw->search)
1029 WinSetDlgItemText(hwnd, SRCH_SEARCH, vw->search);
1030 if (!MLEgetreadonly(vw->hwndmle)) {
1031 if (*vw->replace)
1032 WinSetDlgItemText(hwnd, SRCH_REPLACE, vw->replace);
1033 WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_SETCHECK,
1034 MPFROM2SHORT(vw->sandr, 0), MPVOID);
1035 WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_SETCHECK,
1036 MPFROM2SHORT(vw->rall, 0), MPVOID);
1037 }
1038 else {
1039 WinEnableWindow(WinWindowFromID(hwnd, SRCH_SANDR), FALSE);
1040 WinEnableWindow(WinWindowFromID(hwnd, SRCH_RALL), FALSE);
1041 WinEnableWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1042 WinShowWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1043 *vw->replace = 0;
1044 vw->sandr = FALSE;
1045 vw->rall = FALSE;
1046 }
1047 memset(&vw->se, 0, sizeof(MLE_SEARCHDATA));
1048 vw->se.cb = sizeof(MLE_SEARCHDATA);
1049 vw->se.pchFind = (PCHAR) vw->search;
1050 vw->se.cchFind = (SHORT) strlen(vw->search);
1051 vw->se.pchReplace = (PCHAR) vw->replace;
1052 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1053 vw->se.iptStart = MLEcurpos(vw->hwndmle);
1054 vw->se.iptStop = -1L;
1055 vw->se.cchFound = 0;
1056 PosOverOkay(hwnd);
1057 break;
[2]1058
[551]1059 case WM_CONTROL:
1060 return 0;
[2]1061
[551]1062 case WM_COMMAND:
1063 switch (SHORT1FROMMP(mp1)) {
1064 case IDM_HELP:
1065 saymsg(MB_ENTER | MB_ICONASTERISK,
1066 hwnd,
1067 NullStr,
1068 GetPString(IDS_ENTERSEARCHSTRINGTEXT),
1069 (MLEgetreadonly(vw->hwndmle)) ?
1070 "." : GetPString(IDS_REPLACESTRINGTEXT));
1071 break;
[2]1072
[551]1073 case DID_CANCEL:
1074 WinDismissDlg(hwnd, 0);
1075 break;
[2]1076
[551]1077 case DID_OK:
1078 WinShowWindow(hwnd, FALSE);
1079 {
1080 CHAR temp[257];
1081 APIRET ret;
[2]1082
[551]1083 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_QUERYCHECK,
1084 MPVOID, MPVOID))
1085 vw->sandr = TRUE;
1086 else
1087 vw->sandr = FALSE;
1088 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_QUERYCHECK,
1089 MPVOID, MPVOID))
1090 vw->rall = TRUE;
1091 else
1092 vw->rall = FALSE;
1093 *vw->replace = 0;
1094 WinQueryDlgItemText(hwnd, SRCH_REPLACE, 256, vw->replace);
1095 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1096 WinQueryDlgItemText(hwnd, SRCH_SEARCH, 256, temp);
1097 if (*temp) {
1098 strcpy(vw->search, temp);
1099 vw->se.cchFind = (SHORT) strlen(vw->search);
1100 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1101 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1102 (MLFSEARCH_CASESENSITIVE *
1103 (vw->fInsensitive ==
1104 FALSE)) | (MLFSEARCH_CHANGEALL *
1105 (vw->rall != 0))),
1106 MPFROMP(&vw->se))) {
1107 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1108 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1109 WinDismissDlg(hwnd, 0);
1110 break;
1111 }
1112 else if (vw->sandr && !vw->rall) {
1113 ret = saymsg(MB_YESNOCANCEL,
1114 hwnd,
1115 NullStr,
1116 GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1117 if (ret == MBID_YES)
1118 MLEinsert(vw->hwndmle, vw->replace);
1119 else if (ret == MBID_CANCEL) {
1120 WinDismissDlg(hwnd, 0);
1121 break;
1122 }
1123 WinDismissDlg(hwnd, 1);
1124 break;
1125 }
1126 }
1127 WinDismissDlg(hwnd, 0);
[2]1128 }
[551]1129 break;
1130 }
1131 return 0;
[2]1132
[551]1133 case WM_CLOSE:
1134 break;
[2]1135 }
1136
[551]1137 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]1138}
1139
[551]1140BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
[301]1141{
[551]1142 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1143 MLEgetseltext(vw->hwndmle, vw->search);
[2]1144 vw->search[255] = 0;
1145 }
[551]1146 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1147 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
[2]1148 return TRUE;
1149 return FALSE;
1150}
1151
[551]1152INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
[301]1153{
[551]1154 if (!*vw->search)
[2]1155 return -1;
1156 else {
1157 vw->se.iptStart++;
[551]1158 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1159 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1160 (MLFSEARCH_CASESENSITIVE *
1161 (vw->fInsensitive ==
1162 FALSE)) | (MLFSEARCH_CHANGEALL *
1163 (vw->rall))), MPFROMP(&vw->se)))
1164 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1165 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1166 else if (vw->sandr && !vw->rall) {
[2]1167
1168 APIRET ret;
1169
1170 ret = saymsg(MB_YESNOCANCEL,
[551]1171 hwnd,
1172 NullStr, GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1173 if (ret == MBID_YES)
1174 MLEinsert(vw->hwndmle, vw->replace);
1175 if (ret != MBID_CANCEL)
1176 return 1;
[2]1177 }
1178 }
1179 return 0;
1180}
[793]1181
1182#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
1183#pragma alloc_text(FMMLE,MLEclearall,MLEtextatcursor,MLEtextatpos,MLEsizeofsel)
1184#pragma alloc_text(FMMLE3,MLEdoblock,MLEquotepara,MLEinternet)
1185#pragma alloc_text(FMMLE4,MLEAutoLoad,MLEHexLoad,MLEinsertfile,LoadThread,MLEbackgroundload)
1186#pragma alloc_text(FMMLE5,MLEloadfile,MLEexportfile)
1187#pragma alloc_text(FMMLE3,MLEfindfirst,MLEfindnext,SandRDlgProc)
Note: See TracBrowser for help on using the repository browser.