source: trunk/dll/mle.c@ 1480

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

Fix failure to correctly check for large file support in FindSwapperDat fall back code minor streamling. Add LVM.EXE to partition submenu. Stop using xDosQueryAppType where the file name it is passed is already a local stack variable. Correct a typo in several file header comments.

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