1 /*
2  * edit.c -- TODAY for Windows - external File Editor execute
3  *
4  * Copyright (C) 1999,2003 by Yoshifumi Mori
5  *
6  * tab:4
7  */
8 
9 #include "cdefs.h"
10 #include "extern.h"
11 #if !defined(INCLUDE_CALENDAR)
12 #include "todayfw.rh"
13 #else
14 #include "calfw.rh"
15 #endif
16 #include "commres.rh"
17 
18 #define MAGIC	0x211A		/* temporary file magic number */
19 
20 #if !defined(INCLUDE_CALENDAR)
21 #define szClassToday	szAppName
22 #define szClassCal		szCalClass
23 #else /* INCLUDE_CALENDAR */
24 #define szClassToday	szTodayClass
25 #define szClassCal		szAppName
26 #endif /* INCLUDE_CALENDAR */
27 
28 const struct KIND_FILETBL	kind_filetbl[MAX_KINDITEM] = {
29 #if !defined(INCLUDE_CALENDAR)
30 	{ "EVENT",    (const char **)event_filetbl,    MAXEVENTFILE    },
31 	{ "BIRTHDAY", (const char **)birthday_filetbl, MAXBIRTHFILE    },
32 	{ "HISTORY",  (const char **)history_filetbl,  MAXHISTORYFILE  },
33 	{ "SCHEDULE", (const char **)schedule_filetbl, MAXSCHEDULEFILE },
34 	{ "WEEK",     (const char **)week_filetbl,     MAXWEEKFILE     },
35 	{ "FLOWER",   (const char **)&flower_file,     1               },
36 	{ "GENGO",    (const char **)&gengo_file,      1               },
37 	{ "HOLIDAY",  (const char **)&holiday_file,    1               },
38 	{ "SUFFIX",   (const char **)&suffix_file,     1               },
39 	{ "TODAYCNF", (const char **)&todaycnf_file,   1               },
40 #else /* INCLUDE_CALENDAR */
41 	{ NULL,       (const char **)NULL,             0               },
42 	{ NULL,       (const char **)NULL,             0               },
43 	{ NULL,       (const char **)NULL,             0               },
44 	{ "SCHEDULE", (const char **)schedule_filetbl, MAXSCHEDULEFILE },
45 	{ NULL,       (const char **)NULL,             0               },
46 	{ NULL,       (const char **)NULL,             0               },
47 	{ "GENGO",    (const char **)&gengo_file,      1               },
48 	{ "HOLIDAY",  (const char **)&holiday_file,    1               },
49 	{ NULL,       (const char **)NULL,             0               },
50 	{ "TODAYCNF", (const char **)&todaycnf_file,   1               },
51 #endif /* INCLUDE_CALENDAR */
52 };
53 
54 char	*file_editor_cmd;
55 
56 static HINSTANCE	ghInstance;
57 static int	fileKind = -1;
58 static char	selectFile[MAX_PATH+1];
59 static BOOL	bCreateTempFile = FALSE;
60 static char	szTempFile[MAX_PATH+1];
61 static char	**pathTbl;
62 static int	pathTblcnt;
63 static int	pathTblcnt_s;	/* table_path �����Υơ��֥�� */
64 
65 static const DWORD	editHelpID[] = {
66 	IDC_FILESELECT_CHK_01,	5101,
67 	IDC_FILESELECT_CHK_02,	5101,
68 	IDC_FILESELECT_CHK_03,	5101,
69 	IDC_FILESELECT_CHK_04,	5101,
70 	IDC_FILESELECT_CHK_05,	5101,
71 	IDC_FILESELECT_CHK_06,	5101,
72 	IDC_FILESELECT_CHK_07,	5101,
73 	IDC_FILESELECT_CHK_08,	5101,
74 	IDC_FILESELECT_CHK_09,	5101,
75 	IDC_FILESELECT_CHK_10,	5101,
76 	IDC_FILESELECT_YY,		5102,
77 	IDC_FILESELECT_MM,		5102,
78 	IDC_FILESELECT_FILE,	5103,
79 	IDC_FILESELECT_PATH,	5104,
80 	IDOK,					5105,
81 	IDCANCEL,				5106,
82 	0,						0,
83 };
84 
85 /*
86  * ����ƥ����ȥإ�פ�ɽ��
87  */
commonHelpControl(const DWORD * HelpIdTbl,LPHELPINFO lpInfo)88 void commonHelpControl(const DWORD *HelpIdTbl, LPHELPINFO lpInfo)
89 {
90 	int	i;
91 
92 	for (i = 0; HelpIdTbl[i] != 0; i += 2) {
93 		if (HelpIdTbl[i] == (DWORD)lpInfo->iCtrlId) {
94 			WinHelp(lpInfo->hItemHandle, szHelpFile, HELP_WM_HELP, (DWORD)(LPVOID)HelpIdTbl);
95 			break;
96 		}
97 	}
98 }
99 
100 /*
101  * ���ޥ��̾������
102  *
103  * ���ǥ������ޥ�����饳�ޥ��̾������
104  */
getCommandName(const char * editor_cmd)105 char *getCommandName(const char *editor_cmd)
106 {
107 	char	*cmdname;
108 	char	*p;
109 	const char	*s;
110 
111 	cmdname = strdup(editor_cmd);
112 	if (cmdname != NULL) {
113 		s = editor_cmd;
114 		p = cmdname;
115 		if (*s == '"') {
116 			s++;
117 			while (*s != '"' && *s != '\0') {
118 				*p++ = *s++;
119 			}
120 		} else {
121 			while (*s != ' ' && *s != '\0') {
122 				*p++ = *s++;
123 			}
124 		}
125 		*p = '\0';
126 		if (cmdname[0] == '\0') {
127 			free(cmdname);
128 			cmdname = NULL;
129 		}
130 	}
131 
132 	return (cmdname);
133 }
134 
135 /*
136  * �ե�ѥ��Υե�����̾����Ӥ���
137  * ��ʸ���Ⱦ�ʸ���϶��̤��ʤ�
138  *
139  * return value:
140  *   TRUE  ����
141  *   FALSE �԰���
142  */
143 static
compareFullPath(const char * path1,const char * path2)144 int compareFullPath(const char *path1, const char *path2)
145 {
146 	unsigned int	ch1;
147 	unsigned int	ch2;
148 
149 	for (;;) {
150 		ch1 = *path1++;
151 		if (iskanji(ch1) == 0) {
152 			ch1 = tolower(ch1);
153 		} else if (iskanji2(*path1) != 0) {
154 			ch1 = (ch1 << 8) + *path1;
155 			path1++;
156 		}
157 		ch2 = *path2++;
158 		if (iskanji(ch2) == 0) {
159 			ch2 = tolower(ch2);
160 		} else if (iskanji2(*path2) != 0) {
161 			ch2 = (ch2 << 8) + *path2;
162 			path2++;
163 		}
164 		if (ch1 != ch2) {
165 			return (FALSE);
166 		}
167 		if (ch1 == '\0') {
168 			break;
169 		}
170 	}
171 
172 	return (TRUE);
173 }
174 
175 /*
176  * �ե����륢�������ѤΥѥ��ơ��֥����
177  *
178  * ���Ѹ塢freeSearchPathTbl() ��Ƥ�
179  */
makeSearchPathTbl(const char * searchpath,char ** pathtbl,int * pathtblcnt)180 char **makeSearchPathTbl(const char *searchpath, char **pathtbl, int *pathtblcnt)
181 {
182 	char	*pszSearchPath;
183 	char	*pszPath;
184 	char	*lasts;
185 	char	szAccessPath[MAX_PATH+2];
186 	int		i;
187 	int		cnt;
188 
189 	if (searchpath == NULL) {
190 		goto exit_proc;
191 	}
192 
193 	pszSearchPath = strdup(searchpath);
194 	if (pszSearchPath == NULL) {
195 		goto exit_proc;
196 	}
197 
198 	memset(szAccessPath, '\0', sizeof(szAccessPath));
199 	cnt = *pathtblcnt;
200 
201 	lasts = NULL;
202 	pszPath = strtok_r(pszSearchPath, _T_PATHSEPARATOR, &lasts);
203 	while (pszPath != NULL) {
204 		strncpy(szAccessPath, pszPath, sizeof(szAccessPath)-2);
205 		make_accesspath(replace_accesspath(szAccessPath, sizeof(szAccessPath)-1));
206 		if (pathtbl != NULL) {
207 			for (i = 0; i < cnt; i++) {
208 				if (compareFullPath(szAccessPath, pathtbl[i]) != FALSE) {
209 					goto next;
210 				}
211 			}
212 		}
213 		cnt++;
214 		pathtbl = (char **)realloc(pathtbl, sizeof(char *) * cnt);
215 		if (pathtbl == NULL) {
216 			break;	/* out of memory */
217 		}
218 		pathtbl[cnt - 1] = strdup(szAccessPath);
219 next:
220 		pszPath = strtok_r(NULL, _T_PATHSEPARATOR, &lasts);
221 	}
222 
223 	if (pszSearchPath != NULL) {
224 		free(pszSearchPath);
225 	}
226 
227 	*pathtblcnt = cnt;
228 
229 exit_proc:
230 	return (pathtbl);
231 }
232 
233 /*
234  * �ե����륢�������ѤΥѥ��ơ��֥����
235  */
freeSearchPathTbl(char ** pathtbl,const int pathtblcnt)236 void freeSearchPathTbl(char **pathtbl, const int pathtblcnt)
237 {
238 	int	i;
239 
240 	if (pathtbl != NULL) {
241 		for (i = 0; i < pathtblcnt; i++) {
242 			if (pathtbl[i] != NULL) {
243 				free(pathtbl[i]);
244 			}
245 		}
246 		free(pathtbl);
247 	}
248 }
249 
250 /*
251  * �ե�����̾������
252  *
253  * �������ե�����̾��õ���ե�ѥ������ꤹ��
254  */
255 static
setupSelectFile(HWND hDlg)256 int setupSelectFile(HWND hDlg)
257 {
258 	int		selno;
259 	int		yy;
260 	int		mm;
261 	int		save_errflag;
262 	BOOL	bFlag;
263 	char	szText[MAX_PATH * 2 + 256];
264 	const char	*file;
265 	const char	*p;
266 
267 	selno = SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_GETCURSEL, 0, 0L);
268 	if (selno != LB_ERR) {
269 		selno = SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_GETITEMDATA, selno, 0L);
270 		if (selno >= 0 && selno < kind_filetbl[fileKind].maxfiles) {
271 			yy = GetDlgItemInt(hDlg, IDC_FILESELECT_YY, &bFlag, TRUE);
272 			mm = GetDlgItemInt(hDlg, IDC_FILESELECT_MM, &bFlag, TRUE);
273 			file = make_filename2(kind_filetbl[fileKind].filetbl[selno], yy, mm, 0);
274 			save_errflag = search_errignore;
275 			search_errignore = TRUE;
276 			p = search_filepath(file);
277 			search_errignore = save_errflag;
278 			if (p == NULL) {
279 				selno = SendDlgItemMessage(hDlg, IDC_FILESELECT_PATH, CB_GETCURSEL, 0, 0L);
280 				if (selno >= 0) {
281 					SendDlgItemMessage(hDlg, IDC_FILESELECT_PATH, CB_GETLBTEXT, selno, (LPARAM)selectFile);
282 					sprintf(szText, "%s �����Ĥ���ޤ���\n%s �˿����������ޤ�����", file, selectFile);
283 					if (MessageBox(hDlg, szText, "�ե����������", MB_ICONQUESTION | MB_YESNO) == IDYES) {
284 						strncat(selectFile, file, (sizeof(selectFile) - 1) - strlen(selectFile));
285 						return (TRUE);
286 					}
287 				}
288 			} else {
289 				strncpy(selectFile, p, sizeof(selectFile)-1);
290 				return (TRUE);
291 			}
292 		}
293 	}
294 
295 	return (FALSE);
296 }
297 
298 /*
299  * ���դ����Ϥ�ɬ�פ������å�����
300  */
301 static
checkDateControl(HWND hDlg,int kind,int selno)302 void checkDateControl(HWND hDlg, int kind, int selno)
303 {
304 	int	flags = 0;
305 	const char	**filetbl;
306 
307 	if (selno >= 0 && selno < kind_filetbl[kind].maxfiles) {
308 		filetbl = kind_filetbl[kind].filetbl;
309 		if (filetbl != NULL && filetbl[selno] != NULL) {
310 			make_filename2(filetbl[selno], 0, 0, 0);
311 			flags = mkfile_status;
312 		}
313 	}
314 	EnableWindow(GetDlgItem(hDlg, IDC_FILESELECT_YY), (flags & (SET_MKF_CC | SET_MKF_YY)) != 0 ? TRUE : FALSE);
315 	EnableWindow(GetDlgItem(hDlg, IDC_FILESELECT_MM), (flags & SET_MKF_MM) != 0 ? TRUE : FALSE);
316 }
317 
318 /*
319  * ���ꤷ���ե�������̤Υե�����̾��ꥹ�ȥ��åפ���
320  */
321 static
setupFileList(HWND hDlg,int kind)322 void setupFileList(HWND hDlg, int kind)
323 {
324 	int	i;
325 	int	maxfiles;
326 	int	selno;
327 	const char	**filetbl;
328 
329 	SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_RESETCONTENT, 0, 0L);
330 
331 	filetbl = kind_filetbl[kind].filetbl;
332 	if (filetbl != NULL) {
333 		maxfiles = kind_filetbl[kind].maxfiles;
334 		for (i = 0; i < maxfiles; i++) {
335 			if (filetbl[i] != NULL) {
336 				selno = SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_ADDSTRING, 0, (LPARAM)filetbl[i]);
337 				if (selno >= 0) {
338 					SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_SETITEMDATA, selno, i);
339 				}
340 			}
341 		}
342 	}
343 
344 	i = SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_GETCOUNT, 0, 0L);
345 	if (i > 0) {
346 		SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_SETCURSEL, 0, 0L);
347 		checkDateControl(hDlg, kind, 0);
348 	}
349 	EnableWindow(GetDlgItem(hDlg, IDOK), (i > 0) ? TRUE : FALSE);
350 }
351 
352 /*
353  * �ե�����Υѥ������ꥹ�ȥܥå��������ꤹ��
354  */
355 static
setupFilePath(HWND hDlg)356 void setupFilePath(HWND hDlg)
357 {
358 	int	i;
359 
360 	SendDlgItemMessage(hDlg, IDC_FILESELECT_PATH, CB_RESETCONTENT, 0, 0L);
361 	for (i = 0; i < pathTblcnt; i++) {
362 		if (pathTbl[i] != NULL) {
363 			SendDlgItemMessage(hDlg, IDC_FILESELECT_PATH, CB_ADDSTRING, 0, (LPARAM)pathTbl[i]);
364 		}
365 	}
366 	if (SendDlgItemMessage(hDlg, IDC_FILESELECT_PATH, CB_GETCOUNT, 0, 0l) > 0) {
367 		SendDlgItemMessage(hDlg, IDC_FILESELECT_PATH, CB_SETCURSEL, 0, 0L);
368 	}
369 }
370 
371 /*
372  * �ե���������������� main
373  */
selectFileDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam)374 BOOL CALLBACK selectFileDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
375 {
376 	int	i;
377 	int	selno;
378 	struct DATE_T	*date;
379 
380 	switch (iMessage) {
381 	case WM_INITDIALOG:
382 		SetClassLong(hDlg, GCL_HICON, (LONG)LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_ICON1)));
383 		for (i = 0; i < MAX_KINDITEM; i++) {
384 			if (kind_filetbl[i].maxfiles == 0) {
385 				EnableWindow(GetDlgItem(hDlg, IDC_FILESELECT_CHK_01 + i), FALSE);
386 			}
387 		}
388 		setupFilePath(hDlg);
389 		date = (struct DATE_T *)lParam;
390 		SetDlgItemInt(hDlg, IDC_FILESELECT_YY, date->year, TRUE);
391 		SendDlgItemMessage(hDlg, IDC_FILESELECT_YY, EM_LIMITTEXT, 4, 0L);
392 		SetDlgItemInt(hDlg, IDC_FILESELECT_MM, date->month, TRUE);
393 		SendDlgItemMessage(hDlg, IDC_FILESELECT_MM, EM_LIMITTEXT, 2, 0L);
394 		if (fileKind >= 0 && fileKind < MAX_KINDITEM) {
395 			CheckRadioButton(hDlg, IDC_FILESELECT_CHK_01, IDC_FILESELECT_CHK_01 + MAX_KINDITEM - 1, IDC_FILESELECT_CHK_01 + fileKind);
396 			setupFileList(hDlg, fileKind);
397 			SetFocus(GetDlgItem(hDlg, IDC_FILESELECT_FILE));
398 		} else {
399 			EnableWindow(GetDlgItem(hDlg, IDC_FILESELECT_YY), FALSE);
400 			EnableWindow(GetDlgItem(hDlg, IDC_FILESELECT_MM), FALSE);
401 			EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
402 			SetFocus(GetDlgItem(hDlg, IDCANCEL));
403 		}
404 		return (FALSE);
405 	case WM_COMMAND:
406 		if (HIWORD(wParam) == BN_CLICKED &&
407 		    LOWORD(wParam) >= IDC_FILESELECT_CHK_01 && LOWORD(wParam) < IDC_FILESELECT_CHK_01 + MAX_KINDITEM) {
408 			fileKind = LOWORD(wParam) - IDC_FILESELECT_CHK_01;
409 			setupFileList(hDlg, fileKind);
410 			return (TRUE);
411 		} else if (HIWORD(wParam) == LBN_SELCHANGE && LOWORD(wParam) == IDC_FILESELECT_FILE) {
412 			selno = SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_GETCURSEL, 0, 0L);
413 			if (selno != LB_ERR) {
414 				selno = SendDlgItemMessage(hDlg, IDC_FILESELECT_FILE, LB_GETITEMDATA, selno, 0L);
415 				checkDateControl(hDlg, fileKind, selno);
416 			}
417 			return (TRUE);
418 		} else if (HIWORD(wParam) == LBN_DBLCLK && LOWORD(wParam) == IDC_FILESELECT_FILE) {
419 			SendDlgItemMessage(hDlg, IDOK, BM_CLICK, 0, 0L);
420 			return (TRUE);
421 		} else {
422 			switch (LOWORD(wParam)) {
423 			case IDOK:
424 				if (setupSelectFile(hDlg) == TRUE) {
425 					EndDialog(hDlg, FALSE);
426 				}
427 				return (TRUE);
428 			case IDCANCEL:
429 				EndDialog(hDlg, TRUE);
430 				return (TRUE);
431 			}
432 		}
433 		break;
434 	case WM_CLOSE:
435 		EndDialog(hDlg, TRUE);
436 		return (TRUE);
437 	case WM_HELP:
438 		commonHelpControl(editHelpID, (LPHELPINFO)lParam);
439 		return (TRUE);
440 	}
441 
442 	return (FALSE);
443 }
444 
445 /*
446  * �ե������������
447  */
448 static
selectFileName(HWND hWnd,BOOL * bCancel,struct DATE_T date)449 char *selectFileName(HWND hWnd, BOOL *bCancel, struct DATE_T date)
450 {
451 	memset(selectFile, '\0', sizeof(selectFile));
452 	*bCancel = DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_FILESELECT), hWnd, (DLGPROC)selectFileDlgProc, (LPARAM)&date);
453 	if (*bCancel == TRUE || selectFile[0] == '\0') {
454 		return (NULL);
455 	}
456 	return (selectFile);
457 }
458 
459 /*
460  * �ѥ��ꥹ�Ȥ���Ф�
461  */
462 static
outputPathList(FILE * ofp,int start,int cnt)463 void outputPathList(FILE *ofp, int start, int cnt)
464 {
465 	BOOL	flag = FALSE;
466 
467 	while (cnt-- > 0) {
468 		if (flag != FALSE) {
469 			fputc(';', ofp);
470 		}
471 		if (pathTbl[start] != NULL) {
472 			fprintf(ofp, "%s", pathTbl[start]);
473 			flag = TRUE;
474 		}
475 		start++;
476 	}
477 	if (flag != FALSE) {
478 		fputc('\n', ofp);
479 	}
480 }
481 
482 /*
483  * �ե�����ơ��֥�Υե�����̾�������Ф�
484  */
485 static
outputFileTable(FILE * ofp)486 void outputFileTable(FILE *ofp)
487 {
488 	int		i;
489 	int		j;
490 	int		maxfiles;
491 	int		flag;
492 	char	szKindName[16];
493 	const char	**filetbl;
494 
495 	for (i = 0; i < MAX_KINDITEM; i++) {
496 		maxfiles = kind_filetbl[i].maxfiles;
497 		filetbl = kind_filetbl[i].filetbl;
498 
499 		flag = FALSE;
500 		for (j = 0; j < maxfiles; j++) {
501 			if (filetbl[j] != NULL) {
502 				flag = TRUE;
503 			}
504 		}
505 
506 		if (flag != FALSE) {
507 			strcpy(szKindName, kind_filetbl[i].name);
508 			strlwr(szKindName + 1);
509 			fprintf(ofp, "*%s\n", szKindName);
510 			for (j = 0; j < maxfiles; j++) {
511 				if (filetbl[j] != NULL) {
512 					fprintf(ofp, "%s\n", filetbl[j]);
513 				}
514 			}
515 		}
516 	}
517 }
518 
519 /*
520  * ���ܥե��������
521  */
522 static
makeIndirectFile(struct DATE_T date)523 char *makeIndirectFile(struct DATE_T date)
524 {
525 	char	szTempPath[MAX_PATH+1];
526 	int		rtc;
527 	FILE	*ofp;
528 
529 	rtc = GetTempPath(sizeof(szTempPath), szTempPath);
530 	if (rtc == 0 || rtc > sizeof(szTempPath)) {
531 		strcpy(szTempPath, ".");
532 	}
533 	if (GetTempFileName(szTempPath, szAppName, MAGIC, szTempFile) == 0) {
534 		return (NULL);
535 	}
536 
537 	bCreateTempFile = TRUE;
538 	ofp = fopen(szTempFile, "w");
539 	if (ofp == NULL) {
540 		return (NULL);
541 	}
542 	fprintf(ofp, "*MakeProgram\n%s\n", szAppName);
543 	fprintf(ofp, "*WindowClass\n%s,%s\n", szClassToday, szClassCal);
544 	fprintf(ofp, "*NowDate\n%04d/%02d/%02d\n", date.year, date.month, date.day);
545 	if (pathTbl != NULL && pathTblcnt_s != 0) {
546 		fprintf(ofp, "*TablePath\n");
547 		outputPathList(ofp, 0, pathTblcnt_s);
548 	}
549 	if (pathTbl != NULL && (pathTblcnt - pathTblcnt_s) > 0) {
550 		fprintf(ofp, "*SearchPath\n");
551 		outputPathList(ofp, pathTblcnt_s, pathTblcnt - pathTblcnt_s);
552 	}
553 	if (search_cmdpath != NULL) {
554 		fprintf(ofp, "*ProgramPath\n%s\n", search_cmdpath);
555 	}
556 	outputFileTable(ofp);
557 	fclose(ofp);
558 
559 	return (szTempFile);
560 }
561 
562 /*
563  * ���ޥ�ɵ�ưʸ�������
564  *
565  * ���Ѹ�� free ���뤳��
566  */
567 static
makeCommandLine(HWND hWnd,const char * cmd_fmt,BOOL * bCancel,struct DATE_T date)568 char *makeCommandLine(HWND hWnd, const char *cmd_fmt, BOOL *bCancel, struct DATE_T date)
569 {
570 	int		buflen = _T_MAXPATHNAME * 2 + 32;
571 	int		i;
572 	BOOL	flag;
573 	char	*cmdbuf;
574 	char	*bufp;
575 	char	temp[12];
576 	const char	*cp;
577 
578 	*bCancel = FALSE;
579 
580 	cmdbuf = malloc(buflen);
581 	if (cmdbuf != NULL) {
582 		buflen--;
583 		bufp = cmdbuf;
584 		while (buflen > 0 && *cmd_fmt != '\0') {
585 			if (*bCancel == TRUE) {
586 				goto exit_loop;
587 			}
588 			if (*cmd_fmt != '%') {
589 				*bufp++ = *cmd_fmt++;
590 				buflen--;
591 				continue;
592 			}
593 			cmd_fmt++;
594 			switch (*cmd_fmt) {
595 			case '\0':
596 				goto exit_loop;
597 			case '@':	/* %@ indirect information file */
598 				cp = makeIndirectFile(date);
599 copy_string:
600 				if (cp != NULL) {
601 					while (buflen > 0 && *cp != '\0') {
602 						*bufp++ = *cp++;
603 						buflen--;
604 					}
605 				}
606 				break;
607 			case 'c':	/* %c fullpath TODAY.CNF */
608 				cp = search_filepath(todaycnf_file);
609 				goto copy_string;
610 			case 't':	/* %t table_path */
611 				if (pathTbl != NULL && pathTblcnt_s != 0) {
612 					flag = FALSE;
613 					for (i = 0; i < pathTblcnt_s; i++) {
614 						if (flag != FALSE && buflen > 0) {
615 							*bufp++ = ';';
616 							buflen--;
617 						}
618 						cp = pathTbl[i];
619 						if (cp != NULL) {
620 							flag = TRUE;
621 							while (buflen > 0 && *cp != '\0') {
622 								*bufp++ = *cp++;
623 								buflen--;
624 							}
625 						}
626 					}
627 				}
628 				break;
629 			case 'p':	/* %p command path */
630 				cp = search_cmdpath;
631 				goto copy_string;
632 			case 'd':	/* %d date */
633 				sprintf(temp, "%04d%02d%02d", date.year, date.month, date.day);
634 				cp = temp;
635 				goto copy_string;
636 			case 'f':	/* %f select file name */
637 				cp = selectFileName(hWnd, bCancel, date);
638 				goto copy_string;
639 			case 'i':	/* %i file kind */
640 				if (fileKind >= 0 && fileKind < MAX_KINDITEM) {
641 					cp = kind_filetbl[fileKind].name;
642 				} else {
643 					cp = NULL;
644 				}
645 				goto copy_string;
646 			default:
647 				*bufp++ = *cmd_fmt;
648 				buflen--;
649 				break;
650 			}
651 			cmd_fmt++;
652 		}
653 
654 exit_loop:
655 		*bufp = '\0';
656 	}
657 
658 	return (cmdbuf);
659 }
660 
661 /*
662  * �������ǥ����ε�ư
663  */
externalFileEdit(HWND hWnd,HINSTANCE hInstance,struct DATE_T nowdate)664 BOOL externalFileEdit(HWND hWnd, HINSTANCE hInstance, struct DATE_T nowdate)
665 {
666 	char	*cmdbuf;
667 	char	*cmdname;
668 	char	szCurrent[MAX_PATH+2];
669 	char	szMsg[MAX_PATH+1];
670 	int		rtc;
671 	BOOL	bCancel;
672 	STARTUPINFO	si;
673 	PROCESS_INFORMATION	pi;
674 
675 	if (file_editor_cmd != NULL) {
676 		pathTblcnt = 0;
677 		pathTbl = makeSearchPathTbl(search_path, NULL, &pathTblcnt);
678 		pathTblcnt_s = pathTblcnt;
679 		if (search_envname != NULL) {
680 			pathTbl = makeSearchPathTbl(getenv(search_envname), pathTbl, &pathTblcnt);
681 		}
682 		pathTbl = makeSearchPathTbl(search_cmdpath, pathTbl, &pathTblcnt);
683 //		pathTbl = makeSearchPathTbl(getenv(_T_PATHENV), pathTbl, &pathTblcnt);
684 		rtc = GetCurrentDirectory(sizeof(szCurrent)-1, szCurrent);
685 		if (rtc != 0 && rtc <= sizeof(szCurrent)-1) {
686 			pathTbl = makeSearchPathTbl(szCurrent, pathTbl, &pathTblcnt);
687 		}
688 
689 		ghInstance = hInstance;
690 		cmdbuf = makeCommandLine(hWnd, file_editor_cmd, &bCancel, nowdate);
691 		if (cmdbuf != NULL) {
692 			if (bCancel == FALSE) {
693 				ZeroMemory(&si, sizeof(si));
694 				ZeroMemory(&pi, sizeof(pi));
695 				si.cb = sizeof(si);
696 				si.dwFlags = STARTF_USESHOWWINDOW;
697 				si.wShowWindow = SW_SHOWNORMAL;
698 				if (CreateProcess(NULL, cmdbuf, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) == FALSE) {
699 					cmdname = getCommandName(file_editor_cmd);
700 					if (cmdname != NULL) {
701 						sprintf(szMsg, "%s �����Ĥ���ޤ���", cmdname);
702 						MessageBox(hWnd, szMsg, szAppName, MB_ICONINFORMATION | MB_OK);
703 						free(cmdname);
704 					}
705 				} else {
706 					CloseHandle(pi.hThread);
707 					CloseHandle(pi.hProcess);
708 				}
709 			}
710 			free(cmdbuf);
711 		}
712 
713 		freeSearchPathTbl(pathTbl, pathTblcnt);
714 	}
715 
716 	return (0);
717 }
718 
719 /*
720  * �������ǥ����ε�ư�θ����
721  */
externalFileEditCleanup(void)722 void externalFileEditCleanup(void)
723 {
724 	if (bCreateTempFile != FALSE) {
725 		DeleteFile(szTempFile);
726 	}
727 }
728 
729 /*
730  * winhlp32 -N 902 todayfw.hlp
731  */
732