diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8ab73ffb7fd848513745bbd5b253cf21b87424c1..fbf7fe70c8fda3da8f008c44b24a6665fe6336ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,3 +17,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - id: check-yaml + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v13.0.0 + hooks: + - id: clang-format diff --git a/require-ess/src/afterInit.c b/require-ess/src/afterInit.c index ffd807dda3b3aad159c9d5af185e03d31cea5e36..af66148530511ad145c926814e0dd0ebd8054915 100644 --- a/require-ess/src/afterInit.c +++ b/require-ess/src/afterInit.c @@ -4,8 +4,8 @@ #include <dbAccess.h> #include <epicsExport.h> #include <epicsStdio.h> -#include <errno.h> #include <errlog.h> +#include <errno.h> #include <initHooks.h> #include <iocsh.h> #include <stdlib.h> @@ -18,7 +18,7 @@ struct cmditem { char *a[12]; char cmd[256]; } x; -} *cmdlist, **cmdlast = &cmdlist; +} * cmdlist, **cmdlast = &cmdlist; void afterInitHook(initHookState state) { struct cmditem *item; diff --git a/require-ess/src/common.c b/require-ess/src/common.c index a6228d40667cf6a9c24874f17f864c64f6a1ded3..57de003dd587df0fe6f987bb949e8d9f3a9fced5 100644 --- a/require-ess/src/common.c +++ b/require-ess/src/common.c @@ -1,7 +1,9 @@ +#include "common.h" + #include <dbAccess.h> +#include <errlog.h> #include <errno.h> #include <error.h> -#include <errlog.h> #include <limits.h> #include <osiFileName.h> #include <stdarg.h> @@ -9,8 +11,6 @@ #include <stdlib.h> #include <string.h> -#include "common.h" - char *realpathSeparator(const char *location) { size_t size = 0; char *buffer = realpath(location, NULL); @@ -22,10 +22,10 @@ char *realpathSeparator(const char *location) { size = strnlen(buffer, PATH_MAX); /* linux realpath removes trailing slash */ if (buffer[size - 1] != OSI_PATH_SEPARATOR[0]) { - char* tmp = realloc(buffer, size + sizeof(OSI_PATH_SEPARATOR)); - if (!tmp){ - free(buffer); - return NULL; + char *tmp = realloc(buffer, size + sizeof(OSI_PATH_SEPARATOR)); + if (!tmp) { + free(buffer); + return NULL; } buffer = tmp; strcpy(buffer + size, OSI_PATH_SEPARATOR); @@ -69,8 +69,9 @@ void pathAdd(const char *varname, const char *dirname) { if (!varname || !dirname) { errlogPrintf("usage: pathAdd \"ENVIRONMENT_VARIABLE\",\"directory\"\n"); - errlogPrintf(" Adds or moves the directory to the front of the " - "ENVIRONMENT_VARIABLE\n"); + errlogPrintf( + " Adds or moves the directory to the front of the " + "ENVIRONMENT_VARIABLE\n"); errlogPrintf(" but after a leading \".\".\n"); return; } diff --git a/require-ess/src/common.h b/require-ess/src/common.h index b833cad50f44fa134c83d6a78fcc8433788344bd..bd03bcde1dc118a331050ced6d04c96c2788cfc8 100644 --- a/require-ess/src/common.h +++ b/require-ess/src/common.h @@ -10,4 +10,4 @@ extern int requireDebug; char *realpathSeparator(const char *location); int putenvprintf(const char *format, ...); void pathAdd(const char *varname, const char *dirname); -#endif /*__COMMON_H_*/ +#endif /*__COMMON_H_*/ diff --git a/require-ess/src/module.c b/require-ess/src/module.c index 129d0795347a8323acd1014a7a7ef1649b466682..0f37c91c79d9d07a523d36c0dcbc3bc5d570dc52 100644 --- a/require-ess/src/module.c +++ b/require-ess/src/module.c @@ -1,6 +1,8 @@ +#include "module.h" + #include <dbAccess.h> -#include <error.h> #include <errlog.h> +#include <error.h> #include <limits.h> #include <osiFileName.h> #include <stdio.h> @@ -8,11 +10,10 @@ #include <string.h> #include "common.h" -#include "module.h" #define MAX_MODULE_SIZE 256 -unsigned long int bufferSize=0; +unsigned long int bufferSize = 0; const char *getLibVersion(struct linkedList *linkedlist, const char *libname) { struct module *m = NULL; @@ -42,8 +43,8 @@ int isModuleLoaded(struct linkedList *linkedlist, const char *libname) { return FALSE; } -int registerModule(struct linkedList *linkedlist, const char *moduleName, const char *version, - const char *location) { +int registerModule(struct linkedList *linkedlist, const char *moduleName, + const char *version, const char *location) { char *absLocation = NULL; char *absLocationRequire = NULL; char *argstring = NULL; @@ -62,27 +63,28 @@ int registerModule(struct linkedList *linkedlist, const char *moduleName, const } struct module *module = NULL; - if (!(module = (struct module*)calloc(sizeof(struct module), 1))) { + if (!(module = (struct module *)calloc(sizeof(struct module), 1))) { goto out_of_memory; } /* Check if string is well formated, there is a \0 in the next MAX_MODULE_SIZE bytes. */ - int nameSize = strnlen(moduleName, MAX_MODULE_SIZE)+1; - if(nameSize > MAX_MODULE_SIZE) return -1; - if (!(module->name = calloc(nameSize, sizeof(char)))){ + int nameSize = strnlen(moduleName, MAX_MODULE_SIZE) + 1; + if (nameSize > MAX_MODULE_SIZE) return -1; + if (!(module->name = calloc(nameSize, sizeof(char)))) { goto out_of_memory; } strcpy(module->name, moduleName); - int versionSize = strnlen(version, MAX_MODULE_SIZE)+1; - if(versionSize > MAX_MODULE_SIZE) return -1; - if (!(module->version = calloc(versionSize, sizeof(char)))){ + int versionSize = strnlen(version, MAX_MODULE_SIZE) + 1; + if (versionSize > MAX_MODULE_SIZE) return -1; + if (!(module->version = calloc(versionSize, sizeof(char)))) { goto out_of_memory; } strcpy(module->version, version); - if (!(module->path = calloc(strnlen(absLocation, PATH_MAX)+1, sizeof(char)))){ + if (!(module->path = + calloc(strnlen(absLocation, PATH_MAX) + 1, sizeof(char)))) { goto out_of_memory; } strcpy(module->path, absLocation ? absLocation : ""); @@ -93,7 +95,7 @@ int registerModule(struct linkedList *linkedlist, const char *moduleName, const * function. The size here will be calculated based on the string that is * being written in fillModuleListRecord. So the magic number here is related * to that string format.*/ - bufferSize += nameSize+versionSize+2; + bufferSize += nameSize + versionSize + 2; if (linkedlist->size == 0) { linkedlist->head = module; } else { @@ -130,7 +132,8 @@ int registerModule(struct linkedList *linkedlist, const char *moduleName, const if (asprintf(&argstring, "REQUIRE_IOC=%.30s, MODULE=%.24s, VERSION=%.39s, " "MODULE_COUNT=%u, BUFFER_SIZE=%lu", - getenv("REQUIRE_IOC"), module->name, module->version, linkedlist->size, bufferSize) < 0){ + getenv("REQUIRE_IOC"), module->name, module->version, + linkedlist->size, bufferSize) < 0) { errlogPrintf("Error asprintf failed\n"); return 0; } diff --git a/require-ess/src/module.h b/require-ess/src/module.h index 16d7305f52bf77e3cd35e521dd08dcea959edb67..1630543f393830ae39868162b9845d4755a3d834 100644 --- a/require-ess/src/module.h +++ b/require-ess/src/module.h @@ -2,22 +2,22 @@ #define __MODULE_H__ struct module { - struct module *next; - char *name; - char *version; - char *path; + struct module *next; + char *name; + char *version; + char *path; }; struct linkedList { - struct module *head; - struct module *tail; - unsigned int size; + struct module *head; + struct module *tail; + unsigned int size; }; - const char *getLibVersion(struct linkedList *linkedlist, const char *libname); const char *getLibLocation(struct linkedList *linkedlist, const char *libname); int isModuleLoaded(struct linkedList *linkedlist, const char *libname); -int registerModule(struct linkedList *linkedlist, const char *module, const char* version, const char *location); +int registerModule(struct linkedList *linkedlist, const char *module, + const char *version, const char *location); -#endif /*__MODULE_H__*/ +#endif /*__MODULE_H__*/ diff --git a/require-ess/src/require.c b/require-ess/src/require.c index 2612a228335af65c3cb082dcbf94f5f277076f07..dd2d726979010b1a6a704b9e1d919a0f32fc2664 100644 --- a/require-ess/src/require.c +++ b/require-ess/src/require.c @@ -19,8 +19,8 @@ #include <epicsExport.h> #include <epicsStdio.h> #include <epicsVersion.h> -#include <errno.h> #include <errlog.h> +#include <errno.h> #include <initHooks.h> #include <iocsh.h> #include <osiFileName.h> @@ -81,10 +81,10 @@ int requireDebug; #define IF_OPEN_DIR(f) if ((dir = opendir(f))) #define DIR_ENTRY struct dirent * #define START_DIR_LOOP while ((errno = 0, direntry = readdir(dir)) != NULL) -#define END_DIR_LOOP \ - if (!direntry && errno) \ +#define END_DIR_LOOP \ + if (!direntry && errno) \ errlogPrintf("error reading directory %s: %s\n", filename, \ - strerror(errno)); \ + strerror(errno)); \ if (dir) closedir(dir); #ifdef _DIRENT_HAVE_D_TYPE #define SKIP_NON_DIR(e) \ @@ -175,12 +175,12 @@ static int setupDbPath(const char *module, const char *dbdir) { static int getRecordHandle(const char *namepart, short type, DBADDR *paddr) { char recordname[PVNAME_STRINGSZ] = {0}; - sprintf(recordname, "%.*s%s", (int)(PVNAME_STRINGSZ - strnlen(namepart, PVNAME_STRINGSZ-1) - 1), + sprintf(recordname, "%.*s%s", + (int)(PVNAME_STRINGSZ - strnlen(namepart, PVNAME_STRINGSZ - 1) - 1), getenv("REQUIRE_IOC"), namepart); if (dbNameToAddr(recordname, paddr) != 0) { - errlogPrintf("require:getRecordHandle : record %s not found\n", - recordname); + errlogPrintf("require:getRecordHandle : record %s not found\n", recordname); return -1; } if (paddr->field_type != type) { @@ -206,8 +206,7 @@ initHookAfterFinishDevSup. But use double indirection here because in 3.13 we must wait until initHooks is loaded before we can register the hook. */ static void fillModuleListRecord(initHookState state) { - if (state != initHookAfterFinishDevSup) - return; + if (state != initHookAfterFinishDevSup) return; struct dbAddr modules = {0}, versions = {0}, modver = {0}; char *bufferModules, *bufferVersions, *bufferModver; @@ -219,11 +218,14 @@ static void fillModuleListRecord(initHookState state) { getRecordHandle(":Versions", DBF_STRING, &versions); getRecordHandle(":ModuleVersions", DBF_CHAR, &modver); - bufferModules = (char *)calloc(MAX_STRING_SIZE*loadedModules.size, sizeof(char)); - bufferVersions = (char *)calloc(MAX_STRING_SIZE*loadedModules.size, sizeof(char)); - bufferModver = (char *)calloc(MAX_STRING_SIZE*loadedModules.size, sizeof(char)); + bufferModules = + (char *)calloc(MAX_STRING_SIZE * loadedModules.size, sizeof(char)); + bufferVersions = + (char *)calloc(MAX_STRING_SIZE * loadedModules.size, sizeof(char)); + bufferModver = + (char *)calloc(MAX_STRING_SIZE * loadedModules.size, sizeof(char)); - for (m = loadedModules.head, i = 0; m != NULL ; m = m->next, i++){ + for (m = loadedModules.head, i = 0; m != NULL; m = m->next, i++) { debug("require: %s[%d] = \"%.*s\"\n", modules.precord->name, i, MAX_STRING_SIZE - 1, m->name); sprintf((char *)(bufferModules) + i * MAX_STRING_SIZE, "%.*s", @@ -232,34 +234,33 @@ static void fillModuleListRecord(initHookState state) { MAX_STRING_SIZE - 1, m->version); sprintf((char *)(bufferVersions) + i * MAX_STRING_SIZE, "%.*s", MAX_STRING_SIZE - 1, m->version); - debug("require: %s+=\"%s %s\"\n", modver.precord->name, - m->name, m->version); - c += sprintf((char *)(bufferModver) + c, "%s %s\n", - m->name, m->version); + debug("require: %s+=\"%s %s\"\n", modver.precord->name, m->name, + m->version); + c += sprintf((char *)(bufferModver) + c, "%s %s\n", m->name, m->version); } - if (dbPut(&modules, DBF_STRING, bufferModules, loadedModules.size) != 0){ + if (dbPut(&modules, DBF_STRING, bufferModules, loadedModules.size) != 0) { errlogPrintf("require: Error to put Modules\n"); } - if (dbPut(&versions, DBF_STRING, bufferVersions, loadedModules.size) != 0){ + if (dbPut(&versions, DBF_STRING, bufferVersions, loadedModules.size) != 0) { errlogPrintf("require: Error to put Versions\n"); } - if (dbPut(&modver, DBF_CHAR, bufferModver, strlen(bufferModver)) != 0){ + if (dbPut(&modver, DBF_CHAR, bufferModver, strlen(bufferModver)) != 0) { errlogPrintf("require: Error to put ModuleVersions\n"); } } -static int registerRequire(){ +static int registerRequire() { char *requireLocation = NULL; char *requireVersion = NULL; requireLocation = getenv(E3_REQUIRE_LOCATION); - if (!requireLocation){ + if (!requireLocation) { errlogPrintf("require: Failed to get " E3_REQUIRE_LOCATION "\n"); return -1; } requireVersion = getenv(E3_REQUIRE_VERSION); - if (!requireVersion){ + if (!requireVersion) { errlogPrintf("require: Failed to get " E3_REQUIRE_VERSION "\n"); return -1; } @@ -280,8 +281,7 @@ int libversionShow(const char *outfile) { } } for (m = loadedModules.head; m; m = m->next) { - fprintf(out, "%s-%20s %s\n", m->name, - m->version, m->path); + fprintf(out, "%s-%20s %s\n", m->name, m->version, m->path); } if (fflush(out) < 0 && outfile) { errlogPrintf("can't write to %s: %s\n", outfile, strerror(errno)); @@ -495,11 +495,11 @@ static off_t fileSize(const char *filename) { } #define fileExists(filename) (fileSize(filename) >= 0) #define fileNotEmpty(filename) (fileSize(filename) > 0) -#define TRY_FILE(offs, ...) \ +#define TRY_FILE(offs, ...) \ (snprintf(filename + offs, PATH_MAX - offs, __VA_ARGS__) && \ fileExists(filename)) -#define TRY_NONEMPTY_FILE(offs, ...) \ +#define TRY_NONEMPTY_FILE(offs, ...) \ (snprintf(filename + offs, PATH_MAX - offs, __VA_ARGS__) && \ fileNotEmpty(filename)) @@ -551,8 +551,8 @@ static int handleDependencies(const char *module, char *depfilename) { * * Sets <filename> to be the path the the underlying module. */ -static char* fetch_module_version(char *filename, size_t max_file_len, - const char *module, const char *version) { +static char *fetch_module_version(char *filename, size_t max_file_len, + const char *module, const char *version) { const char *dirname = NULL; const char *driverpath = NULL; const char *end = NULL; @@ -676,17 +676,18 @@ static char* fetch_module_version(char *filename, size_t max_file_len, if (!found) { if (someArchFound) - errlogPrintf("Module %s%s%s not available for %s\n(but maybe for other " - "EPICS versions or architectures)\n", - module, version ? " version " : "", version ? version : "", - targetArch); + errlogPrintf( + "Module %s%s%s not available for %s\n(but maybe for other " + "EPICS versions or architectures)\n", + module, version ? " version " : "", version ? version : "", + targetArch); else if (someVersionFound) errlogPrintf( "Module %s%s%s not available (but other versions are available)\n", module, version ? " version " : "", version ? version : ""); else errlogPrintf("Module %s%s%s not available\n", module, - version ? " version " : "", version ? version : ""); + version ? " version " : "", version ? version : ""); if (founddir) free(founddir); return NULL; } @@ -696,7 +697,7 @@ static char* fetch_module_version(char *filename, size_t max_file_len, found, founddir); snprintf(filename, max_file_len, "%s" OSI_PATH_SEPARATOR, founddir); - versionLength = strlen(found)+1; + versionLength = strlen(found) + 1; selectedVersion = calloc(versionLength, sizeof(char)); memcpy(selectedVersion, found, versionLength); free(founddir); @@ -741,7 +742,7 @@ static const char *compare_module_version(char *filename, const char *module, version, found); if (compareVersions(found, version, FALSE) == MISMATCH) { errlogPrintf("Requested %s version %s not available, found only %s.\n", - module, version, found); + module, version, found); return NULL; } } @@ -802,7 +803,7 @@ static int require_priv(const char *module, const char *version) { debug("require: module=\"%s\" version=\"%s\"\n", module, version); /* check already loaded verion */ - loaded = getLibVersion(&loadedModules,module); + loaded = getLibVersion(&loadedModules, module); if (loaded) { /* Library already loaded. Check Version. */ switch (compareVersions(loaded, version, FALSE)) { @@ -828,7 +829,7 @@ static int require_priv(const char *module, const char *version) { /* Step 1: Search for module in driverpath */ selectedVersion = fetch_module_version(filename, sizeof(filename), module, version); - if (!selectedVersion){ + if (!selectedVersion) { returnvalue = -1; goto require_priv_end; } @@ -858,7 +859,8 @@ static int require_priv(const char *module, const char *version) { /* Step 3: Ensure that we have loaded the correct version */ debug("require: Check that the loaded and requested versions match\n"); - found = compare_module_version(filename, module, selectedVersion, libdiroffs); + found = + compare_module_version(filename, module, selectedVersion, libdiroffs); if (!found) { returnvalue = -1; goto require_priv_end; @@ -941,7 +943,7 @@ static void requireRegister(void) { iocshRegister(&libversionShowDef, libversionShowFunc); iocshRegister(&ldDef, ldFunc); iocshRegister(&pathAddDef, pathAddFunc); - if(registerRequire() != 0){ + if (registerRequire() != 0) { errlogPrintf("require: Could not register require.\n"); }