* do_watch -- handler for \watch * * We break this out of exec_command to avoid having to plaster "volatile" * onto a bunch of exec_command's variables to silence stupider compilers. */
| 4899 | * onto a bunch of exec_command's variables to silence stupider compilers. |
| 4900 | */ |
| 4901 | static bool |
| 4902 | do_watch(PQExpBuffer query_buf, double sleep) |
| 4903 | { |
| 4904 | long sleep_ms = (long) (sleep * 1000); |
| 4905 | printQueryOpt myopt = pset.popt; |
| 4906 | const char *strftime_fmt; |
| 4907 | const char *user_title; |
| 4908 | char *title; |
| 4909 | int title_len; |
| 4910 | int res = 0; |
| 4911 | |
| 4912 | if (!query_buf || query_buf->len <= 0) |
| 4913 | { |
| 4914 | pg_log_error("\\watch cannot be used with an empty query"); |
| 4915 | return false; |
| 4916 | } |
| 4917 | |
| 4918 | /* |
| 4919 | * Choose format for timestamps. We might eventually make this a \pset |
| 4920 | * option. In the meantime, using a variable for the format suppresses |
| 4921 | * overly-anal-retentive gcc warnings about %c being Y2K sensitive. |
| 4922 | */ |
| 4923 | strftime_fmt = "%c"; |
| 4924 | |
| 4925 | /* |
| 4926 | * Set up rendering options, in particular, disable the pager, because |
| 4927 | * nobody wants to be prompted while watching the output of 'watch'. |
| 4928 | */ |
| 4929 | myopt.topt.pager = 0; |
| 4930 | |
| 4931 | /* |
| 4932 | * If there's a title in the user configuration, make sure we have room |
| 4933 | * for it in the title buffer. Allow 128 bytes for the timestamp plus 128 |
| 4934 | * bytes for the rest. |
| 4935 | */ |
| 4936 | user_title = myopt.title; |
| 4937 | title_len = (user_title ? strlen(user_title) : 0) + 256; |
| 4938 | title = pg_malloc(title_len); |
| 4939 | |
| 4940 | for (;;) |
| 4941 | { |
| 4942 | time_t timer; |
| 4943 | char timebuf[128]; |
| 4944 | long i; |
| 4945 | |
| 4946 | /* |
| 4947 | * Prepare title for output. Note that we intentionally include a |
| 4948 | * newline at the end of the title; this is somewhat historical but it |
| 4949 | * makes for reasonably nicely formatted output in simple cases. |
| 4950 | */ |
| 4951 | timer = time(NULL); |
| 4952 | strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer)); |
| 4953 | |
| 4954 | if (user_title) |
| 4955 | snprintf(title, title_len, _("%s\t%s (every %gs)\n"), |
| 4956 | user_title, timebuf, sleep); |
| 4957 | else |
| 4958 | snprintf(title, title_len, _("%s (every %gs)\n"), |
no test coverage detected