Skip to content

Commit a900541

Browse files
committed
Begin rewrite
Redoing in C, coming soon!
1 parent 8aaf16c commit a900541

File tree

11 files changed

+408
-59
lines changed

11 files changed

+408
-59
lines changed

.gitignore

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GNU GENERAL PUBLIC LICENSE
1+
GNU GENERAL PUBLIC LICENSE
22
Version 3, 29 June 2007
33

44
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>

Makefile

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# https://makefiletutorial.com/#makefile-cookbook
2+
TARGET = xbrew
3+
4+
BUILD_DIR := ./build
5+
SRC_DIRS := ./src
6+
7+
# Find all the C/C++ (and assembler) files we want to compile
8+
# Note the single quotes around the * expressions. Make will incorrectly expand these otherwise.
9+
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
10+
11+
# String substitution for every C/C++ file.
12+
# As an example, hello.cpp turns into ./build/hello.cpp.o
13+
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
14+
15+
# String substitution (suffix version without %).
16+
# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d
17+
DEPS := $(OBJS:.o=.d)
18+
19+
# Every folder in ./src will need to be passed to GCC so that it can find header files
20+
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
21+
# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag
22+
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
23+
24+
# The -MMD and -MP flags together generate Makefiles for us!
25+
# These files will have .d instead of .o as the output.
26+
CPPFLAGS := $(INC_FLAGS) -MMD -MP -Wall -Wextra
27+
28+
# The final build step.
29+
$(BUILD_DIR)/$(TARGET): $(OBJS)
30+
$(CC) $(OBJS) -o $@ $(LDFLAGS)
31+
32+
# Build step for C source
33+
$(BUILD_DIR)/%.c.o: %.c
34+
mkdir -p $(dir $@)
35+
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
36+
37+
# Build step for C++ source
38+
$(BUILD_DIR)/%.cpp.o: %.cpp
39+
mkdir -p $(dir $@)
40+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
41+
42+
43+
.PHONY: clean
44+
clean:
45+
rm -r $(BUILD_DIR)
46+
47+
# Include the .d makefiles. The - at the front suppresses the errors of missing
48+
# Makefiles. Initially, all the .d files will be missing, and we don't want those
49+
# errors to show up.
50+
-include $(DEPS)

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# xBrew
2-
CLI homebrew boilerplate utility 🔥
1+
# Caffeine

src/cli.c

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#include "common.h"
2+
3+
#include "cli.h"
4+
#include "version.h"
5+
#include <dirent.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <unistd.h>
10+
11+
static const char *usage =
12+
"\n"
13+
" xbrew <command> [options]\n"
14+
"\n"
15+
" Options:\n"
16+
"\n"
17+
" -h, --help Output this message\n"
18+
" -v, --version Output version information\n"
19+
"\n"
20+
" Commands:\n"
21+
"\n"
22+
" init [console...] Start a new project\n"
23+
" i, install [name...] Install one or more packages\n"
24+
" up, update [name...] Update one or more packages\n"
25+
" uninstall [name...] Uninstall executables\n"
26+
" upgrade [version] Upgrade xbrew to a specified or latest "
27+
"version\n"
28+
" configure [name...] Configure one or more packages\n"
29+
" build [name...] Build one or more packages\n"
30+
" search [query] Search for packages\n"
31+
" help <xbrew> Display help for xbrew\n"
32+
"";
33+
34+
#define BADKEY -1
35+
#define TDS 1
36+
#define NDS 2
37+
#define SWITCH 3
38+
39+
typedef struct {
40+
char *key;
41+
int val;
42+
} t_symstruct;
43+
44+
static t_symstruct lookuptable[] = {
45+
{"3ds", TDS}, {"nds", NDS}, {"switch", SWITCH}};
46+
47+
#define NKEYS (sizeof(lookuptable) / sizeof(t_symstruct))
48+
49+
int key_from_string(char *key) {
50+
long unsigned int i;
51+
for (i = 0; i < NKEYS; i++) {
52+
t_symstruct *sym = &lookuptable[i];
53+
if (strcmp(sym->key, key) == 0)
54+
return sym->val;
55+
}
56+
return BADKEY;
57+
}
58+
59+
void cli_init(const char *name, char *console) {
60+
printf(GRN);
61+
printf("Creating new project %s...\n", name);
62+
printf(reset);
63+
64+
int check;
65+
#ifdef __linux__
66+
check = mkdir(name, 777);
67+
#else
68+
check = _mkdir(name);
69+
#endif
70+
71+
/* Make project directory if check passes */
72+
if (!check) {
73+
#ifdef __linux__
74+
mkdir(name, 777);
75+
#else
76+
_mkdir(name);
77+
#endif
78+
79+
/* Copy over boilerplate */
80+
#ifdef __linux__
81+
struct dirent **name_list;
82+
int n;
83+
84+
n = scandir("templates/", &name_list, NULL, alphasort);
85+
if (n == -1) {
86+
printf(RED);
87+
printf("Scan of 'templates' directory failed... exiting!\n");
88+
printf(reset);
89+
_exit(1);
90+
}
91+
92+
struct dirent **files_list;
93+
int f;
94+
95+
if (0 == strncmp(console, "3ds", 3)) {
96+
f = scandir("templates/3ds/", &files_list, NULL, alphasort);
97+
if (f == -1) {
98+
printf(RED);
99+
printf("Scan of 'templates/3ds' directory failed... exiting!\n");
100+
printf(reset);
101+
_exit(1);
102+
}
103+
while (f--) {
104+
if (strcmp(files_list[f]->d_name, ".") != 0 && strcmp(files_list[f]->d_name, "..") != 0) {
105+
printf("%s\n", files_list[f]->d_name);
106+
107+
108+
}
109+
}
110+
}
111+
free(files_list);
112+
free(name_list);
113+
#else
114+
115+
#endif
116+
117+
printf(GRN);
118+
printf("Successfully created project named %s!\n", name);
119+
printf(reset);
120+
_exit(0);
121+
} else {
122+
printf(RED);
123+
printf("Unable to create directory, does it already exist?\n");
124+
printf(reset);
125+
_exit(1);
126+
}
127+
}
128+
129+
int cli_args(char **argv) {
130+
/* usage */
131+
if (NULL == argv[1] || 0 == strncmp(argv[1], "-h", 2) ||
132+
0 == strncmp(argv[1], "--help", 6)) {
133+
printf("%s\n", usage);
134+
return 0;
135+
}
136+
137+
/* version */
138+
if (0 == strncmp(argv[1], "-v", 2) || 0 == strncmp(argv[1], "--version", 9)) {
139+
printf("%s\n", version);
140+
return 0;
141+
}
142+
143+
/* Project init */
144+
if (0 == strncmp(argv[1], "init", 4)) {
145+
printf("Enter project name\n");
146+
char project[256];
147+
scanf("%s", project);
148+
/* String check */
149+
if (strlen(project) > 0x100) {
150+
printf(RED);
151+
printf("265 character limit! Please choose a smaller name\n");
152+
printf(reset);
153+
}
154+
155+
if (argv[2] != NULL) {
156+
switch (key_from_string(argv[2])) {
157+
case TDS:
158+
cli_init(project, "3ds");
159+
break;
160+
case NDS:
161+
cli_init(project, "nds");
162+
break;
163+
case SWITCH:
164+
cli_init(project, "switch");
165+
break;
166+
case BADKEY:
167+
_exit(1);
168+
}
169+
}
170+
cli_init(project, NULL);
171+
}
172+
173+
/* unknown */
174+
if (0 == strncmp(argv[1], "-", 1)) {
175+
fprintf(stderr, "Unknown option: \"%s\"\n", argv[1]);
176+
return 1;
177+
}
178+
return 0;
179+
}

src/cli.h

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#ifndef CLI_H
2+
#define CLI_H
3+
4+
/* Colors! [https://gist.github.com/RabaDabaDoba/145049536f815903c79944599c6f952a] */
5+
6+
/* Regular text */
7+
#define BLK "\e[0;30m"
8+
#define RED "\e[0;31m"
9+
#define GRN "\e[0;32m"
10+
#define YEL "\e[0;33m"
11+
#define BLU "\e[0;34m"
12+
#define MAG "\e[0;35m"
13+
#define CYN "\e[0;36m"
14+
#define WHT "\e[0;37m"
15+
16+
/* Regular bold text */
17+
#define BBLK "\e[1;30m"
18+
#define BRED "\e[1;31m"
19+
#define BGRN "\e[1;32m"
20+
#define BYEL "\e[1;33m"
21+
#define BBLU "\e[1;34m"
22+
#define BMAG "\e[1;35m"
23+
#define BCYN "\e[1;36m"
24+
#define BWHT "\e[1;37m"
25+
26+
/* Regular underline text */
27+
#define UBLK "\e[4;30m"
28+
#define URED "\e[4;31m"
29+
#define UGRN "\e[4;32m"
30+
#define UYEL "\e[4;33m"
31+
#define UBLU "\e[4;34m"
32+
#define UMAG "\e[4;35m"
33+
#define UCYN "\e[4;36m"
34+
#define UWHT "\e[4;37m"
35+
36+
/* Regular background */
37+
#define BLKB "\e[40m"
38+
#define REDB "\e[41m"
39+
#define GRNB "\e[42m"
40+
#define YELB "\e[43m"
41+
#define BLUB "\e[44m"
42+
#define MAGB "\e[45m"
43+
#define CYNB "\e[46m"
44+
#define WHTB "\e[47m"
45+
46+
/* High intensty background */
47+
#define BLKHB "\e[0;100m"
48+
#define REDHB "\e[0;101m"
49+
#define GRNHB "\e[0;102m"
50+
#define YELHB "\e[0;103m"
51+
#define BLUHB "\e[0;104m"
52+
#define MAGHB "\e[0;105m"
53+
#define CYNHB "\e[0;106m"
54+
#define WHTHB "\e[0;107m"
55+
56+
/* High intensty text */
57+
#define HBLK "\e[0;90m"
58+
#define HRED "\e[0;91m"
59+
#define HGRN "\e[0;92m"
60+
#define HYEL "\e[0;93m"
61+
#define HBLU "\e[0;94m"
62+
#define HMAG "\e[0;95m"
63+
#define HCYN "\e[0;96m"
64+
#define HWHT "\e[0;97m"
65+
66+
/* Bold high intensity text */
67+
#define BHBLK "\e[1;90m"
68+
#define BHRED "\e[1;91m"
69+
#define BHGRN "\e[1;92m"
70+
#define BHYEL "\e[1;93m"
71+
#define BHBLU "\e[1;94m"
72+
#define BHMAG "\e[1;95m"
73+
#define BHCYN "\e[1;96m"
74+
#define BHWHT "\e[1;97m"
75+
76+
/* Reset */
77+
#define reset "\e[0m"
78+
#define CRESET "\e[0m"
79+
#define COLOR_RESET "\e[0m"
80+
81+
int cli_args(char** argv);
82+
void cli_init(const char* name, char *console);
83+
void copy_project(const char *name, char* location, char *dest[], char *console);
84+
85+
#endif

0 commit comments

Comments
 (0)