1*10830Ssam static char *sccsid = "@(#)subr.c 1.3 (Berkeley) 02/09/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; 74*10830Ssam 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 90*10830Ssam 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; 105*10830Ssam 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 { 116*10830Ssam if (string) 117*10830Ssam return(string[0]); 118*10830Ssam else 119*10830Ssam return('\0'); 1201459Sroot } 1211459Sroot 1221459Sroot char next_lastchar(string) 1231459Sroot char *string; 1241459Sroot { 1251459Sroot int length; 126*10830Ssam 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 { 137*10830Ssam int length = 0; 138*10830Ssam if (string) 139*10830Ssam 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; 153*10830Ssam int length = 0; 1541459Sroot 155*10830Ssam if (string) 156*10830Ssam 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; 184*10830Ssam int length = 0; 1851459Sroot 186*10830Ssam if (string) 187*10830Ssam 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; 2171459Sroot 2181459Sroot struct lang_desc lang_table[] = { 2191459Sroot /*INUNKNOWN 0*/ "unknown", cincomment, coutcomment, 2201459Sroot /*INCPP 1*/ "cpp", cincomment, coutcomment, 2211459Sroot /*INCC 2*/ "cc", cincomment, coutcomment, 2221459Sroot /*INAS 3*/ "as", ASINCOMMENT, newline, 2231459Sroot /*INLD 4*/ "ld", cincomment, coutcomment, 2241459Sroot /*INLINT 5*/ "lint", cincomment, coutcomment, 2251459Sroot /*INF77 6*/ "f77", fincomment, foutcomment, 2261459Sroot /*INPI 7*/ "pi", piincomment, pioutcomment, 2271459Sroot /*INPC 8*/ "pc", piincomment, pioutcomment, 2281459Sroot /*INFRANZ 9*/ "franz",lispincomment, newline, 2291459Sroot /*INLISP 10*/ "lisp", lispincomment, newline, 2301459Sroot /*INVAXIMA 11*/ "vaxima",lispincomment,newline, 2311459Sroot /*INRATFOR 12*/ "ratfor",fincomment, foutcomment, 2321459Sroot /*INLEX 13*/ "lex", cincomment, coutcomment, 2331459Sroot /*INYACC 14*/ "yacc", cincomment, coutcomment, 2341459Sroot /*INAPL 15*/ "apl", ".lm", newline, 2351459Sroot /*INMAKE 16*/ "make", ASINCOMMENT, newline, 2361459Sroot /*INRI 17*/ "ri", riincomment, rioutcomment, 2371459Sroot 0, 0, 0 2381459Sroot }; 2391459Sroot 2401459Sroot printerrors(look_at_subclass, errorc, errorv) 2411459Sroot boolean look_at_subclass; 2421459Sroot int errorc; 2435594Srrh Eptr errorv[]; 2441459Sroot { 2455594Srrh reg int i; 2465594Srrh reg Eptr errorp; 2475594Srrh 2481459Sroot for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){ 2491459Sroot if (errorp->error_e_class == C_IGNORE) 2501459Sroot continue; 2511459Sroot if (look_at_subclass && errorp->error_s_class == C_DUPL) 2521459Sroot continue; 2531459Sroot printf("Error %d, (%s error) [%s], text = \"", 2541459Sroot i, 2551459Sroot class_table[errorp->error_e_class], 2561459Sroot lang_table[errorp->error_language].lang_name); 2571459Sroot wordvprint(stdout,errorp->error_lgtext,errorp->error_text); 2581459Sroot printf("\"\n"); 2591459Sroot } 2601459Sroot } 2611459Sroot 2621459Sroot wordvprint(fyle, wordc, wordv) 2631459Sroot FILE *fyle; 2641459Sroot int wordc; 2651459Sroot char *wordv[]; 2661459Sroot { 2671459Sroot int i; 2681459Sroot for(i = 0; i < wordc; i++){ 2691459Sroot fprintf(fyle, "%s",wordv[i]); 2701459Sroot if (i != wordc - 1) 2711459Sroot fprintf(fyle, " "); 2721459Sroot } 2731459Sroot } 2741459Sroot 2751459Sroot /* 2761459Sroot * Given a string, parse it into a number of words, and build 2771459Sroot * a wordc wordv combination pointing into it. 2781459Sroot */ 2791459Sroot wordvbuild(string, r_wordc, r_wordv) 2801459Sroot char *string; 2811459Sroot int *r_wordc; 2821459Sroot char ***r_wordv; 2831459Sroot { 2845594Srrh reg char *cp; 2855594Srrh char *saltedbuffer; 2865594Srrh char **wordv; 2875594Srrh int wordcount; 2885594Srrh int wordindex; 2891459Sroot 2901459Sroot saltedbuffer = strsave(string); 2911459Sroot for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){ 2921459Sroot while (*cp && isspace(*cp)) 2931459Sroot cp++; 2941459Sroot if (*cp == 0) 2951459Sroot break; 2961459Sroot while (!isspace(*cp)) 2971459Sroot cp++; 2981459Sroot } 2991459Sroot wordv = (char **)Calloc(wordcount + 1, sizeof (char *)); 3001459Sroot for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){ 3011459Sroot while (*cp && isspace(*cp)) 3021459Sroot cp++; 3031459Sroot if (*cp == 0) 3041459Sroot break; 3051459Sroot wordv[wordindex] = cp; 3061459Sroot while(!isspace(*cp)) 3071459Sroot cp++; 3081459Sroot *cp++ = '\0'; 3091459Sroot } 3101459Sroot if (wordcount != 0) 3111459Sroot error("Initial miscount of the number of words in a line\n"); 3121459Sroot wordv[wordindex] = (char *)0; 3131459Sroot #ifdef FULLDEBUG 3141459Sroot for (wordcount = 0; wordcount < wordindex; wordcount++) 3151459Sroot printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]); 3161459Sroot printf("\n"); 3171459Sroot #endif 3181459Sroot *r_wordc = wordindex; 3191459Sroot *r_wordv = wordv; 3201459Sroot } 3211459Sroot /* 3221459Sroot * Compare two 0 based wordvectors 3231459Sroot */ 3241459Sroot int wordvcmp(wordv1, wordc, wordv2) 3251459Sroot char **wordv1; 3261459Sroot int wordc; 3271459Sroot char **wordv2; 3281459Sroot { 3295594Srrh reg int i; 3305594Srrh int back; 3311459Sroot for (i = 0; i < wordc; i++){ 332*10830Ssam if (wordv1[i] == 0 || wordv2[i] == 0) 333*10830Ssam return(-1); 3341459Sroot if (back = strcmp(wordv1[i], wordv2[i])){ 3351459Sroot return(back); 3361459Sroot } 3371459Sroot } 3381459Sroot return(0); /* they are equal */ 3391459Sroot } 3401459Sroot 3411459Sroot /* 3421459Sroot * splice a 0 basedword vector onto the tail of a 3431459Sroot * new wordv, allowing the first emptyhead slots to be empty 3441459Sroot */ 3451459Sroot char **wordvsplice(emptyhead, wordc, wordv) 3461459Sroot int emptyhead; 3471459Sroot int wordc; 3481459Sroot char **wordv; 3491459Sroot { 3505594Srrh reg char **nwordv; 3515594Srrh int nwordc = emptyhead + wordc; 3525594Srrh reg int i; 3531459Sroot 3541459Sroot nwordv = (char **)Calloc(nwordc, sizeof (char *)); 3551459Sroot for (i = 0; i < emptyhead; i++) 3561459Sroot nwordv[i] = 0; 3571459Sroot for(i = emptyhead; i < nwordc; i++){ 3581459Sroot nwordv[i] = wordv[i-emptyhead]; 3591459Sroot } 3601459Sroot return(nwordv); 3611459Sroot } 3625594Srrh /* 3635594Srrh * plural'ize and verb forms 3645594Srrh */ 3655594Srrh static char *S = "s"; 3665594Srrh static char *N = ""; 3675594Srrh char *plural(n) 3685594Srrh int n; 3695594Srrh { 3705594Srrh return( n > 1 ? S : N); 3715594Srrh } 3725594Srrh char *verbform(n) 3735594Srrh int n; 3745594Srrh { 3755594Srrh return( n > 1 ? N : S); 3765594Srrh } 3775594Srrh 378