source: trunk/dll/mle.c@ 1225

Last change on this file since 1225 was 1225, checked in by John Small, 17 years ago

Ticket 187: Moved typedef's and some #define's from fm3dll.h

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