source: trunk/dll/mle.c@ 1844

Last change on this file since 1844 was 1673, checked in by Gregg Young, 13 years ago

Update to Doxygen comment style Ticket 55. Also some minor code cleanup.

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