/* crd2a.c (version 1.1)
	Written by Christopher Heckman, January 1997, July 1997.
	This program produces a text version of the chord utility. 
	See the file
            http://www.prism.gatech.edu/~gt7934b/Tab/software.html
   	for a full description of options supported. */
	
#include <stdio.h>
#define EOLN '\n'
#define LB '['
#define RB ']'
char buffer[255];
FILE *input;
unsigned lineno = 1;


int braceline ()
{
char *p, *q, i;

for (p = buffer + 2; (*p != ':') && (*p != EOLN); p++);
if (*p == EOLN) return 0;
for (q = ++p; (*q != EOLN) && (*q != '}'); q++);
*q = 0; i = buffer [1]; 
if ((i == 't') || (i == 'T') || (i == 's') || (i == 'S'))
	for (i = 39 - (q - p) / 2; i; i--) putchar (' ');
printf ("%s\n", p);
return 1;
}


int main (argc, argv) int argc; char *argv[];
{
char chords[255], text[255], *ch, *tx, *p;

if (argc != 2) {
	puts ("usage: crd2a FILENAME");
	exit (1);
	}
if ((input = fopen (argv[1], "r")) == NULL) {
	fprintf (stderr, "can't open %s.  program teminated\n\n", argv[1]);
	exit (1);
	}
for (lineno = 1; fgets (buffer, 255, input); lineno++) {
	if (*buffer == '{'){ braceline (); continue; }
	ch = chords; tx = text;
	for (p = buffer; (*p != LB) && (*p != EOLN); p++)
		*(ch++) = (*p == '\t') ? '\t' : ' ', *(tx++) = *p;
	if (*p == EOLN) { *tx = 0; puts (text); continue; }
	while (*p != EOLN) {
		for (p++; (*p != EOLN) && (*p != RB); p++)
			*(ch++) = *p;
		if (*p == EOLN) {
			fprintf (stderr, "*** error: line %u ends in chord\n",
				lineno); continue; }
		*(ch++) = ' ';
		for (p++; (*p != LB) && (*p != EOLN); p++) *(tx++) = *p;
		while (ch - chords > tx - text) *(tx++) = ' ';
		while (ch - chords < tx - text) *(ch++) = ' ';
		}
	*ch = *tx = 0;
/*	for (ch--; (ch - chords) && (*ch == ' '); ch--); */
	if (ch == chords) printf ("%s\n", text);
	else printf ("\n%s\n%s\n", chords, text);
	}
return 0;
}


