⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaults.c

📁 操作系统SunOS 4.1.3版本的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * node_lookup_name(name, multiple, status) will lookup Name in the defaults * database and return the associated node.  If Path_Name is not in the * database, NULL will be returned. */Nodenode_lookup_name(name, multiple, status)	register Name	name;		/* Name to lookup */	Bool		multiple;	/* TRUE => multiple lookups */	int		*status;	/* Status flag */{	register Node	node;		/* Resultant node */	char		path_name[MAX_STRING];	/* Path name string */	Symbol		temp[MAX_NAME];	/* Temporay name */	if (defaults == NULL)		defaults_init(True);	if (status != NULL){		(void)name_unparse(name, path_name);		warn("Status non-NULL in %s", path_name);	}	if (defaults->test_mode)		return NULL;	read_lazy(name);	if (multiple && (defaults->prefix != NULL)){		temp[0] = NULL;		name_cat(temp, defaults->prefix);		name_cat(temp, name);		node = (Node)hash_lookup(defaults->nodes, temp);		if ((node != NULL) && (!node->deleted))			return node;	}	node = (Node)hash_lookup(defaults->nodes, name);	if ((node != NULL) && (!node->deleted))		return node;	else	return NULL;}/* * node_lookup_path(path_name, multiple, status) will lookup Path_name in the * defaults database and return the associated node.  If Path_Name is not in * the database, NULL will be returned. */Nodenode_lookup_path(path_name, multiple, status)	char	*path_name;	/* Full path name */	Bool	multiple;	/* TRUE => multiple lookups */	int	*status;	/* Status flag */{	Symbol	name[MAX_NAME];	/* Node name */	if (defaults == NULL)		defaults_init(True);	if (strcmp(path_name, SEP_STR) == 0)		return defaults->root;	name_quick_parse(path_name, name);	return node_lookup_name(name, multiple, status);}/* * path_lookup(path_name, status) will lookup Path_Name in the defaults * database and return the associated node.  If Path_Name is not in the * database, a warning message will be printed on the console and NULL will * be returned. */Node	path_lookup(path_name, status)	char		*path_name;	/* Full name */	int		*status;	/* Status flag */{	register Node	node;		/* Resultant node */	node = node_lookup_path(path_name, True, status);	if (node == NULL && defaults->database_dir)		warn("'%s' is not in defaults database", path_name);	return node;}/* * read(file_name, prefix, private) will read in data into the defaults * database from the file File_Name.  Only path names that begin with Prefix * will be read into the database.  If Private is True, the file being read * corresponds to a private database file.  True will be returned if File_Name * could not be opened.  Otherwise, False will be returned. */static Boolread(file_name, prefix, private)	char		*file_name;	/* File name to read */	Name		prefix;		/* Prefix path that must match first */	Bool		private;	/* TRUE => mark nodes as private */{	register int	chr;		/* Temporary character */	Symbol		default_name[MAX_NAME];/* Default name */	Bool		error;		/* TRUE => error has occured */	char		huge[HUGE_STRING]; /* Huge string for W. Teitelman */	register FILE	*in_file;	/* Input file name */	register int	line;		/* Current line number */	Symbol		name[MAX_NAME];	/* Full name of node */	register Name	name_pointer;	/* Pointer into name */	register Node	node;		/* Node associated with name */	register Name	prefix_pointer;	/* Pointer into prefix */	Hash		symbols;	/* String table */	char		temp[MAX_STRING];/* Temporary symbol */	char		*value;		/* Node value */	int		version;	/* Version number */	in_file = fopen(file_name, "r");	if (in_file == NULL)		return True;	line = 1;	/* Make sure the version number is correct. */	error = False;	if (parse_symbol(in_file, temp) == NULL)		error = True;	else {	version = parse_int(in_file, &error);		(void)parse_eol(in_file);	}	if (error || !symbol_equal(temp, VERSION_TAG)){		warn("Line 1 in %s is not a correctly formatted defaults file",		    file_name);		(void)fclose(in_file);		return False;	}	if (version != VERSION_NUMBER){		warn("Line 1 in %s has version number %d rather than %d",			file_name, version, VERSION_NUMBER);		(void)fclose(in_file);		return False;	}	if (defaults == NULL)		defaults_init(True);	symbols = defaults->symbols;	default_name[0] = NULL;	while (True){		/* Dispatch on first printing character. */		chr = parse_whitespace(in_file);		if ((chr == '\n') || (chr == ';')){			(void)parse_eol(in_file);			line++;			continue;		}		if (chr == EOF)			break;		/* Grab the name */		if (name_parse(in_file, symbols, default_name, name) == 0){			warn("Could not parse a node name on line %d from %s",			    line, file_name);			(void)parse_eol(in_file);			line++;			continue;		}		/* See whether name matches prefix. */		name_pointer = name;		prefix_pointer = prefix;		while (*prefix_pointer == *name_pointer){			prefix_pointer++;			name_pointer++;		}		if (*prefix_pointer != NULL){			/* Prefix does not match. */			(void)parse_eol(in_file);			line++;			continue;		}		/* Prefix matches, so look up node and enter value. */		node = node_find_name(name, False, (int *)NULL);		if (node == NULL){			/* This really should not happen. */			(void)name_unparse(name, temp);			warn("Problem entering '%s' into database on line %d",			    temp, line);			(void)parse_eol(in_file);			line++;			break;		}		chr = parse_whitespace(in_file);		value = NULL;		if (chr == '\n')			value = DEFAULTS_UNDEFINED;		else if (chr == '"'){			value = parse_string(in_file, huge, HUGE_STRING);			if (value != NULL)				value = defaults_strdup(huge);		}		if (value == NULL){			(void)name_unparse(name, temp);			warn("%s Could not be assigned a value", temp);			value = "";		}		if (private){			node->private = True;			node->value = value;		} else {			node->default_value = value;			if (!node->private)				node->value = value;		}		(void)parse_eol(in_file);		line++;	}	(void)fclose(in_file);	return False;}/* * read_lazy(name) will cause all the nodes associated with Name to be read * in, if they have not already been read in. */static voidread_lazy(name)	Name		name;		/* Name to use */{	register Node	node;		/* Temporary node */	Symbol		temp_name[2];	/* Temporary name */	temp_name[0] = name[0];	temp_name[1] = NULL;	node = (Node)hash_lookup(defaults->nodes, temp_name);	if ((node != NULL) && (node->file))		read_master(name[0]);}/* * read_master(file_name) will read in File_Name from the master database. */voidread_master(file_name)	char		*file_name;		/* File name to read in */{	register Node	node;			/* Database node */	char		name[MAX_STRING];	/* Temporary file name */	Symbol		prefix[2];		/* Empty prefix */	if (defaults->database_dir == NULL)		return;	prefix[0] = symbol_lookup(file_name);	prefix[1] = NULL;	node = (Node)hash_lookup(defaults->nodes, prefix);	if (node != NULL)		node->file = False;	/* Read from private database. */	if (defaults->private_dir != NULL){		(void)strcpy(name, defaults->private_dir);		(void)strcat(name, file_name);		(void)strcat(name, ".d");		prefix[0] = NULL;		if (!read(name, prefix, False))			return;	}	/* Read from master database. */	(void)strcpy(name, defaults->database_dir);	(void)strcat(name, file_name);	(void)strcat(name, ".d");	prefix[0] = NULL;	if (read(name, prefix, False))		warn("Could not open database file '%s'", name);}/* * read_master_database() will find the master database directory and enter * nodes into the database for each file in the master database directory. */voidread_master_database(){	char	*database_dir;		/* Database directory name */	Node	node;			/* Current node */	char	*private_dir;		/* Private database directory */	/* Find the defaults directory. */	database_dir = defaults->database_dir;	if (database_dir == NULL){		node = node_lookup_path("/Defaults/Directory", True, (int *)NULL);		if (node == NULL)			database_dir = "/usr/lib/defaults/";		else 	database_dir = slash_append(node->value);		defaults->database_dir = database_dir;	}	read_master_database1(database_dir);	/* Find the private defaults directory. */	private_dir = defaults->private_dir;	if (private_dir == NULL){		node = node_lookup_path("/Defaults/Private_Directory",								True, (int *)NULL);		if (node == NULL)			private_dir = NULL;		else	private_dir = slash_append(node->value);		defaults->private_dir = private_dir;	}	if (private_dir != NULL)		read_master_database1(private_dir);}/* * read_master_database1(dir_name) will read in the *.d file names contained * in the Dir_Name directory. */static voidread_master_database1(dir_name)	char			*dir_name;	/* Directory name */{	register DIR		*dir;		/* Default directory */	register struct direct	*dir_entry;	/* Directory entry */	Symbol			name[MAX_NAME];	/* Name to create */	register Node		node;		/* Current node */	register char		*pointer;	/* Pointer into file name */	int			size;		/* Size of string */	char			symbol[MAX_STRING];  /* Temporary symbol */	dir = opendir(dir_name);	if (dir == NULL){		warn("Could not find defaults database directory %s\n", dir_name);		warn("Suppressing subsequent error messages.\n");		defaults->errors = 1000000;	/* A big number */		return;	}		/* Scan the database for all the top level nodes. */	name[1] = NULL;	while (True){		dir_entry = readdir(dir);		if (dir_entry == NULL)			break;		pointer = dir_entry->d_name;		size = strlen(pointer);		if ((size < 3) || (strcmp(&pointer[size - 2], ".d") != 0))			continue;		(void)strcpy(symbol, pointer);		symbol[size - 2] = '\0';		name[0] = symbol_lookup(symbol);		node = node_lookup_name(name, False, (int *)NULL);		if (node == NULL)			node = node_find_name(name, False, (int *)NULL);		if (node == NULL)			warn("Could not create node '%s'\n");		else	node->file = True;	}	closedir(dir);}/* * read_private_database() will read in env var DEFAULTS_FILE */voidread_private_database(){	char	file_name[MAX_STRING];	/* Local defaults file name */	Symbol	prefix[1];		/* Empty prefix */	char    *temp;	file_name[0] = '0';	if ((temp = getenv("DEFAULTS_FILE")) != NULL)  {		(void)strcpy(file_name, temp);	}   else   {		if ((temp = getlogindir()) != NULL);			(void)strcpy(file_name, temp);		(void)strcat(file_name, "/.defaults");	}	prefix[0] = NULL;	read(file_name, prefix, True);}/* * slash_append(text) will return make sure that Text has a slash ('/') * on the end. */char *slash_append(text)	register char	*text;		/* String to append to */{	char temp[MAX_STRING];		/* Maximum string length */	if (text[strlen(text) - 1] == '/')		return text;	(void)strcpy(temp, text);	(void)strcat(temp, "/");	return symbol_copy(temp);}/* * symbol_copy(symbol) will make and return a copy of Symbol. */char *symbol_copy(symbol)	char	 *symbol;		/* Symbol to copy */{	register char *new;		/* New symbol */	new = hash_get_memory(strlen(symbol) + 1);	(void)strcpy(new, symbol);	return new;}/* * symbol_equal(symbol1, symbol2) will return True if Symbol1 equals Symbol2. */Boolsymbol_equal(symbol1, symbol2)	register char	*symbol1;	/* First symbol */	register char	*symbol2;	/* Second symbol */{	register char	chr1;		/* Character from first symbol */	register char	chr2;		/* Character from second symbol */	while (True){		chr1 = *symbol1++;		if (('A' <= chr1) && (chr1 <= 'Z'))			chr1 += 'a' - 'A';		chr2 = *symbol2++;		if (('A' <= chr2) && (chr2 <= 'Z'))			chr2 += 'a' - 'A';		if (chr1 != chr2)			return False;		if (chr1 == '\0')			return True;	}}/* * symbol_hash(symbol) will return a hash of Symbol. */static intsymbol_hash(symbol)	Symbol	symbol;		/* Symbol to hash */{	register char	chr;		/* Character from symbol */	register int	hash;		/* Hash value */	hash = 0;	do {	chr = *symbol++;		if (('A' <= chr) && (chr <= 'Z'))			chr += 'a' - 'A';		hash += chr;	} while (chr != '\0');	return hash;}/* * symbol_lookup(symbol) will return the conical value for Symbol from * the symbols hash table. */Symbolsymbol_lookup(symbol)	Symbol		symbol;		/* Symbol to lookup */{	register Symbol	new;		/* New symbol */	if (defaults == NULL)		defaults_init(True);	new = (char *)hash_lookup(defaults->symbols, (caddr_t)symbol);	if (new == NULL){		new = symbol_copy(symbol);		(void)hash_insert(defaults->symbols, (caddr_t)new, (caddr_t)new);	}	return new;}/* * This is here only because this file seemed a convenient place * to move some data into.  The data came from defaults_put.c, * which we want to compile using the `-R' switch.  Therefore, * that file cannot contain even the tiniest smidgen of data. */Defaults_pairs warn_error_action[] = {        "Continue", 1,        "Abort", 2,        "Suppress", 3,        NULL, 1,};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -