home *** CD-ROM | disk | FTP | other *** search
- /*
- * Tcl command -- provide a Tcl CLI-command with awk-like command syntax
- *
- * Copyright 1990 Hackercorp
- * Permission to use, copy, modify, and distribute this
- * software and its documentation for any purpose and without
- * fee is hereby granted, provided that the above copyright
- * notice appear in all copies. Hackercorp makes no
- * representations about the suitability of this software for
- * any purpose. It is provided "as is" without express or
- * implied warranty.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <tcl.h>
-
- void
- print_result(fp, returnval, result_text)
- FILE *fp;
- int returnval;
- char *result_text;
- {
- if (returnval == TCL_OK)
- {
- if (result_text && *result_text != 0)
- {
- fprintf(fp, "%s\n", result_text);
- }
- }
- else
- {
- fprintf(stderr, "%s: %s\n",
- (returnval == TCL_ERROR) ? "Error" : "Bad return code",
- result_text);
- }
- }
-
- int
- cmdGetEnv(clientData, interp, argc, argv)
- ClientData clientData; /* Not used. */
- Tcl_Interp *interp;
- int argc;
- int *argv;
- {
- char *getenv();
-
- if (argc != 2)
- {
- sprintf(interp->result, "wrong # args: should be \"%.50s name\"",
- argv[0]);
- return TCL_ERROR;
- }
- Tcl_Return(interp, getenv(argv[1]), TCL_STATIC);
- return TCL_OK;
- }
-
- int
- main(argc, argv)
- int argc;
- char **argv;
- {
- Tcl_Interp *interp;
- int result;
-
- interp = Tcl_CreateInterp();
- Tcl_CreateCommand(interp, "getenv", cmdGetEnv, (ClientData) NULL,
- (void (*)()) NULL);
- stream_init(interp);
-
- /*
- * if no arguments, give the user a Tcl command prompt
- *
- * if first arg is "-f", the following arg is a file name to do a
- * "source" command on (to get Tcl to load the file)
- *
- * argv is set to be a list of arguments that follow the filename or an
- * empty string if there are none
- *
- * if there arguments but there wasn't a -f, they are evaluated as a
- * command by the tcl interpreter
- */
-
- if (argc == 1)
- commandloop(interp, stdin, stdout, 1);
- else if ((argc >= 3) && (strcmp(argv[1], "-f") == 0))
- {
- FILE *fp;
-
- if (argc > 3)
- {
- char *args;
-
- args = Tcl_Merge(argc - 3, &argv[3]);
- Tcl_SetVar(interp, "argv", args, 1);
- ckfree(args);
- }
-
- fp = fopen(argv[2], "r");
- if(!fp) {
- perror(argv[2]);
- } else {
- commandloop(interp, fp, stdout, 0);
- fclose(fp);
- }
- }
- else
- {
- if (argc > 2)
- {
- char *args;
-
- args = Tcl_Merge(argc - 2, &argv[2]);
- Tcl_SetVar(interp, "argv", args, 1);
- ckfree(args);
- }
-
- result = Tcl_Eval(interp, argv[1], 0, (char **)NULL);
- print_result(stdout, result, interp->result);
- }
-
- Tcl_DeleteInterp(interp);
- exit(0);
- }
-
- commandloop(interp, in, out, interactive)
- Tcl_Interp *interp;
- FILE *in;
- FILE *out;
- int interactive;
- {
- char *cmd;
- char *p;
- register char *p2;
- int c, i, result;
-
- cmd = (char *)ckalloc(32767);
- while (1)
- {
- if (interactive)
- {
- clearerr(in);
- fputs("% ", out);
- fflush(out);
- }
- p = cmd;
- while (1)
- {
- c = getc(in);
- if (c == EOF)
- {
- if (p == cmd)
- {
- goto endOfFile;
- }
- goto gotCommand;
- }
- if (c == '\n')
- {
- register char *p2;
- int parens, brackets, numBytes;
-
- parens = 0;
- brackets = 0;
- for (p2 = cmd; p2 < p; p2++)
- {
- switch (*p2)
- {
- case '\\':
- Tcl_Backslash(p2, &numBytes);
- p2 += numBytes - 1;
- break;
- case '{':
- parens++;
- break;
- case '}':
- parens--;
- break;
- case '[':
- brackets++;
- break;
- case ']':
- brackets--;
- break;
- }
- }
- if ((parens <= 0) && (brackets <= 0))
- {
- goto gotCommand;
- }
- }
- *p = c;
- p++;
- }
- gotCommand:
- *p = 0;
-
- result = Tcl_Eval(interp, cmd, 0, &p);
- if (interactive)
- print_result(out, result, interp->result);
- }
- endOfFile:
- ckfree(cmd);
- }
-