/contrib/tcsh/tw.help.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 209 lines · 126 code · 24 blank · 59 comment · 41 complexity · caa59bc1a17deffc68241e5f39ec5a3a MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/tw.help.c,v 3.27 2006/08/24 20:56:31 christos Exp $ */
  2. /* tw.help.c: actually look up and print documentation on a file.
  3. * Look down the path for an appropriate file, then print it.
  4. * Note that the printing is NOT PAGED. This is because the
  5. * function is NOT meant to look at manual pages, it only does so
  6. * if there is no .help file to look in.
  7. */
  8. /*-
  9. * Copyright (c) 1980, 1991 The Regents of the University of California.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #include "sh.h"
  37. RCSID("$tcsh: tw.help.c,v 3.27 2006/08/24 20:56:31 christos Exp $")
  38. #include "tw.h"
  39. #include "tc.h"
  40. static int f = -1;
  41. static void cleanf (int);
  42. static Char *skipslist (Char *);
  43. static void nextslist (const Char *, Char *);
  44. static const char *const h_ext[] = {
  45. ".help", ".1", ".8", ".6", "", NULL
  46. };
  47. void
  48. do_help(const Char *command)
  49. {
  50. Char *name, *cmd_p;
  51. /* trim off the whitespace at the beginning */
  52. while (*command == ' ' || *command == '\t')
  53. command++;
  54. /* copy the string to a safe place */
  55. name = Strsave(command);
  56. cleanup_push(name, xfree);
  57. /* trim off the whitespace that may be at the end */
  58. for (cmd_p = name;
  59. *cmd_p != ' ' && *cmd_p != '\t' && *cmd_p != '\0'; cmd_p++)
  60. continue;
  61. *cmd_p = '\0';
  62. /* if nothing left, return */
  63. if (*name == '\0') {
  64. cleanup_until(name);
  65. return;
  66. }
  67. if (adrof1(STRhelpcommand, &aliases)) { /* if we have an alias */
  68. jmp_buf_t osetexit;
  69. size_t omark;
  70. getexit(osetexit); /* make sure to come back here */
  71. omark = cleanup_push_mark();
  72. if (setexit() == 0)
  73. aliasrun(2, STRhelpcommand, name); /* then use it. */
  74. cleanup_pop_mark(omark);
  75. resexit(osetexit); /* and finish up */
  76. }
  77. else { /* else cat something to them */
  78. Char *thpath, *hpath; /* The environment parameter */
  79. Char *curdir; /* Current directory being looked at */
  80. struct Strbuf full = Strbuf_INIT;
  81. /* got is, now "cat" the file based on the path $HPATH */
  82. hpath = str2short(getenv(SEARCHLIST));
  83. if (hpath == NULL)
  84. hpath = str2short(DEFAULTLIST);
  85. thpath = hpath = Strsave(hpath);
  86. cleanup_push(thpath, xfree);
  87. curdir = xmalloc((Strlen(thpath) + 1) * sizeof (*curdir));
  88. cleanup_push(curdir, xfree);
  89. cleanup_push(&full, Strbuf_cleanup);
  90. for (;;) {
  91. const char *const *sp;
  92. size_t ep;
  93. if (!*hpath) {
  94. xprintf(CGETS(29, 1, "No help file for %S\n"), name);
  95. break;
  96. }
  97. nextslist(hpath, curdir);
  98. hpath = skipslist(hpath);
  99. /*
  100. * now make the full path name - try first /bar/foo.help, then
  101. * /bar/foo.1, /bar/foo.8, then finally /bar/foo.6. This is so
  102. * that you don't spit a binary at the tty when $HPATH == $PATH.
  103. */
  104. full.len = 0;
  105. Strbuf_append(&full, curdir);
  106. Strbuf_append(&full, STRslash);
  107. Strbuf_append(&full, name);
  108. ep = full.len;
  109. for (sp = h_ext; *sp; sp++) {
  110. full.len = ep;
  111. Strbuf_append(&full, str2short(*sp));
  112. Strbuf_terminate(&full);
  113. if ((f = xopen(short2str(full.s), O_RDONLY|O_LARGEFILE)) != -1)
  114. break;
  115. }
  116. if (f != -1) {
  117. unsigned char buf[512];
  118. sigset_t oset, set;
  119. struct sigaction osa, sa;
  120. ssize_t len;
  121. /* so cat it to the terminal */
  122. cleanup_push(&f, open_cleanup);
  123. sa.sa_handler = cleanf;
  124. sigemptyset(&sa.sa_mask);
  125. sa.sa_flags = 0;
  126. (void)sigaction(SIGINT, &sa, &osa);
  127. cleanup_push(&osa, sigint_cleanup);
  128. (void)sigprocmask(SIG_UNBLOCK, &set, &oset);
  129. cleanup_push(&oset, sigprocmask_cleanup);
  130. while ((len = xread(f, buf, sizeof(buf))) > 0)
  131. (void) xwrite(SHOUT, buf, len);
  132. cleanup_until(&f);
  133. #ifdef convex
  134. /* print error in case file is migrated */
  135. if (len == -1)
  136. stderror(ERR_SYSTEM, progname, strerror(errno));
  137. #endif /* convex */
  138. break;
  139. }
  140. }
  141. }
  142. cleanup_until(name);
  143. }
  144. static void
  145. /*ARGSUSED*/
  146. cleanf(int snum)
  147. {
  148. USE(snum);
  149. if (f != -1)
  150. xclose(f);
  151. f = -1;
  152. }
  153. /* these next two are stolen from CMU's man(1) command for looking down
  154. * paths. they are prety straight forward. */
  155. /*
  156. * nextslist takes a search list and copies the next path in it
  157. * to np. A null search list entry is expanded to ".".
  158. * If there are no entries in the search list, then np will point
  159. * to a null string.
  160. */
  161. static void
  162. nextslist(const Char *sl, Char *np)
  163. {
  164. if (!*sl)
  165. *np = '\000';
  166. else if (*sl == ':') {
  167. *np++ = '.';
  168. *np = '\000';
  169. }
  170. else {
  171. while (*sl && *sl != ':')
  172. *np++ = *sl++;
  173. *np = '\000';
  174. }
  175. }
  176. /*
  177. * skipslist returns the pointer to the next entry in the search list.
  178. */
  179. static Char *
  180. skipslist(Char *sl)
  181. {
  182. while (*sl && *sl++ != ':')
  183. continue;
  184. return (sl);
  185. }