fix: correct clang-format and other files
This commit is contained in:
448
src/yait.c
448
src/yait.c
@@ -67,11 +67,13 @@
|
||||
|
||||
typedef enum { MIT, GPL, BSD, UNL } licence_t;
|
||||
|
||||
static const struct option longopts[] = {{"author", required_argument, 0, 'a'},
|
||||
{"licence", required_argument, 0, 'l'},
|
||||
{"quiet", no_argument, 0, 'q'},
|
||||
{"force", no_argument, 0, 'f'},
|
||||
{0, 0, 0, 0}};
|
||||
static const struct option longopts[] = {
|
||||
{ "author", required_argument, 0, 'a' },
|
||||
{ "licence", required_argument, 0, 'l' },
|
||||
{ "quiet", no_argument, 0, 'q' },
|
||||
{ "force", no_argument, 0, 'f' },
|
||||
{ 0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
static int exit_status;
|
||||
|
||||
@@ -79,9 +81,10 @@ static void print_help();
|
||||
static void print_version();
|
||||
|
||||
static char *source_replace(const char *restrict template,
|
||||
const char *restrict package,
|
||||
const char *restrict author) {
|
||||
/*
|
||||
const char *restrict package,
|
||||
const char *restrict author)
|
||||
{
|
||||
/*
|
||||
* XXX(vx-clutch):
|
||||
* - package token : {{PACKAGE}}
|
||||
* - author token : {{AUTHOR}}
|
||||
@@ -90,135 +93,140 @@ static char *source_replace(const char *restrict template,
|
||||
* - replace all packages with package and authors with author
|
||||
* - return
|
||||
*/
|
||||
char *buffer = xmalloc(99999999999);
|
||||
return NULL;
|
||||
char *buffer = xmalloc(99999999999);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *get_name() {
|
||||
int fds[2];
|
||||
if (pipe(fds) == -1)
|
||||
goto sysuser;
|
||||
static char *get_name()
|
||||
{
|
||||
int fds[2];
|
||||
if (pipe(fds) == -1)
|
||||
goto sysuser;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == -1) {
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
goto sysuser;
|
||||
}
|
||||
pid_t pid = fork();
|
||||
if (pid == -1) {
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
goto sysuser;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
dup2(fds[1], STDOUT_FILENO);
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
execlp("git", "git", "config", "--get", "user.name", (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
if (pid == 0) {
|
||||
dup2(fds[1], STDOUT_FILENO);
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
execlp("git", "git", "config", "--get", "user.name",
|
||||
(char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
|
||||
close(fds[1]);
|
||||
char buf[256];
|
||||
ssize_t n = read(fds[0], buf, sizeof buf - 1);
|
||||
close(fds[0]);
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
if (n > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
||||
buf[n] = 0;
|
||||
buf[strcspn(buf, "\n")] = 0;
|
||||
return str_dup(buf);
|
||||
}
|
||||
close(fds[1]);
|
||||
char buf[256];
|
||||
ssize_t n = read(fds[0], buf, sizeof buf - 1);
|
||||
close(fds[0]);
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
if (n > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
||||
buf[n] = 0;
|
||||
buf[strcspn(buf, "\n")] = 0;
|
||||
return str_dup(buf);
|
||||
}
|
||||
|
||||
sysuser: {
|
||||
char *name = getlogin();
|
||||
if (name)
|
||||
return str_dup(name);
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw && pw->pw_name)
|
||||
return str_dup(pw->pw_name);
|
||||
char *name = getlogin();
|
||||
if (name)
|
||||
return str_dup(name);
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw && pw->pw_name)
|
||||
return str_dup(pw->pw_name);
|
||||
}
|
||||
return str_dup("author");
|
||||
return str_dup("author");
|
||||
}
|
||||
|
||||
static int get_year() {
|
||||
time_t now = time(NULL);
|
||||
struct tm *t = localtime(&now);
|
||||
static int get_year()
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
struct tm *t = localtime(&now);
|
||||
|
||||
return t->tm_year + 1900;
|
||||
return t->tm_year + 1900;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int optc;
|
||||
int lose = 0;
|
||||
char *package;
|
||||
bool quiet = false;
|
||||
bool force = false;
|
||||
bool editor = false;
|
||||
bool shell = false;
|
||||
char *author = get_name();
|
||||
exit_status = EXIT_SUCCESS;
|
||||
int year = get_year();
|
||||
licence_t licence = BSD;
|
||||
set_prog_name(argv[0]);
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int optc;
|
||||
int lose = 0;
|
||||
char *package;
|
||||
bool quiet = false;
|
||||
bool force = false;
|
||||
bool editor = false;
|
||||
bool shell = false;
|
||||
char *author = get_name();
|
||||
exit_status = EXIT_SUCCESS;
|
||||
int year = get_year();
|
||||
licence_t licence = BSD;
|
||||
set_prog_name(argv[0]);
|
||||
|
||||
parse_standard_options(argc, argv, print_help, print_version);
|
||||
parse_standard_options(argc, argv, print_help, print_version);
|
||||
|
||||
while ((optc = getopt_long(argc, argv, "a:l:EqfS", longopts, NULL)) != -1)
|
||||
switch (optc) {
|
||||
case 'a':
|
||||
if (optarg) {
|
||||
if (author)
|
||||
free(author);
|
||||
author = str_dup(optarg);
|
||||
}
|
||||
break;
|
||||
case 'l':
|
||||
if (!strcmp(optarg, "list")) {
|
||||
puts("BSD\nGPL\nMIT\nUNL");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if (!strcmp(optarg, "GPL"))
|
||||
licence = GPL;
|
||||
else if (!strcmp(optarg, "MIT"))
|
||||
licence = MIT;
|
||||
else if (!strcmp(optarg, "BSD"))
|
||||
licence = BSD;
|
||||
else if (!strcmp(optarg, "UNL"))
|
||||
licence = UNL;
|
||||
else {
|
||||
puts("BSD\nGPL\nMIT\nUNL");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
case 'E':
|
||||
editor = true;
|
||||
break;
|
||||
case 'q':
|
||||
quiet = true;
|
||||
break;
|
||||
case 'f':
|
||||
force = true;
|
||||
break;
|
||||
case 'S':
|
||||
shell = true;
|
||||
break;
|
||||
default:
|
||||
lose = 1;
|
||||
}
|
||||
if (lose) {
|
||||
emit_try_help();
|
||||
}
|
||||
while ((optc = getopt_long(argc, argv, "a:l:EqfS", longopts, NULL)) !=
|
||||
-1)
|
||||
switch (optc) {
|
||||
case 'a':
|
||||
if (optarg) {
|
||||
if (author)
|
||||
free(author);
|
||||
author = str_dup(optarg);
|
||||
}
|
||||
break;
|
||||
case 'l':
|
||||
if (!strcmp(optarg, "list")) {
|
||||
puts("BSD\nGPL\nMIT\nUNL");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if (!strcmp(optarg, "GPL"))
|
||||
licence = GPL;
|
||||
else if (!strcmp(optarg, "MIT"))
|
||||
licence = MIT;
|
||||
else if (!strcmp(optarg, "BSD"))
|
||||
licence = BSD;
|
||||
else if (!strcmp(optarg, "UNL"))
|
||||
licence = UNL;
|
||||
else {
|
||||
puts("BSD\nGPL\nMIT\nUNL");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
case 'E':
|
||||
editor = true;
|
||||
break;
|
||||
case 'q':
|
||||
quiet = true;
|
||||
break;
|
||||
case 'f':
|
||||
force = true;
|
||||
break;
|
||||
case 'S':
|
||||
shell = true;
|
||||
break;
|
||||
default:
|
||||
lose = 1;
|
||||
}
|
||||
if (lose) {
|
||||
emit_try_help();
|
||||
}
|
||||
|
||||
if (optind >= argc) {
|
||||
fatalf("no input name");
|
||||
}
|
||||
if (optind >= argc) {
|
||||
fatalf("no input name");
|
||||
}
|
||||
|
||||
if (optind + 1 < argc) {
|
||||
errorf("extra operand: %s", argv[optind + 1]);
|
||||
emit_try_help();
|
||||
}
|
||||
if (optind + 1 < argc) {
|
||||
errorf("extra operand: %s", argv[optind + 1]);
|
||||
emit_try_help();
|
||||
}
|
||||
|
||||
package = str_dup(argv[optind]);
|
||||
package = str_dup(argv[optind]);
|
||||
|
||||
if (shell) {
|
||||
fs_write(package, "\
|
||||
if (shell) {
|
||||
fs_write(package, "\
|
||||
#!/bin/sh\n\
|
||||
\n\
|
||||
# Usage: $0 [options]...\n\
|
||||
@@ -263,44 +271,44 @@ while [ $# -gt 0 ]; do\n\
|
||||
shift\n\
|
||||
done\n\
|
||||
",
|
||||
year, author);
|
||||
struct stat st;
|
||||
year, author);
|
||||
struct stat st;
|
||||
|
||||
if (stat(package, &st) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (stat(package, &st) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
|
||||
mode_t mode = st.st_mode | S_IXUSR;
|
||||
mode_t mode = st.st_mode | S_IXUSR;
|
||||
|
||||
if (chmod(package, mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
return exit_status;
|
||||
}
|
||||
if (chmod(package, mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
char *pdir;
|
||||
asprintf(&pdir, "%s/", package);
|
||||
char *pdir;
|
||||
asprintf(&pdir, "%s/", package);
|
||||
|
||||
fs_new(pdir);
|
||||
if (chdir(pdir))
|
||||
fatalfa(errno);
|
||||
fs_new(pdir);
|
||||
if (chdir(pdir))
|
||||
fatalfa(errno);
|
||||
|
||||
fs_new("doc/");
|
||||
fs_new("src/");
|
||||
fs_new("tools/");
|
||||
fs_new("lib/");
|
||||
fs_new("doc/");
|
||||
fs_new("src/");
|
||||
fs_new("tools/");
|
||||
fs_new("lib/");
|
||||
|
||||
fs_write("doc/version.texi", "\
|
||||
fs_write("doc/version.texi", "\
|
||||
@set UPDATED %s\
|
||||
@set UPDATED-MONTH %s\
|
||||
@set EDITION 1\
|
||||
@set VERSION alpha\
|
||||
",
|
||||
"1 January 1970", "January 2025");
|
||||
"1 January 1970", "January 2025");
|
||||
|
||||
char *texi_buffer;
|
||||
asprintf(&texi_buffer, "doc/%s.texi", package);
|
||||
fs_write(texi_buffer, "\
|
||||
char *texi_buffer;
|
||||
asprintf(&texi_buffer, "doc/%s.texi", package);
|
||||
fs_write(texi_buffer, "\
|
||||
\\input texinfo @c -*-texinfo-*-\n\
|
||||
@c %**start of header\n\
|
||||
@setfilename foo.info\n\
|
||||
@@ -469,14 +477,14 @@ a @file{ChangeLog} entry.\n\
|
||||
\n\
|
||||
@bye\
|
||||
",
|
||||
author, author, author, author, author, author, author, author,
|
||||
author, author, author, author, author, author);
|
||||
free(texi_buffer);
|
||||
author, author, author, author, author, author, author, author,
|
||||
author, author, author, author, author, author);
|
||||
free(texi_buffer);
|
||||
|
||||
char *src_path;
|
||||
asprintf(&src_path, "src/%s.c", package);
|
||||
fs_write(src_path, "typedef int x;");
|
||||
// fs_write(src_path, "\
|
||||
char *src_path;
|
||||
asprintf(&src_path, "src/%s.c", package);
|
||||
fs_write(src_path, "typedef int x;");
|
||||
// fs_write(src_path, "\
|
||||
// /* Copyright (C) %s\n\
|
||||
// *\n\
|
||||
// * This file is part of %s\n\
|
||||
@@ -537,10 +545,10 @@ a @file{ChangeLog} entry.\n\
|
||||
// exit(exit_status);\n\
|
||||
// }\
|
||||
// ",
|
||||
// author, package, package, package, author);
|
||||
free(src_path);
|
||||
// author, package, package, package, author);
|
||||
free(src_path);
|
||||
|
||||
fs_write("tools/Cleanup", "\
|
||||
fs_write("tools/Cleanup", "\
|
||||
#!/bin/sh\n\
|
||||
# Usage: ./Cleanup\n\
|
||||
\n\
|
||||
@@ -562,7 +570,7 @@ run make distclean\n\
|
||||
\n\
|
||||
echo \"done.\"\
|
||||
");
|
||||
fs_write("tools/format", "\
|
||||
fs_write("tools/format", "\
|
||||
#!/bin/sh\n\
|
||||
\n\
|
||||
# Usage ./format\n\
|
||||
@@ -571,7 +579,7 @@ find . -name \"*.c\" -exec clang-format -i --verbose {} \\;\n\
|
||||
find . -name \"*.h\" -exec clang-format -i --verbose {} \\;\
|
||||
");
|
||||
|
||||
fs_write(".clang-format", "\
|
||||
fs_write(".clang-format", "\
|
||||
---\n\
|
||||
AccessModifierOffset: -4\n\
|
||||
AlignAfterOpenBracket: Align\n\
|
||||
@@ -682,7 +690,7 @@ UseTab: Always\n\
|
||||
...\
|
||||
");
|
||||
|
||||
fs_write(".clangd", "\
|
||||
fs_write(".clangd", "\
|
||||
CompileFlags:\n\
|
||||
Add: [-x, c, -std=c23, -Ilib, -I.]\n\
|
||||
\n\
|
||||
@@ -692,7 +700,7 @@ Diagnostics:\n\
|
||||
Remove: []\
|
||||
");
|
||||
|
||||
fs_write("README", "\
|
||||
fs_write("README", "\
|
||||
This is the README for the GCK %s distribution.\n\
|
||||
%s does a thing.\n\
|
||||
\n\
|
||||
@@ -723,9 +731,9 @@ implementation, etc., would still be very much appreciated.\n\
|
||||
\n\
|
||||
GCK %s is free software. See the file COPYING for copying conditions.\n\
|
||||
\n",
|
||||
package, package, year, package);
|
||||
package, package, year, package);
|
||||
|
||||
fs_write("INSTALL", "\
|
||||
fs_write("INSTALL", "\
|
||||
Installation Instructions\n\
|
||||
*************************\n\
|
||||
\n\
|
||||
@@ -781,9 +789,9 @@ Documentation and other data files still use the regular prefix.\n\
|
||||
`configure` also accepts some other options. Run `configure --help` for more\n\
|
||||
details\n\
|
||||
",
|
||||
year);
|
||||
year);
|
||||
|
||||
fs_write("AUTHORS", "\
|
||||
fs_write("AUTHORS", "\
|
||||
Authors of %s %s.\n\
|
||||
\n\
|
||||
Copyright (C) %d %s.\n\
|
||||
@@ -796,9 +804,9 @@ Also see the THANKS files.\n\
|
||||
\n\
|
||||
%s\n\
|
||||
",
|
||||
author, package, year, package, author);
|
||||
author, package, year, package, author);
|
||||
|
||||
fs_write("THANKS", "\
|
||||
fs_write("THANKS", "\
|
||||
Additional contributors to %s %s.\n\
|
||||
\n\
|
||||
Copyright (C) %d %s.\n\
|
||||
@@ -813,9 +821,9 @@ Thanks to:\n\
|
||||
\n\
|
||||
See also the AUTHORS file.\n\
|
||||
",
|
||||
author, package, year, author);
|
||||
author, package, year, author);
|
||||
|
||||
fs_write("config.h", "\
|
||||
fs_write("config.h", "\
|
||||
#ifndef CONFIG_H\n\
|
||||
#define CONFIG_H\n\
|
||||
\n\
|
||||
@@ -827,9 +835,9 @@ See also the AUTHORS file.\n\
|
||||
\n\
|
||||
#endif\
|
||||
",
|
||||
package, author, year);
|
||||
package, author, year);
|
||||
|
||||
fs_write("configure", "\
|
||||
fs_write("configure", "\
|
||||
#!/bin/sh\n\
|
||||
\n\
|
||||
usage() {\n\
|
||||
@@ -922,7 +930,7 @@ printf \"CC=%%s\n\" \"$CC\"\n\
|
||||
printf \"done\n\"\
|
||||
");
|
||||
|
||||
fs_write("Makefile", "\
|
||||
fs_write("Makefile", "\
|
||||
PACKAGE := %s\n\
|
||||
\n\
|
||||
SRCS := $(wildcard src/*.c) $(wildcard lib/*.c)\n\
|
||||
@@ -978,9 +986,9 @@ release: clean all\n\
|
||||
\n\
|
||||
.PHONY: all clean distclean install uninstall build release\
|
||||
",
|
||||
package);
|
||||
package);
|
||||
|
||||
fs_write("TODO", "\
|
||||
fs_write("TODO", "\
|
||||
%s %s- TODO\n\
|
||||
\n\
|
||||
Todo:\n\
|
||||
@@ -989,75 +997,77 @@ Todo:\n\
|
||||
\n\
|
||||
end of file TODO\
|
||||
",
|
||||
author, package, package);
|
||||
author, package, package);
|
||||
|
||||
switch (licence) {
|
||||
case BSD:
|
||||
fs_write("COPYING", fBSD, year, author);
|
||||
break;
|
||||
case MIT:
|
||||
fs_write("COPYING", fMIT, year, author);
|
||||
break;
|
||||
case GPL:
|
||||
fs_write("COPYING", fGPL);
|
||||
break;
|
||||
case UNL:
|
||||
fs_write("COPYING", fUNL);
|
||||
break;
|
||||
default:
|
||||
fatalf("illegal state");
|
||||
}
|
||||
switch (licence) {
|
||||
case BSD:
|
||||
fs_write("COPYING", fBSD, year, author);
|
||||
break;
|
||||
case MIT:
|
||||
fs_write("COPYING", fMIT, year, author);
|
||||
break;
|
||||
case GPL:
|
||||
fs_write("COPYING", fGPL);
|
||||
break;
|
||||
case UNL:
|
||||
fs_write("COPYING", fUNL);
|
||||
break;
|
||||
default:
|
||||
fatalf("illegal state");
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
struct stat st;
|
||||
|
||||
if (stat("configure", &st) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (stat("configure", &st) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
|
||||
mode_t mode = st.st_mode | S_IXUSR;
|
||||
mode_t mode = st.st_mode | S_IXUSR;
|
||||
|
||||
if (chmod("configure", mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (chmod("tools/format", mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (chmod("tools/Cleanup", mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (chmod("configure", mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (chmod("tools/format", mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
if (chmod("tools/Cleanup", mode) != 0) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
|
||||
return exit_status;
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
static void print_help() {
|
||||
printf("Usage: %s [OPTION]... [project-name]...\n", PROGRAM);
|
||||
fputs("\
|
||||
static void print_help()
|
||||
{
|
||||
printf("Usage: %s [OPTION]... [project-name]...\n", PROGRAM);
|
||||
fputs("\
|
||||
Generates an opinionated C project.\n",
|
||||
stdout);
|
||||
puts("");
|
||||
fputs("\
|
||||
stdout);
|
||||
puts("");
|
||||
fputs("\
|
||||
--help display this help and exit\n\
|
||||
--version display version information and exit\n",
|
||||
stdout);
|
||||
puts("");
|
||||
fputs("\
|
||||
stdout);
|
||||
puts("");
|
||||
fputs("\
|
||||
-E Open $EDITOR after project creation\n\
|
||||
-q, --quiet Only print required messages\n\
|
||||
-f, --force Overwrite existing files\n\
|
||||
--author=NAME Set the program author (default git username|system username)\n\
|
||||
--licence=LICENCE Set the program licence (default BSD)\n",
|
||||
stdout);
|
||||
exit(exit_status);
|
||||
stdout);
|
||||
exit(exit_status);
|
||||
}
|
||||
|
||||
static void print_version() {
|
||||
printf("%s %s %d\n", prog_name, VERSION, COMMIT);
|
||||
static void print_version()
|
||||
{
|
||||
printf("%s %s %d\n", prog_name, VERSION, COMMIT);
|
||||
|
||||
printf("Copyright (C) %d GCK.\n", YEAR);
|
||||
printf("Copyright (C) %d GCK.\n", YEAR);
|
||||
|
||||
puts("This is free software: you are free to change and redistribute it.");
|
||||
puts("There is NO WARRANTY, to the extent permitted by law.");
|
||||
exit(exit_status);
|
||||
puts("This is free software: you are free to change and redistribute it.");
|
||||
puts("There is NO WARRANTY, to the extent permitted by law.");
|
||||
exit(exit_status);
|
||||
}
|
||||
|
||||
/* end of file yait.c */
|
||||
|
||||
Reference in New Issue
Block a user