source: trunk/dll/mle.c@ 1628

Last change on this file since 1628 was 1628, checked in by Gregg Young, 14 years ago

Hard coded the flags for the xDosAlloc* wrappers and added a description for each of them.

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