source: trunk/dll/mle.c@ 1039

Last change on this file since 1039 was 1039, checked in by Gregg Young, 17 years ago

Removed unnecessary xfrees and included fortify.h where needed; moved several misplaced (x)frees;

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