help <- function (topic, data, package = .library(), lib.loc = .lib.loc)
{
  FUN <- function(thing, type.dir, type.name, suffix) {
    file <- system.file(type.dir, paste(thing, suffix, sep = ""))
    if (file == "")
      stop(paste("no documentation for ", type.name, " `", thing, "'",
		 sep = ""))
    else
      system(paste("${RHOME}/cmd/pager", file))
  }
  if (!missing(topic)) {
    topic <- substitute(topic)
    if (is.character(topic) || is.name(topic)) {
      if (!is.character(topic)) 
	topic <- deparse(topic)
      if (!is.na(match(topic, c("+", "-", "*", "/", "^", "%%")))) 
	topic <- "Arithmetic"
      else if (!is.na(match(topic, c("<", ">", "<=", ">=", "==", "!=")))) 
	topic <- "Comparison"
      else if (!is.na(match(topic, c("[", "[[", "$"))))
	topic <- "Extract"
      else if (!is.na(match(topic, c("&", "&&", "|", "||", "!")))) 
	topic <- "Logic"
      system(paste("RLIBS=\"", paste(lib.loc, collapse = " "), "\";",
		   "export RLIBS;", "${RHOME}/cmd/help", topic,
		   paste(package, collapse = " ")))
    } else {
      topic <- as.character(topic)
      CHOICES <- c("data", "package", "library")
      type <- CHOICES[pmatch(as.character(topic[1]), CHOICES)]
      if (length(type) > 1 || is.na(type) || length(topic) == 1)
	stop("unimplemented help feature")
      else
	switch(type,
	       data = FUN(topic[2], "data", "data set", ".doc"),
	       package = FUN(topic[2], "help", "package", "/INDEX"),
	       library = library(lib = eval(parse(text = topic[2]))))
    }
  }
  else if (!missing(data))
    FUN(as.character(substitute(data)), "data", "data set", ".doc")
  else if (!missing(package))
    FUN(as.character(substitute(package)), "help", "package", "/INDEX")
  else if (!missing(lib.loc))
    library(lib = lib.loc)
  else
    system("RLIBS=${RHOME}; ${RHOME}/cmd/help help base")
}
