*** 1.7/vtt.c Tue Sep 7 17:34:05 2004 --- vtt.c Tue Sep 7 22:25:59 2004 *************** *** 1,10 **** #include "stdio.h" #include "string.h" #include "ctype.h" - #define MAXLEN 512 ! /* ! Voynich Transcription Tool ! */ /* Global variables */ --- 1,25 ---- + /* + Voynich Transcription Tool + by Rene Zandbergen + + Last edited on 2004-09-07 22:25:59 by stolfi + Based on vtt 1.7 1998/05/09 RZ + Changes: + * Allowed extra "."s in locus name. + * Allowed "#" inside {}-comments. + * Added "-v" (print program version) option. + * Added return values to "main" procedure. + * Safer allocation of "folname" and "locname". + * Added check for line buffer overflow. + * Changed "if(x = y)" to "if((x = y) != 0)" to avoid warnings. + Should check for overflow in "folname" and "locname", too. + */ + #include "stdio.h" #include "string.h" #include "ctype.h" ! /* Maximum line length (including final '\0'): */ ! #define MAXLEN 512 /* Global variables */ *************** *** 27,35 **** int infarg= -1; /* Argument of input file name */ int oufarg= -1; /* Argument of output file name */ char auth=' '; /* Name of transcriber */ ! char invar[27]; /* List of 'include page' options */ ! char exvar[27]; /* List of 'exclude page' options */ int npgopt = 0; /* Number of include/exclude options */ - FILE *fin, *fout; /* File handles */ /* Some file stats */ --- 42,52 ---- int infarg= -1; /* Argument of input file name */ int oufarg= -1; /* Argument of output file name */ + FILE *fin, *fout; /* File handles */ + + /* Selection options */ char auth=' '; /* Name of transcriber */ ! char invar[27]; /* List of 'include page' options. */ ! char exvar[27]; /* List of 'exclude page' options. */ int npgopt = 0; /* Number of include/exclude options */ /* Some file stats */ *************** *** 48,55 **** int newfoli=0 /* 1 if there are < > without a locus (i.e. newpage)*/; char cwarn=' ' /* Warning character */; ! char *folname= " " /* Folio name, e.g. f85r3 */; ! char *locname= " " /* Locus name, e.g. C3, or blank */; char lineauth= ' ' /* Name of transcriber or blank */; ! char pgvar[27] /* List of page variable settings */; /* These track the text */ --- 65,72 ---- int newfoli=0 /* 1 if there are < > without a locus (i.e. newpage)*/; char cwarn=' ' /* Warning character */; ! char folname[8] /* Folio name, e.g. "f85r3" */; ! char locname[8] /* Locus name, e.g. "C3", "L3.8", or blank */; char lineauth= ' ' /* Name of transcriber or blank */; ! char pgvar[27] /* Page variable settings; pgvar[0] not used. */; /* These track the text */ *************** *** 107,111 **** char cb; ! while ((cb = buf[ii]) && ii < width) { /* Dedicated check for comments. Sets voycode if outside comments */ --- 124,128 ---- char cb; ! while ((cb = buf[ii]) != '\0' && ii < width) { /* Dedicated check for comments. Sets voycode if outside comments */ *************** *** 164,170 **** /* Decide whether to select page based on comparing page options in pgvar[] with include and exclude options ! given by user. Page variable abused to select loci, making this a line selection option instead. ! The value of pgvar[0] is set in GetLine. If iopt=0 check only pgvar[0]. Otherwise check all others. */ --- 181,187 ---- /* Decide whether to select page based on comparing page options in pgvar[] with include and exclude options ! given by user. Page variable "@" abused to select loci, making this a line selection option instead. ! The value of pgvar[0] is set in PrepLine. If iopt=0 check only pgvar[0]. Otherwise check all others. */ *************** *** 520,524 **** /* RTF: hash comment always in comment font */ if (hash) font = 0; ! while (cb = buf[index]) { /* RTF: if not hash comment: keep track of font */ if (hash == 0) { --- 537,541 ---- /* RTF: hash comment always in comment font */ if (hash) font = 0; ! while ((cb = buf[index]) != '\0') { /* RTF: if not hash comment: keep track of font */ if (hash == 0) { *************** *** 552,555 **** --- 569,575 ---- char sign, optn, val; + /* Why not indeed: */ + char *what = "@(#)vtt\t1.7e1\t2004/09/07\tJS\n"; + for (i=0; i<=26; i++) { invar[i]=' '; exvar[i]=' '; *************** *** 657,660 **** --- 677,683 ---- ast=3; brack=2; /* Remove line with ? * or [..] */} break; + case 'v': + fprintf(stderr, "%s\n", what); + break; case 'w': if (val == '0') wrap=0; /* Maintain line wrapping */ *************** *** 922,926 **** return -1; /* Correct EOF */ } else { ! buf[index] = 0; /* Add a null character for safety */ fprintf (stderr, "EOF at record pos. %3d\n", index); fprintf (stderr, "Line read so far: %s\n", buf); --- 945,949 ---- return -1; /* Correct EOF */ } else { ! buf[index] = '\0'; /* Add a null character for safety */ fprintf (stderr, "EOF at record pos. %3d\n", index); fprintf (stderr, "Line read so far: %s\n", buf); *************** *** 938,951 **** /* Check for comment line */ ! if (cget == '#') { ! /* A hash symbol. Not allowed when unwrapping */ ! if (concat > 0) { ! buf[index] = 0; /* Add a null character for safety */ ! fprintf (stderr, "Hash comment after continuation\n"); ! fprintf (stderr, "Line read so far: %s\n", buf); ! return 1; ! } ! /* Check for valid hash comment line */ ! if (index == 0) hash = 1; } --- 961,968 ---- /* Check for comment line */ ! if (cget == '#' && index == 0) { ! /* Hash symbols in invalid positions should be caught by PrepLine. */ ! /* That includes "#" in column 1 of continuation lines. */ ! hash = 1; } *************** *** 980,983 **** --- 997,1006 ---- /* Add the character to the buffer */ buf[index++] = cget; + if (index >= MAXLEN) { + fprintf (stderr, "Line too long - %d chars\n", index); + buf[index-1] = '\0'; + fprintf (stderr, "Line read so far: %s\n", buf); + return -2; + } if (blank == 0) hastext = 1; *************** *** 985,989 **** if (cget == '\n') { cr = 1; ! buf[index] = 0; /* Add null at end of line */ } } /* End if ignore == 0 */ --- 1008,1012 ---- if (cget == '\n') { cr = 1; ! buf[index] = '\0'; /* Add null at end of line */ } } /* End if ignore == 0 */ *************** *** 1014,1018 **** trackinit(); ! while (cget = buf1[ind1]) { /* For hash comment just copy: */ --- 1037,1041 ---- trackinit(); ! while ((cget = buf1[ind1]) != '\0') { /* For hash comment just copy: */ *************** *** 1023,1028 **** /* No hash comment: do all the processing */ ! /* 1. Check for hash later in line and change to = */ ! if (cget == '#') { cwarn = cget; cget = '='; --- 1046,1051 ---- /* No hash comment: do all the processing */ ! /* 1. Check for unprotected hash later in line and change to = */ ! if (cget == '#' && incomm == 0) { cwarn = cget; cget = '='; *************** *** 1134,1138 **** case '>': /* Terminate (optionally) locus ID */ if (inlocu > 0) { ! locname[inlocu] = 0; inlocu = -2; } if (newfoli < 0) { --- 1157,1161 ---- case '>': /* Terminate (optionally) locus ID */ if (inlocu > 0) { ! locname[inlocu] = '\0'; inlocu = -2; } if (newfoli < 0) { *************** *** 1141,1146 **** } break; ! case '.': /* Start of locus */ inlocu = -1; newfoli = 0; break; case ';': --- 1164,1175 ---- } break; ! case '.': ! if (inlocu <= 0) { ! /* Start of locus name */ inlocu = -1; newfoli = 0; + } else { + /* Dot is part of locus name: */ + locname[inlocu-1] = cget; + } break; case ';': *************** *** 1150,1154 **** } /* Terminate locus ID */ ! locname[inlocu] = 0; inlocu = -2; /* Start for author ID */ inauth = -1; --- 1179,1183 ---- } /* Terminate locus ID */ ! locname[inlocu] = '\0'; inlocu = -2; /* Start for author ID */ inauth = -1; *************** *** 1246,1250 **** /* Set pgvar[0] if a new locus was read */ ! if (hasfoli && (newfoli == 0)) { pgvar[0] = locname[0]; if (pgvar[0] >= '0' && pgvar[0] <= '9') pgvar[0] = 'P'; --- 1275,1279 ---- /* Set pgvar[0] if a new locus was read */ ! if (hasfoli != 0 && newfoli == 0) { pgvar[0] = locname[0]; if (pgvar[0] >= '0' && pgvar[0] <= '9') pgvar[0] = 'P'; *************** *** 1282,1286 **** trackinit(); index = 0; ! while (cb = buf[index]) { /* Track the text */ --- 1311,1315 ---- trackinit(); index = 0; ! while ((cb = buf[index]) != '\0') { /* Track the text */ *************** *** 1357,1361 **** trackinit(); index = 0; /* fprintf(stderr,"%s\n",buf); */ ! while (cb = buf[index]) { /* Track the text */ --- 1386,1390 ---- trackinit(); index = 0; /* fprintf(stderr,"%s\n",buf); */ ! while ((cb = buf[index]) != '\0') { /* Track the text */ *************** *** 1493,1497 **** char *buf; { ! int index=0, indout=0, eol=0, output, ii; char cb; char wrapbuf[MAXLEN]; --- 1522,1526 ---- char *buf; { ! int index=0, indout=0, output, ii; char cb; char wrapbuf[MAXLEN]; *************** *** 1522,1526 **** trackinit(); ! while (cb = buf[index]) { output = 1; TrackLight(cb, index); --- 1551,1555 ---- trackinit(); ! while ((cb = buf[index]) != '\0') { output = 1; TrackLight(cb, index); *************** *** 1588,1606 **** /*-----------------------------------------------------------*/ ! void main(argc, argv) int argc; char *argv[]; { char orig[MAXLEN], buf1[MAXLEN], buf2[MAXLEN]; ! int ilin = 0, iprep = 0, iproc = 0, iout = 0; int selpage, selloc; ! /* Why not indeed: */ ! char *what = "@(#)vtt\t1.7\t1998/05/09\tRZ\n"; /* Parse command line options */ if (ParseOpts(argc, argv)) { fprintf (stderr, "%s\n", "Error parsing command line"); ! return; } --- 1617,1635 ---- /*-----------------------------------------------------------*/ ! int main(argc, argv) int argc; char *argv[]; { char orig[MAXLEN], buf1[MAXLEN], buf2[MAXLEN]; ! int ilin = 0, iprep = 0, iproc = 0; int selpage, selloc; ! /* Just in case: */ ! folname[0] = '\0'; locname[0] = '\0'; /* Parse command line options */ if (ParseOpts(argc, argv)) { fprintf (stderr, "%s\n", "Error parsing command line"); ! return 1; } *************** *** 1608,1612 **** if (DumpOpts(argc, argv)) { fprintf (stderr, "%s\n", "Error opening file(s)"); ! return; } fprintf (stderr, "\n%s\n", "Starting..."); --- 1637,1641 ---- if (DumpOpts(argc, argv)) { fprintf (stderr, "%s\n", "Error opening file(s)"); ! return 1; } fprintf (stderr, "\n%s\n", "Starting..."); *************** *** 1656,1664 **** fprintf (stderr, "%7d lines after wrapping\n", nlwrap); } ! return; } else if (ilin > 0) { fprintf (stderr, "%s\n", "Error reading line from stdin"); ! return; } nlread += 1; nlpart += 1; --- 1685,1693 ---- fprintf (stderr, "%7d lines after wrapping\n", nlwrap); } ! return 0; } else if (ilin > 0) { fprintf (stderr, "%s\n", "Error reading line from stdin"); ! return 1; } nlread += 1; nlpart += 1; *************** *** 1673,1677 **** fprintf (stderr, "%s\n", "Error preprocessing line"); fprintf (stderr, "Line: %s\n", orig); ! return; } if (cwarn != ' ') { --- 1702,1706 ---- fprintf (stderr, "%s\n", "Error preprocessing line"); fprintf (stderr, "Line: %s\n", orig); ! return 1; } if (cwarn != ' ') { *************** *** 1692,1696 **** /* Check locus type only if page in principle OK */ ! if (selloc = selpage) selloc = usepage(0); /* Only continue if page/locus as requested */ --- 1721,1725 ---- /* Check locus type only if page in principle OK */ ! if ((selloc = selpage) != 0) selloc = usepage(0); /* Only continue if page/locus as requested */ *************** *** 1703,1707 **** fprintf (stderr, "%s", "Error processing spaces\n"); fprintf (stderr, "Line: %s\n", orig); ! return; } --- 1732,1736 ---- fprintf (stderr, "%s", "Error processing spaces\n"); fprintf (stderr, "Line: %s\n", orig); ! return 1; } *************** *** 1712,1716 **** fprintf (stderr, "%s\n", "Error processing uncertain readings"); fprintf (stderr, "Line: %s\n", orig); ! return; } else { --- 1741,1745 ---- fprintf (stderr, "%s\n", "Error processing uncertain readings"); fprintf (stderr, "Line: %s\n", orig); ! return 1; } else { *************** *** 1725,1729 **** if (PutLine(buf2) != 0) { fprintf (stderr, "Line: %s\n", orig); ! return; } } else { --- 1754,1758 ---- if (PutLine(buf2) != 0) { fprintf (stderr, "Line: %s\n", orig); ! return 0; } } else { *************** *** 1733,1735 **** --- 1762,1765 ---- } } + return 0; }