#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char para[0x400];

void msg(void)
{
	puts(
		"\tconvedit: utility for srctex to generate source specials of TeX\n\n"
		"convedit <editor> [<parameter>] <TeX file> [<parameter>]\n"
		"  This is a program to call <editor> to edit <TeX file>\n"
		"  If the extension of <TeX file> is .tmp, it is replaced by .tex"
	);
	exit(1);
}

int IsSpaceIn(char *s)
{
	if(*s == '\"' && s[strlen(s)-1] == '\"')
		return 0;
	while(*s){
		if(*s++ == ' ')
			return 1;
	}
	return 0;
}
	
void main(int argc,  char *argv[])
{
	int i, len, type;

	if(argc < 3)
		msg();
	type = IsSpaceIn(argv[1]);
	sprintf(para, type?"\"%s\" ":"%s ", argv[1]);
	for(i = 2; i < argc; i++){
		type = IsSpaceIn(argv[i]);
		if(type)
			strcat(para, "\"");
		strcat(para, argv[i]);
		len = strlen(para) - 4;
		if(len > 0){
			if(!strcmp(para + len, ".tmp"))
				strcpy(para + len, ".tex");
			else if(!strcmp(para + len - 1, ".tmp\""))
				strcpy(para + len - 1, ".tex\"");
		}
		strcat(para, type?"\" ":" ");
	}
	system(para);
}