deref.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * deref.c
  3. *
  4. * Make all texinfo references into the one argument form.
  5. *
  6. * Arnold Robbins
  7. * Written: December, 1991
  8. * Released: November, 1998
  9. *
  10. * Copyright 1991, 1998 Arnold David Robbins
  11. *
  12. * DEREF is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * DEREF is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /*
  26. * LIMITATIONS:
  27. * One texinfo cross reference per line.
  28. * Cross references may not cross newlines.
  29. * Use of fgets for input (to be fixed).
  30. */
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. /* for gcc on the 3B1, delete if this gives you grief */
  35. extern int fclose(FILE *fp);
  36. extern int fprintf(FILE *fp, const char *str, ...);
  37. /* extern int sprintf(char *str, const char *fmt, ...); */
  38. extern int fputs(char *buf, FILE *fp);
  39. extern char *strerror(int errno);
  40. extern char *strchr(char *cp, int ch);
  41. extern int strncmp(const char *s1, const char *s2, int count);
  42. extern int errno;
  43. void process(FILE *fp);
  44. void repair(char *line, char *ref, int toffset);
  45. int Errs = 0;
  46. char *Name = "stdin";
  47. int Line = 0;
  48. char *Me;
  49. /* main --- handle arguments, global vars for errors */
  50. int
  51. main(int argc, char **argv)
  52. {
  53. FILE *fp;
  54. Me = argv[0];
  55. if (argc == 1)
  56. process(stdin);
  57. else
  58. for (argc--, argv++; *argv != NULL; argc--, argv++) {
  59. if (argv[0][0] == '-' && argv[0][1] == '\0') {
  60. Name = "stdin";
  61. Line = 0;
  62. process(stdin);
  63. } else if ((fp = fopen(*argv, "r")) != NULL) {
  64. Name = *argv;
  65. Line = 0;
  66. process(fp);
  67. fclose(fp);
  68. } else {
  69. fprintf(stderr, "%s: can not open: %s\n",
  70. *argv, strerror(errno));
  71. Errs++;
  72. }
  73. }
  74. return Errs != 0;
  75. }
  76. /* isref --- decide if we've seen a texinfo cross reference */
  77. int
  78. isref(char *cp)
  79. {
  80. if (strncmp(cp, "@ref{", 5) == 0)
  81. return 5;
  82. if (strncmp(cp, "@xref{", 6) == 0)
  83. return 6;
  84. if (strncmp(cp, "@pxref{", 7) == 0)
  85. return 7;
  86. return 0;
  87. }
  88. /* process --- read files, look for references, fix them up */
  89. void
  90. process(FILE *fp)
  91. {
  92. char buf[BUFSIZ];
  93. char *cp;
  94. int count;
  95. while (fgets(buf, sizeof buf, fp) != NULL) {
  96. Line++;
  97. cp = strchr(buf, '@');
  98. if (cp == NULL) {
  99. fputs(buf, stdout);
  100. continue;
  101. }
  102. do {
  103. count = isref(cp);
  104. if (count == 0) {
  105. cp++;
  106. cp = strchr(cp, '@');
  107. if (cp == NULL) {
  108. fputs(buf, stdout);
  109. goto next;
  110. }
  111. continue;
  112. }
  113. /* got one */
  114. repair(buf, cp, count);
  115. break;
  116. } while (cp != NULL);
  117. next: ;
  118. }
  119. }
  120. /* repair --- turn all texinfo cross references into the one argument form */
  121. void
  122. repair(char *line, char *ref, int toffset)
  123. {
  124. int braces = 1; /* have seen first left brace */
  125. char *cp;
  126. ref += toffset;
  127. /* output line up to and including left brace in reference */
  128. for (cp = line; cp <= ref; cp++)
  129. putchar(*cp);
  130. /* output node name */
  131. for (; *cp && *cp != '}' && *cp != ',' && *cp != '\n'; cp++)
  132. putchar(*cp);
  133. if (*cp != '}') { /* could have been one arg xref */
  134. /* skip to matching right brace */
  135. for (; braces > 0; cp++) {
  136. switch (*cp) {
  137. case '@':
  138. cp++; /* blindly skip next character */
  139. break;
  140. case '{':
  141. braces++;
  142. break;
  143. case '}':
  144. braces--;
  145. break;
  146. case '\n':
  147. case '\0':
  148. Errs++;
  149. fprintf(stderr,
  150. "%s: %s: %d: mismatched braces\n",
  151. Me, Name, Line);
  152. goto out;
  153. default:
  154. break;
  155. }
  156. }
  157. out:
  158. ;
  159. }
  160. putchar('}');
  161. if (*cp == '}')
  162. cp++;
  163. /* now the rest of the line */
  164. for (; *cp; cp++)
  165. putchar(*cp);
  166. return;
  167. }
  168. /* strerror --- return error string, delete if in your library */
  169. char *
  170. strerror(int errno)
  171. {
  172. static char buf[100];
  173. extern int sys_nerr;
  174. extern char *sys_errlist[];
  175. if (errno < sys_nerr && errno >= 0)
  176. return sys_errlist[errno];
  177. sprintf(buf, "unknown error %d", errno);
  178. return buf;
  179. }