group vp* functions; simplify them; rename the module to vpath

This commit is contained in:
anon 2024-11-12 14:40:58 +01:00
parent 6489d782a1
commit 85f86a774a
12 changed files with 104 additions and 193 deletions

View File

@ -44,7 +44,7 @@
#include "scanner.h"
#include "version.inc"
#include "vp.h"
#include "vpath.h"
#if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
# include <ncurses.h>

View File

@ -38,7 +38,7 @@
#include "global.h"
#include "vp.h" /* vpdirs and vpndirs */
#include "vpath.h" /* vpdirs and vpndirs */
#include <stdlib.h>
#include <sys/types.h> /* needed by stat.h and dirent.h */

View File

@ -42,6 +42,7 @@
#endif
#include "invlib.h"
#include "global.h"
#include "vpath.h"
#include <assert.h>

View File

@ -40,11 +40,10 @@ char *compress_path(char *pathname);
char *egrepinit(const char *egreppat);
char *logdir(char *name);
const char *basename(const char *path);
FILE *myfopen(char *path, char *mode);
int myopen(char *path, int flag, int mode);
FILE *myfopen(const char * path, const char * mode);
int myopen(const char * path, int flag, int mode);
FILE *mypopen(char *cmd, char *mode);
int mypclose(FILE *ptr);
FILE *vpfopen(char *filename, char *type);
void egrepcaseless(int i);
#endif /* CSCOPE_LIBRARY_H */

View File

@ -39,7 +39,7 @@
#include "global.h"
#include "build.h"
#include "vp.h"
#include "vpath.h"
#include "version.inc"
#include "scanner.h"

View File

@ -54,7 +54,7 @@
static pid_t popen_pid[20];
static void (*tstat)(int);
int myopen(char *path, int flag, int mode) {
int myopen(const char * path, int flag, int mode) {
/* opens a file descriptor and then sets close-on-exec for the file */
int fd;
@ -82,7 +82,7 @@ int myopen(char *path, int flag, int mode) {
}
}
FILE *myfopen(char *path, char *mode) {
FILE *myfopen(const char * path, const char * mode) {
/* opens a file pointer and then sets close-on-exec for the file */
FILE *fp;

View File

@ -1,7 +1,7 @@
#include "global.h"
#include "build.h"
#include "vp.h"
#include "vpath.h"
#include "version.inc"
#include "auto_vararg.h"

View File

@ -1,53 +0,0 @@
/*===========================================================================
Copyright (c) 1998-2000, The Santa Cruz Operation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
*Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
*Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
*Neither name of The Santa Cruz Operation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
=========================================================================*/
/* vpaccess - view path version of the access system call */
#include <stdio.h>
#include <unistd.h>
#include "vp.h"
#include <sys/types.h>
int vpaccess(char *path, mode_t amode) {
char buf[MAXPATH + 1];
int returncode;
int i;
if((returncode = access(path, amode)) == -1 && path[0] != '/') {
vpinit(NULL);
for(i = 1; i < vpndirs; i++) {
(void)snprintf(buf, sizeof(buf), "%s/%s", vpdirs[i], path);
if((returncode = access(buf, amode)) != -1) { break; }
}
}
return (returncode);
}

View File

@ -36,7 +36,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "vp.h"
#include <sys/types.h>
#include "vpath.h"
#include "library.h"
#include "global.h"
@ -45,12 +46,39 @@
char **vpdirs; /* directories (including current) in view path */
int vpndirs; /* number of directories in view path */
static inline int vp_element_count(const char * vpath);
int vpaccess(const char * path, const mode_t amode) {
char buf[MAXPATH + 1];
int r = access(path, amode);
if (r != -1
|| path[0] == '/') {
return r;
}
vpinit(NULL);
for(int i = 1; i < vpndirs; i++) {
(void)snprintf(buf, sizeof(buf), "%s/%s", vpdirs[i], path);
r = access(buf, amode);
if(r != -1) { break; }
}
return r;
}
/* XXX
* const arg
* scope i
* scope s
* do not dare to get current directory here
* have vpath on the stack
*/
void vpinit(char *current_dir) {
char *suffix; /* path from view path node */
char *vpath; /* VPATH environment variable value */
char buf[MAXPATH + 1];
int i;
char *s;
/* if an existing directory list is to be updated, free it */
if(current_dir != NULL && vpndirs > 0) {
@ -60,6 +88,7 @@ void vpinit(char *current_dir) {
free(vpdirs);
vpndirs = 0;
}
/* return if the directory list has been computed */
/* or there isn't a view path environment variable */
if(vpndirs > 0 || (vpath = getenv("VPATH")) == NULL || *vpath == '\0') { return; }
@ -72,25 +101,24 @@ void vpinit(char *current_dir) {
for(i = 0; vpath[i] == current_dir[i] && vpath[i] != '\0'; ++i) {
;
}
if((vpath[i] != ':' && vpath[i] != '\0') ||
(current_dir[i] != '/' && current_dir[i] != '\0')) {
if((vpath[i] != ':' && vpath[i] != '\0')
|| (current_dir[i] != '/' && current_dir[i] != '\0')) {
return;
}
suffix = &current_dir[i];
/* count the nodes in the view path */
vpndirs = 1;
for(i = 0; vpath[i] != '\0'; ++i) {
if(vpath[i] == ':' && vpath[i + 1]) { ++vpndirs; }
}
vpndirs = vp_element_count(vpath);
/* create the source directory list */
vpdirs = malloc(vpndirs * sizeof(*vpdirs));
/* don't change VPATH in the environment */
vpath = strdup(vpath);
char * s = vpath;
/* split the view path into nodes */
for(i = 0, s = vpath; *s != '\0'; ++i) {
for(int i = 0; *s != '\0'; ++i) {
vpdirs[i] = s;
while(*s != '\0' && *++s != ':') {
if(*s == '\n') { *s = '\0'; }
@ -98,7 +126,7 @@ void vpinit(char *current_dir) {
if(*s != '\0') { *s++ = '\0'; }
}
/* convert the view path nodes to directories */
for(i = 0; i < vpndirs; ++i) {
for(int i = 0; i < vpndirs; ++i) {
s = malloc(strlen(vpdirs[i]) + strlen(suffix) + 1);
(void)strcpy(s, vpdirs[i]);
(void)strcat(s, suffix);
@ -106,3 +134,56 @@ void vpinit(char *current_dir) {
}
free(vpath);
}
#define OPENFLAG_READ 0
int vpopen(const char * path, const int oflag) {
char buf[MAXPATH + 1];
int r = myopen(path, oflag, 0666);
if (r != -1
|| path[0] == '/'
|| oflag != OPENFLAG_READ) {
return r;
}
vpinit(NULL);
for(int i = 1; i < vpndirs; i++) {
(void)snprintf(buf, sizeof(buf), "%s/%s", vpdirs[i], path);
r = myopen(buf, oflag, 0666);
if(r != -1) { break; }
}
return (r);
}
FILE * vpfopen(const char *filename, const char * type) {
char buf[MAXPATH + 1];
FILE * r = myfopen(filename, type);
if (r != NULL
|| filename[0] == '/'
|| type[0] != 'r') { /* && strcmp(type, "r") == 0 */ /* HBB: this breaks if type=="rb" */
return r;
}
vpinit(NULL);
for(int i = 1; i < vpndirs; i++) {
(void)snprintf(buf, sizeof(buf), "%s/%s", vpdirs[i], filename);
r = myfopen(buf, type);
if(r != NULL) { break; }
}
return r;
}
/* NOTE:
* the original code tested for a trailing ':',
* however, thats equivalent to having and empty path
* which could arise from "::"
*/
static inline
int vp_element_count(const char * vpath) {
int r = strspn(vpath, ":");
++r;
return r;
}

View File

@ -54,17 +54,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#if !falseMALLOC
extern char **vpdirs; /* directories (including current) in view path */
#else
# define MAXDIR 25 /* same as libVP */
# define DIRLEN 80 /* same as libVP */
extern char vpdirs[MAXDIR][DIRLEN + 1];
#endif
extern int vpndirs; /* number of directories in view path */
void vpinit(char *current_dir);
int vpopen(char *path, int oflag);
int vpaccess(char *path, mode_t amode);
int vpopen(const char * path, const int oflag);
int vpaccess(const char * path, const mode_t amode);
FILE * vpfopen(const char * filename, const char * type);
#endif /* CSCOPE_VP_H */
#endif

View File

@ -1,56 +0,0 @@
/*===========================================================================
Copyright (c) 1998-2000, The Santa Cruz Operation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
*Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
*Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
*Neither name of The Santa Cruz Operation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
=========================================================================*/
/* vpfopen - view path version of the fopen library function */
#include <stdio.h>
#include <string.h>
#include "vp.h"
#include "global.h"
FILE *vpfopen(char *filename, char *type) {
char buf[MAXPATH + 1];
FILE *returncode;
int i;
if((returncode = myfopen(filename, type)) == NULL
&& filename[0] != '/'
/* && strcmp(type, "r") == 0 */ /* HBB: this breaks if type=="rb" */
&& type[0] == 'r') {
vpinit(NULL);
for(i = 1; i < vpndirs; i++) {
(void)snprintf(buf, sizeof(buf), "%s/%s", vpdirs[i], filename);
if((returncode = myfopen(buf, type)) != NULL) { break; }
}
}
return returncode;
}

View File

@ -1,56 +0,0 @@
/*===========================================================================
Copyright (c) 1998-2000, The Santa Cruz Operation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
*Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
*Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
*Neither name of The Santa Cruz Operation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
=========================================================================*/
/* vpopen - view path version of the open system call */
#include <stdio.h>
#include "global.h"
#include "vp.h"
#define OPENFLAG_READ 0
int vpopen(char *path, int oflag) {
char buf[MAXPATH + 1];
int returncode;
int i;
if((returncode = myopen(path, oflag, 0666)) == -1 && path[0] != '/' &&
oflag == OPENFLAG_READ) {
vpinit(NULL);
for(i = 1; i < vpndirs; i++) {
(void)snprintf(buf, sizeof(buf), "%s/%s", vpdirs[i], path);
if((returncode = myopen(buf, oflag, 0666)) != -1) { break; }
}
}
return (returncode);
}