source: trunk/dll/mle.c@ 1185

Last change on this file since 1185 was 1185, checked in by John Small, 17 years ago

Ticket 187: Draft 2: Move remaining function declarations

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