1 static char *sccsid = "@(#)subr.c 1.4 (Berkeley) 06/14/83"; 2 #include <stdio.h> 3 #include <ctype.h> 4 #include "error.h" 5 /* 6 * Arrayify a list of rules 7 */ 8 arrayify(e_length, e_array, header) 9 int *e_length; 10 Eptr **e_array; 11 Eptr header; 12 { 13 reg Eptr errorp; 14 reg Eptr *array; 15 reg int listlength; 16 reg int listindex; 17 18 for (errorp = header, listlength = 0; 19 errorp; errorp = errorp->error_next, listlength++) 20 continue; 21 array = (Eptr*)Calloc(listlength+1, sizeof (Eptr)); 22 for(listindex = 0, errorp = header; 23 listindex < listlength; 24 listindex++, errorp = errorp->error_next){ 25 array[listindex] = errorp; 26 errorp->error_position = listindex; 27 } 28 array[listindex] = (Eptr)0; 29 *e_length = listlength; 30 *e_array = array; 31 } 32 33 /*VARARGS1*/ 34 error(msg, a1, a2, a3) 35 char *msg; 36 { 37 fprintf(stderr, "Error: "); 38 fprintf(stderr, msg, a1, a2, a3); 39 fprintf(stderr, "\n"); 40 fflush(stdout); 41 fflush(stderr); 42 exit(6); 43 } 44 /*ARGSUSED*/ 45 char *Calloc(nelements, size) 46 int nelements; 47 int size; 48 { 49 char *back; 50 if ( (back = (char *)calloc(nelements, size)) == (char *)NULL){ 51 error("Ran out of memory.\n"); 52 exit(1); 53 } 54 return(back); 55 } 56 57 char *strsave(instring) 58 char *instring; 59 { 60 char *outstring; 61 (void)strcpy(outstring = (char *)Calloc(1, strlen(instring) + 1), 62 instring); 63 return(outstring); 64 } 65 /* 66 * find the position of a given character in a string 67 * (one based) 68 */ 69 int position(string, ch) 70 reg char *string; 71 reg char ch; 72 { 73 reg int i; 74 if (string) 75 for (i=1; *string; string++, i++){ 76 if (*string == ch) 77 return(i); 78 } 79 return(-1); 80 } 81 /* 82 * clobber the first occurance of ch in string by the new character 83 */ 84 char *substitute(string, chold, chnew) 85 char *string; 86 char chold, chnew; 87 { 88 reg char *cp = string; 89 90 if (cp) 91 while (*cp){ 92 if (*cp == chold){ 93 *cp = chnew; 94 break; 95 } 96 cp++; 97 } 98 return(string); 99 } 100 101 char lastchar(string) 102 char *string; 103 { 104 int length; 105 if (string == 0) return('\0'); 106 length = strlen(string); 107 if (length >= 1) 108 return(string[length-1]); 109 else 110 return('\0'); 111 } 112 113 char firstchar(string) 114 char *string; 115 { 116 if (string) 117 return(string[0]); 118 else 119 return('\0'); 120 } 121 122 char next_lastchar(string) 123 char *string; 124 { 125 int length; 126 if (string == 0) return('\0'); 127 length = strlen(string); 128 if (length >= 2) 129 return(string[length - 2]); 130 else 131 return('\0'); 132 } 133 134 clob_last(string, newstuff) 135 char *string, newstuff; 136 { 137 int length = 0; 138 if (string) 139 length = strlen(string); 140 if (length >= 1) 141 string[length - 1] = newstuff; 142 } 143 144 /* 145 * parse a string that is the result of a format %s(%d) 146 * return TRUE if this is of the proper format 147 */ 148 boolean persperdexplode(string, r_perd, r_pers) 149 char *string; 150 char **r_perd, **r_pers; 151 { 152 reg char *cp; 153 int length = 0; 154 155 if (string) 156 length = strlen(string); 157 if ( (length >= 4) 158 && (string[length - 1] == ')' ) ){ 159 for (cp = &string[length - 2]; 160 (isdigit(*cp)) && (*cp != '('); 161 --cp) 162 continue; 163 if (*cp == '('){ 164 string[length - 1] = '\0'; /* clobber the ) */ 165 *r_perd = strsave(cp+1); 166 string[length - 1] = ')'; 167 *cp = '\0'; /* clobber the ( */ 168 *r_pers = strsave(string); 169 *cp = '('; 170 return(TRUE); 171 } 172 } 173 return(FALSE); 174 } 175 /* 176 * parse a quoted string that is the result of a format \"%s\"(%d) 177 * return TRUE if this is of the proper format 178 */ 179 boolean qpersperdexplode(string, r_perd, r_pers) 180 char *string; 181 char **r_perd, **r_pers; 182 { 183 reg char *cp; 184 int length = 0; 185 186 if (string) 187 length = strlen(string); 188 if ( (length >= 4) 189 && (string[length - 1] == ')' ) ){ 190 for (cp = &string[length - 2]; 191 (isdigit(*cp)) && (*cp != '('); 192 --cp) 193 continue; 194 if (*cp == '(' && *(cp - 1) == '"'){ 195 string[length - 1] = '\0'; 196 *r_perd = strsave(cp+1); 197 string[length - 1] = ')'; 198 *(cp - 1) = '\0'; /* clobber the " */ 199 *r_pers = strsave(string + 1); 200 *(cp - 1) = '"'; 201 return(TRUE); 202 } 203 } 204 return(FALSE); 205 } 206 207 static char cincomment[] = CINCOMMENT; 208 static char coutcomment[] = COUTCOMMENT; 209 static char fincomment[] = FINCOMMENT; 210 static char foutcomment[] = FOUTCOMMENT; 211 static char newline[] = NEWLINE; 212 static char piincomment[] = PIINCOMMENT; 213 static char pioutcomment[] = PIOUTCOMMENT; 214 static char lispincomment[] = LISPINCOMMENT; 215 static char riincomment[] = RIINCOMMENT; 216 static char rioutcomment[] = RIOUTCOMMENT; 217 static char troffincomment[] = TROFFINCOMMENT; 218 static char troffoutcomment[] = TROFFOUTCOMMENT; 219 220 struct lang_desc lang_table[] = { 221 /*INUNKNOWN 0*/ "unknown", cincomment, coutcomment, 222 /*INCPP 1*/ "cpp", cincomment, coutcomment, 223 /*INCC 2*/ "cc", cincomment, coutcomment, 224 /*INAS 3*/ "as", ASINCOMMENT, newline, 225 /*INLD 4*/ "ld", cincomment, coutcomment, 226 /*INLINT 5*/ "lint", cincomment, coutcomment, 227 /*INF77 6*/ "f77", fincomment, foutcomment, 228 /*INPI 7*/ "pi", piincomment, pioutcomment, 229 /*INPC 8*/ "pc", piincomment, pioutcomment, 230 /*INFRANZ 9*/ "franz",lispincomment, newline, 231 /*INLISP 10*/ "lisp", lispincomment, newline, 232 /*INVAXIMA 11*/ "vaxima",lispincomment,newline, 233 /*INRATFOR 12*/ "ratfor",fincomment, foutcomment, 234 /*INLEX 13*/ "lex", cincomment, coutcomment, 235 /*INYACC 14*/ "yacc", cincomment, coutcomment, 236 /*INAPL 15*/ "apl", ".lm", newline, 237 /*INMAKE 16*/ "make", ASINCOMMENT, newline, 238 /*INRI 17*/ "ri", riincomment, rioutcomment, 239 /*INTROFF 18*/ "troff",troffincomment,troffoutcomment, 240 0, 0, 0 241 }; 242 243 printerrors(look_at_subclass, errorc, errorv) 244 boolean look_at_subclass; 245 int errorc; 246 Eptr errorv[]; 247 { 248 reg int i; 249 reg Eptr errorp; 250 251 for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){ 252 if (errorp->error_e_class == C_IGNORE) 253 continue; 254 if (look_at_subclass && errorp->error_s_class == C_DUPL) 255 continue; 256 printf("Error %d, (%s error) [%s], text = \"", 257 i, 258 class_table[errorp->error_e_class], 259 lang_table[errorp->error_language].lang_name); 260 wordvprint(stdout,errorp->error_lgtext,errorp->error_text); 261 printf("\"\n"); 262 } 263 } 264 265 wordvprint(fyle, wordc, wordv) 266 FILE *fyle; 267 int wordc; 268 char *wordv[]; 269 { 270 int i; 271 for(i = 0; i < wordc; i++){ 272 fprintf(fyle, "%s",wordv[i]); 273 if (i != wordc - 1) 274 fprintf(fyle, " "); 275 } 276 } 277 278 /* 279 * Given a string, parse it into a number of words, and build 280 * a wordc wordv combination pointing into it. 281 */ 282 wordvbuild(string, r_wordc, r_wordv) 283 char *string; 284 int *r_wordc; 285 char ***r_wordv; 286 { 287 reg char *cp; 288 char *saltedbuffer; 289 char **wordv; 290 int wordcount; 291 int wordindex; 292 293 saltedbuffer = strsave(string); 294 for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){ 295 while (*cp && isspace(*cp)) 296 cp++; 297 if (*cp == 0) 298 break; 299 while (!isspace(*cp)) 300 cp++; 301 } 302 wordv = (char **)Calloc(wordcount + 1, sizeof (char *)); 303 for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){ 304 while (*cp && isspace(*cp)) 305 cp++; 306 if (*cp == 0) 307 break; 308 wordv[wordindex] = cp; 309 while(!isspace(*cp)) 310 cp++; 311 *cp++ = '\0'; 312 } 313 if (wordcount != 0) 314 error("Initial miscount of the number of words in a line\n"); 315 wordv[wordindex] = (char *)0; 316 #ifdef FULLDEBUG 317 for (wordcount = 0; wordcount < wordindex; wordcount++) 318 printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]); 319 printf("\n"); 320 #endif 321 *r_wordc = wordindex; 322 *r_wordv = wordv; 323 } 324 /* 325 * Compare two 0 based wordvectors 326 */ 327 int wordvcmp(wordv1, wordc, wordv2) 328 char **wordv1; 329 int wordc; 330 char **wordv2; 331 { 332 reg int i; 333 int back; 334 for (i = 0; i < wordc; i++){ 335 if (wordv1[i] == 0 || wordv2[i] == 0) 336 return(-1); 337 if (back = strcmp(wordv1[i], wordv2[i])){ 338 return(back); 339 } 340 } 341 return(0); /* they are equal */ 342 } 343 344 /* 345 * splice a 0 basedword vector onto the tail of a 346 * new wordv, allowing the first emptyhead slots to be empty 347 */ 348 char **wordvsplice(emptyhead, wordc, wordv) 349 int emptyhead; 350 int wordc; 351 char **wordv; 352 { 353 reg char **nwordv; 354 int nwordc = emptyhead + wordc; 355 reg int i; 356 357 nwordv = (char **)Calloc(nwordc, sizeof (char *)); 358 for (i = 0; i < emptyhead; i++) 359 nwordv[i] = 0; 360 for(i = emptyhead; i < nwordc; i++){ 361 nwordv[i] = wordv[i-emptyhead]; 362 } 363 return(nwordv); 364 } 365 /* 366 * plural'ize and verb forms 367 */ 368 static char *S = "s"; 369 static char *N = ""; 370 char *plural(n) 371 int n; 372 { 373 return( n > 1 ? S : N); 374 } 375 char *verbform(n) 376 int n; 377 { 378 return( n > 1 ? N : S); 379 } 380 381