1*13539Ssam static char *sccsid = "@(#)subr.c 1.5 (Berkeley) 07/01/83"; 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; 2191459Sroot 2201459Sroot struct lang_desc lang_table[] = { 2211459Sroot /*INUNKNOWN 0*/ "unknown", cincomment, coutcomment, 2221459Sroot /*INCPP 1*/ "cpp", cincomment, coutcomment, 2231459Sroot /*INCC 2*/ "cc", cincomment, coutcomment, 2241459Sroot /*INAS 3*/ "as", ASINCOMMENT, newline, 2251459Sroot /*INLD 4*/ "ld", cincomment, coutcomment, 2261459Sroot /*INLINT 5*/ "lint", cincomment, coutcomment, 2271459Sroot /*INF77 6*/ "f77", fincomment, foutcomment, 2281459Sroot /*INPI 7*/ "pi", piincomment, pioutcomment, 2291459Sroot /*INPC 8*/ "pc", piincomment, pioutcomment, 2301459Sroot /*INFRANZ 9*/ "franz",lispincomment, newline, 2311459Sroot /*INLISP 10*/ "lisp", lispincomment, newline, 2321459Sroot /*INVAXIMA 11*/ "vaxima",lispincomment,newline, 2331459Sroot /*INRATFOR 12*/ "ratfor",fincomment, foutcomment, 2341459Sroot /*INLEX 13*/ "lex", cincomment, coutcomment, 2351459Sroot /*INYACC 14*/ "yacc", cincomment, coutcomment, 2361459Sroot /*INAPL 15*/ "apl", ".lm", newline, 2371459Sroot /*INMAKE 16*/ "make", ASINCOMMENT, newline, 2381459Sroot /*INRI 17*/ "ri", riincomment, rioutcomment, 23913106Srrh /*INTROFF 18*/ "troff",troffincomment,troffoutcomment, 2401459Sroot 0, 0, 0 2411459Sroot }; 2421459Sroot 2431459Sroot printerrors(look_at_subclass, errorc, errorv) 2441459Sroot boolean look_at_subclass; 2451459Sroot int errorc; 2465594Srrh Eptr errorv[]; 2471459Sroot { 2485594Srrh reg int i; 2495594Srrh reg Eptr errorp; 2505594Srrh 2511459Sroot for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){ 2521459Sroot if (errorp->error_e_class == C_IGNORE) 2531459Sroot continue; 2541459Sroot if (look_at_subclass && errorp->error_s_class == C_DUPL) 2551459Sroot continue; 2561459Sroot printf("Error %d, (%s error) [%s], text = \"", 2571459Sroot i, 2581459Sroot class_table[errorp->error_e_class], 2591459Sroot lang_table[errorp->error_language].lang_name); 2601459Sroot wordvprint(stdout,errorp->error_lgtext,errorp->error_text); 2611459Sroot printf("\"\n"); 2621459Sroot } 2631459Sroot } 2641459Sroot 2651459Sroot wordvprint(fyle, wordc, wordv) 2661459Sroot FILE *fyle; 2671459Sroot int wordc; 2681459Sroot char *wordv[]; 2691459Sroot { 2701459Sroot int i; 271*13539Ssam char *sep = ""; 272*13539Ssam 273*13539Ssam for(i = 0; i < wordc; i++) 274*13539Ssam if (wordv[i]) { 275*13539Ssam fprintf(fyle, "%s%s",sep,wordv[i]); 276*13539Ssam sep = " "; 277*13539Ssam } 2781459Sroot } 2791459Sroot 2801459Sroot /* 2811459Sroot * Given a string, parse it into a number of words, and build 2821459Sroot * a wordc wordv combination pointing into it. 2831459Sroot */ 2841459Sroot wordvbuild(string, r_wordc, r_wordv) 2851459Sroot char *string; 2861459Sroot int *r_wordc; 2871459Sroot char ***r_wordv; 2881459Sroot { 2895594Srrh reg char *cp; 2905594Srrh char *saltedbuffer; 2915594Srrh char **wordv; 2925594Srrh int wordcount; 2935594Srrh int wordindex; 2941459Sroot 2951459Sroot saltedbuffer = strsave(string); 2961459Sroot for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){ 2971459Sroot while (*cp && isspace(*cp)) 2981459Sroot cp++; 2991459Sroot if (*cp == 0) 3001459Sroot break; 3011459Sroot while (!isspace(*cp)) 3021459Sroot cp++; 3031459Sroot } 3041459Sroot wordv = (char **)Calloc(wordcount + 1, sizeof (char *)); 3051459Sroot for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){ 3061459Sroot while (*cp && isspace(*cp)) 3071459Sroot cp++; 3081459Sroot if (*cp == 0) 3091459Sroot break; 3101459Sroot wordv[wordindex] = cp; 3111459Sroot while(!isspace(*cp)) 3121459Sroot cp++; 3131459Sroot *cp++ = '\0'; 3141459Sroot } 3151459Sroot if (wordcount != 0) 3161459Sroot error("Initial miscount of the number of words in a line\n"); 3171459Sroot wordv[wordindex] = (char *)0; 3181459Sroot #ifdef FULLDEBUG 3191459Sroot for (wordcount = 0; wordcount < wordindex; wordcount++) 3201459Sroot printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]); 3211459Sroot printf("\n"); 3221459Sroot #endif 3231459Sroot *r_wordc = wordindex; 3241459Sroot *r_wordv = wordv; 3251459Sroot } 3261459Sroot /* 3271459Sroot * Compare two 0 based wordvectors 3281459Sroot */ 3291459Sroot int wordvcmp(wordv1, wordc, wordv2) 3301459Sroot char **wordv1; 3311459Sroot int wordc; 3321459Sroot char **wordv2; 3331459Sroot { 3345594Srrh reg int i; 3355594Srrh int back; 3361459Sroot for (i = 0; i < wordc; i++){ 33710830Ssam if (wordv1[i] == 0 || wordv2[i] == 0) 33810830Ssam return(-1); 3391459Sroot if (back = strcmp(wordv1[i], wordv2[i])){ 3401459Sroot return(back); 3411459Sroot } 3421459Sroot } 3431459Sroot return(0); /* they are equal */ 3441459Sroot } 3451459Sroot 3461459Sroot /* 3471459Sroot * splice a 0 basedword vector onto the tail of a 3481459Sroot * new wordv, allowing the first emptyhead slots to be empty 3491459Sroot */ 3501459Sroot char **wordvsplice(emptyhead, wordc, wordv) 3511459Sroot int emptyhead; 3521459Sroot int wordc; 3531459Sroot char **wordv; 3541459Sroot { 3555594Srrh reg char **nwordv; 3565594Srrh int nwordc = emptyhead + wordc; 3575594Srrh reg int i; 3581459Sroot 3591459Sroot nwordv = (char **)Calloc(nwordc, sizeof (char *)); 3601459Sroot for (i = 0; i < emptyhead; i++) 3611459Sroot nwordv[i] = 0; 3621459Sroot for(i = emptyhead; i < nwordc; i++){ 3631459Sroot nwordv[i] = wordv[i-emptyhead]; 3641459Sroot } 3651459Sroot return(nwordv); 3661459Sroot } 3675594Srrh /* 3685594Srrh * plural'ize and verb forms 3695594Srrh */ 3705594Srrh static char *S = "s"; 3715594Srrh static char *N = ""; 3725594Srrh char *plural(n) 3735594Srrh int n; 3745594Srrh { 3755594Srrh return( n > 1 ? S : N); 3765594Srrh } 3775594Srrh char *verbform(n) 3785594Srrh int n; 3795594Srrh { 3805594Srrh return( n > 1 ? N : S); 3815594Srrh } 3825594Srrh 383