chiark / gitweb /
Create readable text `.bas' for each tokenized BASIC `,ffb' file.
[ssr] / StraySrc / Utilities / c / setdate
1 /*
2  * setdate.c
3  *
4  * Generate AOF file containing date strings
5  *
6  * © 1994-1998 Straylight
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This file is part of Straylight's core utilities (coreutils).
12  *
13  * Coreutils is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * Coreutils is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with coreutils.  If not, write to the Free Software Foundation,
25  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #include "swis.h"
36 #include "kernel.h"
37
38 #include "aof/aof.h"
39 #include "aof/chunk.h"
40
41 #include "_time.h"
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 static aof_file setdate_aof;
46
47 static int cistrcmp(const char *s1,const char *s2)
48 {
49   char c1;
50   char c2;
51   while (*s1 || *s2)
52   {
53     c1=tolower(*(s1++));
54     c2=tolower(*(s2++));
55     if (c1!=c2)
56       return (c1-c2);
57   }
58   return (0);
59 }
60
61 void aof_error(void)
62 {
63   fprintf(stderr,"setdate: not enough memory.\n");
64   exit(1);
65 }
66
67 static void setdate_addSymbol(char *ident,char *format)
68 {
69   _kernel_swi_regs r;
70   int osword[20];
71   char string[256];
72   char time[256];
73   char *p,*q,c;
74
75   /* --- Get the current date --- */
76
77   osword[0]=3;
78   _kernel_osword(14,osword);
79   r.r[0]=(int)osword;
80   r.r[1]=(int)time;
81   r.r[2]=256;
82   r.r[3]=(int)format;
83   if (_kernel_swi(OS_ConvertDateAndTime,&r,&r)!=0)
84   {
85     fprintf(stderr,"setdate: invalid date format.\n");
86     exit(1);
87   }
88
89   /* --- Translate our own magic control characters in the string --- */
90
91   p=time;
92   q=string;
93   while (*p)
94   {
95     switch (c=*p++)
96     {
97       case '\\':
98         switch (c=*p++)
99         {
100           case 't':
101             if (q-string<8)
102               *q++=9;
103             *q++=9;
104             break;
105           case '\\':
106             *q++='\\';
107             break;
108         }
109         break;
110       default:
111         *q++=c;
112         break;
113     }
114   }
115   *q=0;
116
117   /* --- Add the string to the AREA and give it a symbol --- */
118
119   aof_addsym(ident,
120              aof_string(string,setdate_aof.area),
121              0,
122              4,
123              &setdate_aof);
124 }
125
126 int main(int argc,char *argv[])
127 {
128   int o;
129   int i;
130   char *p;
131   char *format;
132   FILE *fp;
133   char *pname;
134
135   aof_chunkinfo obj_area={0};
136   aof_chunkinfo obj_symt={0};
137   aof_chunkinfo obj_strt={0};
138   aof_chunkinfo reloc={0};
139   aof_chunkinfo obj_head={0};
140   aof_chunkinfo obj_idfn={0};
141
142   pname=strrchr(argv[0],'.');
143   if (!pname)
144     pname=argv[0];
145   else
146     pname++;
147
148   if (argc==1)
149   {
150     fprintf(stderr,
151             "Usage: %s <outfile> <identifier>[=<dateformat>]...\n",
152             pname);
153     exit(0);
154   }
155   else if (!cistrcmp(argv[1],"-help"))
156   {
157     fprintf(stderr,
158
159 "%s version 1.00 [%s]\n"
160 "\n"
161 "  © 1994 Straylight\n"
162 "\n"
163 "Syntax: %s <outfile> <identifier>[=<dateformat>]...\n"
164 "\n"
165 "dateformat contains literal characters and format specifiers.  Format\n"
166 "specifiers consist of a '%%' sign, an optional 'z' (indicating\n"
167 "suppression of leading zero) and a two character format string, as\n"
168 "follows:\n"
169 "\n"
170 "String  Value\n"
171 "\n"
172 "  cs    centiseconds\n"
173 "  se    seconds\n"
174 "  mi    minutes\n"
175 "  12    12-hour style hours\n"
176 "  24    14-hour style hours\n"
177 "am/pm   'am' or 'pm' as appropriate\n"
178 "  we    full weekday name\n"
179 "  w3    abbreviated weekday name\n"
180 "  wn    weekday number\n"
181 "  dy    day of the month\n"
182 "  st    suffix for day (e.g. 'st', 'nd', 'th' etc.)\n"
183 "  mo    full month name\n"
184 "  m3    abbreviated month name\n"
185 "  mn    month number\n"
186 "  ce    century number\n"
187 "  yr    year number within century\n"
188 "  wk    number of week in the year\n"
189 "  dn    number of day in the year\n"
190 "  tz    current time zone name\n"
191 "  %%     insert a literal '%%' sign\n"
192 "\n"
193 "If no dateformat is specifed, a default of '%%zdy %%mo %%ce%%yr' is\n"
194 "used.\n",
195
196             pname,
197             _time,
198             pname);
199     exit(0);
200   }
201
202   if (fp=fopen(argv[1],"wb"),!fp)
203   {
204     fprintf(stderr,"setdate: couldn't open output file.\n");
205     exit(1);
206   }
207
208   setdate_aof.area=&obj_area;
209   setdate_aof.symt=&obj_symt;
210   setdate_aof.strt=&obj_strt;
211   setdate_aof.reloc=&reloc;
212
213   /* --- Set up module identification --- */
214
215   aof_string("Straylight setdate utility 1.00",
216              &obj_idfn);
217   aof_align(obj_idfn);
218
219   /* --- Add in the AREA name --- */
220
221   aof_int(0,&obj_strt);
222   aof_string("SetDate$$Data",&obj_strt);
223
224   /* --- Create the date information in the AREA section --- */
225
226   for (i=2;i<argc;i++)
227   {
228     format="%zDY %MO %CE%YR";
229     for (p=argv[i];*p;p++)
230     {
231       if (*p=='=')
232       {
233         *p=0;
234         format=p+1;
235       }
236     }
237     setdate_addSymbol(argv[i],format);
238   }
239   aof_align(obj_area);
240
241   /* --- Build AOF header information --- */
242
243   aof_int((int)aof_RELOC,&obj_head);
244   aof_int(200,&obj_head);
245   aof_int(1,&obj_head);
246   aof_int(obj_symt.next/sizeof(aof_symbol),&obj_head);
247   aof_int(0,&obj_head);
248   aof_int(0,&obj_head);
249
250   aof_int(4,&obj_head);
251   aof_int(0x00002202,&obj_head);
252   aof_int(obj_area.next,&obj_head);
253   aof_int(reloc.next/8,&obj_head);
254   aof_int(0,&obj_head);
255
256   /* --- Add relocation stuff to AREA chunk --- */
257
258   aof_addBlock(reloc.p,reloc.next,&obj_area);
259
260   /* --- Complete string table by adding length word --- */
261
262   aof_fill(obj_strt.next,0,&obj_strt);
263   aof_align(obj_strt);
264
265   /* --- Write completed AOF file out to disk */
266
267   {
268     char _buf[sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry)];
269     chunk_header *h=(chunk_header *)&_buf;
270
271     h->hdr.id=chunk_MAGIC;
272     h->hdr.maxChunks=h->hdr.numChunks=5;
273
274     memcpy(h->table[0].chunkName,"OBJ_IDFN",8);
275     memcpy(h->table[1].chunkName,"OBJ_HEAD",8);
276     memcpy(h->table[2].chunkName,"OBJ_AREA",8);
277     memcpy(h->table[3].chunkName,"OBJ_SYMT",8);
278     memcpy(h->table[4].chunkName,"OBJ_STRT",8);
279
280     o=sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry);
281
282     h->table[0].offset=o;
283     h->table[0].size=obj_idfn.next;
284     o+=obj_idfn.next;
285
286     h->table[1].offset=o;
287     h->table[1].size=obj_head.next;
288     o+=obj_head.next;
289
290     h->table[2].offset=o;
291     h->table[2].size=obj_area.next;
292     o+=obj_area.next;
293
294     h->table[3].offset=o;
295     h->table[3].size=obj_symt.next;
296     o+=obj_symt.next;
297
298     h->table[4].offset=o;
299     h->table[4].size=obj_strt.next;
300     o+=obj_strt.next;
301
302     fwrite(h,sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry),1,fp);
303     fwrite(obj_idfn.p,obj_idfn.next,1,fp);
304     fwrite(obj_head.p,obj_head.next,1,fp);
305     fwrite(obj_area.p,obj_area.next,1,fp);
306     fwrite(obj_symt.p,obj_symt.next,1,fp);
307     fwrite(obj_strt.p,obj_strt.next,1,fp);
308     fclose(fp);
309
310     free(obj_idfn.p);
311     free(obj_area.p);
312     free(obj_strt.p);
313     free(obj_head.p);
314     free(obj_symt.p);
315   }
316 }