1*17499Srrh static char *sccsid = "@(#)subr.c 1.6 (Berkeley) 12/11/84"; 21459Sroot #include <stdio.h> 31459Sroot #include <ctype.h> 41459Sroot #include "error.h" 51459Sroot /* 65594Srrh * Arrayify a list of rules 71459Sroot */ 81459Sroot arrayify(e_length, e_array, header) 95594Srrh int *e_length; 105594Srrh Eptr **e_array; 115594Srrh Eptr header; 121459Sroot { 135594Srrh reg Eptr errorp; 145594Srrh reg Eptr *array; 155594Srrh reg int listlength; 165594Srrh reg int listindex; 171459Sroot 181459Sroot for (errorp = header, listlength = 0; 191459Sroot errorp; errorp = errorp->error_next, listlength++) 201459Sroot continue; 215594Srrh 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 } 285594Srrh 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; 615594Srrh (void)strcpy(outstring = (char *)Calloc(1, strlen(instring) + 1), 625594Srrh 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) 705594Srrh reg char *string; 715594Srrh reg char ch; 721459Sroot { 735594Srrh reg int i; 7410830Ssam if (string) 751459Sroot for (i=1; *string; string++, i++){ 761459Sroot if (*string == ch) 771459Sroot return(i); 781459Sroot } 791459Sroot return(-1); 801459Sroot } 811459Sroot /* 821459Sroot * clobber the first occurance of ch in string by the new character 831459Sroot */ 841459Sroot char *substitute(string, chold, chnew) 851459Sroot char *string; 861459Sroot char chold, chnew; 871459Sroot { 885594Srrh reg char *cp = string; 891459Sroot 9010830Ssam if (cp) 911459Sroot while (*cp){ 921459Sroot if (*cp == chold){ 931459Sroot *cp = chnew; 941459Sroot break; 951459Sroot } 961459Sroot cp++; 971459Sroot } 981459Sroot return(string); 991459Sroot } 1001459Sroot 1011459Sroot char lastchar(string) 1021459Sroot char *string; 1031459Sroot { 1041459Sroot int length; 10510830Ssam if (string == 0) return('\0'); 1061459Sroot length = strlen(string); 1071459Sroot if (length >= 1) 1081459Sroot return(string[length-1]); 1091459Sroot else 1101459Sroot return('\0'); 1111459Sroot } 1121459Sroot 1131459Sroot char firstchar(string) 1141459Sroot char *string; 1151459Sroot { 11610830Ssam if (string) 11710830Ssam return(string[0]); 11810830Ssam else 11910830Ssam return('\0'); 1201459Sroot } 1211459Sroot 1221459Sroot char next_lastchar(string) 1231459Sroot char *string; 1241459Sroot { 1251459Sroot int length; 12610830Ssam if (string == 0) return('\0'); 1271459Sroot length = strlen(string); 1281459Sroot if (length >= 2) 1291459Sroot return(string[length - 2]); 1301459Sroot else 1311459Sroot return('\0'); 1321459Sroot } 1331459Sroot 1341459Sroot clob_last(string, newstuff) 1351459Sroot char *string, newstuff; 1361459Sroot { 13710830Ssam int length = 0; 13810830Ssam if (string) 13910830Ssam length = strlen(string); 1401459Sroot if (length >= 1) 1411459Sroot string[length - 1] = newstuff; 1421459Sroot } 1431459Sroot 1441459Sroot /* 1451459Sroot * parse a string that is the result of a format %s(%d) 1461459Sroot * return TRUE if this is of the proper format 1471459Sroot */ 1481459Sroot boolean persperdexplode(string, r_perd, r_pers) 1491459Sroot char *string; 1501459Sroot char **r_perd, **r_pers; 1511459Sroot { 1525594Srrh reg char *cp; 15310830Ssam int length = 0; 1541459Sroot 15510830Ssam if (string) 15610830Ssam length = strlen(string); 1571459Sroot if ( (length >= 4) 1581459Sroot && (string[length - 1] == ')' ) ){ 1591459Sroot for (cp = &string[length - 2]; 1601459Sroot (isdigit(*cp)) && (*cp != '('); 1611459Sroot --cp) 1621459Sroot continue; 1631459Sroot if (*cp == '('){ 1641459Sroot string[length - 1] = '\0'; /* clobber the ) */ 1651459Sroot *r_perd = strsave(cp+1); 1661459Sroot string[length - 1] = ')'; 1671459Sroot *cp = '\0'; /* clobber the ( */ 1681459Sroot *r_pers = strsave(string); 1691459Sroot *cp = '('; 1701459Sroot return(TRUE); 1711459Sroot } 1721459Sroot } 1731459Sroot return(FALSE); 1741459Sroot } 1751459Sroot /* 1761459Sroot * parse a quoted string that is the result of a format \"%s\"(%d) 1771459Sroot * return TRUE if this is of the proper format 1781459Sroot */ 1791459Sroot boolean qpersperdexplode(string, r_perd, r_pers) 1801459Sroot char *string; 1811459Sroot char **r_perd, **r_pers; 1821459Sroot { 1835594Srrh reg char *cp; 18410830Ssam int length = 0; 1851459Sroot 18610830Ssam if (string) 18710830Ssam length = strlen(string); 1881459Sroot if ( (length >= 4) 1891459Sroot && (string[length - 1] == ')' ) ){ 1901459Sroot for (cp = &string[length - 2]; 1911459Sroot (isdigit(*cp)) && (*cp != '('); 1921459Sroot --cp) 1931459Sroot continue; 1941459Sroot if (*cp == '(' && *(cp - 1) == '"'){ 1951459Sroot string[length - 1] = '\0'; 1961459Sroot *r_perd = strsave(cp+1); 1971459Sroot string[length - 1] = ')'; 1981459Sroot *(cp - 1) = '\0'; /* clobber the " */ 1991459Sroot *r_pers = strsave(string + 1); 2001459Sroot *(cp - 1) = '"'; 2011459Sroot return(TRUE); 2021459Sroot } 2031459Sroot } 2041459Sroot return(FALSE); 2051459Sroot } 2061459Sroot 2071459Sroot static char cincomment[] = CINCOMMENT; 2081459Sroot static char coutcomment[] = COUTCOMMENT; 2091459Sroot static char fincomment[] = FINCOMMENT; 2101459Sroot static char foutcomment[] = FOUTCOMMENT; 2111459Sroot static char newline[] = NEWLINE; 2121459Sroot static char piincomment[] = PIINCOMMENT; 2131459Sroot static char pioutcomment[] = PIOUTCOMMENT; 2141459Sroot static char lispincomment[] = LISPINCOMMENT; 2151459Sroot static char riincomment[] = RIINCOMMENT; 2161459Sroot static char rioutcomment[] = RIOUTCOMMENT; 21713106Srrh static char troffincomment[] = TROFFINCOMMENT; 21813106Srrh static char troffoutcomment[] = TROFFOUTCOMMENT; 219*17499Srrh static char mod2incomment[] = MOD2INCOMMENT; 220*17499Srrh static char mod2outcomment[] = MOD2OUTCOMMENT; 2211459Sroot 2221459Sroot struct lang_desc lang_table[] = { 2231459Sroot /*INUNKNOWN 0*/ "unknown", cincomment, coutcomment, 2241459Sroot /*INCPP 1*/ "cpp", cincomment, coutcomment, 2251459Sroot /*INCC 2*/ "cc", cincomment, coutcomment, 2261459Sroot /*INAS 3*/ "as", ASINCOMMENT, newline, 2271459Sroot /*INLD 4*/ "ld", cincomment, coutcomment, 2281459Sroot /*INLINT 5*/ "lint", cincomment, coutcomment, 2291459Sroot /*INF77 6*/ "f77", fincomment, foutcomment, 2301459Sroot /*INPI 7*/ "pi", piincomment, pioutcomment, 2311459Sroot /*INPC 8*/ "pc", piincomment, pioutcomment, 2321459Sroot /*INFRANZ 9*/ "franz",lispincomment, newline, 2331459Sroot /*INLISP 10*/ "lisp", lispincomment, newline, 2341459Sroot /*INVAXIMA 11*/ "vaxima",lispincomment,newline, 2351459Sroot /*INRATFOR 12*/ "ratfor",fincomment, foutcomment, 2361459Sroot /*INLEX 13*/ "lex", cincomment, coutcomment, 2371459Sroot /*INYACC 14*/ "yacc", cincomment, coutcomment, 2381459Sroot /*INAPL 15*/ "apl", ".lm", newline, 2391459Sroot /*INMAKE 16*/ "make", ASINCOMMENT, newline, 2401459Sroot /*INRI 17*/ "ri", riincomment, rioutcomment, 24113106Srrh /*INTROFF 18*/ "troff",troffincomment,troffoutcomment, 242*17499Srrh /*INMOD2 19*/ "mod2", mod2incomment, mod2outcomment, 2431459Sroot 0, 0, 0 2441459Sroot }; 2451459Sroot 2461459Sroot printerrors(look_at_subclass, errorc, errorv) 2471459Sroot boolean look_at_subclass; 2481459Sroot int errorc; 2495594Srrh Eptr errorv[]; 2501459Sroot { 2515594Srrh reg int i; 2525594Srrh reg Eptr errorp; 2535594Srrh 2541459Sroot for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){ 2551459Sroot if (errorp->error_e_class == C_IGNORE) 2561459Sroot continue; 2571459Sroot if (look_at_subclass && errorp->error_s_class == C_DUPL) 2581459Sroot continue; 2591459Sroot printf("Error %d, (%s error) [%s], text = \"", 2601459Sroot i, 2611459Sroot class_table[errorp->error_e_class], 2621459Sroot lang_table[errorp->error_language].lang_name); 2631459Sroot wordvprint(stdout,errorp->error_lgtext,errorp->error_text); 2641459Sroot printf("\"\n"); 2651459Sroot } 2661459Sroot } 2671459Sroot 2681459Sroot wordvprint(fyle, wordc, wordv) 2691459Sroot FILE *fyle; 2701459Sroot int wordc; 2711459Sroot char *wordv[]; 2721459Sroot { 2731459Sroot int i; 27413539Ssam char *sep = ""; 27513539Ssam 27613539Ssam for(i = 0; i < wordc; i++) 27713539Ssam if (wordv[i]) { 27813539Ssam fprintf(fyle, "%s%s",sep,wordv[i]); 27913539Ssam sep = " "; 28013539Ssam } 2811459Sroot } 2821459Sroot 2831459Sroot /* 2841459Sroot * Given a string, parse it into a number of words, and build 2851459Sroot * a wordc wordv combination pointing into it. 2861459Sroot */ 2871459Sroot wordvbuild(string, r_wordc, r_wordv) 2881459Sroot char *string; 2891459Sroot int *r_wordc; 2901459Sroot char ***r_wordv; 2911459Sroot { 2925594Srrh reg char *cp; 2935594Srrh char *saltedbuffer; 2945594Srrh char **wordv; 2955594Srrh int wordcount; 2965594Srrh int wordindex; 2971459Sroot 2981459Sroot saltedbuffer = strsave(string); 2991459Sroot for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){ 3001459Sroot while (*cp && isspace(*cp)) 3011459Sroot cp++; 3021459Sroot if (*cp == 0) 3031459Sroot break; 3041459Sroot while (!isspace(*cp)) 3051459Sroot cp++; 3061459Sroot } 3071459Sroot wordv = (char **)Calloc(wordcount + 1, sizeof (char *)); 3081459Sroot for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){ 3091459Sroot while (*cp && isspace(*cp)) 3101459Sroot cp++; 3111459Sroot if (*cp == 0) 3121459Sroot break; 3131459Sroot wordv[wordindex] = cp; 3141459Sroot while(!isspace(*cp)) 3151459Sroot cp++; 3161459Sroot *cp++ = '\0'; 3171459Sroot } 3181459Sroot if (wordcount != 0) 3191459Sroot error("Initial miscount of the number of words in a line\n"); 3201459Sroot wordv[wordindex] = (char *)0; 3211459Sroot #ifdef FULLDEBUG 3221459Sroot for (wordcount = 0; wordcount < wordindex; wordcount++) 3231459Sroot printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]); 3241459Sroot printf("\n"); 3251459Sroot #endif 3261459Sroot *r_wordc = wordindex; 3271459Sroot *r_wordv = wordv; 3281459Sroot } 3291459Sroot /* 3301459Sroot * Compare two 0 based wordvectors 3311459Sroot */ 3321459Sroot int wordvcmp(wordv1, wordc, wordv2) 3331459Sroot char **wordv1; 3341459Sroot int wordc; 3351459Sroot char **wordv2; 3361459Sroot { 3375594Srrh reg int i; 3385594Srrh int back; 3391459Sroot for (i = 0; i < wordc; i++){ 34010830Ssam if (wordv1[i] == 0 || wordv2[i] == 0) 34110830Ssam return(-1); 3421459Sroot if (back = strcmp(wordv1[i], wordv2[i])){ 3431459Sroot return(back); 3441459Sroot } 3451459Sroot } 3461459Sroot return(0); /* they are equal */ 3471459Sroot } 3481459Sroot 3491459Sroot /* 3501459Sroot * splice a 0 basedword vector onto the tail of a 3511459Sroot * new wordv, allowing the first emptyhead slots to be empty 3521459Sroot */ 3531459Sroot char **wordvsplice(emptyhead, wordc, wordv) 3541459Sroot int emptyhead; 3551459Sroot int wordc; 3561459Sroot char **wordv; 3571459Sroot { 3585594Srrh reg char **nwordv; 3595594Srrh int nwordc = emptyhead + wordc; 3605594Srrh reg int i; 3611459Sroot 3621459Sroot nwordv = (char **)Calloc(nwordc, sizeof (char *)); 3631459Sroot for (i = 0; i < emptyhead; i++) 3641459Sroot nwordv[i] = 0; 3651459Sroot for(i = emptyhead; i < nwordc; i++){ 3661459Sroot nwordv[i] = wordv[i-emptyhead]; 3671459Sroot } 3681459Sroot return(nwordv); 3691459Sroot } 3705594Srrh /* 3715594Srrh * plural'ize and verb forms 3725594Srrh */ 3735594Srrh static char *S = "s"; 3745594Srrh static char *N = ""; 3755594Srrh char *plural(n) 3765594Srrh int n; 3775594Srrh { 3785594Srrh return( n > 1 ? S : N); 3795594Srrh } 3805594Srrh char *verbform(n) 3815594Srrh int n; 3825594Srrh { 3835594Srrh return( n > 1 ? N : S); 3845594Srrh } 3855594Srrh 386