1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: uudecode.c 1544 2010-09-30 13:00:59Z gyoung $
|
---|
5 |
|
---|
6 | uudecode
|
---|
7 |
|
---|
8 | Copyright (c) 1993-98 M. Kimes
|
---|
9 | Copyright (c) 2005, 2008 Steven H. Levine
|
---|
10 |
|
---|
11 | 06 Jun 05 SHL Indent -i2
|
---|
12 | 06 Jun 05 SHL Drop unused code
|
---|
13 | 17 Jul 06 SHL Use Runtime_Error
|
---|
14 | 29 Jul 06 SHL Use xfgets
|
---|
15 | 01 Sep 06 SHL Back to fgets for now - avoid excess error messages
|
---|
16 | 22 Mar 07 GKY Use QWL_USER
|
---|
17 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
---|
18 | 07 Feb 09 GKY Allow user to turn off alert and/or error beeps in settings notebook.
|
---|
19 |
|
---|
20 | ***********************************************************************/
|
---|
21 |
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <share.h>
|
---|
25 |
|
---|
26 | #define INCL_DOS
|
---|
27 | #define INCL_WIN
|
---|
28 | #define INCL_LONGLONG // dircnrs.h
|
---|
29 |
|
---|
30 | #include "fm3dll.h"
|
---|
31 | #include "fm3dll2.h" // #define's for UM_*, control id's, etc.
|
---|
32 | #include "worker.h" // typedef WORKER
|
---|
33 | #include "notebook.h" // Data declaration(s)
|
---|
34 | #include "fm3dlg.h"
|
---|
35 | #include "fm3str.h"
|
---|
36 | #include "makelist.h" // AddToList
|
---|
37 | #include "errutil.h" // Dos_Error...
|
---|
38 | #include "strutil.h" // GetPString
|
---|
39 | #include "defview.h"
|
---|
40 | #include "uudecode.h"
|
---|
41 | #include "getnames.h" // export_filename
|
---|
42 | #include "valid.h" // IsFile
|
---|
43 | #include "misc.h" // PaintRecessedWindow
|
---|
44 | #include "wrappers.h" // xfgets
|
---|
45 | #include "init.h" // Strings
|
---|
46 |
|
---|
47 | static PSZ pszSrcFile = __FILE__;
|
---|
48 |
|
---|
49 | /* prototypes */
|
---|
50 | static BOOL decode(FILE * in, FILE * out);
|
---|
51 | static void outdec(char *p, FILE * f, int n);
|
---|
52 |
|
---|
53 | /* single character decode */
|
---|
54 | #define DEC(c) (((c) - ' ') & 077)
|
---|
55 |
|
---|
56 | int UUD(char *filename, CHAR * dest)
|
---|
57 | {
|
---|
58 | FILE *in, *out;
|
---|
59 | int mode, ret = 0;
|
---|
60 | char buf[80];
|
---|
61 | char fakedest[CCHMAXPATH];
|
---|
62 | CHAR *moder = "r";
|
---|
63 |
|
---|
64 | if (!dest)
|
---|
65 | dest = fakedest;
|
---|
66 | in = xfsopen(filename, moder, SH_DENYWR, pszSrcFile, __LINE__, TRUE);
|
---|
67 | if (!in) {
|
---|
68 | saymsg(MB_CANCEL,
|
---|
69 | HWND_DESKTOP,
|
---|
70 | GetPString(IDS_ERRORTEXT),
|
---|
71 | GetPString(IDS_COMPCANTOPENTEXT), filename);
|
---|
72 | return ret;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* search for header line */
|
---|
76 | for (;;) {
|
---|
77 | if (!fgets(buf, sizeof(buf), in)) {
|
---|
78 | fclose(in);
|
---|
79 | saymsg(MB_CANCEL,
|
---|
80 | HWND_DESKTOP,
|
---|
81 | GetPString(IDS_ERRORTEXT),
|
---|
82 | GetPString(IDS_UUDNOBEGINTEXT), filename);
|
---|
83 | return ret;
|
---|
84 | }
|
---|
85 | if (!strncmp(buf, "begin ", 6))
|
---|
86 | break;
|
---|
87 | } // for
|
---|
88 | *dest = 0;
|
---|
89 | sscanf(buf, "begin %o %259s", &mode, dest);
|
---|
90 | dest[CCHMAXPATH - 1] = 0;
|
---|
91 | {
|
---|
92 | /* place dest in same directory as filename by default... */
|
---|
93 | char build[CCHMAXPATH], *p;
|
---|
94 |
|
---|
95 | strcpy(build, filename);
|
---|
96 | p = strrchr(build, '\\');
|
---|
97 | if (p) {
|
---|
98 | p++;
|
---|
99 | *p = 0;
|
---|
100 | }
|
---|
101 | else
|
---|
102 | strcat(build, PCSZ_BACKSLASH);
|
---|
103 | strncat(build, dest, CCHMAXPATH - strlen(dest));
|
---|
104 | strcpy(dest, build);
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (!export_filename(HWND_DESKTOP, dest, FALSE)) {
|
---|
108 | fclose(in);
|
---|
109 | return ret;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* create output file */
|
---|
113 | moder = "ab+";
|
---|
114 | out = xfsopen(dest, moder, SH_DENYWR, pszSrcFile, __LINE__, TRUE);
|
---|
115 | if (!out) {
|
---|
116 | fclose(in);
|
---|
117 | saymsg(MB_CANCEL,
|
---|
118 | HWND_DESKTOP,
|
---|
119 | GetPString(IDS_ERRORTEXT),
|
---|
120 | GetPString(IDS_UUDCANTOPENFORTEXT), dest, filename);
|
---|
121 | return ret;
|
---|
122 | }
|
---|
123 |
|
---|
124 | ret = 1;
|
---|
125 | decode(in, out);
|
---|
126 |
|
---|
127 | xfgets(buf, sizeof(buf), in, pszSrcFile, __LINE__);
|
---|
128 |
|
---|
129 | fclose(in);
|
---|
130 | fclose(out);
|
---|
131 | return ret;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * copy from in to out, decoding as you go along.
|
---|
136 | */
|
---|
137 | static BOOL decode(FILE * in, FILE * out)
|
---|
138 | {
|
---|
139 | char buf[80];
|
---|
140 | char *bp;
|
---|
141 | int n;
|
---|
142 |
|
---|
143 | for (;;) {
|
---|
144 | /* for each input line */
|
---|
145 | if (!xfgets(buf, sizeof(buf), in, pszSrcFile, __LINE__))
|
---|
146 | return FALSE;
|
---|
147 | n = DEC(buf[0]);
|
---|
148 | if (n <= 0)
|
---|
149 | break;
|
---|
150 | bp = &buf[1];
|
---|
151 | while (n > 0) {
|
---|
152 | outdec(bp, out, n);
|
---|
153 | bp += 4;
|
---|
154 | n -= 3;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return TRUE;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * output a group of 3 bytes (4 input characters).
|
---|
162 | * the input chars are pointed to by p, they are to
|
---|
163 | * be output to file f. n is used to tell us not to
|
---|
164 | * output all of them at the end of the file.
|
---|
165 | */
|
---|
166 | static void outdec(char *p, FILE * f, int n)
|
---|
167 | {
|
---|
168 | INT c1, c2, c3;
|
---|
169 |
|
---|
170 | c1 = DEC(*p) << 2 | (UINT) DEC(p[1]) >> 4;
|
---|
171 | c2 = DEC(p[1]) << 4 | (UINT) DEC(p[2]) >> 2;
|
---|
172 | c3 = DEC(p[2]) << 6 | DEC(p[3]);
|
---|
173 | if (n >= 1)
|
---|
174 | putc(c1, f);
|
---|
175 | if (n >= 2)
|
---|
176 | putc(c2, f);
|
---|
177 | if (n >= 3)
|
---|
178 | putc(c3, f);
|
---|
179 | }
|
---|
180 |
|
---|
181 | MRESULT EXPENTRY MergeDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
|
---|
182 | {
|
---|
183 | WORKER *wk;
|
---|
184 |
|
---|
185 | switch (msg) {
|
---|
186 | case WM_INITDLG:
|
---|
187 | if (mp2) {
|
---|
188 | WinSetWindowPtr(hwnd, QWL_USER, mp2);
|
---|
189 | wk = (WORKER *) mp2;
|
---|
190 | if (wk->li && wk->li->list && wk->li->list[0]) {
|
---|
191 | WinSendDlgItemMsg(hwnd, MRG_TARGETNAME, EM_SETTEXTLIMIT,
|
---|
192 | MPFROM2SHORT(CCHMAXPATH, 0), MPVOID);
|
---|
193 | PostMsg(hwnd, UM_UNDO, MPVOID, MPVOID);
|
---|
194 | }
|
---|
195 | else
|
---|
196 | WinDismissDlg(hwnd, 0);
|
---|
197 | }
|
---|
198 | else
|
---|
199 | WinDismissDlg(hwnd, 0);
|
---|
200 | break;
|
---|
201 |
|
---|
202 | case UM_UNDO:
|
---|
203 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
|
---|
204 | wk = WinQueryWindowPtr(hwnd, QWL_USER);
|
---|
205 | if (wk) {
|
---|
206 | INT x, numfiles = 0;
|
---|
207 | SHORT start;
|
---|
208 | CHAR *p;
|
---|
209 |
|
---|
210 | WinSetDlgItemText(hwnd, MRG_TARGETNAME, wk->li->targetpath);
|
---|
211 | start = 0;
|
---|
212 | p = strrchr(wk->li->targetpath, '\\');
|
---|
213 | if (p)
|
---|
214 | start = (p + 1) - wk->li->targetpath;
|
---|
215 | WinSendDlgItemMsg(hwnd, MRG_TARGETNAME, EM_SETSEL,
|
---|
216 | MPFROM2SHORT(start, CCHMAXPATH), MPVOID);
|
---|
217 | for (x = 0; wk->li->list[x]; x++) {
|
---|
218 | if (IsFile(wk->li->list[x]) == 1) {
|
---|
219 | numfiles++;
|
---|
220 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_INSERTITEM,
|
---|
221 | MPFROM2SHORT(LIT_END, 0),
|
---|
222 | MPFROMP(wk->li->list[x]));
|
---|
223 | }
|
---|
224 | }
|
---|
225 | WinCheckButton(hwnd, MRG_BINARY, (wk->li->type == IDM_MERGEBINARY));
|
---|
226 | if (!numfiles) {
|
---|
227 | saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
|
---|
228 | hwnd,
|
---|
229 | GetPString(IDS_SILLYERRORTEXT),
|
---|
230 | GetPString(IDS_MERGEWASTETEXT));
|
---|
231 | WinDismissDlg(hwnd, 0);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | return 0;
|
---|
235 |
|
---|
236 | case WM_CONTROL:
|
---|
237 | switch (SHORT1FROMMP(mp1)) {
|
---|
238 | case MRG_LISTBOX:
|
---|
239 | switch (SHORT2FROMMP(mp1)) {
|
---|
240 | case LN_ENTER:
|
---|
241 | {
|
---|
242 | SHORT x;
|
---|
243 | CHAR szBuffer[CCHMAXPATH];
|
---|
244 |
|
---|
245 | x = (SHORT) WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_QUERYSELECTION,
|
---|
246 | MPFROMSHORT(LIT_FIRST), MPVOID);
|
---|
247 | if (x >= 0) {
|
---|
248 | *szBuffer = 0;
|
---|
249 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_QUERYITEMTEXT,
|
---|
250 | MPFROM2SHORT(x, CCHMAXPATH), MPFROMP(szBuffer));
|
---|
251 | if (*szBuffer)
|
---|
252 | QuickEdit(hwnd, szBuffer);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | break;
|
---|
256 | }
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | break;
|
---|
260 |
|
---|
261 | case WM_ADJUSTWINDOWPOS:
|
---|
262 | PostMsg(hwnd, UM_SETDIR, MPVOID, MPVOID);
|
---|
263 | break;
|
---|
264 |
|
---|
265 | case UM_SETDIR:
|
---|
266 | PaintRecessedWindow(WinWindowFromID(hwnd, MRG_HELP), (HPS) 0, FALSE,
|
---|
267 | TRUE);
|
---|
268 | return 0;
|
---|
269 |
|
---|
270 | case WM_COMMAND:
|
---|
271 | switch (SHORT1FROMMP(mp1)) {
|
---|
272 | case IDM_UNDO:
|
---|
273 | PostMsg(hwnd, UM_UNDO, MPVOID, MPVOID);
|
---|
274 | break;
|
---|
275 |
|
---|
276 | case MRG_CHANGETARGET:
|
---|
277 | wk = WinQueryWindowPtr(hwnd, QWL_USER);
|
---|
278 | if (wk) {
|
---|
279 | CHAR filename[CCHMAXPATH];
|
---|
280 |
|
---|
281 | strcpy(filename, wk->li->targetpath);
|
---|
282 | if (export_filename(HWND_DESKTOP, filename, FALSE) && *filename) {
|
---|
283 | strcpy(wk->li->targetpath, filename);
|
---|
284 | WinSetDlgItemText(hwnd, MRG_TARGETNAME, wk->li->targetpath);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | break;
|
---|
288 |
|
---|
289 | case MRG_REMOVE:
|
---|
290 | {
|
---|
291 | SHORT x;
|
---|
292 |
|
---|
293 | x = (SHORT) WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_QUERYSELECTION,
|
---|
294 | MPFROMSHORT(LIT_FIRST), MPVOID);
|
---|
295 | if (x >= 0)
|
---|
296 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_DELETEITEM,
|
---|
297 | MPFROMSHORT(x), MPVOID);
|
---|
298 | }
|
---|
299 | break;
|
---|
300 |
|
---|
301 | case MRG_BOTTOM:
|
---|
302 | case MRG_TOP:
|
---|
303 | {
|
---|
304 | SHORT x;
|
---|
305 | CHAR szBuffer[CCHMAXPATH];
|
---|
306 |
|
---|
307 | x = (SHORT) WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_QUERYSELECTION,
|
---|
308 | MPFROMSHORT(LIT_FIRST), MPVOID);
|
---|
309 | if (x >= 0) {
|
---|
310 | *szBuffer = 0;
|
---|
311 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_QUERYITEMTEXT,
|
---|
312 | MPFROM2SHORT(x, CCHMAXPATH), MPFROMP(szBuffer));
|
---|
313 | if (*szBuffer) {
|
---|
314 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_DELETEITEM,
|
---|
315 | MPFROMSHORT(x), MPVOID);
|
---|
316 | if (SHORT1FROMMP(mp1) == MRG_TOP)
|
---|
317 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_INSERTITEM,
|
---|
318 | MPFROM2SHORT(0, 0), MPFROMP(szBuffer));
|
---|
319 | else
|
---|
320 | WinSendDlgItemMsg(hwnd, MRG_LISTBOX, LM_INSERTITEM,
|
---|
321 | MPFROM2SHORT(LIT_END, 0), MPFROMP(szBuffer));
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | break;
|
---|
326 |
|
---|
327 | case DID_CANCEL:
|
---|
328 | WinDismissDlg(hwnd, 0);
|
---|
329 | break;
|
---|
330 |
|
---|
331 | case IDM_HELP:
|
---|
332 | if (hwndHelp)
|
---|
333 | WinSendMsg(hwndHelp, HM_DISPLAY_HELP,
|
---|
334 | MPFROM2SHORT(HELP_MERGE, 0), MPFROMSHORT(HM_RESOURCEID));
|
---|
335 | break;
|
---|
336 |
|
---|
337 | case DID_OK:
|
---|
338 | wk = WinQueryWindowPtr(hwnd, QWL_USER);
|
---|
339 | if (wk) {
|
---|
340 | BOOL append, binary;
|
---|
341 | CHAR **list = NULL, **test, szBuffer[CCHMAXPATH];
|
---|
342 | UINT numfiles = 0, numalloc = 0;
|
---|
343 | INT error;
|
---|
344 | SHORT x, y;
|
---|
345 |
|
---|
346 | *szBuffer = 0;
|
---|
347 | WinQueryDlgItemText(hwnd, MRG_TARGETNAME, CCHMAXPATH, szBuffer);
|
---|
348 | if (!*szBuffer) {
|
---|
349 | if (!fAlertBeepOff)
|
---|
350 | DosBeep(50, 100);
|
---|
351 | WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, MRG_TARGETNAME));
|
---|
352 | break;
|
---|
353 | }
|
---|
354 | if (DosQueryPathInfo(szBuffer,
|
---|
355 | FIL_QUERYFULLNAME,
|
---|
356 | wk->li->targetpath, CCHMAXPATH)) {
|
---|
357 | if (!fAlertBeepOff)
|
---|
358 | DosBeep(50, 100);
|
---|
359 | WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, MRG_TARGETNAME));
|
---|
360 | break;
|
---|
361 | }
|
---|
362 | WinSetDlgItemText(hwnd, MRG_TARGETNAME, szBuffer);
|
---|
363 | append = WinQueryButtonCheckstate(hwnd, MRG_APPEND);
|
---|
364 | binary = WinQueryButtonCheckstate(hwnd, MRG_BINARY);
|
---|
365 | wk->li->type = (append && binary) ? IDM_MERGEBINARYAPPEND :
|
---|
366 | (append) ? IDM_MERGETEXTAPPEND :
|
---|
367 | (binary) ? IDM_MERGEBINARY : IDM_MERGETEXT;
|
---|
368 | x = (SHORT) WinSendDlgItemMsg(hwnd,
|
---|
369 | MRG_LISTBOX,
|
---|
370 | LM_QUERYITEMCOUNT, MPVOID, MPVOID);
|
---|
371 | for (y = 0; y < x; y++) {
|
---|
372 | *szBuffer = 0;
|
---|
373 | WinSendDlgItemMsg(hwnd,
|
---|
374 | MRG_LISTBOX,
|
---|
375 | LM_QUERYITEMTEXT,
|
---|
376 | MPFROM2SHORT(y, CCHMAXPATH), MPFROMP(szBuffer));
|
---|
377 | if (*szBuffer) {
|
---|
378 | error = AddToList(szBuffer, &list, &numfiles, &numalloc);
|
---|
379 | if (error) {
|
---|
380 | Runtime_Error(pszSrcFile, __LINE__, "AddToList");
|
---|
381 | break;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 | if (numfiles && list && numfiles + 1 < numalloc) {
|
---|
386 | test =
|
---|
387 | xrealloc(list, sizeof(CHAR *) * (numfiles + 1), pszSrcFile,
|
---|
388 | __LINE__);
|
---|
389 | if (test)
|
---|
390 | list = test;
|
---|
391 | }
|
---|
392 | if (!list || !list[0]) {
|
---|
393 | Runtime_Error(pszSrcFile, __LINE__, NULL);
|
---|
394 | break;
|
---|
395 | }
|
---|
396 | else {
|
---|
397 | FreeList(wk->li->list);
|
---|
398 | wk->li->list = list;
|
---|
399 | }
|
---|
400 | }
|
---|
401 | WinDismissDlg(hwnd, 1);
|
---|
402 | break;
|
---|
403 | } // switch WM_COMMAND mp1
|
---|
404 | return 0;
|
---|
405 | } // switch msg
|
---|
406 | return WinDefDlgProc(hwnd, msg, mp1, mp2);
|
---|
407 | }
|
---|
408 |
|
---|
409 | #pragma alloc_text(UUD,UUD,decode,outdec)
|
---|
410 | #pragma alloc_text(MERGE,MergeDlgProc)
|
---|