source: trunk/dll/mle.c@ 1880

Last change on this file since 1880 was 1880, checked in by Gregg Young, 10 years ago

Remove dead code and comments from remaining c files. #if 0 and #if NEVER were not addressed

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