source: trunk/dll/mle.c@ 1498

Last change on this file since 1498 was 1498, checked in by Gregg Young, 16 years ago

Changes to get FM2 to compile with the latest watcom 1.9 beta (mostly type casts of CHAR CONSTANT * to CHAR *). Changes to get the environment settings working everywhere again (broken by the change that moved commands to the INI); Added an environment size variable (set to 2048 which was the largest I found hard coded). Still need to find everywhere the environment size is set and use this variable.

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