chiark / gitweb /
Initial revision
[ssr] / StraySrc / Utilities / c / headerGen
1 /*
2  * headerGen.c
3  *
4  * Generate Sapphire headers automatically
5  *
6  * © 1994-1998 Straylight
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, write to the Free Software Foundation,
23  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "kernel.h"
32 #include "swis.h"
33
34 static void swi(int s,_kernel_swi_regs *r)
35 {
36   _kernel_oserror *e=_kernel_swi(s,r,r);
37   if (e)
38   {
39     fprintf(stderr,"Arrghh -- %s\n",e->errmess);
40     exit(1);
41   }
42 }
43
44 static char *getline(char **p,char *b)
45 {
46   while (**p!=10)
47     *b++=*((*p)++);
48   *b=0;
49   (*p)++;
50   return (b);
51 }
52
53 static void headergen(char *buff,char *infile,char *outfile)
54 {
55   FILE *fp;
56   char b[256];
57   char *p;
58   char *leaf=strrchr(infile,'.');
59
60   if (!leaf)
61     leaf=outfile;
62   else
63     leaf++;
64
65   if (fp=fopen(outfile,"w"),!fp)
66   {
67     fprintf(stderr,"Couldn't open output\n");
68     exit(1);
69   }
70
71   /* --- Do the header (this bit's a little fragile) --- */
72
73   getline(&buff,b);
74   fprintf(fp,"%s\n",b);
75
76   getline(&buff,b);
77   fprintf(fp,"%sh\n",b);
78
79   getline(&buff,b);
80   fprintf(fp,"%s\n",b);
81
82   p=getline(&buff,b);
83   if (p[-1]==')' && p[-5]=='(')
84     p[-6]=0;
85   fprintf(fp,"%s\n",b);
86
87   getline(&buff,b);
88   fprintf(fp,"%s\n",b);
89
90   getline(&buff,b);
91   fprintf(fp,"%s\n",b);
92
93   getline(&buff,b);
94   fprintf(fp,"%s\n",b);
95
96   getline(&buff,b);
97   fprintf(fp,"%s\n",b);
98
99   fprintf(fp,";----- Licensing note -------------------------------------------------------\n"
100              ";\n"
101              "; This file is part of Straylight's Sapphire library.\n"
102              ";\n"
103              "; Sapphire is free software; you can redistribute it and/or modify\n"
104              "; it under the terms of the GNU General Public License as published by\n"
105              "; the Free Software Foundation; either version 2, or (at your option)\n"
106              "; any later version.\n"
107              ";\n"
108              "; Sapphire is distributed in the hope that it will be useful,\n"
109              "; but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
110              "; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
111              "; GNU General Public License for more details.\n"
112              ";\n"
113              "; You should have received a copy of the GNU General Public License\n"
114              "; along with Sapphire.  If not, write to the Free Software Foundation,\n"
115              "; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"
116              "\n"
117              ";----- Overview -------------------------------------------------------------\n"
118              ";\n"
119              "; Functions provided:\n"
120              ";\n");
121
122   /* --- Search for all EXPORTs and output them in the overview --- */
123
124   {
125     char *bb=buff;
126     do
127     {
128       getline(&bb,b);
129       if (!memcmp(b,"\t\tEXPORT",8))
130       {
131         p=b+8;
132         while (!isalnum(*p) && !(*p=='_'))
133           p++;
134         fprintf(fp,";  ");
135         while (isalnum(*p) || *p=='_')
136           fputc(*p++,fp);
137         fputc('\n',fp);
138       }
139     }
140     while (memcmp(b,"\t\tEND",5));
141   }
142   fputc('\n',fp);
143
144   fprintf(fp,"\t\t[\t:LNOT::DEF:%s__dfn\n"
145              "\t\tGBLL\t%s__dfn\n"
146              "\n",
147              leaf,leaf);
148
149   /* --- Now output the block comments etc. --- */
150
151   {
152     char *bb=buff;
153     char *ob=bb;
154     char *cm=0;
155     char *ocm=(char *)0x80000000;
156     do
157     {
158       getline(&bb,b);
159       if (*b==';' && !cm)
160       {
161         cm=ob;
162       }
163       else if (*b!=';' && cm)
164       {
165         ocm=cm;
166         cm=0;
167       }
168       if (!memcmp(b,"\t\tEXPORT",8))
169       {
170         for (p=ocm;p<ob;p++)
171           fputc(*p,fp);
172         fprintf(fp,"\t\tIMPORT\t%s\n\n",b+9);
173       }
174       ob=bb;
175     }
176     while (memcmp(b,"\t\tEND",5));
177   }
178
179   fprintf(fp,"\t\t]\n"
180              "\n"
181              ";----- That's all, folks ----------------------------------------------------\n"
182              "\n"
183              "\t\tEND\n");
184
185   fclose(fp);
186 }
187
188 int main(int argc,char *argv[])
189 {
190   if (argc!=3)
191   {
192     fprintf(stderr,"Syntax: headerGen <input> <output>\n");
193     exit(1);
194   }
195
196   /* --- Load input file into mondo buffer --- */
197
198   {
199     _kernel_swi_regs r;
200     char *fb;
201     r.r[0]=17;
202     r.r[1]=(int)argv[1];
203     swi(OS_File,&r);
204     if (fb=malloc(r.r[4]),!fb)
205     {
206       fprintf(stderr,"No memory -- buy some more\n");
207       exit(1);
208     }
209     r.r[0]=16;
210     r.r[1]=(int)argv[1];
211     r.r[2]=(int)fb;
212     r.r[3]=0;
213     swi(OS_File,&r);
214     headergen(fb,argv[1],argv[2]);
215   }
216   return (0);
217 }