File: main.c

package info (click to toggle)
sip 2.5-2.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,340 kB
  • ctags: 1,472
  • sloc: ansic: 11,902; sh: 6,482; yacc: 1,987; cpp: 540; lex: 423; makefile: 104
file content (298 lines) | stat: -rw-r--r-- 5,199 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (c) 1998 - 2001 Phil Thompson <phil@river-bank.demon.co.uk>
 *
 * The main module for SIP.
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "sip.h"


#ifndef PACKAGE
#define	PACKAGE	"sip"
#endif

#ifndef VERSION
#define	VERSION	"unknown"
#endif


static char *sipPackage = PACKAGE;


static void appendString(stringList **,char *);
static void help(void);
static void version(void);
static void usage(void);
static char parseopt(int,char **,char *,int *,char **);


int main(int argc,char **argv)
{
	char *filename, *docFile, *codeDir, *cppSuffix, *cppMName;
	char arg, *optarg;
	int optnr;
	FILE *file;
	sipSpec spec;
	stringList *makefiles, *timeslots;

	/* Initialise. */

	sipVersion = VERSION;
	includeDirList = NULL;
	timeslots = NULL;
	makefiles = NULL;
	codeDir = NULL;
	docFile = NULL;
	cppSuffix = ".cpp";
	cppMName = NULL;

	/* Parse the command line. */

	optnr = 1;

	while ((arg = parseopt(argc,argv,"hVm:c:d:I:s:p:t:",&optnr,&optarg)) != '\0')
		switch (arg)
		{
		case 'c':
			/* Where to generate the code. */

			codeDir = optarg;
			break;

		case 'm':
			/* Where to generate a Makefile. */

			appendString(&makefiles,optarg);
			break;

		case 'd':
			/* Where to generate the documentation. */

			docFile = optarg;
			break;

		case 't':
			/* Which version to generate code for. */

			appendString(&timeslots,optarg);
			break;

		case 'I':
			/* Where to get included files from. */

			appendString(&includeDirList,optarg);
			break;

		case 's':
			/* The suffix to use for C++ source files. */

			cppSuffix = optarg;
			break;

		case 'p':
			/* The name of the generated C++ module. */

			cppMName = optarg;
			break;

		case 'h':
			/* Help message. */

			help();
			break;

		case 'V':
			/* Display the version number. */

			version();
			break;

		default:
			usage();
		}

	if (optnr < argc)
	{
		file = NULL;
		filename = argv[optnr++];

		if (optnr < argc)
			usage();
	}
	else
	{
		file = stdin;
		filename = "stdin";
	}

	/* Parse the input file. */

	parse(&spec,file,filename,cppMName,timeslots);

	/* Verify and transform the parse tree. */

	transform(&spec);

	/* Generate code. */

	generateCode(&spec,codeDir,makefiles,docFile,cppSuffix);

	/* All done. */

	exit(0);
}


/*
 * Parse the next command line argument - similar to UNIX getopts().
 */

static char parseopt(int argc,char **argv,char *opts,int *optnrp,char **optargp)
{
	char arg, *op;
	int optnr = *optnrp;

	/* Check there is an argument and it is a switch. */

	if (optnr >= argc || argv[optnr] == NULL || argv[optnr][0] != '-')
		return '\0';

	/* Check it is a valid switch. */

	arg = argv[optnr][1];

	if (arg == '\0' || (op = strchr(opts,arg)) == NULL)
		usage();

	/* Check for the switch parameter, if any. */

	if (op[1] == ':')
	{
		if (argv[optnr][2] != '\0')
		{
			*optargp = &argv[optnr][2];
			++optnr;
		}
		else if (optnr + 1 >= argc || argv[optnr + 1] == NULL)
			usage();
		else
		{
			*optargp = argv[optnr + 1];
			optnr += 2;
		}
	}
	else if (argv[optnr][2] != '\0')
		usage();
	else
	{
		*optargp = NULL;
		++optnr;
	}

	*optnrp = optnr;

	return arg;
}


/*
 * Append a string to a list of them.
 */

static void appendString(stringList **headp,char *s)
{
	stringList *sl;

	/* Create the new entry. */

	sl = sipMalloc(sizeof (stringList));

	sl -> s = s;
	sl -> next = NULL;

	/* Append it to the list. */

	while (*headp != NULL)
		headp = &(*headp) -> next;

	*headp = sl;
}


/*
 * Display all or part of a one line error message describing a fatal error.
 * If the message is complete (it has a newline) then the program exits.
 */

void fatal(char *fmt,...)
{
	static int start = TRUE;

	va_list ap;

	if (start)
	{
		fprintf(stderr,"%s: ",sipPackage);
		start = FALSE;
	}

	va_start(ap,fmt);
	vfprintf(stderr,fmt,ap);
	va_end(ap);

	if (strchr(fmt,'\n') != NULL)
		exit(1);
}


/*
 * Display the SIP version number on stdout and exit with zero exit status.
 */

static void version(void)
{
	printf("%s\n",sipVersion);
	exit(0);
}


/*
 * Display the help message on stdout and exit with zero exit status.
 */

static void help(void)
{
	printf(
"Usage:\n"
"    %s [-h] [-V] [-c dir] [-d file] [-m file] [-I dir] [-s suffix] [-p module] [-t version] [file]\n"
"where:\n"
"    -h          display this help message\n"
"    -V          display the %s version number\n"
"    -c dir      the name of the code directory [default not generated]\n"
"    -d file     the name of the documentation file [default not generated]\n"
"    -m file     the name of the Makefile [default none generated]\n"
"    -I dir      look in this directory when including files\n"
"    -s suffix   the suffix to use for C++ source files [default \".cpp\"]\n"
"    -p module   the name of the generated C++ module [default %%Module]\n"
"    -t version  the primary version to generate code for [default latest]\n"
"    file        the name of the specification file [default stdin]\n"
		,sipPackage,sipPackage);

	exit(0);
}


/*
 * Display the usage message.
 */

static void usage(void)
{
	fatal("Usage: %s [-h] [-V] [-c dir] [-d file] [-m file] [-I dir] [-s suffix] [-p module] [-t version] [file]\n",sipPackage);
}