diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c new file mode 100644 index b6cef94..bfd5bf5 *** a/src/bin/psql/mainloop.c --- b/src/bin/psql/mainloop.c *************** MainLoop(FILE *source) *** 43,48 **** --- 43,49 ---- volatile promptStatus_t prompt_status = PROMPT_READY; volatile int count_eof = 0; volatile bool die_on_error = false; + Commands *cmd = pset.commands; /* Save the prior command source */ FILE *prev_cmd_source; *************** MainLoop(FILE *source) *** 135,140 **** --- 136,156 ---- prompt_status = PROMPT_READY; line = gets_interactive(get_prompt(prompt_status)); } + else if (pset.commands != NULL) + { + pset.cur_cmd_interactive = false; + + if (cmd != NULL) + { + line = cmd->actions; + cmd = cmd->next; + } + else + { + successResult = EXIT_SUCCESS; + break; + } + } else { line = gets_fromFile(source); diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h new file mode 100644 index d34dc28..46d2a81 *** a/src/bin/psql/settings.h --- b/src/bin/psql/settings.h *************** enum trivalue *** 77,82 **** --- 77,88 ---- TRI_YES }; + typedef struct _Commands + { + char *actions; + struct _Commands *next; + } Commands; + typedef struct _psqlSettings { PGconn *db; /* connection to backend */ *************** typedef struct _psqlSettings *** 129,134 **** --- 135,141 ---- const char *prompt2; const char *prompt3; PGVerbosity verbosity; /* current error verbosity level */ + Commands *commands; } PsqlSettings; extern PsqlSettings pset; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c new file mode 100644 index 28ba75a..cbceb91 *** a/src/bin/psql/startup.c --- b/src/bin/psql/startup.c *************** enum _actions *** 54,60 **** ACT_SINGLE_SLASH, ACT_LIST_DB, ACT_SINGLE_QUERY, ! ACT_FILE }; struct adhoc_opts --- 54,61 ---- ACT_SINGLE_SLASH, ACT_LIST_DB, ACT_SINGLE_QUERY, ! ACT_FILE, ! ACT_COMMANDS }; struct adhoc_opts *************** main(int argc, char *argv[]) *** 158,163 **** --- 159,166 ---- SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2); SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3); + pset.commands = NULL; + parse_psql_options(argc, argv, &options); /* *************** main(int argc, char *argv[]) *** 326,331 **** --- 329,340 ---- ? EXIT_SUCCESS : EXIT_FAILURE; } + else if (options.action == ACT_COMMANDS) + { + pset.notty = true; + successResult = MainLoop(stdin); + } + /* * or otherwise enter interactive main loop */ *************** parse_psql_options(int argc, char *argv[ *** 403,409 **** memset(options, 0, sizeof *options); ! while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01", long_options, &optindex)) != -1) { switch (c) --- 412,418 ---- memset(options, 0, sizeof *options); ! while ((c = getopt_long(argc, argv, "aAbc:C:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01", long_options, &optindex)) != -1) { switch (c) *************** parse_psql_options(int argc, char *argv[ *** 427,432 **** --- 436,460 ---- else options->action = ACT_SINGLE_QUERY; break; + case 'C': + { + Commands *cmd = pg_malloc(sizeof(Commands)); + Commands *ptr = pset.commands; + + if (ptr == NULL) + pset.commands = cmd; + else + { + while (ptr->next != NULL) + ptr = ptr->next; + + ptr->next = cmd; + } + cmd->next = NULL; + cmd->actions = pg_strdup(optarg); + options->action = ACT_COMMANDS; + } + break; case 'd': options->dbname = pg_strdup(optarg); break;