summaryrefslogtreecommitdiff
path: root/maje.c
blob: 9ba22c2732815e112f3f2afa8d56f0c2c9b13341 (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
#define _XOPEN_SOURCE 700
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "maje.h"

static void usage(char *progname)
{
	printf("usage: %s [dir]\n", progname);
}

int main(int argc, char *argv[])
{
	char *srcdir = NULL;

	int c;
	while ((c = getopt(argc, argv, "")) != -1) {
		switch (c) {
		default:
			usage(argv[0]);
			return 1;
		}
	}

	if (optind < argc - 1) {
		fprintf(stderr, "maje: extra operands\n");
		return 1;
	}

	srcdir = argv[optind];

	if (srcdir == NULL) {
		srcdir = ".";
	}

	struct majefile *sources = find_source_files(srcdir);
	char *mainname = find_main(sources);
	if (!mainname) {
		printf("libraries not yet supported\n");
		return 0;
	}
	char *target = strdup(mainname);
	target[strlen(target) - 2] = '\0';
	target = basename(target);

	FILE *makefile = fopen("Makefile", "w");
	if (makefile == NULL) {
		perror("fopen");
		return 1;
	}

	make_makefile("Makefile", sources, target);

	return 0;
}