source: trunk/dll/mle.c@ 1036

Last change on this file since 1036 was 1029, checked in by Gregg Young, 17 years ago

Fixed early memory free; Added free_... functions to make fortify checking easier; Added fortify scopes; Delete now moves to trash can on systems with the xworkplace trash can installed.

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