source: trunk/dll/mle.c@ 1009

Last change on this file since 1009 was 1009, checked in by Steven Levine, 17 years ago

Add xfree xstrdup Fortify support
Add MT capable Fortify scope logic

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