fix: handle system() errors
This commit is contained in:
236
yait/main.c
236
yait/main.c
@@ -104,81 +104,88 @@ create_project (format_t fmt)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (fmt.git)
|
if (fmt.git)
|
||||||
system ("git init --quiet");
|
err = system ("git init --quiet");
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
printfn ("failed on git initialize: %s", strerror (err));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
if (!fmt.name)
|
if (!fmt.name)
|
||||||
fmt.name = DEFAULT_USER_NAME;
|
fmt.name = DEFAULT_USER_NAME;
|
||||||
create_file_with_content ("README",
|
create_file_with_content (
|
||||||
"%s ( concise description )\n\n"
|
"README",
|
||||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
"%s ( concise description )\n\n"
|
||||||
"eiusmod tempor\n"
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
||||||
"incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
|
"eiusmod tempor\n"
|
||||||
"veniam, quis\n"
|
"incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
|
||||||
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo "
|
"veniam, quis\n"
|
||||||
"consequat.\n"
|
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo "
|
||||||
"Duis aute irure dolor in reprehenderit in voluptate velit esse "
|
"consequat.\n"
|
||||||
"cillum dolore eu\n"
|
"Duis aute irure dolor in reprehenderit in voluptate velit esse "
|
||||||
"fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
|
"cillum dolore eu\n"
|
||||||
"proident, sunt in\n"
|
"fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
|
||||||
"culpa qui officia deserunt mollit anim id est laborum.",
|
"proident, sunt in\n"
|
||||||
fmt.project ? fmt.project : DEFAULT_PROJECT_NAME);
|
"culpa qui officia deserunt mollit anim id est laborum.",
|
||||||
create_file_with_content ("configure",
|
fmt.project ? fmt.project : DEFAULT_PROJECT_NAME);
|
||||||
"#!/bin/sh\n"
|
create_file_with_content (
|
||||||
"\n"
|
"configure",
|
||||||
"usage() {\n"
|
"#!/bin/sh\n"
|
||||||
"cat <<EOF\n"
|
"\n"
|
||||||
"Usage: $0 [OPTION]... [VAR=VALUE]...\n"
|
"usage() {\n"
|
||||||
"\n"
|
"cat <<EOF\n"
|
||||||
"To assign environment variables (e.g., CC, CFLAGS...), specify them "
|
"Usage: $0 [OPTION]... [VAR=VALUE]...\n"
|
||||||
"as\n"
|
"\n"
|
||||||
"VAR=VALUE.\n"
|
"To assign environment variables (e.g., CC, CFLAGS...), specify them "
|
||||||
"\n"
|
"as\n"
|
||||||
" CC C compiler command [detected]\n"
|
"VAR=VALUE.\n"
|
||||||
" CFLAGS C compiler flags [-g, ...]\n"
|
"\n"
|
||||||
"\n"
|
" CC C compiler command [detected]\n"
|
||||||
"EOF\n"
|
" CFLAGS C compiler flags [-g, ...]\n"
|
||||||
"exit 0\n"
|
"\n"
|
||||||
"}\n"
|
"EOF\n"
|
||||||
"\n"
|
"exit 0\n"
|
||||||
"echo () { printf \"%%s\\n\" \"$*\" ; }\n"
|
"}\n"
|
||||||
"cmdexists () { type \"$1\" >/dev/null 2>&1 ; }\n"
|
"\n"
|
||||||
"trycc () { test -z \"$CC\" && cmdexists \"$1\" && CC=$1 ; }\n"
|
"echo () { printf \"%%s\\n\" \"$*\" ; }\n"
|
||||||
"\n"
|
"cmdexists () { type \"$1\" >/dev/null 2>&1 ; }\n"
|
||||||
"prefix=/usr/bin/\n"
|
"trycc () { test -z \"$CC\" && cmdexists \"$1\" && CC=$1 ; }\n"
|
||||||
"CFLAGS=\"-Wall -Wextra -O2\"\n"
|
"\n"
|
||||||
"LDFLAGS=\n"
|
"prefix=/usr/bin/\n"
|
||||||
"CC=\n"
|
"CFLAGS=\"-Wall -Wextra -O2\"\n"
|
||||||
"\n"
|
"LDFLAGS=\n"
|
||||||
"for arg ; do\n"
|
"CC=\n"
|
||||||
"case \"$arg\" in\n"
|
"\n"
|
||||||
"--help|h) usage ;;\n"
|
"for arg ; do\n"
|
||||||
"CFLAGS=*) CFLAGS=${arg#*=} ;;\n"
|
"case \"$arg\" in\n"
|
||||||
"LDFLAGS=*) LDFLAGS=${arg#*=} ;;\n"
|
"--help|h) usage ;;\n"
|
||||||
"esac\n"
|
"CFLAGS=*) CFLAGS=${arg#*=} ;;\n"
|
||||||
"done\n"
|
"LDFLAGS=*) LDFLAGS=${arg#*=} ;;\n"
|
||||||
"\n"
|
"esac\n"
|
||||||
"printf \"checking for C compiler... \"\n"
|
"done\n"
|
||||||
"trycc gcc\n"
|
"\n"
|
||||||
"trycc cc\n"
|
"printf \"checking for C compiler... \"\n"
|
||||||
"trycc clang\n"
|
"trycc gcc\n"
|
||||||
"printf \"%%s\\n\" \"$CC\"\n"
|
"trycc cc\n"
|
||||||
"\n"
|
"trycc clang\n"
|
||||||
"printf \"checking weather C compiler works... \"\n"
|
"printf \"%%s\\n\" \"$CC\"\n"
|
||||||
"status=\"fail\"\n"
|
"\n"
|
||||||
"tmpc=\"$(mktemp -d)/test.c\"\n"
|
"printf \"checking weather C compiler works... \"\n"
|
||||||
"echo \"typedef int x;\" > \"$tmpc\"\n"
|
"status=\"fail\"\n"
|
||||||
"if output=$($CC $CFLAGS -c -o /dev/null \"$tmpc\" 2>&1) ; then\n"
|
"tmpc=\"$(mktemp -d)/test.c\"\n"
|
||||||
"printf \"yes\\n\"\n"
|
"echo \"typedef int x;\" > \"$tmpc\"\n"
|
||||||
"else\n"
|
"if output=$($CC $CFLAGS -c -o /dev/null \"$tmpc\" 2>&1) ; then\n"
|
||||||
"printf \"no; %%s\\n\" \"$output\"\n"
|
"printf \"yes\\n\"\n"
|
||||||
"exit 1\n"
|
"else\n"
|
||||||
"fi\n"
|
"printf \"no; %%s\\n\" \"$output\"\n"
|
||||||
"\n"
|
"exit 1\n"
|
||||||
"printf \"creating config.mak... \"\n"
|
"fi\n"
|
||||||
"printf \"PREFIX=%%s\\n\" \"$prefix\" > config.mak\n"
|
"\n"
|
||||||
"printf \"CFLAGS=%%s\\n\" \"$CFLAGS\" >> config.mak\n"
|
"printf \"creating config.mak... \"\n"
|
||||||
"printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n"
|
"printf \"PREFIX=%%s\\n\" \"$prefix\" > config.mak\n"
|
||||||
"printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
|
"printf \"CFLAGS=%%s\\n\" \"$CFLAGS\" >> config.mak\n"
|
||||||
"printf \"done\\n\"\n");
|
"printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n"
|
||||||
|
"printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
|
||||||
|
"printf \"done\\n\"\n");
|
||||||
// Create a safe uppercase version of the project name for Makefile variables
|
// Create a safe uppercase version of the project name for Makefile variables
|
||||||
char *mkfile_name = strdup (fmt.project);
|
char *mkfile_name = strdup (fmt.project);
|
||||||
if (!mkfile_name)
|
if (!mkfile_name)
|
||||||
@@ -192,43 +199,45 @@ create_project (format_t fmt)
|
|||||||
if (*p >= 'a' && *p <= 'z')
|
if (*p >= 'a' && *p <= 'z')
|
||||||
*p = *p - 'a' + 'A';
|
*p = *p - 'a' + 'A';
|
||||||
}
|
}
|
||||||
create_file_with_content ("Makefile",
|
create_file_with_content (
|
||||||
"prefix = /usr/bin\n\n"
|
"Makefile",
|
||||||
"%s_SRCS := $(wildcard *.c)\n"
|
"prefix = /usr/bin\n\n"
|
||||||
"%s_OBJS := $(patsubst %%.c,c-out/obj/%%.o,$(%s_SRCS))\n\n"
|
"%s_SRCS := $(wildcard *.c)\n"
|
||||||
"%s := c-out/bin/%s\n\n"
|
"%s_OBJS := $(patsubst %%.c,c-out/obj/%%.o,$(%s_SRCS))\n\n"
|
||||||
"-include config.mak\n\n"
|
"%s := c-out/bin/%s\n\n"
|
||||||
"ifeq ($(wildcard config.mak),)\n"
|
"-include config.mak\n\n"
|
||||||
"all:\n"
|
"ifeq ($(wildcard config.mak),)\n"
|
||||||
"\t@echo \"File config.mak not found, run configure\"\n"
|
"all:\n"
|
||||||
"\t@exit 1\n"
|
"\t@echo \"File config.mak not found, run configure\"\n"
|
||||||
"else\n\n"
|
"\t@exit 1\n"
|
||||||
"all: build $(%s)\n\n"
|
"else\n\n"
|
||||||
"build:\n"
|
"all: build $(%s)\n\n"
|
||||||
"\tmkdir -p c-out/bin\n"
|
"build:\n"
|
||||||
"\tmkdir -p c-out/obj\n\n"
|
"\tmkdir -p c-out/bin\n"
|
||||||
"c-out/obj/%%.o: %%.c\n"
|
"\tmkdir -p c-out/obj\n\n"
|
||||||
"\t$(CC) $(CFLAGS) -c $< -o $@\n\n"
|
"c-out/obj/%%.o: %%.c\n"
|
||||||
"$(%s): $(%s_OBJS)\n"
|
"\t$(CC) $(CFLAGS) -c $< -o $@\n\n"
|
||||||
"\t$(CC) $(CFLAGS) -DCOMMIT=$(shell git rev-list --count --all "
|
"$(%s): $(%s_OBJS)\n"
|
||||||
"2>/dev/null || echo 0) $^ -o $@\n\n"
|
"\t$(CC) $(CFLAGS) -DCOMMIT=$(shell git rev-list --count --all "
|
||||||
"endif\n\n"
|
"2>/dev/null || echo 0) $^ -o $@\n\n"
|
||||||
"install:\n"
|
"endif\n\n"
|
||||||
"\t@echo \"NOT IMPL\"\n"
|
"install:\n"
|
||||||
"\texit 1\n\n"
|
"\t@echo \"NOT IMPL\"\n"
|
||||||
"uninstall:\n"
|
"\texit 1\n\n"
|
||||||
"\t@echo \"NOT IMPL\"\n"
|
"uninstall:\n"
|
||||||
"\texit 1\n\n"
|
"\t@echo \"NOT IMPL\"\n"
|
||||||
"clean:\n"
|
"\texit 1\n\n"
|
||||||
"\trm -rf c-out\n\n"
|
"clean:\n"
|
||||||
"dist-clean: clean\n"
|
"\trm -rf c-out\n\n"
|
||||||
"\trm -f config.mak\n\n"
|
"dist-clean: clean\n"
|
||||||
".PHONY: all clean dist-clean install uninstall build format\n",
|
"\trm -f config.mak\n\n"
|
||||||
mkfile_name, mkfile_name, mkfile_name, mkfile_name, fmt.project,
|
".PHONY: all clean dist-clean install uninstall build format\n",
|
||||||
mkfile_name, mkfile_name, mkfile_name);
|
mkfile_name, mkfile_name, mkfile_name, mkfile_name, fmt.project,
|
||||||
|
mkfile_name, mkfile_name, mkfile_name);
|
||||||
free (mkfile_name);
|
free (mkfile_name);
|
||||||
if (fmt.clang_format)
|
if (fmt.clang_format)
|
||||||
create_file_with_content (".clang-format", "Language: Cpp\nBasedOnStyle: GNU\n");
|
create_file_with_content (".clang-format",
|
||||||
|
"Language: Cpp\nBasedOnStyle: GNU\n");
|
||||||
switch (fmt.licence)
|
switch (fmt.licence)
|
||||||
{
|
{
|
||||||
case BSD3:
|
case BSD3:
|
||||||
@@ -266,10 +275,11 @@ create_project (format_t fmt)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
create_and_enter_directory (fmt.project);
|
create_and_enter_directory (fmt.project);
|
||||||
create_file_with_content ("main.c",
|
create_file_with_content (
|
||||||
"#include <stdio.h>\n\nint main(void) {\n printf(\"%s: Hello "
|
"main.c",
|
||||||
"%s!\\n\");\nreturn 0;\n}",
|
"#include <stdio.h>\n\nint main(void) {\n printf(\"%s: Hello "
|
||||||
fmt.project ? fmt.project : DEFAULT_PROJECT_NAME,
|
"%s!\\n\");\nreturn 0;\n}",
|
||||||
fmt.name ? fmt.name : "World");
|
fmt.project ? fmt.project : DEFAULT_PROJECT_NAME,
|
||||||
|
fmt.name ? fmt.name : "World");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user