ack/util/cmisc/cid.c

206 lines
3.1 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1986-10-20 14:42:41 +00:00
/* Change IDentifiers occurring in C programs outside comment, strings
and character constants.
-Dname=text : replace all occerences of name by text
-Dname : same as -Dname=
-Ffile : read sentences of the from name=text from file
Author: Erik Baalbergen
Date: Oct 23, 1985
*/
#include <stdio.h>
#include <string.h>
2019-05-10 17:14:01 +00:00
#include <stdlib.h>
1986-10-20 14:42:41 +00:00
#ifndef DEF_LENGTH
#define DEF_LENGTH 8
#endif
#define LINE_LEN 1024
#define HASHSIZE 257
struct idf {
struct idf *id_next;
char *id_name;
char *id_text;
};
struct idf *hash_tab[HASHSIZE];
2019-05-10 17:14:01 +00:00
char *Malloc(unsigned int);
char *Salloc(char *);
int EnHash(char*);
void EndOfProgram(void);
void DoOption(char*);
void CheckId(char *, int);
void DoMacro(char *);
void GetMacros(char *);
void InsertMacro(char *, char *);
struct idf *FindId(char *);
1986-10-20 14:42:41 +00:00
1986-11-11 13:44:13 +00:00
extern char *ProgName;
2019-05-10 17:14:01 +00:00
void DoOption(char* str)
1986-10-20 14:42:41 +00:00
{
switch (str[1]) {
case 'D':
DoMacro(&str[2]);
break;
case 'F':
GetMacros(&str[2]);
break;
default:
fprintf(stderr, "%s: bad option \"%s\"\n", ProgName, str);
break;
}
}
1988-10-07 10:26:37 +00:00
/*ARGSUSED*/
2019-05-10 17:14:01 +00:00
void CheckId(char *id, int len)
1986-10-20 14:42:41 +00:00
{
struct idf *idp = FindId(id);
if (idp) {
printf("%s", idp->id_text);
}
else {
printf("%s", id);
}
}
2019-05-10 17:14:01 +00:00
void DoMacro(char *str)
1986-10-20 14:42:41 +00:00
{
char *id, *text;
id = str++;
while (*str != '\0' && *str != '=') {
str++;
}
if (*str == '=') {
*str++ = '\0';
text = str;
}
else {
text = "";
}
InsertMacro(id, text);
}
2019-05-10 17:14:01 +00:00
void GetMacros(char *fn)
1986-10-20 14:42:41 +00:00
{
FILE *fp;
2019-05-10 17:14:01 +00:00
register int c;
1986-10-20 14:42:41 +00:00
char buf[LINE_LEN];
char *bufp = &buf[0];
if ((fp = fopen(fn, "r")) == NULL) {
fprintf(stderr, "%s: cannot read file \"%s\"\n", ProgName, fn);
1988-10-07 10:26:37 +00:00
return;
1986-10-20 14:42:41 +00:00
}
while ((c = getc(fp)) != EOF) {
if (c == '\n' && bufp != &buf[0]) {
*bufp = '\0';
DoMacro(&buf[0]);
bufp = &buf[0];
}
else {
*bufp++ = c;
}
}
fclose(fp);
}
2019-05-10 17:14:01 +00:00
void InsertMacro(char *id, char *text)
1986-10-20 14:42:41 +00:00
{
int hash_val = EnHash(id);
struct idf *idp = hash_tab[hash_val];
while (idp) {
if (strcmp(idp->id_name, id) == 0) {
fprintf(stderr, "%s: (warning) redefinition of %s\n",
ProgName, id);
break;
}
idp = idp->id_next;
}
if (idp == 0) {
idp = (struct idf *) Malloc(sizeof(struct idf));
}
idp->id_next = hash_tab[hash_val];
idp->id_name = Salloc(id);
idp->id_text = Salloc(text);
hash_tab[hash_val] = idp;
}
2019-05-10 17:14:01 +00:00
char *Malloc(unsigned int n)
1986-10-20 14:42:41 +00:00
{
2019-05-10 17:14:01 +00:00
char *mem;
1986-10-20 14:42:41 +00:00
if ((mem = malloc(n)) == 0) {
fprintf(stderr, "%s: out of memory\n", ProgName);
exit(1);
}
return mem;
}
2019-05-10 17:14:01 +00:00
char *Salloc(char *str)
1986-10-20 14:42:41 +00:00
{
if (str == 0) {
str = "";
}
1988-10-07 10:26:37 +00:00
return strcpy(Malloc((unsigned)strlen(str) + 1), str);
1986-10-20 14:42:41 +00:00
}
2019-05-10 17:14:01 +00:00
struct idf *FindId(char *id)
1986-10-20 14:42:41 +00:00
{
2019-05-10 17:14:01 +00:00
register int hash_val = EnHash(id);
1986-10-20 14:42:41 +00:00
register struct idf *idp = hash_tab[hash_val];
while (idp) {
if (strcmp(idp->id_name, id) == 0) {
return idp;
}
idp = idp->id_next;
}
return 0;
}
2019-05-10 17:14:01 +00:00
int EnHash(char *id)
1986-10-20 14:42:41 +00:00
{
register unsigned hash_val = 0;
while (*id) {
hash_val = 31 * hash_val + *id++;
}
return hash_val % (unsigned) HASHSIZE;
}
1986-11-11 13:44:13 +00:00
extern int GCcopy;
2019-05-10 17:14:01 +00:00
void BeginOfProgram(void)
1986-11-11 13:44:13 +00:00
{
GCcopy = 1;
}
2019-05-10 17:14:01 +00:00
void EndOfProgram(void)
1986-11-11 13:44:13 +00:00
{
}