source: trunk/dll/mle.c@ 1036

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

Fixed early memory free; Added free_... functions to make fortify checking easier; Added fortify scopes; Delete now moves to trash can on systems with the xworkplace trash can installed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.1 KB
Line 
1
2/***********************************************************************
3
4 $Id: mle.c 1029 2008-06-23 01:30:16Z 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 xfree(sel, pszSrcFile, __LINE__);
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 xfree(sel, pszSrcFile, __LINE__);
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 xfree(sel, pszSrcFile, __LINE__);
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 xfree(sel, pszSrcFile, __LINE__);
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 xfree(sel, pszSrcFile, __LINE__);
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 xfree(sel, pszSrcFile, __LINE__);
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 xfree(buffer, pszSrcFile, __LINE__);
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 //xfree(bkg, pszSrcFile, __LINE__);
799 WinDestroyMsgQueue(thmq);
800 }
801 DecrThreadUsage();
802 WinTerminate(thab);
803 xfree(bkg, pszSrcFile, __LINE__);
804 bkg = NULL;
805# ifdef FORTIFY
806 Fortify_LeaveScope();
807# endif
808 _endthread();
809 }
810 // fixme to be gone?
811 if (bkg) {
812 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
813 xfree(bkg, pszSrcFile, __LINE__);
814# ifdef FORTIFY
815 Fortify_LeaveScope();
816# endif
817 }
818 }
819}
820
821INT MLEbackgroundload(HWND hwndReport, ULONG msg, HWND h, CHAR * filename,
822 INT hex)
823{
824 /* load a file into the MLE in the background (via a separate thread)
825 * return _beginthread status
826 */
827
828 BKGLOAD *bkg;
829 INT rc;
830
831 bkg = xmallocz(sizeof(BKGLOAD), pszSrcFile, __LINE__);
832 if (!bkg)
833 return -1;
834 bkg->size = sizeof(BKGLOAD);
835 bkg->hex = (USHORT) hex;
836 bkg->hwndReport = hwndReport;
837 bkg->msg = msg;
838 bkg->h = h;
839 strcpy(bkg->filename, filename);
840 rc = _beginthread(LoadThread, NULL, 65536, bkg);
841 if (rc == -1)
842 Runtime_Error(pszSrcFile, __LINE__,
843 GetPString(IDS_COULDNTSTARTTHREADTEXT));
844 return rc;
845}
846
847BOOL MLEloadfile(HWND h, CHAR * filename)
848{
849 /* load a file into the MLE, getting rid of whatever was already
850 * there. Note this returns without erasing existing text if the
851 * file to load does not exist
852 */
853
854 FILESTATUS3 fsa;
855 BOOL ret;
856
857 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, sizeof(fsa)) &&
858 ~fsa.attrFile & FILE_DIRECTORY) {
859 MLEclearall(h);
860 ret = MLEinsertfile(h, filename);
861 MLEsetchanged(h, FALSE);
862 return ret;
863 }
864 return FALSE;
865}
866
867BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
868 BOOL striptraillines, BOOL striptrailspaces)
869{
870 /* save the MLE contents as a file. Format the output so that
871 * the file is CR/LF terminated as presented in the MLE.
872 */
873
874 FILE *fp = NULL;
875 CHAR *buffer = NULL;
876 CHAR *p;
877 BOOL ok = TRUE;
878 INT blanklines = 0;
879 BOOL fWrap = MLEgetwrap(h);
880 APIRET rc;
881
882 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
883 if (!MLEgetlen(h)) /* nothing to save; forget it */
884 return TRUE;
885
886 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
887
888 if (striptraillines) {
889
890 register LONG x;
891 LONG numlines;
892
893 numlines = MLEnumlines(h);
894 for (x = numlines; x; x--) {
895 if (MLElinelen(h, x - 1L) < 2)
896 MLEdeleteline(h, x - 1L);
897 else
898 break;
899 }
900 if (!MLEgetlen(h)) {
901 /* nothing to save; forget it */
902 MLEsetwrap(h, fWrap); // Restore
903 return TRUE;
904 }
905 }
906
907 rc = DosAllocMem((PVOID) & buffer, 4096L,
908 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
909 if (rc || !buffer) {
910 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
911 GetPString(IDS_OUTOFMEMORY));
912 ok = FALSE;
913 }
914 else {
915 fp = fopen(filename, "a+");
916 if (!fp)
917 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
918 if (!fp)
919 ok = FALSE;
920 else {
921 LONG numlines, ret, len, wl, temp;
922 IPT s;
923
924 fseek(fp, 0L, SEEK_END);
925 numlines = MLEnumlines(h);
926
927 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(4095L));
928 for (temp = 0; temp < numlines; temp++) {
929 s = MLEstartofline(h, temp);
930 wl = len = (LONG) MLElinelenleft(h, s);
931 ret = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&s), MPFROMP(&len));
932 if (ret < 0)
933 break;
934 wl = min(wl, ret);
935 buffer[wl] = 0;
936 if (*buffer) {
937 p = buffer + strlen(buffer) - 1;
938 while (p >= buffer && (*p == '\n' || *p == '\r')) {
939 *p = 0;
940 p--;
941 }
942 }
943 if (tabspaces) {
944 p = buffer;
945 while (*p) {
946 if (*p == '\t') {
947 *p = ' ';
948 memmove((p + tabspaces) - 1, p, strlen(p) + 1);
949 memset(p, ' ', tabspaces);
950 }
951 p++;
952 }
953 }
954 if (striptrailspaces && *buffer) {
955 p = buffer + strlen(buffer) - 1;
956 while (p >= buffer && (*p == ' ' || *p == '\t')) {
957 *p = 0;
958 p--;
959 }
960 }
961 if (striptraillines) {
962 if (!*buffer) {
963 blanklines++;
964 continue;
965 }
966 else {
967 while (blanklines) {
968 fwrite("\n", 1, 1, fp);
969 blanklines--;
970 }
971 }
972 }
973 strcat(buffer, "\n");
974 // buffer = translate_out(buffer,4095,h,filename);
975 if (fwrite(buffer, 1, strlen(buffer), fp) < 1) {
976 saymsg(MB_ENTER,
977 h, GetPString(IDS_ARGHTEXT), GetPString(IDS_WRITEERRORTEXT));
978 break;
979 }
980 } // for lines
981 }
982 }
983
984 MLEsetwrap(h, fWrap); // Restore
985
986 if (fp)
987 fclose(fp);
988 if (buffer)
989 DosFreeMem(buffer);
990
991 return ok;
992}
993
994MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
995{
996 /* initiate search(/replace)s in edit mode */
997
998 SRCHPTR *vw;
999
1000 if (msg != WM_INITDLG)
1001 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
1002 else
1003 vw = NULL;
1004
1005 switch (msg) {
1006 case WM_INITDLG:
1007 vw = (SRCHPTR *) mp2;
1008 if (!vw) {
1009 WinDismissDlg(hwnd, 0);
1010 break;
1011 }
1012 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
1013 WinSendDlgItemMsg(hwnd, SRCH_SEARCH, EM_SETTEXTLIMIT,
1014 MPFROM2SHORT(256, 0), MPVOID);
1015 WinSendDlgItemMsg(hwnd, SRCH_REPLACE, EM_SETTEXTLIMIT,
1016 MPFROM2SHORT(256, 0), MPVOID);
1017 if (*vw->search)
1018 WinSetDlgItemText(hwnd, SRCH_SEARCH, vw->search);
1019 if (!MLEgetreadonly(vw->hwndmle)) {
1020 if (*vw->replace)
1021 WinSetDlgItemText(hwnd, SRCH_REPLACE, vw->replace);
1022 WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_SETCHECK,
1023 MPFROM2SHORT(vw->sandr, 0), MPVOID);
1024 WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_SETCHECK,
1025 MPFROM2SHORT(vw->rall, 0), MPVOID);
1026 }
1027 else {
1028 WinEnableWindow(WinWindowFromID(hwnd, SRCH_SANDR), FALSE);
1029 WinEnableWindow(WinWindowFromID(hwnd, SRCH_RALL), FALSE);
1030 WinEnableWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1031 WinShowWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1032 *vw->replace = 0;
1033 vw->sandr = FALSE;
1034 vw->rall = FALSE;
1035 }
1036 memset(&vw->se, 0, sizeof(MLE_SEARCHDATA));
1037 vw->se.cb = sizeof(MLE_SEARCHDATA);
1038 vw->se.pchFind = (PCHAR) vw->search;
1039 vw->se.cchFind = (SHORT) strlen(vw->search);
1040 vw->se.pchReplace = (PCHAR) vw->replace;
1041 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1042 vw->se.iptStart = MLEcurpos(vw->hwndmle);
1043 vw->se.iptStop = -1L;
1044 vw->se.cchFound = 0;
1045 PosOverOkay(hwnd);
1046 break;
1047
1048 case WM_CONTROL:
1049 return 0;
1050
1051 case WM_COMMAND:
1052 switch (SHORT1FROMMP(mp1)) {
1053 case IDM_HELP:
1054 saymsg(MB_ENTER | MB_ICONASTERISK,
1055 hwnd,
1056 NullStr,
1057 GetPString(IDS_ENTERSEARCHSTRINGTEXT),
1058 (MLEgetreadonly(vw->hwndmle)) ?
1059 "." : GetPString(IDS_REPLACESTRINGTEXT));
1060 break;
1061
1062 case DID_CANCEL:
1063 WinDismissDlg(hwnd, 0);
1064 break;
1065
1066 case DID_OK:
1067 WinShowWindow(hwnd, FALSE);
1068 {
1069 CHAR temp[257];
1070 APIRET ret;
1071
1072 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_QUERYCHECK,
1073 MPVOID, MPVOID))
1074 vw->sandr = TRUE;
1075 else
1076 vw->sandr = FALSE;
1077 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_QUERYCHECK,
1078 MPVOID, MPVOID))
1079 vw->rall = TRUE;
1080 else
1081 vw->rall = FALSE;
1082 *vw->replace = 0;
1083 WinQueryDlgItemText(hwnd, SRCH_REPLACE, 256, vw->replace);
1084 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1085 WinQueryDlgItemText(hwnd, SRCH_SEARCH, 256, temp);
1086 if (*temp) {
1087 strcpy(vw->search, temp);
1088 vw->se.cchFind = (SHORT) strlen(vw->search);
1089 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1090 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1091 (MLFSEARCH_CASESENSITIVE *
1092 (vw->fInsensitive ==
1093 FALSE)) | (MLFSEARCH_CHANGEALL *
1094 (vw->rall != 0))),
1095 MPFROMP(&vw->se))) {
1096 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1097 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1098 WinDismissDlg(hwnd, 0);
1099 break;
1100 }
1101 else if (vw->sandr && !vw->rall) {
1102 ret = saymsg(MB_YESNOCANCEL,
1103 hwnd,
1104 NullStr,
1105 GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1106 if (ret == MBID_YES)
1107 MLEinsert(vw->hwndmle, vw->replace);
1108 else if (ret == MBID_CANCEL) {
1109 WinDismissDlg(hwnd, 0);
1110 break;
1111 }
1112 WinDismissDlg(hwnd, 1);
1113 break;
1114 }
1115 }
1116 WinDismissDlg(hwnd, 0);
1117 }
1118 break;
1119 }
1120 return 0;
1121
1122 case WM_CLOSE:
1123 break;
1124 }
1125
1126 return WinDefDlgProc(hwnd, msg, mp1, mp2);
1127}
1128
1129BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
1130{
1131 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1132 MLEgetseltext(vw->hwndmle, vw->search);
1133 vw->search[255] = 0;
1134 }
1135 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1136 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
1137 return TRUE;
1138 return FALSE;
1139}
1140
1141INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
1142{
1143 if (!*vw->search)
1144 return -1;
1145 else {
1146 vw->se.iptStart++;
1147 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1148 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1149 (MLFSEARCH_CASESENSITIVE *
1150 (vw->fInsensitive ==
1151 FALSE)) | (MLFSEARCH_CHANGEALL *
1152 (vw->rall))), MPFROMP(&vw->se)))
1153 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1154 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1155 else if (vw->sandr && !vw->rall) {
1156
1157 APIRET ret;
1158
1159 ret = saymsg(MB_YESNOCANCEL,
1160 hwnd,
1161 NullStr, GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1162 if (ret == MBID_YES)
1163 MLEinsert(vw->hwndmle, vw->replace);
1164 if (ret != MBID_CANCEL)
1165 return 1;
1166 }
1167 }
1168 return 0;
1169}
1170
1171#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
1172#pragma alloc_text(FMMLE,MLEclearall,MLEtextatcursor,MLEtextatpos,MLEsizeofsel)
1173#pragma alloc_text(FMMLE3,MLEdoblock,MLEquotepara,MLEinternet)
1174#pragma alloc_text(FMMLE4,MLEAutoLoad,MLEHexLoad,MLEinsertfile,LoadThread,MLEbackgroundload)
1175#pragma alloc_text(FMMLE5,MLEloadfile,MLEexportfile)
1176#pragma alloc_text(FMMLE3,MLEfindfirst,MLEfindnext,SandRDlgProc)
Note: See TracBrowser for help on using the repository browser.