home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
r
/
ril.zip
/
ril.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-08
|
14KB
|
602 lines
/* -*- Mode: C -*-
* ril.c
*
* Description : Roger's Interface Language
* This file implements a TCL front end for processing "RIL" files.
* An RIL file is a file of "RIL code" which can be translated into
* X resource specifications. These resource specs are then merged
* into the current X resource data base.
*
* Author : Roger Reynolds
* Created On : Wed Mar 18 12:34:20 1992
*
* Configuration Management
*
* @(#)ril.c 1.3 6/30/92
*
* VERSION SPR MODIFIER DATE AND TIME
* 1.7 0 rogerr Mon Aug 31 12:23:17 1992
* pass PROJECTROOT define through to ril so it can know where
* to look for app-defaults directory
* 1.6 0 rogerr Mon Aug 31 11:49:44 1992
* use XtNewString for strsave look for files in
* /usr/X11R5/lib/.... now
* 1.5 0 rogerr Tue Jul 28 14:12:11 1992
* fix bug pointed out by use of acc, whereby I was
* re-allocating widgetBuf for every input line read!
* 1.4 0 rogerr Tue Jul 14 16:42:56 1992
* separate paths for Widget and Resource commands, though
* they ultimately end up in the same place, for now.
* 1.3 0 rogerr Tue Jun 30 13:17:55 1992
* set rilXVersion before loading file so a script can tell if
* it is 11.5 or 11.4
* 1.2 0 rogerr Fri Jun 26 16:13:53 1992
* added rilInterInit
*
*/
#include <stdio.h>
#include <errno.h>
#include <ril.h>
static char *ProgramName;
char *rilResolvePathName();
/* Widget command resources - a long string */
static char *widgetBuf;
static int widgetBufLen, widgetBufAllocCnt;
/* Resource command resources - a list of strings */
static char **resbuf;
static int resbufLen, resbufAllocCnt;
static char res[4096];
static char buf[4096];
#define MAX_PARENT_LEVEL 10
static char *rilParentStrings[MAX_PARENT_LEVEL+1];
static char rilParentVar[256];
static int rilParent;
#ifdef DEBUG_ON
#define DEBUG(stmt) { stmt }
#else
#define DEBUG(stmt)
#endif
static int
_widgetCmd(clientData, interp, argc, argv, flag)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
Boolean flag; /* TRUE - place in list for fallback resources */
{
int i, len;
char *name = argv[1];
char *p1, *p2, *p3, *p4;
if (flag)
{
if (resbufAllocCnt == 0)
{
resbuf = (char**)XtCalloc((resbufAllocCnt=1000), sizeof(char*));
}
}
else
{
if (widgetBufAllocCnt == 0)
{
widgetBuf = (char*)XtMalloc(widgetBufAllocCnt=1000);
sprintf (widgetBuf, "\n");
}
}
for (i=2; i < argc; i++)
{
DEBUG (fprintf (stderr, "args[%d] = <%s>", i, argv[i]););
sprintf (buf, "%s\n", argv[i]);
for (p1=buf; *p1 && isspace(*p1); p1++)
if (! *p1) break;
while ((p2 = (char*)strchr(p1, '\n')) != NULL)
{
if (*(p2-1) == '\\')
{
*(p2-1) = ' ';
continue;
}
*p2 = NULL;
if (rilParent > 0)
{
sprintf (res, "%s%s.", rilParentVar, name);
}
else
sprintf (res, "%s.", name);
p4 = res + strlen(res);
for (p3=p1; p3 <= p2; p3++)
{
if (*p3 == '$' && *(p3-1) != '\\')
{
char *var, *val, saved;
for (var= ++p3;
*p3 &&
((*p3 >= '0' && *p3 <= '9') ||
(*p3 >= 'A' && *p3 <= 'Z') ||
(*p3 >= 'a' && *p3 <= 'z') ||
(*p3 == '_'));
p3++)
;
saved = *p3;
*p3 = NULL;
val = Tcl_GetVar(interp, var, NULL);
DEBUG (fprintf (stderr, "var <%s> val=<%s>\n", var, val););
for (; val && *val; val++)
*p4++ = *val;
*p4++ = saved;
*p4 = NULL;
}
else
{
*p4++ = *p3;
*p4 = NULL;
}
}
len = strlen(res);
DEBUG (fprintf (stderr, "<%s> len=%d\n", res, len););
if (flag)
{
if (resbufLen >= resbufAllocCnt)
{
resbufAllocCnt+=100;
resbuf = (char**)
XtRealloc(resbuf, (sizeof(char*)*(resbufAllocCnt)));
}
}
else
{
if ((widgetBufLen + len + 1) >= widgetBufAllocCnt)
{
widgetBufAllocCnt += len + 1000;
widgetBuf = (char*)
XtRealloc(widgetBuf, (widgetBufAllocCnt));
}
}
/* chop off trailing spaces in resource spec */
for (p3=p4-2; isspace(*p3); p3--)
*p3 = NULL;
if (flag)
{
resbuf[resbufLen++] = XtNewString(res);
}
else
{
strcat (widgetBuf, res);
strcat (widgetBuf, "\n");
widgetBufLen += len + 1;
}
for (p1= ++p2; *p1 && isspace(*p1); p1++)
if (! *p1) break;
}
}
return (TCL_OK);
}
static int
widgetCmd(clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
return (_widgetCmd (clientData, interp, argc, argv, FALSE));
}
static int
resourceCmd(clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
/* for now, this does the same thing as widgetCommand */
return (_widgetCmd (clientData, interp, argc, argv, FALSE));
}
static int
rilParentCmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
int i;
if (!strcmp(argv[1], "push"))
{
DEBUG (fprintf (stderr, "RilParent push <%s>\n", argv[2]););
if (++rilParent >= MAX_PARENT_LEVEL)
{
fprintf (stderr, "RilParent: too many levels deep\n");
exit(-1);
}
rilParentStrings[rilParent] = (char*) XtMalloc(strlen(argv[2])+1);
strcpy (rilParentStrings[rilParent], argv[2]);
strcpy (rilParentVar, rilParentStrings[1]);
for (i=2; i <= rilParent; i++)
strcat(rilParentVar, rilParentStrings[i]);
Tcl_SetVar (interp, "rilParent", rilParentVar, NULL);
}
else if (!strcmp(argv[1], "pop"))
{
DEBUG (fprintf (stderr, "RilParent pop\n"););
if (rilParent < 1)
{
fprintf (stderr, "RilParent: too many pops\n");
exit(-1);
}
XtFree (rilParentStrings[rilParent--]);
strcpy (rilParentVar, rilParentStrings[1]);
for (i=2; i <= rilParent; i++)
strcat(rilParentVar, rilParentStrings[i]);
Tcl_SetVar (interp, "rilParent", rilParentVar, NULL);
}
else
{
fprintf (stderr, "RilParent: push or pop required\n");
exit (-1);
}
DEBUG (fprintf (stderr, "rilParentVar = <%s>\n", rilParentVar););
return (TCL_OK);
}
static int
rilSourceCmd (clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char *argv[];
{
return (doLoadFile(interp, argv[1]));
}
static int
rilCreateCommands(interp)
Tcl_Interp *interp;
{
Tcl_CreateCommand(interp, "Widget", widgetCmd, (ClientData) "Widget",
(Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "Resources", resourceCmd, (ClientData) "Resources",
(Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "RilParent", rilParentCmd, (ClientData) "RilParent",
(Tcl_CmdDeleteProc *) NULL);
Tcl_CreateCommand(interp, "RilSource", rilSourceCmd, (ClientData) "RilSource",
(Tcl_CmdDeleteProc *) NULL);
}
int
rilLoadFile(dpy, filename, interp)
Display *dpy;
char *filename;
Tcl_Interp *interp;
{
Boolean flag = FALSE;
char buf[10];
if (!interp)
{
interp = Tcl_CreateInterp();
flag = TRUE;
}
rilCreateCommands(interp);
sprintf (buf, "%d.%d", XProtocolVersion(dpy), XtSpecificationRelease);
Tcl_SetVar (interp, "rilXVersion", buf, NULL);
Tcl_SetVar (interp, "rilProjectRoot", PROJECTROOT, NULL);
doLoadFile(interp, filename);
if (flag)
Tcl_DeleteInterp(interp);
}
static int
doLoadFile(interp, filename)
Tcl_Interp *interp;
char *filename;
{
int result;
char cmdbuf[256];
/*
* look for .ril file in current dir, then in XAPPLRESDIR then in
* default /usr/X11R5/lib/X11/app-defaults
*/
sprintf (cmdbuf, "source \"%s\"", filename);
if ((result = rilEvalCommand(interp, cmdbuf)) == TCL_OK)
{
DEBUG (fprintf(stderr,"executed <%s> ok\n", cmdbuf););
return (result);
}
Tcl_ResetResult(interp);
sprintf (cmdbuf, "source \"%s/%s\"",
getenv("XAPPLRESDIR"), filename);
if ((result = rilEvalCommand(interp, cmdbuf)) == TCL_OK)
{
DEBUG (fprintf(stderr,"executed <%s> ok\n", cmdbuf););
return (result);
}
Tcl_ResetResult(interp);
sprintf (cmdbuf, "source \"%s/lib/X11/app-defaults/%s\"",
PROJECTROOT, filename);
if ((result = rilEvalCommand(interp, cmdbuf)) == TCL_OK)
{
DEBUG (fprintf(stderr,"executed <%s> ok\n", cmdbuf););
return (result);
}
Tcl_ResetResult(interp);
sprintf (cmdbuf, "source \"%s/lib/X11/app-defaults/%s/%s\"",
PROJECTROOT, ProgramName, filename);
if ((result = rilEvalCommand(interp, cmdbuf)) == TCL_OK)
{
DEBUG (fprintf(stderr,"executed <%s> ok\n", cmdbuf););
return (result);
}
fprintf (stderr, "Could not find ril file %s\n", filename);
exit(-1);
}
static int rilEvalCommand (interp, cmd)
Tcl_Interp *interp;
char *cmd;
{
errno = 0;
if (Tcl_Eval(interp, cmd, 0, 0) == TCL_OK)
return (TCL_OK);
else if (errno == ENOENT)
return (TCL_ERROR);
else
{
#if 0
/* nice if line number was correct */
fprintf (stderr, "Error <%s> command <%s> line %d\n",
interp->result, cmd, interp->errorLine);
#else
fprintf (stderr, "Error <%s> command <%s>\n", interp->result, cmd);
#endif
exit(-1);
}
}
int
rilCompile(filename)
char *filename;
{
int i, result;
char cmdbuf[256];
Tcl_Interp *interp;
/* -- Get ready TCL */
interp = Tcl_CreateInterp();
rilCreateCommands(interp);
sprintf (cmdbuf, "source %s", filename);
if ((result = Tcl_Eval(interp, cmdbuf, 0, 0)) == TCL_OK)
{
printf ("\n!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf ("! RESOURCE COMMAND\n");
printf ("!!!!!!!!!!!!!!!!!!!!!!!!!\n");
for (i=0; i < resbufLen; i++)
printf ("%s\n", resbuf[i]);
printf ("\n!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf ("! WIDGET COMMAND \n");
printf ("!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf ("\n%s\n", widgetBuf);
}
fprintf (stderr, "%s\n", interp->result);
return (result);
}
int
rilGenerate (filename, str)
char *filename;
char **str;
{
#if 0
int result;
char cmdbuf[256];
Tcl_Interp *interp;
/* -- Get ready TCL */
interp = Tcl_CreateInterp();
rilCreateCommands(interp);
sprintf (cmdbuf, "source %s", filename);
result = Tcl_Eval(interp, cmdbuf, 0, 0);
fprintf (stderr, "%s\n", interp->result);
*str = resbuf;
return (result);
#endif
}
Widget
rilInterpInit(interp, app_context_return, application_class, options, num_options,
argc_in_out, argv_in_out, fallback_resources,
args_in, num_args_in)
Tcl_Interp *interp;
XtAppContext * app_context_return;
String application_class;
XrmOptionDescRec *options;
Cardinal num_options, *argc_in_out, num_args_in;
String *argv_in_out, * fallback_resources;
ArgList args_in;
{
XtAppContext app_con;
Display * dpy;
String *saved_argv;
register int i, saved_argc = *argc_in_out;
Widget root;
Arg args[3], *merged_args;
Cardinal num = 0;
char rilFileName[80];
XrmDatabase db;
XtToolkitInitialize();
/*
* Save away argv and argc so we can set the properties later
*/
saved_argv = (String *)
XtMalloc ( (Cardinal)((*argc_in_out + 1) * sizeof(String)) );
for (i = 0 ; i < saved_argc ; i++)
saved_argv[i] = argv_in_out[i];
saved_argv[i] = NULL; /* NULL terminate that sucker. */
app_con = XtCreateApplicationContext();
dpy = XtOpenDisplay(app_con, (String) NULL, NULL, application_class,
options, num_options, argc_in_out, argv_in_out);
if (dpy == NULL)
{
fprintf (stderr, "Can't Open display");
exit (-1);
}
if ((ProgramName = (char*)strrchr(argv_in_out[0], '/')) != NULL)
++ProgramName;
else
ProgramName = argv_in_out[0];
sprintf (rilFileName, "%s.ril", application_class);
rilLoadFile (dpy, rilFileName, interp);
if (resbufLen > 0)
{
resbuf[resbufLen] = NULL;
XtAppSetFallbackResources(app_con, resbuf);
}
else
{
}
db = XrmGetStringDatabase(widgetBuf);
XrmMergeDatabases (db, &(dpy->db));
XtSetArg(args[num], XtNscreen, DefaultScreenOfDisplay(dpy)); num++;
XtSetArg(args[num], XtNargc, saved_argc); num++;
XtSetArg(args[num], XtNargv, saved_argv); num++;
merged_args = XtMergeArgLists(args_in, num_args_in, args, num);
num += num_args_in;
root = XtAppCreateShell(NULL, application_class,
applicationShellWidgetClass,dpy, merged_args, num);
if (app_context_return != NULL)
*app_context_return = app_con;
XtFree((XtPointer)merged_args);
XtFree((XtPointer)saved_argv);
return(root);
}
Widget
rilInitialize(app_context_return, application_class, options, num_options,
argc_in_out, argv_in_out, fallback_resources,
args_in, num_args_in)
XtAppContext * app_context_return;
String application_class;
XrmOptionDescRec *options;
Cardinal num_options, *argc_in_out, num_args_in;
String *argv_in_out, * fallback_resources;
ArgList args_in;
{
return (rilInterpInit(NULL, app_context_return, application_class,
options, num_options,
argc_in_out, argv_in_out, fallback_resources,
args_in, num_args_in));
}
/* A work in progress */
char *rilResolvePathName(dpy)
Display *dpy;
{
#if 0
char* getenv();
char* filename;
char* path;
Boolean deallocate = False;
if (!(path = getenv("XUSERFILESEARCHPATH")))
{
char *old_path;
char *homedir;
homedir = getenv("HOME");
if (!(old_path = getenv("XAPPLRESDIR")))
{
char *path_default =
"%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
path = XtMalloc((6*strlen(homedir) + strlen(path_default)));
sprintf(path, path_default,
homedir, homedir, homedir, homedir, homedir, homedir );
}
else
{
char *path_default =
"%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
path = XtMalloc((6*strlen(old_path) + 2*strlen(homedir)
+ strlen(path_default)));
sprintf(path, path_default, old_path, old_path, old_path, homedir,
old_path, old_path, old_path, homedir);
}
deallocate = True;
}
filename = XtResolvePathname(dpy, NULL, NULL, NULL, path, NULL, 0, NULL);
if (deallocate)
XtFree(path);
return (filename);
#endif
}