source: trunk/dll/mle.c@ 1628

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

Hard coded the flags for the xDosAlloc* wrappers and added a description for each of them.

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