source: trunk/dll/mle.c@ 1880

Last change on this file since 1880 was 1880, checked in by Gregg Young, 10 years ago

Remove dead code and comments from remaining c files. #if 0 and #if NEVER were not addressed

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