#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
static char rcsid[] = "$Id: dtwmrc.c,v 1.1.1.1 1999/09/26 12:34:39 mib Exp $";

#include "olmenu_yacc.h"
#include "olmenu.h"

static FILE *outfile;

extern struct menu *menu_list;

void fputs_exec(FILE *outfile,char *str);

int
dtwmrc(char *file_name) {
	struct menu *menu_ptr;
	struct menu_item *itemptr;
	char *label;
	char prefix;
	if( file_name == NULL || strcmp(file_name,"-")==0 ) {
		outfile=stdout;
	} else {
		if( (outfile=fopen(file_name,"w")) == 0 ) {
			perror(file_name);
			exit(1);
		}
	}
	menu_list->name="DtRootMenu";
	for(menu_ptr=menu_list; menu_ptr; menu_ptr=menu_ptr->next) {
	    fprintf(outfile,"Menu %s\n{\n",menu_ptr->name);
	    if( menu_ptr->title )
	        fprintf(outfile,"  %-20s f.title\n",menu_ptr->title);
	    for(itemptr=menu_ptr->first_item; itemptr; itemptr=itemptr->next) {
		label = itemptr->label;
		if ( label == 0 ) {
			label = " ";
			prefix = ' ';
		} else {
			prefix = *label & 0x80 ? '@' : ' ' ;
			*label &= 0x7f;  /* destructive */
		}
		switch( itemptr->type ) {
		case BACK_SELN:
		    fprintf(outfile,"  %c%-20s f.circle_down\n",prefix,label);
		    break;
		case DIRMENU:
		    break;
		case EXIT:
		case EXIT_NO_CONFIRM:
		    fprintf(outfile,"  %c%-20s f.action ExitSession\n",prefix,label);
		    break;
		case FLIPDRAG:
		    break;
		case FLIPFOCUS:
		    break;
		case FULL_RESTORE_SIZE_SELN:
		    break;
		case INCLUDE:
		case MENU:
		    fprintf(outfile,"  %c%-20s f.menu %s\n",prefix,label,label);
		    break;
		case MOVE_DESKTOP:
		    if( itemptr->arg.desktop==0 )
			fprintf(outfile,"  %c%-20s f.next_workspace\n",prefix,label);
		    else
			fprintf(outfile,"  %c%-20s f.goto_workspace %d\n",prefix,label,itemptr->arg.desktop);
		    break;
		case NOP:
		    fprintf(outfile,"  %c%-20s f.nop  %s\n",prefix,label,label);
		    break;
		case OPEN_CLOSE_SELN:
		    break;
		case QUIT_SELN:
		    break;
		case PROPERTIES:
		    break;
		case REFRESH:
		    fprintf(outfile,"  %c%-20s f.refresh\n",prefix,label);
		    break;
		case REREAD_MENU_FILE:
		    break;
		case RESTART:
		    fprintf(outfile,"  %c%-20s f.restart\n",prefix,label);
		    break;
		case SAVE_WORKSPACE:
		    break;
		case SEPARATOR:
		    fprintf(outfile,"  %-21s f.separator\n"," no-label");
		    break;
		case START_DSDM:
		    break;
		case STICK_UNSTICK_SELN:
		    break;
		case STOP_DSDM:
		    break;
		case WINMENU:
		    break;
		case WMEXIT:
		    fprintf(outfile,"  %c%-20s f.quit_mwm\n",prefix,label);
		    break;
		case EXEC:
		    fprintf(outfile,"  %c%-20s f.exec ",prefix,label);
		    fputs_exec(outfile,itemptr->arg.exec);
		    fprintf(outfile,"\n");
		    break;
		case SUBMENU:
		    fprintf(outfile,"  %c%-20s f.menu %s\n",prefix,label,itemptr->arg.submenu->name);
		    break;
 		default:
		    break;
		}
		if(prefix=='@')
			*label |= 0x80;
	    }
	    fprintf(outfile,"}\n\n");
	}
}


void
fputs_exec(FILE *outfile,char *str) {
	putc('"',outfile);
	while(*str) {
		switch(*str) {
		case '"':
			putc('\\',outfile);
		default:
			putc(*str,outfile);
		}
		str++;
	}
	putc('"',outfile);
}
