source: trunk/dll/mle.c@ 1185

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

Ticket 187: Draft 2: Move remaining function declarations

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