Esempio n. 1
0
/* This function converts key labels to raw codes */
static int 
keylabel (ELVCHAR  *given,   /* what the user typed in as the key name */
          int  givenlen,  /* length of the "given" string */
          ELVCHAR  **label,  /* standard name for that key */
          ELVCHAR  **rawptr) /* control code sent by that key */
{
  int  i;

  /* compare the given text to each key's strings */
  for (i = 0; i < QTY(keys); i++)
    {
      /* ignore unsupported keys */
      if (!keys[i].rawin)
        continue;

      /* does given string match key label or raw characters? */
      if ((!strncmp(keys[i].label, tochar8(given), 
          (size_t)givenlen) && !keys[i].label[givenlen])
          || (!strncmp(keys[i].rawin, tochar8(given), 
          (size_t)givenlen) && !keys[i].rawin[givenlen]))
        {

          /* Set the label and rawptr pointers, return rawlen */
          *label = toCHAR(keys[i].label);
          *rawptr = toCHAR(keys[i].rawin);
          return CHARlen(*rawptr);
        }
    }

  /* We reached the end of the keys[] array without finding a match,
   * so this given string is not a key.
   */
  return 0;
}
Esempio n. 2
0
File: guiwin.c Progetto: mbert/elvis
static void gwretitle (GUIWIN *gw, char *name)

{
    GUI_WINDOW      *gwp = (GUI_WINDOW *)gw;
#if 0
    char            title[_MAX_PATH + 20];
    BUFFER	    buf;

    buf = buffind(toCHAR(name));
    if (buf && o_filename(buf))
        name = tochar8(o_filename(buf));
    sprintf (title, "WinElvis - [%s]", name);
    SetWindowText (gwp->frameHWnd, title);
#else
    Char *argv[2];
    Char *title;
    ELVBOOL wasmsg;

    /* evaluate the titleformat string */
    argv[0] = (Char *)name;
    argv[1] = NULL;
    title = o_titleformat(gwp);
    if (!title)
        title = (Char *)"$1";
    wasmsg = msghide(ElvTrue);
    title = calculate(title, argv, CALC_MSG);
    msghide(wasmsg);
    if (title && *title)
        name = (char *)title;

    /* change the window's title */
    SetWindowText (gwp->frameHWnd, name);
#endif
}
Esempio n. 3
0
/* initialize the PC BIOS interface. */
static int 
vio_init (int argc,     /* number of command-line arguments */
          char **argv)  /* values of command-line arguments */
{
  int  i;

  /* Choose font colors in the absence of user settings. */
  vc_term = vio_get_default_color ();
  
  vc_current = vc_term;
  vc_color = vc_term;
  vc_italic = 0xff;
  vc_underlined = 0xff;

  /* Dark if black or blue... */
  o_background = (VC_BG (vc_term) <= 1)? 'd': 'l';
  DPRINTF (("o_background: %x, %c\n", VC_BG (vc_term), o_background));

  /* change the terminal mode to cbreak/noecho */
  vio_resume (ElvTrue);

  /* try to get true screen size, from the operating system */
  vio_getsize ();

  /* pretend the cursor is in an impossible place, so we're guaranteed
   * to move it on the first vio_moveto() or vio_draw() call.
   */
  physx = physy = -100;

  /* map the arrow keys */
  for (i = 0; i < QTY (keys); i++)
    {
      if (keys[i].cooked != NULL && keys[i].rawin != NULL)
        {
          mapinsert (toCHAR (keys[i].rawin), (int)strlen (keys[i].rawin),
                     toCHAR (keys[i].cooked), (int)strlen (keys[i].cooked),
                     toCHAR (keys[i].label),
                     keys[i].flags, NULL);
        }
    }

  /* add the global options to the list known to :set */
  o_ttyunderline = ElvTrue;
  optinsert ("vio", QTY (goptdesc), goptdesc, &ttygoptvals.term);

  return argc;
}
Esempio n. 4
0
/* This function inializes the global options.  This can't be done in the
 * usual way, because the values are declared as a union, and C doesn't allow
 * unions to be initialized.
 */
void optglobinit()
{
	int	i;
	char	*envval;

	assert(QTY(ogdesc) == QTY_GLOBAL_OPTS);
#ifdef FEATURE_LPR
	assert(QTY(lpdesc) == QTY_LP_OPTS);
#endif

	/* set each option to a reasonable default */
	optpreset(o_blksize, BLKSIZE, OPT_LOCK|OPT_HIDE);
	optpreset(o_blkhash, BLKHASH, OPT_HIDE);
	optpreset(o_blkcache, BLKCACHE, OPT_HIDE);
	optpreset(o_blkgrow, BLKGROW, OPT_HIDE);
	optflags(o_blkfill) = OPT_LOCK|OPT_HIDE;
	optflags(o_blkhit) = OPT_LOCK|OPT_HIDE;
	optflags(o_blkmiss) = OPT_LOCK|OPT_HIDE;
	optflags(o_blkwrite) = OPT_LOCK|OPT_HIDE;
	optpreset(o_version, toCHAR(VERSION), OPT_LOCK|OPT_HIDE);
	optpreset(o_bitsperchar, 8 * sizeof(CHAR), OPT_LOCK|OPT_HIDE);
	optpreset(o_os, toCHAR(OSNAME), OPT_LOCK|OPT_HIDE);
	optflags(o_gui) = OPT_LOCK|OPT_HIDE;
	optflags(o_session) = OPT_LOCK|OPT_HIDE;
	optflags(o_recovering) = OPT_HIDE;
	o_magic = ElvTrue;
	optpreset(o_magicchar, toCHAR("^$.[*"), OPT_HIDE);
	optpreset(o_magicname, ElvFalse, OPT_HIDE);
	optpreset(o_magicperl, ElvFalse, OPT_HIDE);
	o_prompt = ElvTrue;
	o_autoprint = ElvTrue;
	o_remap = ElvTrue;
	o_report = 5;
	optpreset(o_modeline, ElvFalse, OPT_UNSAFE);
	optpreset(o_modelines, 5, OPT_HIDE);
#ifdef OSSHELLENV
	optpreset(o_shell, toCHAR(getenv(OSSHELLENV)), OPT_HIDE | OPT_UNSAFE);
#else
	optpreset(o_shell, toCHAR(getenv("SHELL")), OPT_HIDE | OPT_UNSAFE);
#endif
	if (!o_shell)
		o_shell = toCHAR(OSSHELL);
	o_tagstack = ElvTrue;
	o_tagkind = ElvFalse;
	o_taglibrary = ElvFalse;
	o_warn = ElvTrue;
	o_window = 12;
	o_wrapscan = ElvTrue;
	optpreset(o_initialstate,'v', OPT_HIDE | OPT_NODFLT); /* vi */
	o_keytime = 3;
	o_usertime = 15;
	optflags(o_exitcode) = OPT_HIDE;
	optpreset(o_security, 'n', OPT_HIDE|OPT_UNSAFE|OPT_NODFLT); /* normal */
	optflags(o_tempsession) = OPT_HIDE;
	optflags(o_newsession) = OPT_HIDE;
	optpreset(o_nearscroll, 10, OPT_HIDE);
	optflags(o_previousfile) = OPT_HIDE|OPT_LOCK;
	optflags(o_previousfileline) = OPT_HIDE|OPT_LOCK;
	optflags(o_previouscommand) = OPT_HIDE|OPT_LOCK;
	optflags(o_previoustag) = OPT_HIDE|OPT_LOCK;
	optflags(o_tagprg) = OPT_HIDE|OPT_UNSAFE;
	o_optimize = ElvTrue;
	optpreset(o_pollfrequency, 20, OPT_HIDE);
	optflags(o_sentenceend) = OPT_HIDE;
	optflags(o_sentencequote) = OPT_HIDE;
	optpreset(o_sentencegap, 2, OPT_HIDE);
	if (!o_directory)
		o_directory = toCHAR(getenv("TMP"));
#ifdef OSDIRECTORY
	if (!o_directory)
		o_directory = toCHAR(OSDIRECTORY);
#endif
	o_errorbells = ElvTrue;
	optpreset(o_nonascii, 'm', OPT_HIDE); /* most */
	optflags(o_digraph) = OPT_HIDE;
	optpreset(o_sync, ElvFalse, OPT_HIDE);
	optflags(o_autoselect) = OPT_HIDE;
	optflags(o_defaultreadonly) = OPT_HIDE;
	optflags(o_exrefresh) = OPT_HIDE;
	optflags(o_verbose) = OPT_HIDE;
	optflags(o_anyerror) = OPT_HIDE;
	optflags(o_program) = OPT_HIDE;
	optpreset(o_mesg, ElvTrue, OPT_HIDE);
	optpreset(o_maptrace, 'o', OPT_HIDE); /* off */
	optpreset(o_maplog, 'o', OPT_HIDE); /* off */
	o_timeout = ElvTrue;
	optpreset(o_matchchar, toCHAR("[]{}()"), OPT_HIDE);
	optpreset(o_show, toCHAR("spell/tag,region"), OPT_HIDE);
	o_writeeol = 's'; /* same */
	optpreset(o_binary, ElvFalse, OPT_HIDE);
	optpreset(o_saveregexp, ElvTrue, OPT_HIDE);
	optpreset(o_hardtabs, 8, OPT_HIDE);
	optpreset(o_redraw, ElvFalse, OPT_HIDE);
	optpreset(o_true, msgtranslate("True"), OPT_HIDE|OPT_FREE);
	optpreset(o_false, msgtranslate("False"), OPT_HIDE|OPT_FREE);
	optpreset(o_animation, 3, OPT_HIDE);
	optpreset(o_completebinary, ElvFalse, OPT_HIDE);
	optpreset(o_optionwidth, 24, OPT_HIDE);
	optpreset(o_smarttab, ElvFalse, OPT_HIDE);
	optpreset(o_smartcase, ElvFalse, OPT_HIDE);
	optpreset(o_hlsearch, ElvFalse, OPT_HIDE);
	o_incsearch = ElvFalse;
	optpreset(o_spelldict, NULL, OPT_HIDE | OPT_UNSAFE);
	optpreset(o_spellsuffix, NULL, OPT_HIDE);
	optpreset(o_locale, NULL, OPT_HIDE);
	optpreset(o_mkexrcfile, NULL, OPT_HIDE);
	optpreset(o_prefersyntax, 'n', OPT_HIDE); /* never */
	optpreset(o_eventignore, NULL, OPT_HIDE);
	optpreset(o_eventerrors, ElvFalse, OPT_HIDE);
	optpreset(o_tweaksection, ElvTrue, OPT_HIDE);
	optpreset(o_listchars, toCHAR("eol:$"), OPT_HIDE);
#ifdef FEATURE_LISTCHARS
	(void)dmnlistchars('<', -1L, 0, NULL, NULL);
#endif
	optpreset(o_cleantext, toCHAR("long"), OPT_HIDE);
	optpreset(o_filenamerules, toCHAR("tilde,dollar,paren,wildcard,special,space"), OPT_HIDE);

	/* Set the "home" option from $HOME */
	envval = getenv("HOME");
	if (envval)
	{
		if (optflags(o_home) & OPT_FREE)
			safefree(o_home);
		o_home = toCHAR(envval);
	}
	else if (!o_home)
	{
		o_home = toCHAR(".");
	}
	optflags(o_home) |= OPT_HIDE | OPT_UNSAFE;

	/* Set the "previousdir" option from $OLDPWD */
	envval = getenv("OLDPWD");
	if (envval)
		o_previousdir = toCHAR(envval);
	else
		o_previousdir = toCHAR(".");
	optflags(o_previousdir) |= OPT_HIDE;

	/* Set the "tags" option from $TAGPATH */
	o_tags = toCHAR(getenv("TAGPATH"));
	if (!o_tags)
	{
		o_tags = toCHAR("tags");
	}

	/* Set the "background" option from $ELVISBG */
	envval = getenv("ELVISBG");
	if (!envval || (strcmp(envval, "dark") && strcmp(envval, "light")))
		envval = "dark";
	optpreset(o_background, *envval, OPT_HIDE|OPT_NODFLT);

	/* Generate the default elvispath value. */
	envval = getenv("ELVISPATH");
	if (envval)
	{
		if (optflags(o_elvispath) & OPT_FREE)
			safefree(o_elvispath);
		o_elvispath = toCHAR(envval);
	}
	else if (!o_elvispath)
	{
		o_elvispath = toCHAR(OSLIBPATH);
	}
	optflags(o_elvispath) |= OPT_HIDE | OPT_UNSAFE;

	/* Generate the default sessionpath value. */
	envval = getenv("SESSIONPATH");
	if (envval)
	{
		if (optflags(o_sessionpath) & OPT_FREE)
			safefree(o_sessionpath);
		o_sessionpath = toCHAR(envval);
	}
	else if (!o_sessionpath)
	{
#ifdef OSSESSIONPATH
		o_sessionpath = toCHAR(OSSESSIONPATH);
#else
		o_sessionpath = toCHAR("~:.");
#endif
	}
	optflags(o_sessionpath) |= (OPT_HIDE | OPT_LOCK);

#ifdef FEATURE_LPR
	/* initialize the printing options */
# ifdef OSLPTYPE
	optpreset(o_lptype, toCHAR(OSLPTYPE), OPT_HIDE);
# else
	optpreset(o_lptype, toCHAR("dumb"), OPT_HIDE);
# endif
	optpreset(o_lpcrlf, ElvFalse, OPT_HIDE);
	optpreset(o_lpout, toCHAR(OSLPOUT), OPT_HIDE | OPT_UNSAFE);
	optpreset(o_lpcolumns, 80, OPT_HIDE);
	optpreset(o_lpwrap, ElvTrue, OPT_HIDE);
	optpreset(o_lplines, 60, OPT_HIDE);
	optpreset(o_lpconvert, ElvFalse, OPT_HIDE);
	optpreset(o_lpformfeed, ElvFalse, OPT_HIDE);
	optpreset(o_lpoptions, NULL, OPT_HIDE);
	optpreset(o_lpnumber, ElvFalse, OPT_HIDE);
	optpreset(o_lpheader, ElvTrue, OPT_HIDE);
	optpreset(o_lpcolor, ElvFalse, OPT_HIDE);
	optpreset(o_lpcontrast, 50, OPT_HIDE);
#endif /* FEATURE_LPR */

	/* inform the options code about these options */
	optinsert("global", QTY(ogdesc), ogdesc, optglob);
#ifdef FEATURE_LPR
	optinsert("lp", QTY(lpdesc), lpdesc, lpval);
#endif

#ifndef NO_USERVARS
	/* initialize user variables */
	for (i = 0; i < QTY(userdesc); i++)
	{
		optflags(optuser[i]) = OPT_HIDE;
	}
	optinsert("user", QTY(userdesc), userdesc, optuser);
#endif
}
Esempio n. 5
0
/* Marks the end of writing.  Returns ElvTrue if all is okay, or ElvFalse if
 * error.
 */
ELVBOOL prggo(void)
{
	int	old0;	/* elvis' stdin */
	int	old1;	/* elvis' stdout */
	int	old2;	/* elvis' stderr */
	CHAR	*arg[3];/* arguments when evaluating command string */

	/* are we supposed to redirect stdin? */
	if (tempwrite[0])
	{
		/* close the file we've been writing to */
		close(fd);

		/* save the old stdin as another fd so we can switch back later */
		old0 = dup(0);
		assert(old0 > 2);

		/* open the temp file as stdin */
		close(0);
#ifdef NDEBUG
		(void)open(tempwrite, O_RDONLY);
#else
		assert(open(tempwrite, O_RDONLY) == 0);
#endif
	}

	/* are we supposed to redirect stdout/stderr ? */
	if (tempread[0])
	{
		/* save the old stdout and stderr as other fds */
		old1 = dup(1);
		old2 = dup(2);
		assert(old1 > 2 && old2 > 2);

		/* open the temp file as stdout/stderr */
		close(1);
		close(2);
#ifdef NDEBUG
		(void)open(tempread, O_WRONLY);
		dup(1);
#else
		assert(open(tempread, O_WRONLY) == 1);
		assert(dup(1) == 2);
#endif
	}

#if 0
	/* if redirecting anything, then evaluate the command string like a
	 * message, substituting the input and/or output files for $1 and $2
	 */
	if ((tempwrite[0] || tempread[0]) && CHARchr(cmd, '$'))
	{
		arg[0] = toCHAR(tempwrite);
		arg[1] = toCHAR(tempread);
		arg[2] = NULL;
		cmd = tochar8(calculate(toCHAR(cmd), arg, CALC_MSG));
		if (!cmd)
		{
			if (tempwrite[0])
				remove(tempwrite);
			if (tempread[0])
				remove(tempread);
			return ElvFalse;
		}
	}
#endif

	/* run the program */
	status = swapsystem(cmd);

	/* if we redirected stdin, undo it now */
	if (tempwrite[0])
	{
		/* undo the redirection */
		close(0);
#ifdef NDEBUG
		(void)dup(old0);
#else
		assert(dup(old0) == 0);
#endif
		close(old0);

		/* delete the temp file */
		remove(tempwrite);
	}

	/* if we redirected stdout/stderr, undo it now and open the temp file */
	if (tempread[0])
	{
		/* undo the redirection */
		close(1);
		close(2);
#ifdef NDEBUG
		(void)dup(old1);
		(void)dup(old2);
#else
		assert(dup(old1) == 1);
		assert(dup(old2) == 2);
#endif
		close(old1);
		close(old2);

		/* open the temp file */
		fd = open(tempread, O_RDONLY);
		assert(fd > 0);
	}

	return ElvTrue;
}
Esempio n. 6
0
/* Repeatedly get events (keystrokes), and call elvis' event functions */
static void 
vio_loop (void)
{
  char  buf[20];
  int    len;
  int    timeout = 0;
  MAPSTATE mst = MAP_CLEAR;
  VWIN  *scan;

  /* perform the -c command or -t tag */
  mainfirstcmd(windefault);

  while (vwins)
    {
      /* reset the ttycaught bitmap */
      ttycaught = 0;

      /* if no window is current, then make the newest current */
      if (!current)
        {
          current = vwins;
        }

      /* redraw the window(s) */
      {
        /* redraw each window; the current one last */
        for (scan = vwins; scan; scan = scan->next)
          { 
            if (scan != current)
              {
                scan->shape = eventdraw ((GUIWIN *)scan);
              }
          }
        current->shape = eventdraw ((GUIWIN *)current);
        movecurs (current);

        /* make the cursor be this window's shape */
        cursorshape (current->shape);
      }

      /* choose a timeout value */
      switch (mst)
        {
        case MAP_CLEAR:  timeout = 0;    break;
        case MAP_USER:  timeout = o_usertime;  break;
        case MAP_KEY:    timeout = o_keytime;  break;
        }

      /* read events */
      do_flush ();
      vio_CM (c_row, c_col);
      len = vio_read (buf, sizeof buf, timeout);

      /* process keystroke data */
      if (len == -2)
        {
          /* vio_read() itself did something.  We don't need to
           * do anything except the usual screen updates.
           */
        }
      else if (len == -1)
        {
          /* Maybe the screen was resized?  Get new size */
          vio_getsize ();

          /* Resize the windows to match the new screen.  The
           * easiest way to do this is to "change" the size of the
           * current window to its original size and force the
           * other windows to compensate.  If there is only one
           * window, then should be resized to the screen size.
           */
          chgsize (current, vwins->next ? 
            current->height : (int)o_ttyrows, ElvTrue);
        }
      else
        {
          mst = eventkeys ((GUIWIN *)current, toCHAR (buf), len);

          /* if first keystroke after running an external
           * program, then we need to expose every window.
           */
          if (afterprg == 1)
            {
              /* reset the flag BEFORE exposing windows,
               * or else the eventexpose() won't work right.
               */
              afterprg = 0;
              vio_resume (ElvTrue);
              for (scan = vwins; scan; scan = scan->next)
                { 
                  eventexpose  ((GUIWIN *)scan, 0, 0,
                                scan->height - 1, (int)(o_ttycolumns - 1));
                }
            }
          else if (afterprg == 2)
            {
              /* it became 2 while processing the earlier
               * keystrokes.  Set it to 1 now, so we'll
               * read one more keystroke before exposing
               * all the windows.
               */
              afterprg = 1;
            }
        }
    }
}
Esempio n. 7
0
File: guiwin.c Progetto: mbert/elvis
static ELVBOOL gwcreategw(char *name, char *firstcmd)
{
    GUI_WINDOW      *gwp;
    DWORD           dwStyle = WS_CHILD | WS_CLIPSIBLINGS |
                              WS_BORDER | WS_VISIBLE;

    /* allocate a new GUI_WINDOW */
    if ((gwp = calloc (1, sizeof (GUI_WINDOW))) == NULL)
        return ElvFalse;
    gwp->nextp = gw_def_win.nextp;
    gw_def_win.nextp = gwp;
    gwp->bg = colorinfo[COLOR_FONT_NORMAL].bg;

    /* set default options */
    gwp->options = gw_def_win.options;
    o_font(gwp) = CHARdup(o_font(&gw_def_win));
    o_propfont(gwp) = CHARdup(o_propfont(&gw_def_win));
    o_titleformat(gwp) = CHARdup(o_titleformat(&gw_def_win));

    /* create the Windows windows */
    gwp->frameHWnd = CreateWindow ("ElvisFrameWnd", "WinElvis",
                                   WS_OVERLAPPEDWINDOW,
                                   CW_USEDEFAULT, 0,
                                   CW_USEDEFAULT, 0,
                                   NULL, NULL, hInst, NULL);
    gw_create_toolbar (gwp);
    gw_create_status_bar (gwp);
    if (o_scrollbar (gwp))
        dwStyle |= WS_VSCROLL;
    gwp->clientHWnd = CreateWindow ("ElvisClientWnd", NULL,
                                    dwStyle, 0, 0, 0, 0,
                                    gwp->frameHWnd, NULL, hInst, NULL);
    gwp->menuHndl = o_menubar (gwp) ?
                    LoadMenu (hInst, MAKEINTRESOURCE (IDM_ELVIS)) : NULL;
    SetMenu (gwp->frameHWnd, gwp->menuHndl);
    if (gwp->menuHndl != NULL)
        DrawMenuBar (gwp->frameHWnd);

    /* resize client window */
    SendMessage (gwp->frameHWnd, WM_SIZE, 0, 0);

    /* set the cursor */
    ShowCursor (FALSE);
    SetCursor (hLeftArrow);
    ShowCursor (TRUE);

    /* set the fonts */
    gw_set_fonts (gwp);

    /* set new title */
    gwretitle ((GUIWIN *)gwp, name);

    /* set window size */
    gw_get_win_size (gwp);
    gw_set_win_size (gwp, 0);

    /* tell elvis that we have a new window */
    eventcreate((GUIWIN *)gwp, &gwp->options.scrollbar, name,
                gwp->numrows, gwp->numcols);

    /* make window active */
    eventfocus((GUIWIN *)gwp, ElvTrue);
    gwp->active = 1;

    /* show the window */
    ShowWindow (gwp->frameHWnd, SW_SHOW);

    /* accept dragging of files */
    DragAcceptFiles (gwp->clientHWnd, TRUE);

    /* disable printing if no printing */
    if (!gw_printing_ok)
        gw_disable_printing (gwp);

    /* if there is a firstcmd, then execute it */
    if (firstcmd)
    {
        winoptions(winofgw((GUIWIN *)gwp));
        exstring(windefault, toCHAR(firstcmd), "+cmd");
    }

    return ElvTrue;
}
Esempio n. 8
0
File: guiwin.c Progetto: mbert/elvis
static ELVBOOL gwcolor (int fontcode, Char *colornam, ELVBOOL isfg, long *colorptr, unsigned char rgb[3])
{
    register int        i, j;
    int			r, g, b;
    char		*rgbfile;
    char		rgbname[100];
    FILE		*fp;
#ifdef FEATURE_IMAGE
    HBITMAP		newimg;
    long		average;
    char		*imagefile;

    /* split the name into a "colornam" part and an "imagefile" part */
    imagefile = colornam;
    if (*imagefile == '#')
        imagefile++;
    while (*imagefile && !elvpunct(*imagefile))
        imagefile++;
    while (*imagefile && imagefile > colornam && !elvspace(*imagefile))
        imagefile--;
    if (!*imagefile)
        imagefile = NULL;
    else if (imagefile > colornam)
        *imagefile++ = '\0';
    else
        colornam = "";
#endif

    /* parse the color name */
    if (*colornam == '#')
    {
        /* Accept X11's "#rrggbb" or "#rgb" notations */
        if (sscanf(tochar8(colornam), "#%2x%*2x%2x%*2x%2x", &r, &g, &b) == 3)
            /* do nothing */;
        else if (sscanf(tochar8(colornam), "#%2x%2x%2x", &r, &g, &b) == 3)
            /* do nothing */;
        else if (sscanf(tochar8(colornam), "#%1x%1x%1x", &r, &g, &b) == 3)
        {
            r *= 17;
            g *= 17;
            b *= 17;
        }
        else
        {
            msg(MSG_ERROR, "[S]bad color notation $1", colornam);
            return ElvFalse;
        }
    }
    else if (!*colornam)
    {
        r = g = b = -1;
    }
    else
    {
        /* Normalize the color name by converting to lowercase and removing
         * whitespace.  We can safely modify the colornam[] buffer in-place.
         */
        for (i = j = 0; colornam[i]; i++)
            if (!elvspace(colornam[i]))
                colornam[j++] = elvtolower(colornam[i]);
        colornam[j] = '\0';

        /* look up the color */
        for (i = 0;
                colortbl[i].name && CHARcmp(toCHAR(colortbl[i].name), colornam);
                i++)
        {
        }
        if (colortbl[i].name)
        {
            /* Use the found color */
            r = colortbl[i].rgb[0];
            g = colortbl[i].rgb[1];
            b = colortbl[i].rgb[2];
        }
        else /* not found -- try "rgb.txt" */
        {
            /* search for the color in the "rgb.txt" file */
            *rgbname = '\0';
            rgbfile = iopath(o_elvispath, "rgb.txt", ElvFalse);
            if (rgbfile) {
                fp = fopen(rgbfile, "r");
                if (fp) {
                    while (fscanf(fp, "%d %d %d %s", &r, &g, &b, &rgbname) == 4
                            && CHARcmp(tochar8(rgbname), colornam))
                    {
                    }
                    fclose(fp);
                }
            }

            /* if we didn't find it there, then fail */
            if (CHARcmp(tochar8(rgbname), colornam)) {
                if (isfg) {
                    memcpy(rgb, colorinfo[COLOR_FONT_NORMAL].da.fg_rgb, 3);
                    *colorptr = colorinfo[COLOR_FONT_NORMAL].fg;
                } else {
                    memcpy(rgb, colorinfo[COLOR_FONT_NORMAL].da.bg_rgb, 3);
                    *colorptr = colorinfo[COLOR_FONT_NORMAL].bg;
                }
                msg(MSG_ERROR, "[S]unknown color $1", colornam);
                return ElvFalse;
            }
        }
    }

#ifdef FEATURE_IMAGE
    if (imagefile && isfg)
    {
        msg(MSG_ERROR, "Can't use images for foreground");
        return ElvFalse;
    }
#endif

    /* if no image or color, then fail */
    if (
#ifdef FEATURE_IMAGE
        !imagefile &&
#endif
        r < 0)
    {
        msg(MSG_ERROR, "missing color name");
        return ElvFalse;
    }

#ifdef FEATURE_IMAGE
    /* if image name was given for "normal" or "idle" font, then load image */
    if (imagefile && (fontcode==COLOR_FONT_NORMAL || fontcode==COLOR_FONT_IDLE))
    {
        /* decide whether to use a tint */
        if (r >= 0)
            average = RGB(r, g, b);
        else
            average = -1;

        /* load the image */
        newimg = gw_load_xpm(imagefile, average, &average, NULL);
        if (newimg)
        {
            /* use the average colors */
            r = GetRValue(average);
            g = GetGValue(average);
            b = GetBValue(average);

            /* if there was an old image, discard it now */
            if (fontcode == COLOR_FONT_NORMAL && normalimage)
                gw_unload_xpm(normalimage);
            if (fontcode == COLOR_FONT_IDLE && idleimage)
                gw_unload_xpm(idleimage);

            /* store the new image */
            if (fontcode == COLOR_FONT_NORMAL)
                normalimage = newimg;
            else
                idleimage = newimg;
        }
        else
        {
            return ElvFalse;
        }
    }
    if (!imagefile && fontcode==COLOR_FONT_NORMAL && normalimage && !isfg)
    {
        gw_unload_xpm(normalimage);
        normalimage = NULL;
    }
    if (!imagefile && fontcode==COLOR_FONT_IDLE && idleimage && !isfg)
    {
        gw_unload_xpm(idleimage);
        idleimage = NULL;
    }
#endif

    /* Success!  Store the color and return ElvTrue */
    *colorptr = RGB(r, g, b);
    rgb[0] = r;
    rgb[1] = g;
    rgb[2] = b;
    return ElvTrue;
}
Esempio n. 9
0
/* This function creates a new block file, and returns ElvTrue if successful,
 * or ElvFalse if failed because the file was already busy.
 */
ELVBOOL 
blkopen (ELVBOOL force,    /* if ElvTrue, open even if "in use" flag set */
         BLK *buf)         /* buffer, holds SUPER block */
{
  static char  dfltname[256];
  int  i;
  APIRET rc;
  ULONG open_flags;
  ULONG open_mode;
  ULONG action;
  ULONG actual;

#ifdef FEATURE_RAM
  if (o_session && !CHARcmp(o_session, toCHAR("ram")))
  {
    nblks = 1024;
    blklist = (BLK **)calloc(nblks, sizeof(BLK *));
    blklist[0] = (BLK *)malloc(o_blksize);
    memcpy(blklist[0], buf, o_blksize);
    return ElvTrue;
  }
#endif

    /* This is a little bit dirty :-)  We try to find a directory for
     * our session files from the list o_sessionpath.  It will be the
     * first writable directory from the list we can find.  The dirty
     * part is we don't want to do this more than once.
     */
    if (sessionDirPtr == NULL) {
        char pathlist[128];
        char *ptr = pathlist;
        unsigned last;
        FILESTATUS fileInfo = {{0}};
        sprintf (sessionDir, "%c", OSPATHDELIM);

    /* search through sessionpath for a writable directory */
    if (!o_sessionpath)
      o_sessionpath = toCHAR(".");
        /* endif */

        /* Go through the directory list in o_sessionpath and use
         * the first writable one for the session file. */
        pathlist[sizeof pathlist - 1u] = '\0';
        strncpy (pathlist, o_sessionpath, sizeof pathlist - 1u);
        if ((ptr = strtok (pathlist, sessionDir)) != NULL) {
            do {
                last = strlen (ptr);
                if (ptr[last-1] == OSPATHSEP)
                    ptr[last-1] = '\0';
                /* endif */
                if (DosQueryPathInfo ((PSZ)ptr, FIL_STANDARD, &fileInfo, 
                        (ULONG)sizeof fileInfo) == NO_ERROR 
                        && (fileInfo.attrFile & 0x01) == 0
                        && (fileInfo.attrFile & 0x10) != 0)
                    break;
                /* endif */
            } while ((ptr = strtok (NULL, sessionDir)) != NULL);
        }/* if */

        /* Found a directory for the session file?  If not, just use
         * the current one else use the one found. */
        if (ptr == NULL)
            sprintf (sessionDir, "%c", '.');
        else {
            strncpy (sessionDir, ptr, sizeof sessionDir - 1u);
            sessionDir[sizeof sessionDir - 1u] = '\0';
        } /* if */

        /* Append a path delimiter and terminate string.  Be sure
         * not to write outside the buffer boundaries... */
        if ((last = strlen (sessionDir)) < sizeof sessionDir - 1u){
            sessionDir[last] = OSPATHSEP;
            sessionDir[last+1] = '\0';
        }/* if */
    } /* if */

  /* If elvis runs other programs, prevent them from inheriting
   * the session file's descriptor.  Also prevent write access
   * to the session file by other processes while this process
   * is using it.
   */
  open_mode = OPEN_FLAGS_NOINHERIT 
        | OPEN_SHARE_DENYWRITE
        | OPEN_ACCESS_READWRITE;

  /* If no session file was explicitly requested, try successive
   * defaults until we find an existing file (if we're trying to
   * recover) or a non-existent file (if we're not trying to recover).
   */
  if (!o_session) {
    i = 1;

    o_session = toCHAR (dfltname);
    o_tempsession = ElvTrue;
    do
    {
      /* protect against trying a ridiculous number of files */
      if (i >= 1000) {
	msg(MSG_FATAL, o_recovering
			? "[s]no session file found in $1"
			: "[s]too many session files in $1", sessionDir);
      }
      sprintf (dfltname, DEFAULT_SESSION, sessionDir, i++);
      open_flags = o_recovering
        ? OPEN_ACTION_FAIL_IF_NEW   | OPEN_ACTION_OPEN_IF_EXISTS
        : OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;

      rc = DosOpen (tochar8 (o_session),
             &fd, 
             &action,
             0L,
             FILE_NORMAL,
             open_flags,
             open_mode,
             NULL);

    } while (rc == ERROR_OPEN_FAILED);
  } else {
    /* Try to open the session file */
    open_flags = 
      OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;

    rc = DosOpen (tochar8(o_session),
           &fd, 
           &action,
           0L,
           FILE_NORMAL,
           open_flags,
           open_mode,
           NULL);
  } /* if */

  /* Error checking. */
  switch (rc)
  {
  case NO_ERROR:
    break;

  case ERROR_SHARING_VIOLATION:
    msg(MSG_FATAL, "session file busy");

  default:
    msg(MSG_FATAL, "no such session");
  } /* if */

  if (action == FILE_EXISTED) {
    /* we're opening an existing session -- definitely not temporary */
    o_tempsession = ElvFalse;
  } else {
    o_newsession = ElvTrue;
    rc = DosWrite (fd, buf, BLKSIZE, &actual);
    if (rc != NO_ERROR || actual < BLKSIZE) {
      DosClose (fd);
      DosDelete (tochar8(o_session));
      fd = NULLHANDLE;
      msg (MSG_FATAL, "no such session");
    } else {
      (void)DosSetFilePtr (fd, 0L, FILE_BEGIN, &actual);
    } /* if */
  } /* if */

  /* Read the first block & mark the session file as being "in use".
   * If already marked as "in use" and !force, then fail.
   */
  rc = DosRead (fd, buf, sizeof buf->super, &actual);
  if (rc != NO_ERROR || actual != sizeof buf->super) {
    msg (MSG_FATAL, "blkopen's read failed");
  } /* if */
  if (buf->super.inuse && !force) {
    return ElvFalse;
  } /* if */
  buf->super.inuse = getpid();

  (void)DosSetFilePtr (fd, 0L, FILE_BEGIN, &actual);
  (void)DosWrite (fd, buf, sizeof buf->super, &actual);

  /* done! */
  return ElvTrue;
}