/* * Copyright (c) 2026 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include "cat.h" #include "util.h" struct selected_file { short vrefnum; long dirid; Str63 name; }; MenuHandle apple_menu, file_menu; short quitting = 0; struct selected_file selected_files[32] = { 0 }; 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 main(void) { Handle mbar; EventRecord event; WindowPtr event_win; short event_in, n; char key; InitGraf(&thePort); InitFonts(); FlushEvents(everyEvent, 0); InitWindows(); InitMenus(); TEInit(); InitDialogs(0); InitCursor(); MaxApplZone(); util_init(); if (!(mbar = GetNewMBar(MBAR_ID))) panic("no mbar"); SetMenuBar(mbar); if (!(apple_menu = GetMHandle(APPLE_MENU_ID))) panic("no apple menu"); AddResMenu(apple_menu, 'DRVR'); if (!(file_menu = GetMHandle(FILE_MENU_ID))) panic("no file menu"); DrawMenuBar(); select_files(); while (!quitting) { SystemTask(); if (!GetNextEvent(everyEvent, &event)) continue; switch (event.what) { case nullEvent: break; case keyDown: case autoKey: key = event.message & charCodeMask; if ((event.modifiers & cmdKey) != 0 && handle_menu(MenuKey(key)) == true) break; break; case mouseDown: event_in = FindWindow(event.where, &event_win); switch (event_in) { case inMenuBar: handle_menu(MenuSelect(event.where)); break; case inSysWindow: SystemClick(&event, event_win); break; case inDrag: SelectWindow(event_win); DragWindow(event_win, event.where, &screenBits.bounds); break; case inGrow: break; case inGoAway: break; case inContent: break; } break; case activateEvt: break; } } return 0; } bool handle_menu(long menu_id) { bool ret = false, quit; size_t n; switch (HiWord(menu_id)) { case APPLE_MENU_ID: switch (LoWord(menu_id)) { case APPLE_MENU_ABOUT_ID: about(PROGRAM_NAME); break; default: { Str255 da; GrafPtr save_port; GetItem(apple_menu, LoWord(menu_id), &da); GetPort(&save_port); OpenDeskAcc(da); SetPort(save_port); break; } } break; case FILE_MENU_ID: switch (LoWord(menu_id)) { case FILE_MENU_OPEN_ID: select_files(); break; case FILE_MENU_QUIT_ID: ExitToShell(); break; } break; } HiliteMenu(0); return ret; } pascal short select_files_hook(short item, DialogPtr dlg) { Handle ihandle; Rect irect; short itype; switch (item) { case -1: /* 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) { char buf[1024]; Str255 tbuf; SFReply reply; struct tm *ttm = NULL; struct selected_file *file; long fsize, pos, tsize, wsize; short error, n, orefnum, ifrefnum; bool c_header = false; nselected_files = 0; while (nselected_files < nitems(selected_files)) { file = &selected_files[nselected_files]; SFGetFile(centered_sfget_dialog(), NULL, select_files_filter, -1, 0, select_files_hook, &reply); if (!reply.good) break; memcpy(file->name, reply.fName, sizeof(file->name)); file->vrefnum = reply.vRefNum; file->dirid = CurDirStore; nselected_files++; } if (nselected_files == 0) return; SFPutFile(centered_sfput_dialog(), "\pSave concatenated file as:", "\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'); if (error && error != dupFNErr) panic("Failed to create file %s: %d", PtoCstr(reply.fName), error); error = FSOpen(reply.fName, reply.vRefNum, &orefnum); if (error) panic("Failed to open output file %s: %d", PtoCstr(reply.fName), error); error = SetEOF(orefnum, 0); if (error) panic("Failed to truncate file %s: %d", PtoCstr(reply.fName), error); if (reply.fName[0] > 2 && reply.fName[reply.fName[0] - 1] == '.' && (reply.fName[reply.fName[0]] == 'c' || reply.fName[reply.fName[0]] == 'h')) { /* writing a .c or .h file, include comment headers */ c_header = true; memcpy(tbuf, reply.fName, reply.fName[0] + 1); PtoCstr(tbuf); ttm = localtime((time_t *)&Time); wsize = snprintf(buf, sizeof(buf), "/* %s concatenated on " "%04d-%02d-%02d %02d:%02d:%02d */\r", tbuf, ttm->tm_year + 1900, ttm->tm_mon + 1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min, ttm->tm_sec); error = FSWrite(orefnum, &wsize, &buf); if (error) panic("Failed to write file header to %s: %d", PtoCstr(reply.fName), error); } for (n = 0; n < nselected_files; n++) { file = &selected_files[n]; memcpy(tbuf, file->name, file->name[0] + 1); PtoCstr(tbuf); progress("Appending %s...", tbuf); error = FSOpen(file->name, file->vrefnum, &ifrefnum); if (error) panic("Failed to open source file %s: %d", PtoCstr(file->name), error); error = GetEOF(ifrefnum, &fsize); if (error || fsize < 1) panic("Failed to get size of file %s: %d", PtoCstr(file->name), error); error = SetFPos(ifrefnum, fsFromStart, 0); if (error) panic("Failed to get pos of file %s: %d", PtoCstr(file->name), error); if (c_header) { 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) tsize = fsize - pos; error = FSRead(ifrefnum, &tsize, &buf); if (error) panic("Failed to read %lu of file %s: %d", sizeof(buf), PtoCstr(file->name), error); wsize = tsize; error = FSWrite(orefnum, &wsize, &buf); if (error || wsize != tsize) panic("Failed to write %lu to file %s: %d", wsize, PtoCstr(reply.fName), error); pos += wsize; if (pos == fsize && c_header && tsize > 0 && buf[tsize - 1] != '\r') { /* c'ish file with no trailing newline, add one */ wsize = 1; buf[0] = '\r'; error = FSWrite(orefnum, &wsize, &buf); if (error || wsize != 1) panic("Failed to write newline to file %s: %d", wsize, PtoCstr(reply.fName), error); break; } } FSClose(ifrefnum); } progress(NULL); FSClose(orefnum); }