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