AmendHub

Download

jcs

/

cat

/

cat.c

 

(View History)

jcs   *: Do c header addition based on output, not input filenames Latest amendment: 3 on 2026-10-16

1 /*
2 * Copyright (c) 2026 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <time.h>
20
21 #include "cat.h"
22 #include "util.h"
23
24 struct selected_file {
25 short vrefnum;
26 long dirid;
27 Str63 name;
28 };
29
30 MenuHandle apple_menu, file_menu;
31 short quitting = 0;
32 struct selected_file selected_files[32] = { 0 };
33 short nselected_files = 0;
34
35 bool handle_menu(long menu_id);
36 void handle_exit(void);
37 pascal short select_files_hook(short item, DialogPtr dlg);
38 pascal Boolean select_files_filter(struct FileParam *pbp);
39 void select_files(void);
40
41 short
42 main(void)
43 {
44 Handle mbar;
45 EventRecord event;
46 WindowPtr event_win;
47 short event_in, n;
48 char key;
49
50 InitGraf(&thePort);
51 InitFonts();
52 FlushEvents(everyEvent, 0);
53 InitWindows();
54 InitMenus();
55 TEInit();
56 InitDialogs(0);
57 InitCursor();
58 MaxApplZone();
59
60 util_init();
61
62 if (!(mbar = GetNewMBar(MBAR_ID)))
63 panic("no mbar");
64 SetMenuBar(mbar);
65 if (!(apple_menu = GetMHandle(APPLE_MENU_ID)))
66 panic("no apple menu");
67 AddResMenu(apple_menu, 'DRVR');
68 if (!(file_menu = GetMHandle(FILE_MENU_ID)))
69 panic("no file menu");
70 DrawMenuBar();
71
72 select_files();
73
74 while (!quitting) {
75 SystemTask();
76 if (!GetNextEvent(everyEvent, &event))
77 continue;
78
79 switch (event.what) {
80 case nullEvent:
81 break;
82 case keyDown:
83 case autoKey:
84 key = event.message & charCodeMask;
85 if ((event.modifiers & cmdKey) != 0 &&
86 handle_menu(MenuKey(key)) == true)
87 break;
88 break;
89 case mouseDown:
90 event_in = FindWindow(event.where, &event_win);
91
92 switch (event_in) {
93 case inMenuBar:
94 handle_menu(MenuSelect(event.where));
95 break;
96 case inSysWindow:
97 SystemClick(&event, event_win);
98 break;
99 case inDrag:
100 SelectWindow(event_win);
101 DragWindow(event_win, event.where, &screenBits.bounds);
102 break;
103 case inGrow:
104 break;
105 case inGoAway:
106 break;
107 case inContent:
108 break;
109 }
110 break;
111 case activateEvt:
112 break;
113 }
114 }
115
116 return 0;
117 }
118
119 bool
120 handle_menu(long menu_id)
121 {
122 bool ret = false, quit;
123 size_t n;
124
125 switch (HiWord(menu_id)) {
126 case APPLE_MENU_ID:
127 switch (LoWord(menu_id)) {
128 case APPLE_MENU_ABOUT_ID:
129 about(PROGRAM_NAME);
130 break;
131 default: {
132 Str255 da;
133 GrafPtr save_port;
134
135 GetItem(apple_menu, LoWord(menu_id), &da);
136 GetPort(&save_port);
137 OpenDeskAcc(da);
138 SetPort(save_port);
139 break;
140 }
141 }
142 break;
143 case FILE_MENU_ID:
144 switch (LoWord(menu_id)) {
145 case FILE_MENU_OPEN_ID:
146 select_files();
147 break;
148 case FILE_MENU_QUIT_ID:
149 ExitToShell();
150 break;
151 }
152 break;
153 }
154
155 HiliteMenu(0);
156 return ret;
157 }
158
159 pascal short
160 select_files_hook(short item, DialogPtr dlg)
161 {
162 Handle ihandle;
163 Rect irect;
164 short itype;
165
166 switch (item) {
167 case -1:
168 /* first call, rename Open to Add */
169 GetDItem(dlg, getOpen, &itype, &ihandle, &irect);
170 SetCTitle((ControlHandle)ihandle, "\pAdd");
171
172 /* and Cancel to Done except on first iteration */
173 if (nselected_files > 0) {
174 GetDItem(dlg, getCancel, &itype, &ihandle, &irect);
175 SetCTitle((ControlHandle)ihandle, "\pDone");
176 }
177 break;
178 }
179
180 return item;
181 }
182
183 pascal Boolean
184 select_files_filter(struct FileParam *pbp)
185 {
186 short n;
187
188 for (n = 0; n < nselected_files; n++) {
189 if (selected_files[n].dirid == CurDirStore &&
190 memcmp(selected_files[n].name, pbp->ioNamePtr,
191 pbp->ioNamePtr[0] + 1) == 0)
192 /* already selected, filter out */
193 return true;
194 }
195
196 return false;
197 }
198
199 void
200 select_files(void)
201 {
202 char buf[1024];
203 Str255 tbuf;
204 SFReply reply;
205 struct tm *ttm = NULL;
206 struct selected_file *file;
207 long fsize, pos, tsize, wsize;
208 short error, n, orefnum, ifrefnum;
209 bool c_header = false;
210
211 nselected_files = 0;
212
213 while (nselected_files < nitems(selected_files)) {
214 file = &selected_files[nselected_files];
215
216 SFGetFile(centered_sfget_dialog(), NULL, select_files_filter, -1,
217 0, select_files_hook, &reply);
218 if (!reply.good)
219 break;
220
221 memcpy(file->name, reply.fName, sizeof(file->name));
222 file->vrefnum = reply.vRefNum;
223 file->dirid = CurDirStore;
224 nselected_files++;
225 }
226
227 if (nselected_files == 0)
228 return;
229
230 SFPutFile(centered_sfput_dialog(), "\pSave concatenated file as:",
231 "\p", NULL, &reply);
232 if (!reply.good)
233 return;
234
235 for (n = 0; n < nselected_files; n++) {
236 if (selected_files[n].dirid == CurDirStore &&
237 memcmp(selected_files[n].name, reply.fName,
238 reply.fName[0] + 1) == 0)
239 panic("Output file cannot be one of the input files");
240 }
241
242 /* do cat */
243
244 error = Create(reply.fName, reply.vRefNum, 'cat ', 'TEXT');
245 if (error && error != dupFNErr)
246 panic("Failed to create file %s: %d", PtoCstr(reply.fName), error);
247
248 error = FSOpen(reply.fName, reply.vRefNum, &orefnum);
249 if (error)
250 panic("Failed to open output file %s: %d", PtoCstr(reply.fName),
251 error);
252
253 error = SetEOF(orefnum, 0);
254 if (error)
255 panic("Failed to truncate file %s: %d", PtoCstr(reply.fName), error);
256
257 if (reply.fName[0] > 2 &&
258 reply.fName[reply.fName[0] - 1] == '.' &&
259 (reply.fName[reply.fName[0]] == 'c' ||
260 reply.fName[reply.fName[0]] == 'h')) {
261 /* writing a .c or .h file, include comment headers */
262 c_header = true;
263
264 memcpy(tbuf, reply.fName, reply.fName[0] + 1);
265 PtoCstr(tbuf);
266
267 ttm = localtime((time_t *)&Time);
268
269 wsize = snprintf(buf, sizeof(buf), "/* %s concatenated on "
270 "%04d-%02d-%02d %02d:%02d:%02d */\r",
271 tbuf, ttm->tm_year + 1900, ttm->tm_mon + 1, ttm->tm_mday,
272 ttm->tm_hour, ttm->tm_min, ttm->tm_sec);
273
274 error = FSWrite(orefnum, &wsize, &buf);
275 if (error)
276 panic("Failed to write file header to %s: %d",
277 PtoCstr(reply.fName), error);
278 }
279
280 for (n = 0; n < nselected_files; n++) {
281 file = &selected_files[n];
282
283 memcpy(tbuf, file->name, file->name[0] + 1);
284 PtoCstr(tbuf);
285 progress("Appending %s...", tbuf);
286
287 error = FSOpen(file->name, file->vrefnum, &ifrefnum);
288 if (error)
289 panic("Failed to open source file %s: %d", PtoCstr(file->name),
290 error);
291
292 error = GetEOF(ifrefnum, &fsize);
293 if (error || fsize < 1)
294 panic("Failed to get size of file %s: %d",
295 PtoCstr(file->name), error);
296
297 error = SetFPos(ifrefnum, fsFromStart, 0);
298 if (error)
299 panic("Failed to get pos of file %s: %d",
300 PtoCstr(file->name), error);
301
302 if (c_header) {
303 wsize = 0;
304 if (n > 0) {
305 buf[wsize++] = '\r';
306 buf[wsize++] = '\r';
307 }
308 buf[wsize++] = '/';
309 buf[wsize++] = '*';
310 buf[wsize++] = ' ';
311 memcpy(buf + wsize, file->name + 1, file->name[0]);
312 wsize += file->name[0];
313 buf[wsize++] = ' ';
314 buf[wsize++] = '*';
315 buf[wsize++] = '/';
316 buf[wsize++] = '\r';
317
318 error = FSWrite(orefnum, &wsize, &buf);
319 if (error)
320 panic("Failed to write filename header to %s: %d",
321 PtoCstr(reply.fName), error);
322 }
323
324 for (pos = 0; pos < fsize; ) {
325 tsize = sizeof(buf);
326 if (pos + tsize > fsize)
327 tsize = fsize - pos;
328
329 error = FSRead(ifrefnum, &tsize, &buf);
330 if (error)
331 panic("Failed to read %lu of file %s: %d", sizeof(buf),
332 PtoCstr(file->name), error);
333
334 wsize = tsize;
335 error = FSWrite(orefnum, &wsize, &buf);
336 if (error || wsize != tsize)
337 panic("Failed to write %lu to file %s: %d", wsize,
338 PtoCstr(reply.fName), error);
339
340 pos += wsize;
341
342 if (pos == fsize && c_header && tsize > 0 &&
343 buf[tsize - 1] != '\r') {
344 /* c'ish file with no trailing newline, add one */
345 wsize = 1;
346 buf[0] = '\r';
347 error = FSWrite(orefnum, &wsize, &buf);
348 if (error || wsize != 1)
349 panic("Failed to write newline to file %s: %d", wsize,
350 PtoCstr(reply.fName), error);
351 break;
352 }
353 }
354
355 FSClose(ifrefnum);
356 }
357
358 progress(NULL);
359
360 FSClose(orefnum);
361 }