summaryrefslogtreecommitdiff
path: root/muse/muse.c
blob: ccd8c582ea0ada04724ccd8c09ddb1a657c8b457 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "util.h"
#include "parse.h"

#include "../config.h"

/* FIXME: move into one place...? */
Node *file;
char *outfile;
char *pkgname;
int show;
char debugopt[128];
char **incpaths;
size_t nincpaths;
char **extralibs;
size_t nextralibs;

static void usage(char *prog)
{
	printf("%s [-hIdos] [-o outfile] [-p pkgname] [-m] inputs\n", prog);
	printf("\t-h\tprint this help\n");
	printf("\t\tThe outfile must be the same name as each package merged.\n");
	printf("\t-I path\tAdd 'path' to use search path\n");
	printf("\t-d\tPrint debug dumps\n");
	printf("\t-o out\tOutput to outfile\n");
	printf("\t-s\tShow the contents of usefiles `inputs`\n");
}

static void mergeuse(char *path)
{
	FILE *f;
	Stab *st;

	st = file->file.globls;
	f = fopen(path, "r");
	if (!f)
		die("Couldn't open %s\n", path);
	loaduse(path, f, st, Visexport);
	fclose(f);
}

int main(int argc, char **argv)
{
	Optctx ctx;
	size_t i;
	FILE *f;

	optinit(&ctx, "sd:hmo:p:I:l:", argv, argc);
	while (!optdone(&ctx)) {
		switch (optnext(&ctx)) {
		case 'h':
			usage(argv[0]);
			exit(0);
			break;
		case 'p':
			pkgname = ctx.optarg;
			break;
		case 'o':
			outfile = ctx.optarg;
			break;
		case 'd':
			while (ctx.optarg && *ctx.optarg)
				debugopt[*ctx.optarg++ & 0x7f] = 1;
			break;
		case 'I':
			lappend(&incpaths, &nincpaths, ctx.optarg);
			break;
		case 'l':
			lappend(&extralibs, &nextralibs, ctx.optarg);
			break;
		case 's':
			show = 1;
			break;
		default:
			usage(argv[0]);
			exit(0);
			break;
		}
	}

	lappend(&incpaths, &nincpaths, Instroot "/lib/myr");
	if (!outfile) {
		fprintf(stderr, "output file needed when merging usefiles.\n");
		exit(1);
	}
	if (!pkgname)
		pkgname = outfile;

	/* read and parse the file */
	file = mkfile("internal");
	file->file.globls = mkstab(0);
	updatens(file->file.globls, pkgname);
	tyinit(file->file.globls);
	for (i = 0; i < ctx.nargs; i++)
		mergeuse(ctx.args[i]);
	infer(file);
	tagexports(file, 1);
	addextlibs(file, extralibs, nextralibs);

	/* generate the usefile */
	f = fopen(outfile, "w");
	if (debugopt['s'] || show)
		dumpstab(file->file.globls, stdout);
	else
		writeuse(f, file);
	fclose(f);
	return 0;
}