source: trunk/dll/mle.c@ 1627

Last change on this file since 1627 was 1627, checked in by Gregg Young, 14 years ago

Add a low mem version of xDosAlloc* wrappers; move error checking into all the xDosAlloc* wrappers. Ticket 471

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