jcs
/cat
/amendments
/2
*: Filter out already-selected files, add comment for .c and .h files
jcs made amendment 2 about 1 month ago
--- cat.c Wed Sep 30 16:48:12 2026
+++ cat.c Fri Oct 16 15:19:41 2026
@@ -34,6 +34,7 @@ short nselected_files = 0;
bool handle_menu(long menu_id);
void handle_exit(void);
pascal short select_files_hook(short item, DialogPtr dlg);
+pascal Boolean select_files_filter(struct FileParam *pbp);
void select_files(void);
short
@@ -163,15 +164,37 @@ select_files_hook(short item, DialogPtr dlg)
switch (item) {
case -1:
- /* first call, rename Cancel to Done */
- GetDItem(dlg, getCancel, &itype, &ihandle, &irect);
- SetCTitle((ControlHandle)ihandle, "\pDone");
+ /* first call, rename Open to Add */
+ GetDItem(dlg, getOpen, &itype, &ihandle, &irect);
+ SetCTitle((ControlHandle)ihandle, "\pAdd");
+
+ /* and Cancel to Done except on first iteration */
+ if (nselected_files > 0) {
+ GetDItem(dlg, getCancel, &itype, &ihandle, &irect);
+ SetCTitle((ControlHandle)ihandle, "\pDone");
+ }
break;
}
return item;
}
+pascal Boolean
+select_files_filter(struct FileParam *pbp)
+{
+ short n;
+
+ for (n = 0; n < nselected_files; n++) {
+ if (selected_files[n].dirid == CurDirStore &&
+ memcmp(selected_files[n].name, pbp->ioNamePtr,
+ pbp->ioNamePtr[0] + 1) == 0)
+ /* already selected, filter out */
+ return true;
+ }
+
+ return false;
+}
+
void
select_files(void)
{
@@ -186,8 +209,8 @@ select_files(void)
while (nselected_files < nitems(selected_files)) {
file = &selected_files[nselected_files];
- SFGetFile(centered_sfget_dialog(), NULL, NULL, -1, 0,
- nselected_files == 0 ? NULL : select_files_hook, &reply);
+ SFGetFile(centered_sfget_dialog(), NULL, select_files_filter, -1,
+ 0, select_files_hook, &reply);
if (!reply.good)
break;
@@ -204,7 +227,14 @@ select_files(void)
"\p", NULL, &reply);
if (!reply.good)
return;
-
+
+ for (n = 0; n < nselected_files; n++) {
+ if (selected_files[n].dirid == CurDirStore &&
+ memcmp(selected_files[n].name, reply.fName,
+ reply.fName[0] + 1) == 0)
+ panic("Output file cannot be one of the input files");
+ }
+
/* do cat */
error = Create(reply.fName, reply.vRefNum, 'cat ', 'TEXT');
@@ -238,6 +268,31 @@ select_files(void)
panic("Failed to get pos of file %s: %d",
PtoCstr(file->name), error);
+ if (file->name[0] > 2 && file->name[file->name[0] - 1] == '.' &&
+ (file->name[file->name[0]] == 'c' ||
+ file->name[file->name[0]] == 'h')) {
+ /* .c or .h, include filename */
+ wsize = 0;
+ if (n > 0) {
+ buf[wsize++] = '\r';
+ buf[wsize++] = '\r';
+ }
+ buf[wsize++] = '/';
+ buf[wsize++] = '*';
+ buf[wsize++] = ' ';
+ memcpy(buf + wsize, file->name + 1, file->name[0]);
+ wsize += file->name[0];
+ buf[wsize++] = ' ';
+ buf[wsize++] = '*';
+ buf[wsize++] = '/';
+ buf[wsize++] = '\r';
+
+ error = FSWrite(orefnum, &wsize, &buf);
+ if (error)
+ panic("Failed to write filename header to %s: %d",
+ PtoCstr(reply.fName), error);
+ }
+
for (pos = 0; pos < fsize; ) {
tsize = sizeof(buf);
if (pos + tsize > fsize)