source: trunk/dll/mle.c@ 1402

Last change on this file since 1402 was 1402, checked in by Gregg Young, 16 years ago

Remove variable aurgs from docopy & unlinkf (not used); Move more strings to PCSZs and string table; Move PCSZs to compile time initialization; Fix hang on startup caused by a drive scan and a dircnr scan trying to update a drive in the tree at the same time (related to the "treeswitch options); Code cleanup mainly removal of old printfs, SayMsgs, DbgMsg and unneeded %s.

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