source: trunk/dll/mle.c@ 1544

Last change on this file since 1544 was 1544, checked in by Gregg Young, 15 years ago

Changes to fopen and _fsopen to allow FM2 to be loaded in high memory

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