source: trunk/dll/mle.c@ 1544

Last change on this file since 1544 was 1544, checked in by Gregg Young, 15 years ago

Changes to fopen and _fsopen to allow FM2 to be loaded in high memory

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