source: trunk/dll/mle.c@ 1402

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

Remove variable aurgs from docopy & unlinkf (not used); Move more strings to PCSZs and string table; Move PCSZs to compile time initialization; Fix hang on startup caused by a drive scan and a dircnr scan trying to update a drive in the tree at the same time (related to the "treeswitch options); Code cleanup mainly removal of old printfs, SayMsgs, DbgMsg and unneeded %s.

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