source: trunk/dll/mle.c@ 1627

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

Add a low mem version of xDosAlloc* wrappers; move error checking into all the xDosAlloc* wrappers. Ticket 471

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