Skip to content

Commit

Permalink
Lots of updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsh committed Oct 29, 2023
1 parent b54447e commit e7e3b78
Show file tree
Hide file tree
Showing 23 changed files with 888 additions and 657 deletions.
8 changes: 7 additions & 1 deletion .cz.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"commitizen": {
"tag_format": "v$version",
"version_files": ["CMakeLists.txt", "README.md", "WinPrefs/WinPrefs.psd1", "package.json"],
"version_files": [
"CMakeLists.txt",
"Doxyfile.in",
"README.md",
"WinPrefs/WinPrefs.psd1",
"package.json"
],
"version_provider": "npm",
"version_scheme": "semver"
}
Expand Down
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ project(
LANGUAGES C
VERSION 0.2.1)

option(BUILD_DOCS "Build documentation." OFF)
option(BUILD_TESTS "Build and run tests." OFF)
option(ENABLE_COVERAGE "Link tests with gcov." OFF)
option(ENABLE_VLD "Debug only: enable Visual Leak Detector." OFF)
Expand All @@ -14,6 +15,18 @@ if(BUILD_TESTS)
find_package(cmocka REQUIRED)
endif()

if(BUILD_DOCS)
find_package(Doxygen REQUIRED)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${DOXYGEN_OUT} @ONLY)
add_custom_target(
doc ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/docs/html
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/doc/bincookie-0.1.6)
endif()

add_subdirectory(native)

set(CPACK_PACKAGE_NAME "winprefs")
Expand Down
31 changes: 21 additions & 10 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
include(GNUInstallDirs)

if(CMAKE_BUILD_TYPE MATCHES "Debug|RelWithDebInfo" AND WITH_VLD)
find_library(VLD vld HINTS "C:/Program Files (x86)/Visual Leak Detector/lib/Win64"
"C:/Program Files/Visual Leak Detector/lib/Win64" REQUIRED)
find_path(VLD_H vld.h HINTS "C:/Program Files (x86)/Visual Leak Detector/include"
"C:/Program Files/Visual Leak Detector/include" REQUIRED)
find_library(
VLD vld HINTS "C:/Program Files (x86)/Visual Leak Detector/lib/Win64"
"C:/Program Files/Visual Leak Detector/lib/Win64" REQUIRED)
find_path(VLD_H vld.h
HINTS "C:/Program Files (x86)/Visual Leak Detector/include"
"C:/Program Files/Visual Leak Detector/include" REQUIRED)
endif()

set(GCC_CLANG_SHARED_C_FLAGS -fno-builtin -std=gnu2x)
Expand Down Expand Up @@ -49,10 +51,16 @@ add_executable(
arg.h
constants.c
constants.h
debug.c
debug.h
git.c
git.h
macros.h
main.c
reg_command.c
reg_command.h
registry.c
registry.h
shell.c
shell.h)
target_compile_definitions(winprefs PRIVATE UNICODE _UNICODE)
Expand All @@ -72,14 +80,17 @@ target_link_libraries(
PRIVATE
$<$<AND:$<BOOL:${WITH_VLD}>,$<OR:$<STREQUAL:CMAKE_BUILD_TYPE,Debug>,$<STREQUAL:CMAKE_BUILD_TYPE,RelWithDebInfo>>>:${VLD}>
)
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU" OR CMAKE_C_COMPILER MATCHES "/winegcc$")
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU" OR CMAKE_C_COMPILER MATCHES
"/winegcc$")
get_property(IS_64BIT GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
# mingw can do 2000 without extra help. 64-bit requires at least XP.
target_compile_definitions(winprefs
PRIVATE _WIN32_WINNT=$<IF:$<STREQUAL:${IS_64BIT},TRUE>,0x501,0x0500>)
target_compile_options(winprefs PRIVATE ${GCC_CLANG_SHARED_C_FLAGS}
$<$<CONFIG:Debug>:${GCC_CLANG_DEBUG_C_FLAGS}>)
target_link_libraries(winprefs PRIVATE ole32 shell32)
target_compile_definitions(
winprefs
PRIVATE _WIN32_WINNT=$<IF:$<STREQUAL:${IS_64BIT},TRUE>,0x501,0x0500>)
target_compile_options(
winprefs PRIVATE ${GCC_CLANG_SHARED_C_FLAGS}
$<$<CONFIG:Debug>:${GCC_CLANG_DEBUG_C_FLAGS}>)
target_link_libraries(winprefs PRIVATE shell32)
target_link_options(winprefs PRIVATE -municode)
if(CMAKE_C_COMPILER MATCHES "/winegcc$")
target_include_directories(winprefs PRIVATE /usr/include/wine/msvcrt)
Expand Down
234 changes: 126 additions & 108 deletions native/arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,50 @@

#ifndef ARG_H_INCLUDED

static int
ARG_LONG_func(wchar_t **argv0, char const *name)
{
wchar_t *argIt = *argv0;
while (*argIt == *name && *argIt)
argIt++, name++;
if (*argIt == *name || (*argIt == '=' && !*name)) {
*argv0 = argIt;
return 1;
}
return 0;
static int ARG_LONG_func(wchar_t **argv0, char const *name) {
wchar_t *argIt = *argv0;
while (*argIt == *name && *argIt)
argIt++, name++;
if (*argIt == *name || (*argIt == '=' && !*name)) {
*argv0 = argIt;
return 1;
}
return 0;
}

#define ARG_BEGIN do { \
for (argv[0] && (--argc, ++argv); \
argv[0] && argv[0][0] == '-'; \
argv[0] && (--argc, ++argv)) { \
int isFlag = 1; \
wchar_t *arg = argv[0]; \
if (arg[1] == '-' && arg[2] == 0 && (--argc, ++argv, 1)) \
break; \
ARG_BEGIN_REP: \
switch ((++arg)[0]) { \
case '-': \
isFlag = 0; \
if (arg[-1] == '-') \
++arg;
#define ARG_BEGIN \
do { \
for (argv[0] && (--argc, ++argv); argv[0] && argv[0][0] == '-'; \
argv[0] && (--argc, ++argv)) { \
int isFlag = 1; \
wchar_t *arg = argv[0]; \
if (arg[1] == '-' && arg[2] == 0 && (--argc, ++argv, 1)) \
break; \
ARG_BEGIN_REP: \
switch ((++arg)[0]) { \
case '-': \
isFlag = 0; \
if (arg[-1] == '-') \
++arg;

#define ARG_LONG(name) ARG_LONG_func(&(arg), (name))

#define ARG_VAL() \
(isFlag ? (arg[1] ? ++arg : *(--argc, ++argv)) : \
(arg[0] == '=' ? ++arg : *(--argc, ++argv)))
#define ARG_VAL() \
(isFlag ? (arg[1] ? ++arg : *(--argc, ++argv)) : (arg[0] == '=' ? ++arg : *(--argc, ++argv)))

#define ARG_FLAG() if (isFlag && arg[1]) goto ARG_BEGIN_REP
#define ARG_FLAG() \
if (isFlag && arg[1]) \
goto ARG_BEGIN_REP

#define ARG_END } } } while(0)
#define ARG_END \
} \
} \
} \
while (0)

#define ARG_H_INCLUDED
#endif


/*
* Example:
*/
Expand All @@ -56,72 +58,90 @@ ARG_LONG_func(wchar_t **argv0, char const *name)

// spell-checker: disable

#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef ARG_FUZZ
#define main argMain
#undef stderr
#define stderr stdout
#endif


int
main(int argc, char **argv)
{
char *argv0 = argv[0];
int a = 0, b = 0, c = 0, reverse = 0;
char const *input = "default", *output = "default";
int readstdin = 0;

ARG_BEGIN {
if (0) {
case 'a': a = 1; ARG_FLAG(); break;
case 'b': b = 1; ARG_FLAG(); break;
case 'c': c = 1; ARG_FLAG(); break;
case '\0': readstdin = 1; break;
} else if (ARG_LONG("reverse")) case 'r': {
reverse = 1;
ARG_FLAG();
} else if (ARG_LONG("input")) case 'i': {
input = ARG_VAL();
} else if (ARG_LONG("output")) case 'o': {
output = ARG_VAL();
} else if (ARG_LONG("help")) case 'h': case '?': {
printf("Usage: %s [OPTION...] [STRING...]\n", argv0);
puts("Example usage of arg.h\n");
puts("Options:");
puts(" -a, set a to true");
puts(" -b, set b to true");
puts(" -c, set c to true");
puts(" -r, --reverse set reverse to true");
puts(" -i, --input=STR set input string to STR");
puts(" -o, --output=STR set output string to STR");
puts(" -h, --help display this help and exit");
return EXIT_SUCCESS;
} else { default:
fprintf(stderr,
"%s: invalid option '%s'\n"
"Try '%s --help' for more information.\n",
argv0, *argv, argv0);
return EXIT_FAILURE;
}
} ARG_END;

printf("a = %s\n", a ? "true" : "false");
printf("b = %s\n", b ? "true" : "false");
printf("c = %s\n", c ? "true" : "false");
printf("reverse = %s\n", reverse ? "true" : "false");
printf("readstdin = %s\n", readstdin ? "true" : "false");
printf("input = %s\n", input);
printf("output = %s\n", output);

printf("\nargc: %d", argc);
puts("\nargv:");
while (*argv)
printf(" %s\n", *argv++);

return 0;
int main(int argc, char **argv) {
char *argv0 = argv[0];
int a = 0, b = 0, c = 0, reverse = 0;
char const *input = "default", *output = "default";
int readstdin = 0;

ARG_BEGIN {
if (0) {
case 'a':
a = 1;
ARG_FLAG();
break;
case 'b':
b = 1;
ARG_FLAG();
break;
case 'c':
c = 1;
ARG_FLAG();
break;
case '\0':
readstdin = 1;
break;
} else if (ARG_LONG("reverse"))
case 'r': {
reverse = 1;
ARG_FLAG();
}
else if (ARG_LONG("input")) case 'i': {
input = ARG_VAL();
}
else if (ARG_LONG("output")) case 'o': {
output = ARG_VAL();
}
else if (ARG_LONG("help")) case 'h':
case '?': {
printf("Usage: %s [OPTION...] [STRING...]\n", argv0);
puts("Example usage of arg.h\n");
puts("Options:");
puts(" -a, set a to true");
puts(" -b, set b to true");
puts(" -c, set c to true");
puts(" -r, --reverse set reverse to true");
puts(" -i, --input=STR set input string to STR");
puts(" -o, --output=STR set output string to STR");
puts(" -h, --help display this help and exit");
return EXIT_SUCCESS;
}
else {
default:
fprintf(stderr,
"%s: invalid option '%s'\n"
"Try '%s --help' for more information.\n",
argv0,
*argv,
argv0);
return EXIT_FAILURE;
}
}
ARG_END;

printf("a = %s\n", a ? "true" : "false");
printf("b = %s\n", b ? "true" : "false");
printf("c = %s\n", c ? "true" : "false");
printf("reverse = %s\n", reverse ? "true" : "false");
printf("readstdin = %s\n", readstdin ? "true" : "false");
printf("input = %s\n", input);
printf("output = %s\n", output);

printf("\nargc: %d", argc);
puts("\nargv:");
while (*argv)
printf(" %s\n", *argv++);

return 0;
}

#endif /* ARG_EXAMPLE */
Expand All @@ -136,33 +156,31 @@ main(int argc, char **argv)
#include <string.h>
#define MAX_ARGC 50000

int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
if (size < 2)
return -1;
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size < 2)
return -1;

char *buf = malloc(size+2);
memcpy(buf, data, size);
buf[size] = buf[size+1] = 0;
char *buf = malloc(size + 2);
memcpy(buf, data, size);
buf[size] = buf[size + 1] = 0;

static char *argv[MAX_ARGC+1];
size_t argc = 0;
for (char *ptr = buf; *ptr && argc < MAX_ARGC;) {
argv[argc++] = ptr;
while (*ptr++);
}
argv[argc] = 0;
static char *argv[MAX_ARGC + 1];
size_t argc = 0;
for (char *ptr = buf; *ptr && argc < MAX_ARGC;) {
argv[argc++] = ptr;
while (*ptr++)
;
}
argv[argc] = 0;

main(argc, argv);
free(buf);
main(argc, argv);
free(buf);

return 0;
return 0;
}

#endif


/*
* Copyright (c) 2021 Olaf Berstein
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
5 changes: 5 additions & 0 deletions native/constants.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/** \file */
#ifndef CONSTANTS_H
#define CONSTANTS_H

#include <stddef.h>

//! Maximum `cmd.exe` command line length.
extern const size_t CMD_MAX_COMMAND_LENGTH;
//! Maximum length of a registry key.
extern const size_t MAX_KEY_LENGTH;
//! Maximum length of the value-name in a `reg add` command.
extern const size_t MAX_VALUE_NAME;
//! Alias of `sizeof(wchar_t)`.
extern const size_t WL;
extern const wchar_t *AUTOMATIC_COMMIT_MESSAGE_PREFIX;

Expand Down
Loading

0 comments on commit e7e3b78

Please sign in to comment.