--- dvidvi-1.0.orig/dvidvi.c
+++ dvidvi-1.0/dvidvi.c
@@ -136,6 +136,12 @@
 integer *pagenumbers ;
 int prettycolumn ;       /* the column we are at when running pretty */
 
+// To handle xxx1 for headers
+// Lets suppose there will be less than 256 of those.
+char *headers[256];
+integer ln_headers[256];
+integer nb_headers = 0L;
+
 /*
  *   This array holds values that indicate the length of a command, if
  *   we aren't concerned with that command (which is most of them) or
@@ -289,9 +295,12 @@
 void stringdvibuf(p,n)
 integer p,n;
 {
+  char *tmp;
   fseek(infile,p,SEEK_SET);
+  tmp = temp;
   while ( n-- > 0 )
     *temp++ = dvibyte();
+  temp=tmp;
 }
 
 /*
@@ -835,6 +844,7 @@
    (void)dvibuf(p);
    pagenumbers[num]=signedquad();
 }
+
 /*
  *   This routine simply reads the entire dvi file, and then initializes
  *   some values about it.
@@ -909,6 +919,7 @@
  */
 
    p = pageloc[0L] + 45 ;
+nextcmd:
    c=dvibuf(p);
    while (comlen[c]) {
       p += comlen[c] ;
@@ -920,6 +931,29 @@
         landscape = p ;
         rem0special = 1 ;
       }
+      stringdvibuf(p+2L,6L);
+      if (strncmp(temp, "header", 6)==0) {
+	char l1;
+	l1 = dvibuf(p+1);
+	stringdvibuf(p+2,l1);
+	if ( nb_headers == 256 ) {
+	   fprintf(stderr,"\n"
+			  "There are more than 256 PS headers in your DVI file.\n"
+			  "Please ask a wizzard to enlarge me :-)\n");
+	}
+	ln_headers[nb_headers] = l1;
+	headers[nb_headers++] = strdup(temp);
+	/*
+	 * Seek at the end of the xxx1, then goto reading next commands.
+	 * In a normal DVI file, as produced by LaTeX, all of those special
+	 * commands (xxx1 with PS headers) are outputed on the very first
+	 * page, all together. So a simple loop might be enough. Just in
+	 * case we have a strange file we accept that there might be some
+	 * typesetting commandes between those xxx1.
+	 */
+	p = p + l1 + 2;
+	goto nextcmd;
+      }
    }
 }
 /*
@@ -1043,6 +1077,7 @@
    integer v, oldp ;
    unsigned char c;
 
+
 /*
  *   We want to take the base 10 log of the number.  It's probably
  *   small, so we do it quick.
@@ -1072,6 +1107,31 @@
    p = pageloc[num] + 45 ;
    c=dvibuf(p);
    while (c != 140) {
+      if ( nb_headers ) {
+	 integer i;
+	 /*
+	  * Need to output the 'nb_headers' headers that we collected.
+	  * If we are outputing the real first page of the previous
+	  * DVI file, then the headers will be doubled, which is not
+	  * troublesome: dvips will keep only one of those.
+	  */
+	 for ( i=0; i<nb_headers; i++ ) {
+	    outdvibyte(239);
+	    outdvibyte(ln_headers[i]);
+	    putstr(headers[i]);
+	    if ( !quiet ) {
+	       fprintf(stderr,"<%s>",headers[i]+7);
+	       prettycolumn += ln_headers[i]+2-7;
+	    }
+	 }
+	 /*
+	  * We will output the headers only one time. That is possible that
+	  * those headers are not usefull (the pages where they are used might
+	  * not be kept in the out file, but there is no simple way to avoid
+	  * it.
+	  */
+         nb_headers = 0;
+      }
       if ((len=comlen[c]) > 0) {    /* most commands are simple */
          outdvibyte(c);
          putbuf((long)len-1) ;
@@ -1198,5 +1258,8 @@
    processargs(argc, argv) ;
    readdvifile() ;
    writedvifile() ;
+   if (!quiet) {
+      fprintf(stderr,"\n");
+   }
 }
 

