man.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* mini man implementation for busybox
  2. * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
  3. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  4. */
  5. #include "libbb.h"
  6. enum {
  7. OPT_a = 1, /* all */
  8. OPT_w = 2, /* print path */
  9. };
  10. /* This is what I see on my desktop system deing executed:
  11. (
  12. echo ".ll 12.4i"
  13. echo ".nr LL 12.4i"
  14. echo ".pl 1100i"
  15. gunzip -c '/usr/man/man1/bzip2.1.gz'
  16. echo ".\\\""
  17. echo ".pl \n(nlu+10"
  18. ) | gtbl | nroff -Tlatin1 -mandoc | less
  19. */
  20. static int run_pipe(const char *unpacker, const char *pager, char *man_filename)
  21. {
  22. char *cmd;
  23. if (access(man_filename, R_OK) != 0)
  24. return 0;
  25. if (option_mask32 & OPT_w) {
  26. puts(man_filename);
  27. return 1;
  28. }
  29. cmd = xasprintf("%s '%s' | gtbl | nroff -Tlatin1 -mandoc | %s",
  30. unpacker, man_filename, pager);
  31. system(cmd);
  32. free(cmd);
  33. return 1;
  34. }
  35. /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
  36. static int show_manpage(const char *pager, char *man_filename)
  37. {
  38. int len;
  39. if (run_pipe("bunzip2 -c", pager, man_filename))
  40. return 1;
  41. len = strlen(man_filename) - 1;
  42. man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
  43. man_filename[len - 2] = 'g';
  44. if (run_pipe("gunzip -c", pager, man_filename))
  45. return 1;
  46. man_filename[len - 3] = '\0'; /* ".gz" -> "" */
  47. if (run_pipe("cat", pager, man_filename))
  48. return 1;
  49. return 0;
  50. }
  51. int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  52. int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
  53. {
  54. FILE *cf;
  55. const char *pager;
  56. char **man_path_list;
  57. char *sec_list;
  58. char *cur_path, *cur_sect;
  59. char *line, *value;
  60. int count_mp, cur_mp;
  61. int opt;
  62. opt_complementary = "-1"; /* at least one argument */
  63. opt = getopt32(argv, "+aw");
  64. argv += optind;
  65. sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
  66. /* Last valid man_path_list[] is [0x10] */
  67. man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
  68. count_mp = 0;
  69. man_path_list[0] = xstrdup(getenv("MANPATH"));
  70. if (man_path_list[0])
  71. count_mp++;
  72. pager = getenv("MANPAGER");
  73. if (!pager) {
  74. pager = getenv("PAGER");
  75. if (!pager)
  76. pager = "more";
  77. }
  78. /* Parse man.conf */
  79. cf = fopen_or_warn("/etc/man.conf", "r");
  80. if (cf) {
  81. /* go through man configuration file and search relevant paths, sections */
  82. while ((line = xmalloc_fgetline(cf)) != NULL) {
  83. trim(line); /* remove whitespace at the beginning/end */
  84. if (isspace(line[7])) {
  85. line[7] = '\0';
  86. value = skip_whitespace(&line[8]);
  87. *skip_non_whitespace(value) = '\0';
  88. if (strcmp("MANPATH", line) == 0) {
  89. man_path_list[count_mp] = xstrdup(value);
  90. count_mp++;
  91. /* man_path_list is NULL terminated */
  92. man_path_list[count_mp] = NULL;
  93. if (!(count_mp & 0xf)) { /* 0x10, 0x20 etc */
  94. /* so that last valid man_path_list[] is [count_mp + 0x10] */
  95. man_path_list = xrealloc(man_path_list,
  96. (count_mp + 0x11) * sizeof(man_path_list[0]));
  97. }
  98. }
  99. if (strcmp("MANSECT", line) == 0) {
  100. free(sec_list);
  101. sec_list = xstrdup(value);
  102. }
  103. }
  104. free(line);
  105. }
  106. fclose(cf);
  107. }
  108. do { /* for each argv[] */
  109. cur_mp = 0;
  110. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  111. /* for each MANPATH */
  112. do { /* for each MANPATH item */
  113. char *next_path = strchrnul(cur_path, ':');
  114. int path_len = next_path - cur_path;
  115. cur_sect = sec_list;
  116. do { /* for each section */
  117. char *next_sect = strchrnul(cur_sect, ':');
  118. int sect_len = next_sect - cur_sect;
  119. char *man_filename = xasprintf("%.*s/man%.*s/%s.%.*s" ".bz2",
  120. path_len, cur_path,
  121. sect_len, cur_sect,
  122. *argv,
  123. sect_len, cur_sect);
  124. int found = show_manpage(pager, man_filename);
  125. free(man_filename);
  126. if (found && !(opt & OPT_a))
  127. goto next_arg;
  128. cur_sect = next_sect;
  129. while (*cur_sect == ':')
  130. cur_sect++;
  131. } while (*cur_sect);
  132. cur_path = next_path;
  133. while (*cur_path == ':')
  134. cur_path++;
  135. } while (*cur_path);
  136. }
  137. next_arg:
  138. argv++;
  139. } while (*argv);
  140. return EXIT_SUCCESS;
  141. }