source: trunk/dll/mle.c@ 1063

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

Fortify ifdef reformat

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