remove empty dir
[ghc-hetmet.git] / utils / hp2ps / Main.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "Defines.h"
6 #include "AuxFile.h"
7 #include "AreaBelow.h"
8 #include "Dimensions.h"
9 #include "HpFile.h"
10 #include "PsFile.h"
11 #include "Reorder.h"
12 #include "Scale.h"
13 #include "TopTwenty.h"
14 #include "TraceElement.h"
15 #include "Deviation.h"
16 #include "Error.h"
17 #include "Utilities.h"
18
19 boolish pflag = 0;      /* read auxiliary file                  */
20 boolish eflag = 0;      /* scaled EPSF                          */ 
21 boolish dflag = 0;      /* sort by standard deviation           */
22 int     iflag = 0;      /* sort by identifier (3-way flag)      */
23 boolish gflag = 0;      /* output suitable for previewer        */
24 boolish yflag = 0;      /* ignore marks                         */
25 boolish bflag = 0;      /* use a big title box                  */
26 boolish sflag = 0;      /* use a small title box                */
27 int     mflag = 0;      /* max no. of bands displayed (default 20) */
28 boolish tflag = 0;      /* ignored threshold specified          */
29 boolish cflag = 0;      /* colour output                        */
30
31 boolish filter;         /* true when running as a filter        */
32
33 static floatish WidthInPoints PROTO((char *));            /* forward */
34 static FILE *Fp PROTO((char *, char **, char *, char *)); /* forward */
35
36 char *hpfile;
37 char *psfile;
38 char *auxfile;
39
40 char *programname;
41
42 static char *pathName;
43 static char *baseName; /* "basename" is a std C library name (sigh) */
44
45 FILE* hpfp;
46 FILE* psfp;
47 FILE* auxfp;
48
49 floatish xrange = 0.0;
50 floatish yrange = 0.0;
51
52 floatish auxxrange = 0.0;
53 floatish auxyrange = 0.0;
54
55 floatish epsfwidth;
56 floatish areabelow;
57
58 intish nsamples;
59 intish nmarks;
60 intish nidents;
61
62 floatish THRESHOLD_PERCENT = DEFAULT_THRESHOLD;
63 int TWENTY = DEFAULT_TWENTY;
64
65 int main(argc, argv)
66 int argc;
67 char* argv[];
68 {
69
70     programname = copystring(Basename(argv[0]));
71
72     argc--, argv++;
73     while (argc && argv[0][0] == '-') {
74         while (*++*argv)
75             switch(**argv) {
76             case 'p':
77                 pflag++;
78                 break;
79             case 'e':
80                 eflag++;
81                 epsfwidth = WidthInPoints(*argv + 1);
82                 goto nextarg;
83             case 'd':
84                 dflag++;
85                 goto nextarg;
86             case 'i':
87                 switch( *(*argv + 1) ) {
88                   case '-':
89                     iflag = -1;
90                   case '+':
91                   default:
92                     iflag = 1;
93                 }
94                 goto nextarg;
95             case 'g':
96                 gflag++;
97                 goto nextarg;
98             case 'y':
99                 yflag++;
100                 goto nextarg;
101             case 'b':
102                 bflag++;
103                 goto nextarg;
104             case 's':
105                 sflag++;
106                 goto nextarg;
107             case 'm':
108                 mflag++;
109                 TWENTY = atoi(*argv + 1);
110                 if (TWENTY > DEFAULT_TWENTY)
111                     Usage(*argv-1);
112                 goto nextarg;
113             case 't':
114                 tflag++;
115                 THRESHOLD_PERCENT = (floatish) atof(*argv + 1);
116                 if (THRESHOLD_PERCENT < 0 || THRESHOLD_PERCENT > 5)
117                     Usage(*argv-1);
118                 goto nextarg;
119             case 'c':
120                 cflag++;
121                 goto nextarg;
122             case '?':
123             default:
124                 Usage(*argv-1);
125             }
126 nextarg: ;
127         argc--, argv++;
128     }
129
130     hpfile = "stdin";
131     psfile = "stdout";
132
133     hpfp = stdin;
134     psfp = stdout;
135
136     filter = argc < 1;
137
138
139
140     if (!filter) {
141         pathName = copystring(argv[0]);
142         DropSuffix(pathName, ".hp");
143         baseName = copystring(Basename(pathName));
144
145         hpfp  = Fp(pathName, &hpfile, ".hp", "r"); 
146         psfp  = Fp(baseName, &psfile, ".ps", "w"); 
147
148         if (pflag) auxfp = Fp(baseName, &auxfile, ".aux", "r");
149     }
150
151     GetHpFile(hpfp);
152
153     if (!filter && pflag) GetAuxFile(auxfp);
154
155
156     TraceElement();          /* Orders on total, Removes trace elements (tflag) */
157
158     if (dflag) Deviation();  /* ReOrders on deviation */
159
160     if (iflag) Identorder(iflag); /* ReOrders on identifier */
161
162     if (pflag) Reorder();    /* ReOrders on aux file */
163
164     if (TWENTY) TopTwenty(); /* Selects top twenty (mflag) */
165
166     Dimensions();
167
168     areabelow = AreaBelow();
169
170     Scale();
171
172     PutPsFile();
173
174     if (!filter) {
175         auxfp = Fp(baseName, &auxfile, ".aux", "w");
176         PutAuxFile(auxfp);
177     } 
178
179     return(0);
180 }
181
182
183
184 typedef enum {POINTS, INCHES, MILLIMETRES} pim;
185
186 static pim Units PROTO((char *));   /* forward */
187
188 static floatish
189 WidthInPoints(wstr)
190   char *wstr;
191 {
192     floatish result;
193
194     result = (floatish) atof(wstr);
195
196     switch (Units(wstr)) {
197         case INCHES:            
198             result *= 72.0;
199             break;
200         case MILLIMETRES:       
201             result *= 2.834646;
202             break;
203         case POINTS:
204         default: ;
205     }
206
207     if (result <= 144)   /* Minimum of 2in wide ! */
208         Usage(wstr);
209
210     return result;
211 }
212
213         
214 static pim
215 Units(wstr)
216   char* wstr;
217 {
218 int i;
219
220     i = strlen(wstr) - 2;
221
222     if (wstr[i] == 'p' && wstr[i+1] == 't') {
223         return POINTS;
224     } else if (wstr[i] == 'i' && wstr[i+1] == 'n') {
225         return INCHES;  
226     } else if (wstr[i] == 'm' && wstr[i+1] == 'm') {
227         return MILLIMETRES;
228     } else {
229         return POINTS;
230     }
231 }
232
233 static FILE *
234 Fp(rootname, filename, suffix, mode)
235   char* rootname; char** filename; char* suffix; char* mode;
236 {
237     *filename = copystring2(rootname, suffix);
238
239     return(OpenFile(*filename, mode));
240 }
241
242 #ifdef DEBUG
243 void
244 _stgAssert (filename, linenum)
245   char          *filename;
246   unsigned int  linenum;
247 {
248     fflush(stdout);
249     fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
250     fflush(stderr);
251     abort();
252 }
253 #endif