utils/lookup*.c: constify the API

extern int Lookup(const char *str, const char * const *table);
extern int LookupAny(char, const char * const *);
extern int LookupFull(const char *, const char **);
extern int LookupStruct(const char *str, const LookupTable *table_start, int size);
extern int LookupStructFull(const char *str, const char * const *table, int size);
This commit is contained in:
Darryl L. Miles 2024-10-10 20:11:37 +01:00 committed by Tim Edwards
parent aa43cc164e
commit f5b41a06d6
4 changed files with 32 additions and 29 deletions

View File

@ -56,8 +56,8 @@ static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/
int int
Lookup(str, table) Lookup(str, table)
char *str; /* Pointer to a string to be looked up */ const char *str; /* Pointer to a string to be looked up */
char *(table[]); /* Pointer to an array of string pointers const char * const *table; /* Pointer to an array of string pointers
* which are the valid commands. * which are the valid commands.
* The end of * The end of
* the table is indicated by a NULL string. * the table is indicated by a NULL string.
@ -68,7 +68,7 @@ char *(table[]); /* Pointer to an array of string pointers
int ststart = 0; int ststart = 0;
#ifdef MAGIC_WRAPPER #ifdef MAGIC_WRAPPER
static char *namespace = "::magic::"; static const char *namespace = "::magic::";
/* Skip over prefix of qualified namespaces "::magic::" and "magic::" */ /* Skip over prefix of qualified namespaces "::magic::" and "magic::" */
for (pos = 0; pos < 9; pos++) for (pos = 0; pos < 9; pos++)
@ -85,8 +85,8 @@ char *(table[]); /* Pointer to an array of string pointers
/* search for match */ /* search for match */
for(pos=0; table[pos]!=NULL; pos++) for(pos=0; table[pos]!=NULL; pos++)
{ {
char *tabc = table[pos]; const char *tabc = table[pos];
char *strc = &(str[ststart]); const char *strc = &(str[ststart]);
while(*strc!='\0' && *tabc!=' ' && while(*strc!='\0' && *tabc!=' ' &&
((*tabc==*strc) || ((*tabc==*strc) ||
(isupper(*tabc) && islower(*strc) && (tolower(*tabc)== *strc))|| (isupper(*tabc) && islower(*strc) && (tolower(*tabc)== *strc))||
@ -136,7 +136,7 @@ char *(table[]); /* Pointer to an array of string pointers
* *
* struct * struct
* { * {
* char *string; * const char *string;
* ... rest of structure * ... rest of structure
* }; * };
* *
@ -164,19 +164,21 @@ char *(table[]); /* Pointer to an array of string pointers
*/ */
int int
LookupStruct(str, table, size) LookupStruct(str, table_start, size)
char str[]; /* Pointer to a string to be looked up */ const char *str; /* Pointer to a string to be looked up */
char **table; /* Pointer to an array of structs containing string const LookupTable *table_start;
/* Pointer to an array of structs containing string
* pointers to valid commands. * pointers to valid commands.
* The last table entry should have a NULL * The last table entry should have a NULL
* string pointer. * string pointer.
*/ */
int size; /* The size, in bytes, of each table entry */ int size; /* The size, in bytes, of each table entry */
{ {
const char * const *table = (const char * const *)table_start;
int match = -2; /* result, initialized to -2 = no match */ int match = -2; /* result, initialized to -2 = no match */
char **entry; const char * const *entry;
int pos; int pos;
char *tabc , *strc ; const char *tabc , *strc ;
/* search for match */ /* search for match */
for (entry = table, pos = 0; *entry != NULL; ) for (entry = table, pos = 0; *entry != NULL; )
@ -217,7 +219,7 @@ LookupStruct(str, table, size)
} }
} }
pos++; pos++;
entry = (char **)((long)entry + (long) size); entry = (const char **)((long)entry + (long) size);
} }
return(match); return(match);
} }

View File

@ -47,9 +47,9 @@ static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/
int int
LookupAny(c, table) LookupAny(c, table)
char c; char c;
char **table; const char * const *table;
{ {
char **tp; const char * const *tp;
for (tp = table; *tp; tp++) for (tp = table; *tp; tp++)
if (strchr(*tp, c) != (char *) 0) if (strchr(*tp, c) != (char *) 0)

View File

@ -50,10 +50,10 @@ static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/
int int
LookupFull(name, table) LookupFull(name, table)
char *name; const char *name;
char **table; const char * const *table;
{ {
char **tp; const char * const *tp;
for (tp = table; *tp; tp++) for (tp = table; *tp; tp++)
{ {
@ -61,7 +61,7 @@ LookupFull(name, table)
return (tp - table); return (tp - table);
else else
{ {
char *sptr, *tptr; const char *sptr, *tptr;
for (sptr = name, tptr = *tp; ((*sptr != '\0') && (*tptr != '\0')); for (sptr = name, tptr = *tp; ((*sptr != '\0') && (*tptr != '\0'));
sptr++, tptr++) sptr++, tptr++)
if (toupper(*sptr) != toupper(*tptr)) if (toupper(*sptr) != toupper(*tptr))
@ -84,7 +84,7 @@ LookupFull(name, table)
* *
* struct * struct
* { * {
* char *string; * const char *string;
* ... rest of structure * ... rest of structure
* }; * };
* *
@ -107,22 +107,23 @@ LookupFull(name, table)
int int
LookupStructFull(str, table, size) LookupStructFull(str, table, size)
char str[]; /* Pointer to a string to be looked up */ const char *str; /* Pointer to a string to be looked up */
char **table; /* Pointer to an array of structs containing string const char * const *table;
/* Pointer to an array of structs containing string
* pointers to valid commands. * pointers to valid commands.
* The last table entry should have a NULL * The last table entry should have a NULL
* string pointer. * string pointer.
*/ */
int size; /* The size, in bytes, of each table entry */ int size; /* The size, in bytes, of each table entry */
{ {
char **entry; const char * const *entry;
int pos; int pos;
for(entry=table, pos=0; *entry!=NULL; pos++) { for(entry=table, pos=0; *entry!=NULL; pos++) {
if( strcmp(str, *entry) == 0 ) { if( strcmp(str, *entry) == 0 ) {
return pos; return pos;
} }
entry = (char **)((long)entry + (long)size); entry = (const char * const *)((long)entry + (long)size);
} }
return -1; return -1;

View File

@ -34,18 +34,18 @@
*/ */
typedef struct typedef struct
{ {
char *d_str; const char *d_str;
} LookupTable; } LookupTable;
/* The following stuff just defines the global routines provided /* The following stuff just defines the global routines provided
* by files other than hash and stack and geometry. * by files other than hash and stack and geometry.
*/ */
extern int Lookup(); extern int Lookup(const char *str, const char * const *table);
extern int LookupAny(char, char **); extern int LookupAny(char, const char * const *);
extern int LookupFull(char *, char **); extern int LookupFull(const char *, const char * const *);
extern int LookupStruct(); extern int LookupStruct(const char *str, const LookupTable *table_start, int size);
extern int LookupStructFull(); extern int LookupStructFull(const char *str, const char * const *table, int size);
extern int PaExpand(char **, char **, int); extern int PaExpand(char **, char **, int);
extern char *nextName(); extern char *nextName();
extern FILE *PaOpen(char *, char *, char *, char *, char *, char **); extern FILE *PaOpen(char *, char *, char *, char *, char *, char **);