source: trunk/dll/mle.c@ 1040

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

Fixed last 2 (at least that I could find) hard coded command line lengths (ticket 233)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.9 KB
Line 
1
2/***********************************************************************
3
4 $Id: mle.c 1040 2008-07-05 23:21:38Z gyoung $
5
6 MLE text editor/viewer
7
8 Copyright (c) 1993-97 M. Kimes
9 Copyright (c) 2004, 2007 Steven H.Levine
10
11 01 Aug 04 SHL Rework lstrip/rstrip usage
12 16 Apr 06 SHL MLEexportfile: rework to avoid wrap problems
13 14 Jul 06 SHL Use Runtime_Error
14 03 Nov 06 SHL Count thread usage
15 22 Mar 07 GKY Use QWL_USER
16 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
17 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
18 26 Aug 07 GKY DosSleep(1) in loops changed to (0)
19 17 Dec 07 GKY Make WPURLDEFAULTSETTINGS the fall back for ftp/httprun
20 29 Feb 08 GKY Refactor global command line variables to notebook.h
21 29 Feb 08 GKY Use xfree where appropriate
22 22 Jun 08 GKY Fixed memory buffer access after it had been freed
23
24***********************************************************************/
25
26#include <stdlib.h>
27#include <string.h>
28#include <ctype.h>
29#include <share.h>
30#include <process.h> // _beginthread
31
32#define INCL_DOS
33#define INCL_WIN
34#define INCL_LONGLONG
35
36#include "fm3dlg.h"
37#include "mle.h"
38#include "fm3str.h"
39#include "errutil.h" // Dos_Error...
40#include "strutil.h" // GetPString
41#include "notebook.h" // httprun etc
42#include "fm3dll.h"
43#include "fortify.h"
44
45static PSZ pszSrcFile = __FILE__;
46
47#define FAKEROT 1
48#define DOROT13(c) (!isalpha((c)))?(c):((((c) >= (char) 'A') && \
49 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
50 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
51 ((c) - (char) 0xd):(c)
52
53/*((FAKEROT==0)?(c):(FAKEROT==1)?(!isalpha((c)))?(c):((((c) >= (char) 'A') && \
54 ((c) <= (char) 'M')) || (((c) >= (char) 'a') && ((c) <= (char) 'm')))?((c) + (char) 0xd)\
55 :((((c) >= (char) 'N') && ((c) <= (char) 'Z')) || (((c) >= (char) 'n') && ((c) <= (char) 'z')))?\
56 ((c) - (char) 0xd):(c):((c) >= (char) '!') ? ((((c) + (char) 47) > (char) '~') ? ((c) - (char) 47) :\
57 ((c) + (char) 47)) : (c))*/
58
59LONG MLEgetlinetext(HWND h, LONG l, CHAR * buf, INT maxlen)
60{
61 /* get text of line l from MLE */
62
63 IPT s, e;
64
65 s = MLEstartofline(h, l);
66 e = MLElinelenleft(h, s);
67 return MLEtextatpos(h, s, buf, min((INT) e, maxlen));
68}
69
70LONG MLEdeleteline(HWND h, LONG l)
71{
72 /* delete line l from MLE */
73
74 IPT s, e;
75
76 s = MLEstartofline(h, l);
77 e = MLElinelenleft(h, s);
78 return MLEdelete(h, s, e);
79}
80
81LONG MLEdeletecurline(HWND h)
82{
83 /* delete current line from MLE */
84
85 LONG l;
86
87 l = MLEcurline(h);
88 return MLEdeleteline(h, l);
89}
90
91LONG MLEdeletetoeol(HWND h)
92{
93 /* delete from cursor pos to end of line */
94
95 IPT s, e;
96
97 s = MLEcurpos(h);
98 e = MLEcurlenleft(h);
99 return MLEdelete(h, s, e);
100}
101
102VOID MLEclearall(HWND h)
103{
104 /* remove all text from MLE */
105 LONG len;
106
107 len = MLEgetlen(h);
108 if (len)
109 MLEdelete(h, 0, len);
110}
111
112LONG MLEtextatcursor(HWND h, CHAR * buffer, INT buflen)
113{
114 /* place up to buflen chars of text from cursor pos into buffer
115 * return # of chars imported
116 */
117
118 IPT i;
119
120 i = MLEcurpos(h);
121 return MLEtextatpos(h, i, buffer, buflen);
122}
123
124LONG MLEtextatpos(HWND h, IPT i, CHAR * buffer, INT buflen)
125{
126 /* place up to buflen chars of text from pos i in buffer
127 * return # of chars imported
128 */
129
130 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer),
131 MPFROMLONG((LONG) buflen));
132 return (LONG) WinSendMsg(h, MLM_EXPORT,
133 MPFROMP(&i), MPFROMLONG((PLONG) & buflen));
134}
135
136LONG MLEsizeofsel(HWND h)
137{
138 /* return length of selected text */
139
140 IPT cursor, anchor, test;
141
142 cursor = MLEcurpos(h);
143 anchor = MLEancpos(h);
144 test = min(cursor, anchor);
145 /* MLE fakes us out; get real length in bytes */
146 return (LONG) WinSendMsg(h, MLM_QUERYFORMATTEXTLENGTH,
147 MPFROMLONG(test),
148 MPFROMLONG((LONG) ((cursor < anchor) ?
149 (anchor - cursor) :
150 (cursor - anchor))));
151}
152
153VOID MLEinternet(HWND h, BOOL ftp)
154{
155 CHAR *temp = NULL;
156 IPT ancpos, curpos, here;
157 LONG len, oldlen;
158 APIRET rc;
159 ULONG size;
160
161 len = MLEsizeofsel(h);
162 len = min(2048, len);
163 oldlen = len;
164 if (len) {
165 len++;
166 rc = DosAllocMem((PVOID) & temp, 4096,
167 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
168 if (rc || !temp)
169 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
170 GetPString(IDS_OUTOFMEMORY));
171 else {
172 ancpos = MLEancpos(h);
173 curpos = MLEcurpos(h);
174 here = min(curpos, ancpos);
175 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(temp), MPFROMLONG(len));
176 len = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&here), MPFROMP(&len));
177 if (len <= 1)
178 Runtime_Error(pszSrcFile, __LINE__, "len <= 1");
179 else {
180 if (len > oldlen)
181 len--;
182 temp[len] = 0;
183 bstripcr(temp);
184 if (*temp) {
185 if (ftp) {
186 if (fFtpRunWPSDefault) {
187 CHAR WPSDefaultFtpRun[CCHMAXPATH], WPSDefaultFtpRunDir[CCHMAXPATH];
188
189 size = sizeof(WPSDefaultFtpRun);
190 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
191 "DefaultBrowserExe", WPSDefaultFtpRun, &size);
192 size = sizeof(WPSDefaultFtpRunDir);
193 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
194 "DefaultWorkingDir", WPSDefaultFtpRunDir, &size);
195 runemf2(SEPARATE | WINDOWED,
196 h, pszSrcFile, __LINE__,
197 WPSDefaultFtpRunDir,
198 fLibPathStrictFtpRun ? "SET LIBPATHSTRICT=TRUE" : NULL,
199 "%s %s", WPSDefaultFtpRun, temp);
200 }
201 else
202 runemf2(SEPARATE | WINDOWED,
203 h, pszSrcFile, __LINE__,
204 ftprundir, NULL, "%s %s", ftprun, temp);
205 }
206 else
207 if (fHttpRunWPSDefault) {
208 CHAR WPSDefaultHttpRun[CCHMAXPATH], WPSDefaultHttpRunDir[CCHMAXPATH];
209
210 size = sizeof(WPSDefaultHttpRun);
211 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
212 "DefaultBrowserExe", WPSDefaultHttpRun, &size);
213 size = sizeof(WPSDefaultHttpRunDir);
214 PrfQueryProfileData(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
215 "DefaultWorkingDir", WPSDefaultHttpRunDir, &size);
216 runemf2(SEPARATE | WINDOWED,
217 h, pszSrcFile, __LINE__,
218 WPSDefaultHttpRunDir,
219 fLibPathStrictHttpRun ? "SET LIBPATHSTRICT=TRUE" : NULL,
220 "%s %s", WPSDefaultHttpRun, temp);
221 }
222 else
223 runemf2(SEPARATE | WINDOWED,
224 h, pszSrcFile, __LINE__,
225 httprundir, NULL, "%s %s", httprun, temp);
226 }
227 }
228 DosFreeMem(temp);
229 }
230 }
231}
232
233BOOL MLEdoblock(HWND h, INT action, CHAR * filename)
234{
235 /* perform action on text in selection */
236
237 register CHAR *p;
238 CHAR *sel, *temp = NULL;
239 IPT ancpos, curpos, here;
240 LONG sellen, oldlen;
241 APIRET rc;
242
243 oldlen = MLEsizeofsel(h);
244 if (!oldlen)
245 return TRUE;
246 sel = xmallocz((size_t) (oldlen + 2), pszSrcFile, __LINE__);
247 if (!sel)
248 return FALSE;
249 rc = DosAllocMem((PVOID) & temp, 32768L,
250 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
251 if (rc || !temp) {
252 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
253 GetPString(IDS_OUTOFMEMORY));
254 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 bkg = (BKGLOAD *) arg;
774 if (bkg) {
775 thab = WinInitialize(0);
776 if (thab) {
777 thmq = WinCreateMsgQueue(thab, 0);
778 if (thmq) {
779 WinCancelShutdown(thmq, TRUE);
780 IncrThreadUsage();
781# ifdef FORTIFY
782 Fortify_EnterScope();
783# endif
784 priority_normal();
785 if (bkg->hex == 1)
786 fSuccess = MLEHexLoad(bkg->h, bkg->filename);
787 else if (bkg->hex == 2)
788 fSuccess = MLEloadfile(bkg->h, bkg->filename);
789 else
790 fSuccess = MLEAutoLoad(bkg->h, bkg->filename);
791 priority_bumped();
792 if (bkg->hwndReport && WinIsWindow(thab, bkg->hwndReport))
793 PostMsg(bkg->hwndReport, bkg->msg, MPFROMLONG(fSuccess),
794 MPFROMP(bkg->filename));
795#ifdef __DEBUG_ALLOC__
796 _heap_check();
797#endif
798 WinDestroyMsgQueue(thmq);
799 }
800 else
801 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
802 DecrThreadUsage();
803 WinTerminate(thab);
804 free(bkg);
805 bkg = NULL;
806# ifdef FORTIFY
807 Fortify_LeaveScope();
808# endif
809 _endthread();
810 }
811 // fixme to be gone?
812 else {
813 PostMsg(bkg->hwndReport, bkg->msg, MPVOID, MPVOID);
814 free(bkg);
815 bkg = NULL;
816# ifdef FORTIFY
817 Fortify_LeaveScope();
818# endif
819 }
820 }
821}
822
823INT MLEbackgroundload(HWND hwndReport, ULONG msg, HWND h, CHAR * filename,
824 INT hex)
825{
826 /* load a file into the MLE in the background (via a separate thread)
827 * return _beginthread status
828 */
829
830 BKGLOAD *bkg;
831 INT rc;
832
833 bkg = xmallocz(sizeof(BKGLOAD), pszSrcFile, __LINE__);
834 if (!bkg)
835 return -1;
836 bkg->size = sizeof(BKGLOAD);
837 bkg->hex = (USHORT) hex;
838 bkg->hwndReport = hwndReport;
839 bkg->msg = msg;
840 bkg->h = h;
841 strcpy(bkg->filename, filename);
842 rc = _beginthread(LoadThread, NULL, 65536, bkg);
843 if (rc == -1)
844 Runtime_Error(pszSrcFile, __LINE__,
845 GetPString(IDS_COULDNTSTARTTHREADTEXT));
846 return rc;
847}
848
849BOOL MLEloadfile(HWND h, CHAR * filename)
850{
851 /* load a file into the MLE, getting rid of whatever was already
852 * there. Note this returns without erasing existing text if the
853 * file to load does not exist
854 */
855
856 FILESTATUS3 fsa;
857 BOOL ret;
858
859 if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, sizeof(fsa)) &&
860 ~fsa.attrFile & FILE_DIRECTORY) {
861 MLEclearall(h);
862 ret = MLEinsertfile(h, filename);
863 MLEsetchanged(h, FALSE);
864 return ret;
865 }
866 return FALSE;
867}
868
869BOOL MLEexportfile(HWND h, CHAR * filename, INT tabspaces,
870 BOOL striptraillines, BOOL striptrailspaces)
871{
872 /* save the MLE contents as a file. Format the output so that
873 * the file is CR/LF terminated as presented in the MLE.
874 */
875
876 FILE *fp = NULL;
877 CHAR *buffer = NULL;
878 CHAR *p;
879 BOOL ok = TRUE;
880 INT blanklines = 0;
881 BOOL fWrap = MLEgetwrap(h);
882 APIRET rc;
883
884 // saymsg(MB_ENTER,h,DEBUG_STRING,"len = %ld",MLEgetlen(h));
885 if (!MLEgetlen(h)) /* nothing to save; forget it */
886 return TRUE;
887
888 MLEsetwrap(h, FALSE); // Need wrap off to export MLFIE_NOTRANS
889
890 if (striptraillines) {
891
892 register LONG x;
893 LONG numlines;
894
895 numlines = MLEnumlines(h);
896 for (x = numlines; x; x--) {
897 if (MLElinelen(h, x - 1L) < 2)
898 MLEdeleteline(h, x - 1L);
899 else
900 break;
901 }
902 if (!MLEgetlen(h)) {
903 /* nothing to save; forget it */
904 MLEsetwrap(h, fWrap); // Restore
905 return TRUE;
906 }
907 }
908
909 rc = DosAllocMem((PVOID) & buffer, 4096L,
910 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
911 if (rc || !buffer) {
912 Dos_Error(MB_CANCEL, rc, h, pszSrcFile, __LINE__,
913 GetPString(IDS_OUTOFMEMORY));
914 ok = FALSE;
915 }
916 else {
917 fp = fopen(filename, "a+");
918 if (!fp)
919 fp = xfopen(filename, "w", pszSrcFile, __LINE__);
920 if (!fp)
921 ok = FALSE;
922 else {
923 LONG numlines, ret, len, wl, temp;
924 IPT s;
925
926 fseek(fp, 0L, SEEK_END);
927 numlines = MLEnumlines(h);
928
929 WinSendMsg(h, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(4095L));
930 for (temp = 0; temp < numlines; temp++) {
931 s = MLEstartofline(h, temp);
932 wl = len = (LONG) MLElinelenleft(h, s);
933 ret = (LONG) WinSendMsg(h, MLM_EXPORT, MPFROMP(&s), MPFROMP(&len));
934 if (ret < 0)
935 break;
936 wl = min(wl, ret);
937 buffer[wl] = 0;
938 if (*buffer) {
939 p = buffer + strlen(buffer) - 1;
940 while (p >= buffer && (*p == '\n' || *p == '\r')) {
941 *p = 0;
942 p--;
943 }
944 }
945 if (tabspaces) {
946 p = buffer;
947 while (*p) {
948 if (*p == '\t') {
949 *p = ' ';
950 memmove((p + tabspaces) - 1, p, strlen(p) + 1);
951 memset(p, ' ', tabspaces);
952 }
953 p++;
954 }
955 }
956 if (striptrailspaces && *buffer) {
957 p = buffer + strlen(buffer) - 1;
958 while (p >= buffer && (*p == ' ' || *p == '\t')) {
959 *p = 0;
960 p--;
961 }
962 }
963 if (striptraillines) {
964 if (!*buffer) {
965 blanklines++;
966 continue;
967 }
968 else {
969 while (blanklines) {
970 fwrite("\n", 1, 1, fp);
971 blanklines--;
972 }
973 }
974 }
975 strcat(buffer, "\n");
976 // buffer = translate_out(buffer,4095,h,filename);
977 if (fwrite(buffer, 1, strlen(buffer), fp) < 1) {
978 saymsg(MB_ENTER,
979 h, GetPString(IDS_ARGHTEXT), GetPString(IDS_WRITEERRORTEXT));
980 break;
981 }
982 } // for lines
983 }
984 }
985
986 MLEsetwrap(h, fWrap); // Restore
987
988 if (fp)
989 fclose(fp);
990 if (buffer)
991 DosFreeMem(buffer);
992
993 return ok;
994}
995
996MRESULT EXPENTRY SandRDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
997{
998 /* initiate search(/replace)s in edit mode */
999
1000 SRCHPTR *vw;
1001
1002 if (msg != WM_INITDLG)
1003 vw = (SRCHPTR *) WinQueryWindowPtr(hwnd, QWL_USER);
1004 else
1005 vw = NULL;
1006
1007 switch (msg) {
1008 case WM_INITDLG:
1009 vw = (SRCHPTR *) mp2;
1010 if (!vw) {
1011 WinDismissDlg(hwnd, 0);
1012 break;
1013 }
1014 WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mp2);
1015 WinSendDlgItemMsg(hwnd, SRCH_SEARCH, EM_SETTEXTLIMIT,
1016 MPFROM2SHORT(256, 0), MPVOID);
1017 WinSendDlgItemMsg(hwnd, SRCH_REPLACE, EM_SETTEXTLIMIT,
1018 MPFROM2SHORT(256, 0), MPVOID);
1019 if (*vw->search)
1020 WinSetDlgItemText(hwnd, SRCH_SEARCH, vw->search);
1021 if (!MLEgetreadonly(vw->hwndmle)) {
1022 if (*vw->replace)
1023 WinSetDlgItemText(hwnd, SRCH_REPLACE, vw->replace);
1024 WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_SETCHECK,
1025 MPFROM2SHORT(vw->sandr, 0), MPVOID);
1026 WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_SETCHECK,
1027 MPFROM2SHORT(vw->rall, 0), MPVOID);
1028 }
1029 else {
1030 WinEnableWindow(WinWindowFromID(hwnd, SRCH_SANDR), FALSE);
1031 WinEnableWindow(WinWindowFromID(hwnd, SRCH_RALL), FALSE);
1032 WinEnableWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1033 WinShowWindow(WinWindowFromID(hwnd, SRCH_REPLACE), FALSE);
1034 *vw->replace = 0;
1035 vw->sandr = FALSE;
1036 vw->rall = FALSE;
1037 }
1038 memset(&vw->se, 0, sizeof(MLE_SEARCHDATA));
1039 vw->se.cb = sizeof(MLE_SEARCHDATA);
1040 vw->se.pchFind = (PCHAR) vw->search;
1041 vw->se.cchFind = (SHORT) strlen(vw->search);
1042 vw->se.pchReplace = (PCHAR) vw->replace;
1043 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1044 vw->se.iptStart = MLEcurpos(vw->hwndmle);
1045 vw->se.iptStop = -1L;
1046 vw->se.cchFound = 0;
1047 PosOverOkay(hwnd);
1048 break;
1049
1050 case WM_CONTROL:
1051 return 0;
1052
1053 case WM_COMMAND:
1054 switch (SHORT1FROMMP(mp1)) {
1055 case IDM_HELP:
1056 saymsg(MB_ENTER | MB_ICONASTERISK,
1057 hwnd,
1058 NullStr,
1059 GetPString(IDS_ENTERSEARCHSTRINGTEXT),
1060 (MLEgetreadonly(vw->hwndmle)) ?
1061 "." : GetPString(IDS_REPLACESTRINGTEXT));
1062 break;
1063
1064 case DID_CANCEL:
1065 WinDismissDlg(hwnd, 0);
1066 break;
1067
1068 case DID_OK:
1069 WinShowWindow(hwnd, FALSE);
1070 {
1071 CHAR temp[257];
1072 APIRET ret;
1073
1074 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_SANDR, BM_QUERYCHECK,
1075 MPVOID, MPVOID))
1076 vw->sandr = TRUE;
1077 else
1078 vw->sandr = FALSE;
1079 if ((USHORT) WinSendDlgItemMsg(hwnd, SRCH_RALL, BM_QUERYCHECK,
1080 MPVOID, MPVOID))
1081 vw->rall = TRUE;
1082 else
1083 vw->rall = FALSE;
1084 *vw->replace = 0;
1085 WinQueryDlgItemText(hwnd, SRCH_REPLACE, 256, vw->replace);
1086 vw->se.cchReplace = (SHORT) strlen(vw->replace);
1087 WinQueryDlgItemText(hwnd, SRCH_SEARCH, 256, temp);
1088 if (*temp) {
1089 strcpy(vw->search, temp);
1090 vw->se.cchFind = (SHORT) strlen(vw->search);
1091 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1092 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1093 (MLFSEARCH_CASESENSITIVE *
1094 (vw->fInsensitive ==
1095 FALSE)) | (MLFSEARCH_CHANGEALL *
1096 (vw->rall != 0))),
1097 MPFROMP(&vw->se))) {
1098 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1099 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1100 WinDismissDlg(hwnd, 0);
1101 break;
1102 }
1103 else if (vw->sandr && !vw->rall) {
1104 ret = saymsg(MB_YESNOCANCEL,
1105 hwnd,
1106 NullStr,
1107 GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1108 if (ret == MBID_YES)
1109 MLEinsert(vw->hwndmle, vw->replace);
1110 else if (ret == MBID_CANCEL) {
1111 WinDismissDlg(hwnd, 0);
1112 break;
1113 }
1114 WinDismissDlg(hwnd, 1);
1115 break;
1116 }
1117 }
1118 WinDismissDlg(hwnd, 0);
1119 }
1120 break;
1121 }
1122 return 0;
1123
1124 case WM_CLOSE:
1125 break;
1126 }
1127
1128 return WinDefDlgProc(hwnd, msg, mp1, mp2);
1129}
1130
1131BOOL MLEfindfirst(HWND hwnd, SRCHPTR * vw)
1132{
1133 if (MLEsizeofsel(vw->hwndmle) < 256L) {
1134 MLEgetseltext(vw->hwndmle, vw->search);
1135 vw->search[255] = 0;
1136 }
1137 if (WinDlgBox(HWND_DESKTOP, hwnd, SandRDlgProc, FM3ModHandle,
1138 SRCH_FRAME, MPFROMP(vw)) && *vw->search)
1139 return TRUE;
1140 return FALSE;
1141}
1142
1143INT MLEfindnext(HWND hwnd, SRCHPTR * vw)
1144{
1145 if (!*vw->search)
1146 return -1;
1147 else {
1148 vw->se.iptStart++;
1149 if (!WinSendMsg(vw->hwndmle, MLM_SEARCH,
1150 MPFROMLONG(MLFSEARCH_SELECTMATCH |
1151 (MLFSEARCH_CASESENSITIVE *
1152 (vw->fInsensitive ==
1153 FALSE)) | (MLFSEARCH_CHANGEALL *
1154 (vw->rall))), MPFROMP(&vw->se)))
1155 saymsg(MB_ENTER | MB_ICONASTERISK, hwnd, NullStr,
1156 GetPString(IDS_STRINGNOTFOUNDTEXT), vw->search);
1157 else if (vw->sandr && !vw->rall) {
1158
1159 APIRET ret;
1160
1161 ret = saymsg(MB_YESNOCANCEL,
1162 hwnd,
1163 NullStr, GetPString(IDS_CONFIRMREPLACETEXT), vw->replace);
1164 if (ret == MBID_YES)
1165 MLEinsert(vw->hwndmle, vw->replace);
1166 if (ret != MBID_CANCEL)
1167 return 1;
1168 }
1169 }
1170 return 0;
1171}
1172
1173#pragma alloc_text(FMMLE,MLEgetlinetext,MLEdeleteline,MLEdeletecurline,MLEdeletetoeol)
1174#pragma alloc_text(FMMLE,MLEclearall,MLEtextatcursor,MLEtextatpos,MLEsizeofsel)
1175#pragma alloc_text(FMMLE3,MLEdoblock,MLEquotepara,MLEinternet)
1176#pragma alloc_text(FMMLE4,MLEAutoLoad,MLEHexLoad,MLEinsertfile,LoadThread,MLEbackgroundload)
1177#pragma alloc_text(FMMLE5,MLEloadfile,MLEexportfile)
1178#pragma alloc_text(FMMLE3,MLEfindfirst,MLEfindnext,SandRDlgProc)
Note: See TracBrowser for help on using the repository browser.