source: trunk/dll/mle.c@ 888

Last change on this file since 888 was 888, checked in by Gregg Young, 18 years ago

runemf2 now quotes executable strings if needed (Ticket 180); it also reports where it was called from on errors

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