1*5594Srrh static char *sccsid = "@(#)subr.c 1.2 (Berkeley) 01/22/82"; 21459Sroot #include <stdio.h> 31459Sroot #include <ctype.h> 41459Sroot #include "error.h" 51459Sroot /* 6*5594Srrh * Arrayify a list of rules 71459Sroot */ 81459Sroot arrayify(e_length, e_array, header) 9*5594Srrh int *e_length; 10*5594Srrh Eptr **e_array; 11*5594Srrh Eptr header; 121459Sroot { 13*5594Srrh reg Eptr errorp; 14*5594Srrh reg Eptr *array; 15*5594Srrh reg int listlength; 16*5594Srrh reg int listindex; 171459Sroot 181459Sroot for (errorp = header, listlength = 0; 191459Sroot errorp; errorp = errorp->error_next, listlength++) 201459Sroot continue; 21*5594Srrh array = (Eptr*)Calloc(listlength+1, sizeof (Eptr)); 221459Sroot for(listindex = 0, errorp = header; 231459Sroot listindex < listlength; 241459Sroot listindex++, errorp = errorp->error_next){ 251459Sroot array[listindex] = errorp; 261459Sroot errorp->error_position = listindex; 271459Sroot } 28*5594Srrh array[listindex] = (Eptr)0; 291459Sroot *e_length = listlength; 301459Sroot *e_array = array; 311459Sroot } 321459Sroot 331459Sroot /*VARARGS1*/ 341459Sroot error(msg, a1, a2, a3) 351459Sroot char *msg; 361459Sroot { 371459Sroot fprintf(stderr, "Error: "); 381459Sroot fprintf(stderr, msg, a1, a2, a3); 391459Sroot fprintf(stderr, "\n"); 401459Sroot fflush(stdout); 411459Sroot fflush(stderr); 421459Sroot exit(6); 431459Sroot } 441459Sroot /*ARGSUSED*/ 451459Sroot char *Calloc(nelements, size) 461459Sroot int nelements; 471459Sroot int size; 481459Sroot { 491459Sroot char *back; 501459Sroot if ( (back = (char *)calloc(nelements, size)) == (char *)NULL){ 511459Sroot error("Ran out of memory.\n"); 521459Sroot exit(1); 531459Sroot } 541459Sroot return(back); 551459Sroot } 561459Sroot 571459Sroot char *strsave(instring) 581459Sroot char *instring; 591459Sroot { 601459Sroot char *outstring; 61*5594Srrh (void)strcpy(outstring = (char *)Calloc(1, strlen(instring) + 1), 62*5594Srrh instring); 631459Sroot return(outstring); 641459Sroot } 651459Sroot /* 661459Sroot * find the position of a given character in a string 671459Sroot * (one based) 681459Sroot */ 691459Sroot int position(string, ch) 70*5594Srrh reg char *string; 71*5594Srrh reg char ch; 721459Sroot { 73*5594Srrh reg int i; 741459Sroot for (i=1; *string; string++, i++){ 751459Sroot if (*string == ch) 761459Sroot return(i); 771459Sroot } 781459Sroot return(-1); 791459Sroot } 801459Sroot /* 811459Sroot * clobber the first occurance of ch in string by the new character 821459Sroot */ 831459Sroot char *substitute(string, chold, chnew) 841459Sroot char *string; 851459Sroot char chold, chnew; 861459Sroot { 87*5594Srrh reg char *cp = string; 881459Sroot 891459Sroot while (*cp){ 901459Sroot if (*cp == chold){ 911459Sroot *cp = chnew; 921459Sroot break; 931459Sroot } 941459Sroot cp++; 951459Sroot } 961459Sroot return(string); 971459Sroot } 981459Sroot 991459Sroot char lastchar(string) 1001459Sroot char *string; 1011459Sroot { 1021459Sroot int length; 1031459Sroot length = strlen(string); 1041459Sroot if (length >= 1) 1051459Sroot return(string[length-1]); 1061459Sroot else 1071459Sroot return('\0'); 1081459Sroot } 1091459Sroot 1101459Sroot char firstchar(string) 1111459Sroot char *string; 1121459Sroot { 1131459Sroot return(string[0]); 1141459Sroot } 1151459Sroot 1161459Sroot char next_lastchar(string) 1171459Sroot char *string; 1181459Sroot { 1191459Sroot int length; 1201459Sroot length = strlen(string); 1211459Sroot if (length >= 2) 1221459Sroot return(string[length - 2]); 1231459Sroot else 1241459Sroot return('\0'); 1251459Sroot } 1261459Sroot 1271459Sroot clob_last(string, newstuff) 1281459Sroot char *string, newstuff; 1291459Sroot { 1301459Sroot int length; 1311459Sroot length = strlen(string); 1321459Sroot if (length >= 1) 1331459Sroot string[length - 1] = newstuff; 1341459Sroot } 1351459Sroot 1361459Sroot /* 1371459Sroot * parse a string that is the result of a format %s(%d) 1381459Sroot * return TRUE if this is of the proper format 1391459Sroot */ 1401459Sroot boolean persperdexplode(string, r_perd, r_pers) 1411459Sroot char *string; 1421459Sroot char **r_perd, **r_pers; 1431459Sroot { 144*5594Srrh reg char *cp; 145*5594Srrh int length; 1461459Sroot 1471459Sroot length = strlen(string); 1481459Sroot if ( (length >= 4) 1491459Sroot && (string[length - 1] == ')' ) ){ 1501459Sroot for (cp = &string[length - 2]; 1511459Sroot (isdigit(*cp)) && (*cp != '('); 1521459Sroot --cp) 1531459Sroot continue; 1541459Sroot if (*cp == '('){ 1551459Sroot string[length - 1] = '\0'; /* clobber the ) */ 1561459Sroot *r_perd = strsave(cp+1); 1571459Sroot string[length - 1] = ')'; 1581459Sroot *cp = '\0'; /* clobber the ( */ 1591459Sroot *r_pers = strsave(string); 1601459Sroot *cp = '('; 1611459Sroot return(TRUE); 1621459Sroot } 1631459Sroot } 1641459Sroot return(FALSE); 1651459Sroot } 1661459Sroot /* 1671459Sroot * parse a quoted string that is the result of a format \"%s\"(%d) 1681459Sroot * return TRUE if this is of the proper format 1691459Sroot */ 1701459Sroot boolean qpersperdexplode(string, r_perd, r_pers) 1711459Sroot char *string; 1721459Sroot char **r_perd, **r_pers; 1731459Sroot { 174*5594Srrh reg char *cp; 175*5594Srrh int length; 1761459Sroot 1771459Sroot length = strlen(string); 1781459Sroot if ( (length >= 4) 1791459Sroot && (string[length - 1] == ')' ) ){ 1801459Sroot for (cp = &string[length - 2]; 1811459Sroot (isdigit(*cp)) && (*cp != '('); 1821459Sroot --cp) 1831459Sroot continue; 1841459Sroot if (*cp == '(' && *(cp - 1) == '"'){ 1851459Sroot string[length - 1] = '\0'; 1861459Sroot *r_perd = strsave(cp+1); 1871459Sroot string[length - 1] = ')'; 1881459Sroot *(cp - 1) = '\0'; /* clobber the " */ 1891459Sroot *r_pers = strsave(string + 1); 1901459Sroot *(cp - 1) = '"'; 1911459Sroot return(TRUE); 1921459Sroot } 1931459Sroot } 1941459Sroot return(FALSE); 1951459Sroot } 1961459Sroot 1971459Sroot static char cincomment[] = CINCOMMENT; 1981459Sroot static char coutcomment[] = COUTCOMMENT; 1991459Sroot static char fincomment[] = FINCOMMENT; 2001459Sroot static char foutcomment[] = FOUTCOMMENT; 2011459Sroot static char newline[] = NEWLINE; 2021459Sroot static char piincomment[] = PIINCOMMENT; 2031459Sroot static char pioutcomment[] = PIOUTCOMMENT; 2041459Sroot static char lispincomment[] = LISPINCOMMENT; 2051459Sroot static char riincomment[] = RIINCOMMENT; 2061459Sroot static char rioutcomment[] = RIOUTCOMMENT; 2071459Sroot 2081459Sroot struct lang_desc lang_table[] = { 2091459Sroot /*INUNKNOWN 0*/ "unknown", cincomment, coutcomment, 2101459Sroot /*INCPP 1*/ "cpp", cincomment, coutcomment, 2111459Sroot /*INCC 2*/ "cc", cincomment, coutcomment, 2121459Sroot /*INAS 3*/ "as", ASINCOMMENT, newline, 2131459Sroot /*INLD 4*/ "ld", cincomment, coutcomment, 2141459Sroot /*INLINT 5*/ "lint", cincomment, coutcomment, 2151459Sroot /*INF77 6*/ "f77", fincomment, foutcomment, 2161459Sroot /*INPI 7*/ "pi", piincomment, pioutcomment, 2171459Sroot /*INPC 8*/ "pc", piincomment, pioutcomment, 2181459Sroot /*INFRANZ 9*/ "franz",lispincomment, newline, 2191459Sroot /*INLISP 10*/ "lisp", lispincomment, newline, 2201459Sroot /*INVAXIMA 11*/ "vaxima",lispincomment,newline, 2211459Sroot /*INRATFOR 12*/ "ratfor",fincomment, foutcomment, 2221459Sroot /*INLEX 13*/ "lex", cincomment, coutcomment, 2231459Sroot /*INYACC 14*/ "yacc", cincomment, coutcomment, 2241459Sroot /*INAPL 15*/ "apl", ".lm", newline, 2251459Sroot /*INMAKE 16*/ "make", ASINCOMMENT, newline, 2261459Sroot /*INRI 17*/ "ri", riincomment, rioutcomment, 2271459Sroot 0, 0, 0 2281459Sroot }; 2291459Sroot 2301459Sroot printerrors(look_at_subclass, errorc, errorv) 2311459Sroot boolean look_at_subclass; 2321459Sroot int errorc; 233*5594Srrh Eptr errorv[]; 2341459Sroot { 235*5594Srrh reg int i; 236*5594Srrh reg Eptr errorp; 237*5594Srrh 2381459Sroot for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){ 2391459Sroot if (errorp->error_e_class == C_IGNORE) 2401459Sroot continue; 2411459Sroot if (look_at_subclass && errorp->error_s_class == C_DUPL) 2421459Sroot continue; 2431459Sroot printf("Error %d, (%s error) [%s], text = \"", 2441459Sroot i, 2451459Sroot class_table[errorp->error_e_class], 2461459Sroot lang_table[errorp->error_language].lang_name); 2471459Sroot wordvprint(stdout,errorp->error_lgtext,errorp->error_text); 2481459Sroot printf("\"\n"); 2491459Sroot } 2501459Sroot } 2511459Sroot 2521459Sroot wordvprint(fyle, wordc, wordv) 2531459Sroot FILE *fyle; 2541459Sroot int wordc; 2551459Sroot char *wordv[]; 2561459Sroot { 2571459Sroot int i; 2581459Sroot for(i = 0; i < wordc; i++){ 2591459Sroot fprintf(fyle, "%s",wordv[i]); 2601459Sroot if (i != wordc - 1) 2611459Sroot fprintf(fyle, " "); 2621459Sroot } 2631459Sroot } 2641459Sroot 2651459Sroot /* 2661459Sroot * Given a string, parse it into a number of words, and build 2671459Sroot * a wordc wordv combination pointing into it. 2681459Sroot */ 2691459Sroot wordvbuild(string, r_wordc, r_wordv) 2701459Sroot char *string; 2711459Sroot int *r_wordc; 2721459Sroot char ***r_wordv; 2731459Sroot { 274*5594Srrh reg char *cp; 275*5594Srrh char *saltedbuffer; 276*5594Srrh char **wordv; 277*5594Srrh int wordcount; 278*5594Srrh int wordindex; 2791459Sroot 2801459Sroot saltedbuffer = strsave(string); 2811459Sroot for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){ 2821459Sroot while (*cp && isspace(*cp)) 2831459Sroot cp++; 2841459Sroot if (*cp == 0) 2851459Sroot break; 2861459Sroot while (!isspace(*cp)) 2871459Sroot cp++; 2881459Sroot } 2891459Sroot wordv = (char **)Calloc(wordcount + 1, sizeof (char *)); 2901459Sroot for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){ 2911459Sroot while (*cp && isspace(*cp)) 2921459Sroot cp++; 2931459Sroot if (*cp == 0) 2941459Sroot break; 2951459Sroot wordv[wordindex] = cp; 2961459Sroot while(!isspace(*cp)) 2971459Sroot cp++; 2981459Sroot *cp++ = '\0'; 2991459Sroot } 3001459Sroot if (wordcount != 0) 3011459Sroot error("Initial miscount of the number of words in a line\n"); 3021459Sroot wordv[wordindex] = (char *)0; 3031459Sroot #ifdef FULLDEBUG 3041459Sroot for (wordcount = 0; wordcount < wordindex; wordcount++) 3051459Sroot printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]); 3061459Sroot printf("\n"); 3071459Sroot #endif 3081459Sroot *r_wordc = wordindex; 3091459Sroot *r_wordv = wordv; 3101459Sroot } 3111459Sroot /* 3121459Sroot * Compare two 0 based wordvectors 3131459Sroot */ 3141459Sroot int wordvcmp(wordv1, wordc, wordv2) 3151459Sroot char **wordv1; 3161459Sroot int wordc; 3171459Sroot char **wordv2; 3181459Sroot { 319*5594Srrh reg int i; 320*5594Srrh int back; 3211459Sroot for (i = 0; i < wordc; i++){ 3221459Sroot if (back = strcmp(wordv1[i], wordv2[i])){ 3231459Sroot return(back); 3241459Sroot } 3251459Sroot } 3261459Sroot return(0); /* they are equal */ 3271459Sroot } 3281459Sroot 3291459Sroot /* 3301459Sroot * splice a 0 basedword vector onto the tail of a 3311459Sroot * new wordv, allowing the first emptyhead slots to be empty 3321459Sroot */ 3331459Sroot char **wordvsplice(emptyhead, wordc, wordv) 3341459Sroot int emptyhead; 3351459Sroot int wordc; 3361459Sroot char **wordv; 3371459Sroot { 338*5594Srrh reg char **nwordv; 339*5594Srrh int nwordc = emptyhead + wordc; 340*5594Srrh reg int i; 3411459Sroot 3421459Sroot nwordv = (char **)Calloc(nwordc, sizeof (char *)); 3431459Sroot for (i = 0; i < emptyhead; i++) 3441459Sroot nwordv[i] = 0; 3451459Sroot for(i = emptyhead; i < nwordc; i++){ 3461459Sroot nwordv[i] = wordv[i-emptyhead]; 3471459Sroot } 3481459Sroot return(nwordv); 3491459Sroot } 350*5594Srrh /* 351*5594Srrh * plural'ize and verb forms 352*5594Srrh */ 353*5594Srrh static char *S = "s"; 354*5594Srrh static char *N = ""; 355*5594Srrh char *plural(n) 356*5594Srrh int n; 357*5594Srrh { 358*5594Srrh return( n > 1 ? S : N); 359*5594Srrh } 360*5594Srrh char *verbform(n) 361*5594Srrh int n; 362*5594Srrh { 363*5594Srrh return( n > 1 ? N : S); 364*5594Srrh } 365*5594Srrh 366