/release/picobsd/tinyware/help/help.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 157 lines · 119 code · 10 blank · 28 comment · 36 complexity · c248637285b9f0ccae7c103aa7a06017 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1998 Eric P. Scott <eps@sirius.com>
  3. * Copyright (c) 1998 Andrzej Bialecki <abial@freebsd.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ar.h>
  32. #include <sys/ioctl.h>
  33. int display(FILE *, const char *);
  34. static int cnt, crt=-1;
  35. int
  36. main(int argc, char *argv[])
  37. {
  38. register int i, s;
  39. FILE *fd;
  40. struct ttysize ts;
  41. if (!(fd=fopen("/help.a", "r"))) {
  42. (void)fputs("Couldn't open help archive.\n", stderr);
  43. exit(1);
  44. }
  45. cnt=0;
  46. if (ioctl(fileno(stdout), TIOCGWINSZ, &ts)>=0) {
  47. crt=ts.ts_lines-1;
  48. }
  49. if (crt<3) crt=23;
  50. s=display(fd, argc>1 ? argv[1] : "help");
  51. if (s<0) s=0;
  52. else for (i=2;i<argc;) {
  53. rewind(fd);
  54. s|=display(fd, argv[i++]);
  55. if (s<0) {
  56. s=0;
  57. break;
  58. }
  59. }
  60. (void)fclose(fd);
  61. exit(s);
  62. }
  63. int
  64. more(void)
  65. {
  66. char buf[8];
  67. (void)fflush(stdout);
  68. (void)fputs("\033[7mPress Enter to continue\033[m", stderr);
  69. (void)fflush(stderr);
  70. cnt=0;
  71. if (fgets(buf, sizeof buf, stdin)) return 0;
  72. (void)fputc('\n', stderr);
  73. return 1;
  74. }
  75. int
  76. display(FILE *fd, const char *fname)
  77. {
  78. register char *p;
  79. register int c, n, o;
  80. struct ar_hdr ar;
  81. char aname[20];
  82. if (!fgets(aname, sizeof aname, fd)) {
  83. return 1;
  84. }
  85. if (strncmp(aname, ARMAG, SARMAG)) return 1;
  86. (void)snprintf(aname, sizeof(aname), "%s/", fname);
  87. for (;;) {
  88. if (fread((void *)&ar, sizeof ar, 1, fd)!=1) return 1;
  89. if (strncmp(ar.ar_fmag, ARFMAG, 2)) return 1;
  90. n=0;
  91. p=ar.ar_size;
  92. do {
  93. if ((c=(int)(*p++-'0'))<0||c>9) break;
  94. n*=10; n+=c;
  95. } while (p<&ar.ar_size[sizeof ar.ar_size]);
  96. if (!strncmp(ar.ar_name, aname, strlen(aname))) break;
  97. if (fseek(fd, (long)n, SEEK_CUR)<0) return 1;
  98. if ((n&1)&&fgetc(fd)!='\n') return 1;
  99. }
  100. if (cnt>=crt&&more()) return -1;
  101. (void)fputc('\n', stdout);
  102. cnt++;
  103. o=0; while (o<n&&(c=fgetc(fd))!=EOF) {
  104. per:
  105. o++;
  106. (void)fputc(c, stdout);
  107. if (c!='\n') continue;
  108. if (++cnt<crt) continue;
  109. if (o>=n||(c=fgetc(fd))==EOF) break;
  110. if (more()) return -1;
  111. goto per;
  112. }
  113. if (cnt>=crt&&more()) return -1;
  114. (void)fputc('\n', stdout);
  115. cnt++;
  116. if (!strcmp(fname, "help")) {
  117. rewind(fd);
  118. (void)fgets(aname, sizeof aname, fd);
  119. if (cnt>=crt&&more()) return -1;
  120. (void)fputs("The following help items are available:\n",
  121. stdout);
  122. cnt++;
  123. o=0;
  124. while (fread((void *)&ar, sizeof ar, 1, fd)==1) {
  125. if (strncmp(ar.ar_fmag, ARFMAG, 2)) break;
  126. if ((o%6)==0) {
  127. (void)fputc('\n', stdout);
  128. if (++cnt>=crt&&more()) return -1;
  129. }
  130. *(index(ar.ar_name,'/'))=' ';
  131. (void)printf("%.13s", ar.ar_name);
  132. ++o;
  133. n=0;
  134. p=ar.ar_size;
  135. do {
  136. if ((c=(int)(*p++-'0'))<0||c>9) break;
  137. n*=10; n+=c;
  138. } while (p<&ar.ar_size[sizeof ar.ar_size]);
  139. if (fseek(fd, (long)n, SEEK_CUR)<0) break;
  140. if ((n&1)&&fgetc(fd)!='\n') break;
  141. }
  142. if (cnt>=crt&&more()) return -1;
  143. (void)fputc('\n', stdout);
  144. cnt++;
  145. }
  146. return 0;
  147. }