summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-06-07 13:59:53 (GMT)
committerGuido van Rossum <guido@python.org>1991-06-07 13:59:53 (GMT)
commit689e701919e542803f71a1464e6bf7548f0fd4b7 (patch)
treef7e2984c6a78d80bde71776f04ef3d9a83df82d9 /Python
parent971443b7d46f82f24412560934e9baeefaa094bc (diff)
downloadcpython-689e701919e542803f71a1464e6bf7548f0fd4b7.zip
cpython-689e701919e542803f71a1464e6bf7548f0fd4b7.tar.gz
cpython-689e701919e542803f71a1464e6bf7548f0fd4b7.tar.bz2
Add run_command() to implement "-c command".
Diffstat (limited to 'Python')
-rw-r--r--Python/pythonmain.c82
1 files changed, 73 insertions, 9 deletions
diff --git a/Python/pythonmain.c b/Python/pythonmain.c
index 12e0dfe..b855520 100644
--- a/Python/pythonmain.c
+++ b/Python/pythonmain.c
@@ -47,32 +47,77 @@ extern grammar gram; /* From graminit.c */
int debugging; /* Needed by parser.c */
#endif
+/* Interface to getopt(): */
+extern int optind;
+extern char *optarg;
+extern int getopt PROTO((int, char **, char *));
+
main(argc, argv)
int argc;
char **argv;
{
+ int c;
+ int sts;
+ char *command = NULL;
char *filename = NULL;
FILE *fp = stdin;
- initargs(&argc, &argv);
+ initargs(&argc, &argv); /* Defined in config*.c */
+
+ while ((c = getopt(argc, argv, "c:")) != EOF) {
+ switch (c) {
+
+ default:
+ fprintf(stderr,
+ "usage: %s [-c cmd | file | -] [arg] ...\n",
+ argv[0]);
+ exit(2);
+ /*NOTREACHED*/
+
+ case 'c':
+ if (command != NULL) {
+ fprintf(stderr, "%s: duplicate -c option\n",
+ argv[0]);
+ exit(2);
+ }
+ command = malloc(strlen(optarg) + 2);
+ /* Ignore malloc errors this early... */
+ strcpy(command, optarg);
+ strcat(command, "\n");
+ break;
+
+ }
+ }
- if (argc > 1 && strcmp(argv[1], "-") != 0)
- filename = argv[1];
+ if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
+ filename = argv[optind];
if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
- fprintf(stderr, "python: can't open file '%s'\n",
- filename);
+ fprintf(stderr, "%s: can't open file '%s'\n",
+ argv[0], filename);
exit(2);
}
}
- initall();
+ initall(); /* Defined in config*.c */
setpythonpath(getpythonpath());
- setpythonargv(argc-1, argv+1);
-
- goaway(run(fp, filename == NULL ? "<stdin>" : filename));
+ if (command != NULL) {
+ /* Backup optind and force sys.argv[0] = '-c' */
+ optind--;
+ argv[optind] = "-c";
+ }
+ setpythonargv(argc-optind, argv+optind);
+
+ if (command) {
+ sts = run_command(command) != 0;
+ }
+ else {
+ sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
+ }
+
+ goaway(sts);
/*NOTREACHED*/
}
@@ -219,6 +264,25 @@ run_script(fp, filename)
return 0;
}
+int
+run_command(command)
+ char *command;
+{
+ object *m, *d, *v;
+ m = add_module("__main__");
+ if (m == NULL)
+ return -1;
+ d = getmoduledict(m);
+ v = run_string(command, file_input, d, d);
+ flushline();
+ if (v == NULL) {
+ print_error();
+ return -1;
+ }
+ DECREF(v);
+ return 0;
+}
+
void
print_error()
{