/*
 *	@(#)popups.c 1.7 91/09/14 Copyright 1990-91 Sun Microsystems
 *
 * This file is a product of Sun Microsystems, Inc. and is provided for
 * unrestricted use provided that this legend is included on all tape
 * media and as a part of the software program in whole or part.  Users
 * may copy or modify this file without charge, but are not authorized to
 * license or distribute it to anyone else except as part of a product
 * or program developed by the user.
 * 
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 * 
 * This file is provided with no support and without any obligation on the
 * part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 * 
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
 * OR ANY PART THEREOF.
 * 
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even
 * if Sun has been advised of the possibility of such damages.
 * 
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */

#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <wire/wire.h>
#include "buffer.h"
#include "popups_cps.h"

#define LOAD_POPUP	0
#define SAVE_POPUP	1
#define INCLUDE_POPUP	2
#define NPOPUPS		3

static struct popup {
    char    *ps_name;
    char    *label;
    char    *button;
    int	    tag;
} FilePopups[NPOPUPS] = {
    "LoadPopup", "Jed:Load", "Load file", -1,
    "SavePopup", "Jed:Save", "Save file", -1,
    "IncludePopup", "Jed:Include", "Include file", -1
};

static int  popup_tags = -1;

extern char *initial_working_directory;

static void
get_filename_from_wire(char *buffer, int *format)
{
    char    dir_name[256], file_name[128];

    wire_ReadString(dir_name);
    wire_ReadString(file_name);
    *format = wire_ReadInt();
    sprintf(buffer, "%s/%s", dir_name, file_name);
    if (strpbrk(buffer, "*[?~$")) {
	FILE	*pfp;
	char	cmd[512];

	sprintf(cmd, "csh -fc \"echo %s\"", buffer);
	pfp = popen(cmd, "r");
	if (fgets(buffer, 256, pfp) != NULL)
	    buffer[strlen(buffer) - 1] = '\0';
	pclose(pfp);
	if (strchr(buffer, ' ') != 0)
	    signal_warning("Filename wild-card produced more than one result: %s", buffer);
    }
}

static void
file_popup_callback(int tag, caddr_t data)
{
    char    filename[256], *which;
    int	    format;

    get_filename_from_wire(filename, &format);
    if (format == -1)
	current_buffer->file_format = UNDETERMINED;
    else if (format == 0)
	current_buffer->file_format = TEXTONLY;
    else
	current_buffer->file_format = JOTFORMAT;
    if (tag == FilePopups[LOAD_POPUP].tag)
	Buffer_LoadFile(current_buffer, filename);
    else if (tag == FilePopups[SAVE_POPUP].tag) {
	Buffer_ConfirmWrite(current_buffer, filename);
	(void) Buffer_WriteFile(current_buffer, filename, TRUE);
	Buffer_SetFileName(current_buffer, filename);
	Buffer_UnModified(current_buffer);
    } else
	Buffer_InsertFile(current_buffer, filename, TRUE);
    cps_maybeunmap(FilePopups[tag - popup_tags].ps_name);
}

static struct popup *
find_popup(char *name)
{
    struct popup    *p;
    int		    i;

    for (p = FilePopups, i = 0; i < NPOPUPS; i++, p++)
	if (strcmp(p->ps_name, name) == 0)
	    return p;
    return 0;
}

void
map_popup(char *which)
{
    struct popup    *p;
    char	    *dir, *file;

    if (popup_tags == -1) {
	int i;

	popup_tags = wire_AllocateTags(3);
	for (i = 0; i < NPOPUPS; i++)
	    wire_RegisterTag(popup_tags + i, file_popup_callback, NULL);
    }
    p = find_popup(which);
    assert(p != 0);
    if (p->tag == -1) {
	p->tag = popup_tags + (p - FilePopups);
	cps_build_popup(p->ps_name, p->label, p->button, p->tag);
    }
    dir = current_buffer->directory;
    file = current_buffer->filename;
    cps_setfile(p->ps_name, dir == 0 ? "" : dir,
		file == 0 ? "" : file,
		current_buffer->file_format == UNDETERMINED ? "[]" :
		current_buffer->file_format == JOTFORMAT ? "[1]" : "[0]");
    cps_open(p->ps_name);
}
