source: trunk/dll/mle.c@ 1009

Last change on this file since 1009 was 1009, checked in by Steven Levine, 17 years ago

Add xfree xstrdup Fortify support
Add MT capable Fortify scope logic

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