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
Line 
1
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
9 Copyright (c) 2004, 2008 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
25***********************************************************************/
26
27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#include <share.h>
31// #include <process.h> // _beginthread
32
33#define INCL_DOS
34#define INCL_WIN
35#define INCL_LONGLONG
36
37#include "fm3dll.h"
38#include "fm3dll2.h" // #define's for UM_*, control id's, etc.
39#include "mle.h"
40#include "init.h" // Data declaration(s)
41#include "mainwnd.h" // Data declaration(s)
42#include "newview.h" // Data declarations
43#include "fm3dlg.h"
44#include "fm3str.h"
45#include "errutil.h" // Dos_Error...
46#include "strutil.h" // GetPString
47#include "notebook.h" // httprun etc
48#include "autoview.h" // CreateHexDump
49#include "saveclip.h" // SaveToClip
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
57#include "fortify.h"
58#include "excputil.h" // xbeginthread
59
60static PSZ pszSrcFile = __FILE__;
61
62#define FAKEROT 1
63#define DOROT13(c) (!isalpha((c)))?(c):((((c) >= (char) 'A') && \
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)
67
68/*((FAKEROT==0)?(c):(FAKEROT==1)?(!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):((c) >= (char) '!') ? ((((c) + (char) 47) > (char) '~') ? ((c) - (char) 47) :\
72 ((c) + (char) 47)) : (c))*/
73
74LONG MLEgetlinetext(HWND h, LONG l, CHAR * buf, INT maxlen)
75{
76 /* get text of line l from MLE */
77
78 IPT s, e;
79
80 s = MLEstartofline(h, l);
81 e = MLElinelenleft(h, s);
82 return MLEtextatpos(h, s, buf, min((INT) e, maxlen));
83}
84
85LONG MLEdeleteline(HWND h, LONG l)
86{
87 /* delete line l from MLE */
88
89 IPT s, e;
90
91 s = MLEstartofline(h, l);
92 e = MLElinelenleft(h, s);
93 return MLEdelete(h, s, e);
94}
95
96LONG MLEdeletecurline(HWND h)
97{
98 /* delete current line from MLE */
99
100 LONG l;
101
102 l = MLEcurline(h);
103 return MLEdeleteline(h, l);
104}
105
106LONG MLEdeletetoeol(HWND h)
107{
108 /* delete from cursor pos to end of line */
109
110 IPT s, e;
111
112 s = MLEcurpos(h);
113 e = MLEcurlenleft(h);
114 return MLEdelete(h, s, e);
115}
116
117VOID MLEclearall(HWND h)
118{
119 /* remove all text from MLE */
120 LONG len;
121
122 len = MLEgetlen(h);
123 if (len)
124 MLEdelete(h, 0, len);
125}
126
127LONG MLEtextatcursor(HWND h, CHAR * buffer, INT buflen)
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 /* 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 APIRET rc;
174 ULONG size;
175
176 len = MLEsizeofsel(h);
177 len = min(2048, len);
178 oldlen = len;
179 if (len) {
180 len++;
181 rc = DosAllocMem((PVOID) & temp, 4096,
182 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
183 if (rc || !temp)
184 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
185 GetPString(IDS_OUTOFMEMORY));
186 else {
187 ancpos = MLEancpos(h);
188 curpos = MLEcurpos(h);
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));
192 if (len <= 1)
193 Runtime_Error(pszSrcFile, __LINE__, "len <= 1");
194 else {
195 if (len > oldlen)
196 len--;
197 temp[len] = 0;
198 bstripcr(temp);
199 if (*temp) {
200 if (ftp) {
201 if (fFtpRunWPSDefault) {
202 CHAR WPSDefaultFtpRun[CCHMAXPATH], WPSDefaultFtpRunDir[CCHMAXPATH];
203
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];
224
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);
241 }
242 }
243 DosFreeMem(temp);
244 }
245 }
246}
247
248BOOL MLEdoblock(HWND h, INT action, CHAR * filename)
249{
250 /* perform action on text in selection */
251
252 register CHAR *p;
253 CHAR *sel, *temp = NULL;
254 IPT ancpos, curpos, here;
255 LONG sellen, oldlen;
256 APIRET rc;
257
258 oldlen = MLEsizeofsel(h);
259 if (!oldlen)
260 return TRUE;
261 sel = xmallocz((size_t) (oldlen + 2), pszSrcFile, __LINE__);
262 if (!sel)
263 return FALSE;
264 rc = DosAllocMem((PVOID) & temp, 32768L,
265 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
266 if (rc || !temp) {
267 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
268 GetPString(IDS_OUTOFMEMORY));
269 free(sel);
270# ifdef FORTIFY
271 Fortify_LeaveScope();
272# endif
273 DosPostEventSem(CompactSem);
274 return FALSE;
275 }
276
277 ancpos = MLEancpos(h);
278 curpos = MLEcurpos(h);
279 here = min(curpos, ancpos);
280 p = sel;
281 MLEdisable(h);
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));
287 if (sellen < 1) {
288 Runtime_Error(pszSrcFile, __LINE__, "len < 1");
289 free(sel);
290# ifdef FORTIFY
291 Fortify_LeaveScope();
292# endif
293 DosPostEventSem(CompactSem);
294 return FALSE;
295 }
296 if (sellen > min(oldlen, 32700))
297 sellen--;
298 memcpy(p, temp, sellen);
299 p += sellen;
300 oldlen -= sellen;
301 }
302 switch (action) {
303 case APPENDCLIP:
304 SaveToClip(h, sel, TRUE);
305 DosFreeMem(temp);
306 free(sel);
307# ifdef FORTIFY
308 Fortify_LeaveScope();
309# endif
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
329 DosFreeMem(temp);
330 free(sel);
331# ifdef FORTIFY
332 Fortify_LeaveScope();
333# endif
334 MLEenable(h);
335 DosPostEventSem(CompactSem);
336 return TRUE;
337 }
338
339 case UPPERCASE:
340 p = sel;
341 while (*p) {
342 if (isalpha(*p))
343 *p = toupper(*p);
344 p++;
345 }
346 break;
347
348 case LOWERCASE:
349 p = sel;
350 while (*p) {
351 if (isalpha(*p))
352 *p = tolower(*p);
353 p++;
354 }
355 break;
356
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);
365 }
366 p++;
367 }
368 break;
369
370 case ROT13:
371 p = sel;
372 while (*p) {
373 *p = DOROT13(*p); // fixme condition both true and false?
374 p++;
375 }
376 break;
377
378 case XOR:
379 p = sel;
380 while (*p) {
381 *p = (~*p);
382 p++;
383 }
384 break;
385
386 case FORMAT:
387 p = sel;
388 while (*p) {
389 if (*p == '\r') {
390 memmove(p, p + 1, strlen(p));
391 continue;
392 }
393 if (*p == '\n') {
394 *p = ' ';
395 continue;
396 }
397 p++;
398 }
399 break;
400
401 default: /* unknown action */
402#ifdef __DEBUG_ALLOC__
403 _heap_check();
404#endif
405 DosFreeMem(temp);
406 free(sel);
407# ifdef FORTIFY
408 Fortify_LeaveScope();
409# endif
410 DosPostEventSem(CompactSem);
411 MLEenable(h);
412 return FALSE;
413 }
414
415 /* replace selection with altered text */
416 p = sel;
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));
427 if (!sellen) {
428 Runtime_Error(pszSrcFile, __LINE__, "sellen 0");
429 break;
430 }
431 p += sellen;
432 oldlen -= sellen;
433 if (oldlen && *p == '\n' /* && *(p - 1) == '\r' */ )
434 p--;
435 } // while
436 WinSendMsg(h, MLM_SETSEL, MPFROMLONG(ancpos), MPFROMLONG(curpos));
437 MLEenable(h);
438#ifdef __DEBUG_ALLOC__
439 _heap_check();
440#endif
441 DosFreeMem(temp);
442 free(sel);
443# ifdef FORTIFY
444 Fortify_LeaveScope();
445# endif
446 DosPostEventSem(CompactSem);
447 return TRUE;
448}
449
450BOOL MLEquotepara(HWND h, CHAR * initials, BOOL fQuoteOld)
451{
452 LONG num;
453 CHAR lineend[2], line[8], *p;
454
455 if (!initials || !*initials)
456 initials = " > ";
457 num = MLEcurline(h);
458 while (MLElinelen(h, num) < 3L && MLEnumlines(h) >= num)
459 num++;
460 while (MLElinelen(h, num) > 2L && MLEnumlines(h) >= num) {
461 memset(line, 0, 8);
462 MLEgetlinetext(h, num, line, 7L);
463 line[7] = 0;
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");
471 }
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, ">");
477 }
478 num++;
479 }
480 MLEsetcurpos(h, MLEstartofline(h, num));
481 return TRUE;
482}
483
484BOOL MLEAutoLoad(HWND h, CHAR * filename)
485{
486 XMLEWNDPTR *vw;
487
488 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
489 if (vw && vw->size != sizeof(XMLEWNDPTR))
490 vw = NULL;
491 if (TestBinary(filename)) {
492 if (vw)
493 vw->hex = 1;
494 return MLEHexLoad(h, filename);
495 }
496 if (vw)
497 vw->hex = 2;
498 return MLEloadfile(h, filename);
499}
500
501BOOL MLEHexLoad(HWND h, CHAR * filename)
502{
503 /* insert a file into the current position in the MLE */
504
505 HAB hab;
506 CHAR *buffer = NULL, *hexbuff = NULL;
507 IPT iptOffset = -1;
508 ULONG numread, howmuch, numimport, action, len, left = 0;
509 BOOL ret = TRUE, first = TRUE;
510 CHAR titletext[512];
511 HWND grandpa;
512 XMLEWNDPTR *vw;
513 HFILE handle;
514 APIRET rc;
515
516 *titletext = 0;
517 hab = WinQueryAnchorBlock(h);
518 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
519 if (vw && vw->size != sizeof(XMLEWNDPTR))
520 vw = NULL;
521 grandpa = GrandparentOf(h);
522 *titletext = 0;
523 WinQueryWindowText(grandpa, 512, titletext);
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);
529 if (rc) {
530 ret = FALSE;
531 }
532 else {
533 DosChgFilePtr(handle, 0, FILE_END, &len);
534 DosChgFilePtr(handle, 0, FILE_BEGIN, &action);
535 if (len) {
536 rc = DosAllocMem((PVOID) & hexbuff, 50001,
537 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
538 if (rc || !hexbuff) {
539 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
540 GetPString(IDS_OUTOFMEMORY));
541 ret = FALSE;
542 }
543 else {
544 buffer = xmalloc(10000, pszSrcFile, __LINE__);
545 if (!buffer)
546 ret = FALSE;
547 else {
548 MLEclearall(h);
549 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(hexbuff),
550 MPFROMLONG(50000L));
551 if (!DosRead(handle, buffer, min(10000, len), &numread) && numread) {
552
553 CHAR s[81];
554
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 }
574// fprintf(stderr,"%d bytes of %d imported\n",howmuch,numimport);
575 if (howmuch < 1) {
576 numimport = 0;
577 break;
578 }
579 while (howmuch < numimport) {
580 numimport -= howmuch;
581 memmove(hexbuff, hexbuff + howmuch, numimport);
582 DosSleep(0); //26 Aug 07 GKY 1
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 }
607 DosSleep(1);
608 }
609 else
610 ret = FALSE;
611 free(buffer);
612# ifdef FORTIFY
613 Fortify_LeaveScope();
614# endif
615 }
616 DosFreeMem(hexbuff);
617 }
618 }
619 if (WinIsWindow(hab, h))
620 WinSetWindowText(grandpa, titletext);
621 DosClose(handle);
622 }
623 if (!first) {
624 WinEnableWindowUpdate(h, TRUE);
625 MLEenable(h);
626 }
627 MLEsetchanged(h, FALSE);
628 return ret;
629}
630
631//== MLEinsertfile() insert a file into the current position in the MLE ==
632
633BOOL MLEinsertfile(HWND h, CHAR * filename)
634{
635
636 HAB hab;
637 FILE *fp;
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;
645 XMLEWNDPTR *vw;
646 APIRET rc;
647
648 *titletext = 0;
649 hab = WinQueryAnchorBlock(h);
650 vw = (XMLEWNDPTR *) WinQueryWindowPtr(WinQueryWindow(h, QW_PARENT), QWL_USER);
651 if (vw && vw->size != sizeof(XMLEWNDPTR))
652 vw = NULL;
653 grandpa = GrandparentOf(h);
654 *titletext = 0;
655 WinQueryWindowText(grandpa, 512, titletext);
656 fp = _fsopen(filename, "r", SH_DENYNO);
657 if (!fp)
658 ret = FALSE;
659 else {
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);
667 if (rc || !buffer) {
668 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
669 GetPString(IDS_OUTOFMEMORY));
670 ret = FALSE;
671 }
672 else {
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... */
679
680 CHAR s[81];
681
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 }
723// fprintf(stderr,"%d bytes of %d imported\n",howmuch,numread);
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 }
741 DosSleep(0); //26 Aug 07 GKY 1
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 }
752 DosSleep(0); //26 Aug 07 GKY 1
753 }
754 DosFreeMem(buffer);
755 }
756 }
757 if (WinIsWindow(hab, h))
758 WinSetWindowText(grandpa, titletext);
759 fclose(fp);
760 }
761 if (!first) {
762 WinEnableWindowUpdate(h, TRUE);
763 MLEenable(h);
764 }
765 return ret;
766}
767
768typedef struct
769{
770 USHORT size;
771 USHORT hex;
772 HWND h;
773 CHAR filename[CCHMAXPATH];
774 HWND hwndReport;
775 HWND msg;
776}
777BKGLOAD;
778
779VOID LoadThread(VOID * arg)
780{
781 BKGLOAD *bkg;
782 BOOL fSuccess;
783 HAB thab;
784 HMQ thmq;
785
786 DosError(FERR_DISABLEHARDERR);
787
788# ifdef FORTIFY
789 Fortify_EnterScope();
790# endif
791
792 bkg = (BKGLOAD *) arg;
793 if (bkg) {
794 thab = WinInitialize(0);
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));
811#ifdef __DEBUG_ALLOC__
812 _heap_check();
813#endif
814 WinDestroyMsgQueue(thmq);
815 }
816 else
817 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
818 DecrThreadUsage();
819 WinTerminate(thab);
820 }
821 else {
822 // fixme to be gone? - can not PostMsg without HAB
823 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
824 }
825 free(bkg);
826 } // if bkg
827# ifdef FORTIFY
828 Fortify_LeaveScope();
829# endif
830 // _endthread(); // 10 Dec 08 SHL
831}
832
833INT MLEbackgroundload(HWND hwndReport, ULONG msg, HWND h, CHAR * filename,
834 INT hex)
835{
836 /* load a file into the MLE in the background (via a separate thread)
837 * return _beginthread status
838 */
839
840 BKGLOAD *bkg;
841
842 bkg = xmallocz(sizeof(BKGLOAD), pszSrcFile, __LINE__);
843 if (!bkg)
844 return -1;
845 bkg->size = sizeof(BKGLOAD);
846 bkg->hex = (USHORT) hex;
847 bkg->hwndReport = hwndReport;
848 bkg->msg = msg;
849 bkg->h = h;
850 strcpy(bkg->filename, filename);
851 return xbeginthread(LoadThread,
852 65536,
853 bkg,
854 pszSrcFile,
855 __LINE__);
856}
857
858BOOL MLEloadfile(HWND h, CHAR * filename)
859{
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
865 FILESTATUS3 fsa;
866 BOOL ret;
867
868 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, sizeof(fsa)) &&
869 ~fsa.attrFile & FILE_DIRECTORY) {
870 MLEclearall(h);
871 ret = MLEinsertfile(h, filename);
872 MLEsetchanged(h, FALSE);
873 return ret;
874 }
875 return FALSE;
876}
877
878BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
879 BOOL striptraillines, BOOL striptrailspaces)
880{
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
885 FILE *fp = NULL;
886 CHAR *buffer = NULL;
887 CHAR *p;
888 BOOL ok = TRUE;
889 INT blanklines = 0;
890 BOOL fWrap = MLEgetwrap(h);
891 APIRET rc;
892
893 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
894 if (!MLEgetlen(h)) /* nothing to save; forget it */
895 return TRUE;
896
897 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
898
899 if (striptraillines) {
900
901 register LONG x;
902 LONG numlines;
903
904 numlines = MLEnumlines(h);
905 for (x = numlines; x; x--) {
906 if (MLElinelen(h, x - 1L) < 2)
907 MLEdeleteline(h, x - 1L);
908 else
909 break;
910 }
911 if (!MLEgetlen(h)) {
912 /* nothing to save; forget it */
913 MLEsetwrap(h, fWrap); // Restore
914 return TRUE;
915 }
916 }
917
918 rc = DosAllocMem((PVOID) & buffer, 4096L,
919 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
920 if (rc || !buffer) {
921 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
922 GetPString(IDS_OUTOFMEMORY));
923 ok = FALSE;
924 }
925 else {
926 fp = fopen(filename, "a+");
927 if (!fp)
928 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
929 if (!fp)
930 ok = FALSE;
931 else {
932 LONG numlines, ret, len, wl, temp;
933 IPT s;
934
935 fseek(fp, 0L, SEEK_END);
936 numlines = MLEnumlines(h);
937
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
992 }
993 }
994
995 MLEsetwrap(h, fWrap); // Restore
996
997 if (fp)
998 fclose(fp);
999 if (buffer)
1000 DosFreeMem(buffer);
1001
1002 return ok;
1003}
1004
1005MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
1006{
1007 /* initiate search(/replace)s in edit mode */
1008
1009 SRCHPTR *vw;
1010
1011 if (msg != WM_INITDLG)
1012 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
1013 else
1014 vw = NULL;
1015
1016 switch (msg) {
1017 case WM_INITDLG:
1018 vw = (SRCHPTR *) mp2;
1019 if (!vw) {
1020 WinDismissDlg(hwnd, 0);
1021 break;
1022 }
1023 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
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;
1058
1059 case WM_CONTROL:
1060 return 0;
1061
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;
1072
1073 case DID_CANCEL:
1074 WinDismissDlg(hwnd, 0);
1075 break;
1076
1077 case DID_OK:
1078 WinShowWindow(hwnd, FALSE);
1079 {
1080 CHAR temp[257];
1081 APIRET ret;
1082
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);
1128 }
1129 break;
1130 }
1131 return 0;
1132
1133 case WM_CLOSE:
1134 break;
1135 }
1136
1137 return WinDefDlgProc(hwnd, msg, mp1, mp2);
1138}
1139
1140BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
1141{
1142 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1143 MLEgetseltext(vw->hwndmle, vw->search);
1144 vw->search[255] = 0;
1145 }
1146 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1147 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
1148 return TRUE;
1149 return FALSE;
1150}
1151
1152INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
1153{
1154 if (!*vw->search)
1155 return -1;
1156 else {
1157 vw->se.iptStart++;
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) {
1167
1168 APIRET ret;
1169
1170 ret = saymsg(MB_YESNOCANCEL,
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;
1177 }
1178 }
1179 return 0;
1180}
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.