[5ca5f8e] | 1 | /* Copyright (c) 2004 James Kretchmar. All rights reserved. |
---|
[7d4fbcd] | 2 | * |
---|
[5ca5f8e] | 3 | * Redistribution and use in source and binary forms, with or without |
---|
| 4 | * modification, are permitted provided that the following conditions are |
---|
| 5 | * met: |
---|
| 6 | * |
---|
| 7 | * * Redistributions of source code must retain the above copyright |
---|
| 8 | * notice, this list of conditions and the following disclaimer. |
---|
| 9 | * |
---|
| 10 | * * Redistributions in binary form must reproduce the above copyright |
---|
| 11 | * notice, this list of conditions and the following disclaimer in |
---|
| 12 | * the documentation and/or other materials provided with the |
---|
| 13 | * distribution. |
---|
| 14 | * |
---|
| 15 | * * Redistributions in any form must be accompanied by information on |
---|
| 16 | * how to obtain complete source code for the Owl software and any |
---|
| 17 | * accompanying software that uses the Owl software. The source code |
---|
| 18 | * must either be included in the distribution or be available for no |
---|
| 19 | * more than the cost of distribution plus a nominal fee, and must be |
---|
| 20 | * freely redistributable under reasonable conditions. For an |
---|
| 21 | * executable file, complete source code means the source code for |
---|
| 22 | * all modules it contains. It does not include source code for |
---|
| 23 | * modules or files that typically accompany the major components of |
---|
| 24 | * the operating system on which the executable file runs. |
---|
| 25 | * |
---|
| 26 | * |
---|
| 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
| 28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 29 | * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR |
---|
| 30 | * NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
---|
| 31 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 32 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 33 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
---|
| 34 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
---|
| 35 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
---|
| 36 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
---|
| 37 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
[7d4fbcd] | 38 | */ |
---|
| 39 | |
---|
| 40 | #include <stdio.h> |
---|
| 41 | #include <unistd.h> |
---|
| 42 | #include <stdlib.h> |
---|
| 43 | #include <string.h> |
---|
| 44 | #include <signal.h> |
---|
[5145235] | 45 | #include <time.h> |
---|
[7d4fbcd] | 46 | #include <sys/param.h> |
---|
[700c712] | 47 | #include <sys/types.h> |
---|
[8232149] | 48 | #include <termios.h> |
---|
[38cf544c] | 49 | #include <sys/stat.h> |
---|
[7d4fbcd] | 50 | #include "owl.h" |
---|
| 51 | |
---|
[2a2bb60] | 52 | #if OWL_STDERR_REDIR |
---|
[e8b95f8] | 53 | #ifdef HAVE_SYS_IOCTL_H |
---|
[2a2bb60] | 54 | #include <sys/ioctl.h> |
---|
[e8b95f8] | 55 | #endif |
---|
| 56 | #ifdef HAVE_SYS_FILIO_H |
---|
| 57 | #include <sys/filio.h> |
---|
| 58 | #endif |
---|
[2a2bb60] | 59 | int stderr_replace(void); |
---|
| 60 | #endif |
---|
| 61 | |
---|
[1aee7d9] | 62 | static const char fileIdent[] = "$Id$"; |
---|
| 63 | |
---|
[b2b0773] | 64 | owl_global g; |
---|
| 65 | |
---|
[de22c3d] | 66 | int main(int argc, char **argv, char **env) |
---|
| 67 | { |
---|
[7d4fbcd] | 68 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
| 69 | owl_editwin *tw; |
---|
| 70 | owl_popwin *pw; |
---|
[ced25d1] | 71 | int j, ret, initialsubs, debug, argcsave, followlast; |
---|
[d09e5a1] | 72 | int newmsgs, zpendcount, nexttimediff; |
---|
[7d4fbcd] | 73 | struct sigaction sigact; |
---|
[bc220b2] | 74 | char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE], *perl_mainloop_hook; |
---|
[7d4fbcd] | 75 | owl_filter *f; |
---|
[bd3f232] | 76 | owl_style *s; |
---|
[a15a84f] | 77 | time_t nexttime, now; |
---|
| 78 | struct tm *today; |
---|
[38cf544c] | 79 | char *dir; |
---|
[8232149] | 80 | struct termios tio; |
---|
[09489b89] | 81 | #ifdef HAVE_LIBZEPHYR |
---|
[be0a79f] | 82 | ZNotice_t notice; |
---|
[09489b89] | 83 | #endif |
---|
[2a2bb60] | 84 | #if OWL_STDERR_REDIR |
---|
| 85 | int newstderr; |
---|
| 86 | #endif |
---|
[7d4fbcd] | 87 | |
---|
| 88 | argcsave=argc; |
---|
| 89 | argvsave=argv; |
---|
| 90 | configfile=NULL; |
---|
| 91 | tty=NULL; |
---|
| 92 | debug=0; |
---|
[bd3f232] | 93 | initialsubs=1; |
---|
[bc220b2] | 94 | perl_mainloop_hook = NULL; |
---|
[7d4fbcd] | 95 | if (argc>0) { |
---|
| 96 | argv++; |
---|
| 97 | argc--; |
---|
| 98 | } |
---|
| 99 | while (argc>0) { |
---|
| 100 | if (!strcmp(argv[0], "-n")) { |
---|
| 101 | initialsubs=0; |
---|
| 102 | argv++; |
---|
| 103 | argc--; |
---|
| 104 | } else if (!strcmp(argv[0], "-c")) { |
---|
| 105 | if (argc<2) { |
---|
| 106 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
| 107 | usage(); |
---|
| 108 | exit(1); |
---|
| 109 | } |
---|
| 110 | configfile=argv[1]; |
---|
| 111 | argv+=2; |
---|
| 112 | argc-=2; |
---|
| 113 | } else if (!strcmp(argv[0], "-t")) { |
---|
| 114 | if (argc<2) { |
---|
| 115 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
| 116 | usage(); |
---|
| 117 | exit(1); |
---|
| 118 | } |
---|
| 119 | tty=argv[1]; |
---|
| 120 | argv+=2; |
---|
| 121 | argc-=2; |
---|
| 122 | } else if (!strcmp(argv[0], "-d")) { |
---|
| 123 | debug=1; |
---|
| 124 | argv++; |
---|
| 125 | argc--; |
---|
[a68f05d] | 126 | } else if (!strcmp(argv[0], "-D")) { |
---|
| 127 | debug=1; |
---|
| 128 | unlink(OWL_DEBUG_FILE); |
---|
| 129 | argv++; |
---|
| 130 | argc--; |
---|
[7d4fbcd] | 131 | } else if (!strcmp(argv[0], "-v")) { |
---|
| 132 | printf("This is owl version %s\n", OWL_VERSION_STRING); |
---|
| 133 | exit(0); |
---|
| 134 | } else { |
---|
| 135 | fprintf(stderr, "Uknown argument\n"); |
---|
| 136 | usage(); |
---|
| 137 | exit(1); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
[176d3443] | 141 | owl_function_debugmsg("startup: Finished parsing arguments"); |
---|
| 142 | |
---|
[be0a79f] | 143 | #ifdef HAVE_LIBZEPHYR |
---|
[7d4fbcd] | 144 | /* zephyr init */ |
---|
[be0a79f] | 145 | ret=owl_zephyr_initialize(); |
---|
| 146 | if (ret) { |
---|
[7d4fbcd] | 147 | exit(1); |
---|
| 148 | } |
---|
[be0a79f] | 149 | #endif |
---|
| 150 | |
---|
[7d4fbcd] | 151 | /* signal handler */ |
---|
[c9e72d1] | 152 | /*sigact.sa_handler=sig_handler;*/ |
---|
| 153 | sigact.sa_sigaction=sig_handler; |
---|
[7d4fbcd] | 154 | sigemptyset(&sigact.sa_mask); |
---|
[c9e72d1] | 155 | sigact.sa_flags=SA_SIGINFO; |
---|
[7d4fbcd] | 156 | sigaction(SIGWINCH, &sigact, NULL); |
---|
| 157 | sigaction(SIGALRM, &sigact, NULL); |
---|
[c9e72d1] | 158 | sigaction(SIGPIPE, &sigact, NULL); |
---|
[fe1f605] | 159 | sigaction(SIGTERM, &sigact, NULL); |
---|
| 160 | sigaction(SIGHUP, &sigact, NULL); |
---|
[7d4fbcd] | 161 | |
---|
[8232149] | 162 | /* save initial terminal settings */ |
---|
| 163 | tcgetattr(0, owl_global_get_startup_tio(&g)); |
---|
| 164 | |
---|
| 165 | /* turn ISTRIP off */ |
---|
| 166 | tcgetattr(0, &tio); |
---|
| 167 | tio.c_iflag &= ~ISTRIP; |
---|
| 168 | tcsetattr(0, TCSAFLUSH, &tio); |
---|
| 169 | |
---|
[7d4fbcd] | 170 | /* screen init */ |
---|
[b20de4f] | 171 | if (!getenv("TERMINFO")) { |
---|
| 172 | sprintf(buff, "TERMINFO=%s", TERMINFO); |
---|
| 173 | putenv(buff); |
---|
| 174 | owl_function_debugmsg("startup: setting TERMINFO to %s", TERMINFO); |
---|
| 175 | } else { |
---|
| 176 | owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO")); |
---|
| 177 | } |
---|
[7d4fbcd] | 178 | initscr(); |
---|
| 179 | start_color(); |
---|
[ac70242] | 180 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
[7d4fbcd] | 181 | use_default_colors(); |
---|
[ac70242] | 182 | #endif |
---|
[7d4fbcd] | 183 | raw(); |
---|
| 184 | noecho(); |
---|
| 185 | |
---|
| 186 | /* define simple color pairs */ |
---|
| 187 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
| 188 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, -1); |
---|
| 189 | init_pair(OWL_COLOR_RED, COLOR_RED, -1); |
---|
| 190 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, -1); |
---|
| 191 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, -1); |
---|
| 192 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, -1); |
---|
| 193 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1); |
---|
| 194 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, -1); |
---|
| 195 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, -1); |
---|
| 196 | } |
---|
[38cf544c] | 197 | |
---|
[bd3f232] | 198 | /* owl global init */ |
---|
[7d4fbcd] | 199 | owl_global_init(&g); |
---|
| 200 | if (debug) owl_global_set_debug_on(&g); |
---|
[b20de4f] | 201 | owl_function_debugmsg("startup: first available debugging message"); |
---|
[7d4fbcd] | 202 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
[09489b89] | 203 | #ifdef HAVE_LIBZEPHYR |
---|
| 204 | owl_global_set_havezephyr(&g); |
---|
| 205 | #endif |
---|
| 206 | owl_global_set_haveaim(&g); |
---|
[2a2bb60] | 207 | |
---|
| 208 | #if OWL_STDERR_REDIR |
---|
[a0a5179] | 209 | /* Do this only after we've started curses up... */ |
---|
| 210 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
[2a2bb60] | 211 | newstderr = stderr_replace(); |
---|
[afbf668] | 212 | owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ, |
---|
| 213 | stderr_redirect_handler, NULL); |
---|
[2a2bb60] | 214 | #endif |
---|
| 215 | |
---|
[38cf544c] | 216 | /* create the owl directory, in case it does not exist */ |
---|
[a0a5179] | 217 | owl_function_debugmsg("startup: creating owl directory, if not present"); |
---|
[38cf544c] | 218 | dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR); |
---|
| 219 | mkdir(dir, S_IRWXU); |
---|
| 220 | owl_free(dir); |
---|
[5145235] | 221 | |
---|
[ced25d1] | 222 | /* set the tty, either from the command line, or by figuring it out */ |
---|
[a0a5179] | 223 | owl_function_debugmsg("startup: setting tty name"); |
---|
[61e79a9] | 224 | if (tty) { |
---|
| 225 | owl_global_set_tty(&g, tty); |
---|
| 226 | } else { |
---|
| 227 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
| 228 | } |
---|
[7d4fbcd] | 229 | |
---|
[bd3f232] | 230 | /* setup the built-in styles */ |
---|
[a0a5179] | 231 | owl_function_debugmsg("startup: creating built-in styles"); |
---|
[bd3f232] | 232 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 233 | owl_style_create_internal(s, "default", &owl_stylefunc_default, "Default message formatting"); |
---|
[bd3f232] | 234 | owl_global_add_style(&g, s); |
---|
| 235 | |
---|
| 236 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 237 | owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting."); |
---|
[bd3f232] | 238 | owl_global_add_style(&g, s); |
---|
[ec6ff52] | 239 | #if 0 |
---|
| 240 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 241 | owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting."); |
---|
[ec6ff52] | 242 | owl_global_add_style(&g, s); |
---|
| 243 | #endif |
---|
[8b32593] | 244 | s=owl_malloc(sizeof(owl_style)); |
---|
[176d3443] | 245 | owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message"); |
---|
[8b32593] | 246 | owl_global_add_style(&g, s); |
---|
| 247 | |
---|
[7d4fbcd] | 248 | /* setup the default filters */ |
---|
[3abf28b] | 249 | /* the personal filter will need to change again when AIM chat's are |
---|
| 250 | * included. Also, there should be an %aimme% */ |
---|
[a0a5179] | 251 | owl_function_debugmsg("startup: creating default filters"); |
---|
[bd3f232] | 252 | f=owl_malloc(sizeof(owl_filter)); |
---|
[3abf28b] | 253 | owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ " |
---|
| 254 | "and class ^message$ and instance ^personal$ " |
---|
| 255 | "and ( recipient ^%me%$ or sender ^%me%$ ) ) " |
---|
[3723f31] | 256 | "or ( type ^aim$ and login ^none$ )"); |
---|
[7d4fbcd] | 257 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 258 | |
---|
[bd3f232] | 259 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0c502e9] | 260 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
[7d4fbcd] | 261 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 262 | |
---|
[bd3f232] | 263 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0236842] | 264 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
| 265 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 266 | |
---|
[bd3f232] | 267 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0236842] | 268 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
| 269 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 270 | |
---|
[bd3f232] | 271 | f=owl_malloc(sizeof(owl_filter)); |
---|
[0c502e9] | 272 | owl_filter_init_fromstring(f, "login", "not login ^none$"); |
---|
[0236842] | 273 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 274 | |
---|
[bd3f232] | 275 | f=owl_malloc(sizeof(owl_filter)); |
---|
[7d4fbcd] | 276 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
| 277 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 278 | |
---|
[bd3f232] | 279 | f=owl_malloc(sizeof(owl_filter)); |
---|
[5a6e6b9] | 280 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
| 281 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 282 | |
---|
[bd3f232] | 283 | f=owl_malloc(sizeof(owl_filter)); |
---|
[31e48a3] | 284 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
| 285 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 286 | |
---|
[bd3f232] | 287 | f=owl_malloc(sizeof(owl_filter)); |
---|
[31e48a3] | 288 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
| 289 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 290 | |
---|
[bd3f232] | 291 | f=owl_malloc(sizeof(owl_filter)); |
---|
[89426ab] | 292 | owl_filter_init_fromstring(f, "none", "false"); |
---|
| 293 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 294 | |
---|
[bd3f232] | 295 | f=owl_malloc(sizeof(owl_filter)); |
---|
[89426ab] | 296 | owl_filter_init_fromstring(f, "all", "true"); |
---|
[7d4fbcd] | 297 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
| 298 | |
---|
| 299 | /* set the current view */ |
---|
[a0a5179] | 300 | owl_function_debugmsg("startup: setting the current view"); |
---|
[c3ab155] | 301 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
[7d4fbcd] | 302 | |
---|
[96f8e5b] | 303 | /* AIM init */ |
---|
[a0a5179] | 304 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
[96f8e5b] | 305 | owl_aim_init(); |
---|
| 306 | |
---|
[38cf544c] | 307 | /* process the startup file */ |
---|
[a0a5179] | 308 | owl_function_debugmsg("startup: processing startup file"); |
---|
[2404c3a] | 309 | owl_function_source(NULL); |
---|
[38cf544c] | 310 | |
---|
[7d4fbcd] | 311 | /* read the config file */ |
---|
[a0a5179] | 312 | owl_function_debugmsg("startup: processing config file"); |
---|
[38cf544c] | 313 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
[f1e629d] | 314 | perlerr=owl_perlconfig_readconfig(configfile); |
---|
| 315 | if (perlerr) { |
---|
[7d4fbcd] | 316 | endwin(); |
---|
[4e0f545] | 317 | owl_function_error("Error parsing configfile: %s\n", perlerr); |
---|
| 318 | fprintf(stderr, "\nError parsing configfile: %s\n", perlerr); |
---|
[c3acb0b] | 319 | fflush(stderr); |
---|
[4e0f545] | 320 | printf("\nError parsing configfile: %s\n", perlerr); |
---|
[c3acb0b] | 321 | fflush(stdout); |
---|
[7d4fbcd] | 322 | exit(1); |
---|
| 323 | } |
---|
[ced25d1] | 324 | |
---|
[e075479] | 325 | /* if the config defines a formatting function, add 'perl' as a style */ |
---|
[bd3f232] | 326 | if (owl_global_is_config_format(&g)) { |
---|
| 327 | owl_function_debugmsg("Found perl formatting"); |
---|
| 328 | s=owl_malloc(sizeof(owl_style)); |
---|
[f1e629d] | 329 | owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap", |
---|
| 330 | "User-defined perl style that calls owl::format_msg" |
---|
| 331 | "with legacy global variable support"); |
---|
[bd3f232] | 332 | owl_global_add_style(&g, s); |
---|
[e075479] | 333 | owl_global_set_default_style(&g, "perl"); |
---|
[bd3f232] | 334 | } |
---|
| 335 | |
---|
[ced25d1] | 336 | /* execute the startup function in the configfile */ |
---|
[a0a5179] | 337 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
[f1e629d] | 338 | perlout = owl_perlconfig_execute("owl::startup();"); |
---|
[7d4fbcd] | 339 | if (perlout) owl_free(perlout); |
---|
| 340 | |
---|
| 341 | /* hold on to the window names for convenience */ |
---|
| 342 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
| 343 | recwin=owl_global_get_curs_recwin(&g); |
---|
| 344 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
| 345 | typwin=owl_global_get_curs_typwin(&g); |
---|
| 346 | tw=owl_global_get_typwin(&g); |
---|
| 347 | |
---|
[252a5c2] | 348 | /* welcome message */ |
---|
[a0a5179] | 349 | owl_function_debugmsg("startup: creating splash message"); |
---|
[52f3507] | 350 | strcpy(startupmsg, "-----------------------------------------------------------------------\n"); |
---|
[252a5c2] | 351 | sprintf(buff, "Welcome to Owl version %s. Press 'h' for on-line help. \n", OWL_VERSION_STRING); |
---|
| 352 | strcat(startupmsg, buff); |
---|
[52f3507] | 353 | strcat(startupmsg, " \n"); |
---|
[39c036d] | 354 | strcat(startupmsg, "This is an UNOFFICIAL DEVELOPMENT BUILD of owl. If you are using this \n"); |
---|
| 355 | strcat(startupmsg, "build regularly, please add yourself to dirty-owl-hackers@mit.edu \n"); |
---|
[52f3507] | 356 | strcat(startupmsg, " ^ ^ \n"); |
---|
[39c036d] | 357 | strcat(startupmsg, "Any bugs should be reported to dirty-owl-hackers@mit.edu OvO \n"); |
---|
| 358 | strcat(startupmsg, "Do not seek help with this build from bug-owl or ktools. ( ) \n"); |
---|
[52f3507] | 359 | strcat(startupmsg, "-----------------------------------------------------------------m-m---\n"); |
---|
[252a5c2] | 360 | owl_function_adminmsg("", startupmsg); |
---|
| 361 | sepbar(NULL); |
---|
| 362 | |
---|
[7d4fbcd] | 363 | wrefresh(sepwin); |
---|
| 364 | |
---|
| 365 | /* load zephyr subs */ |
---|
[bd3f232] | 366 | if (initialsubs) { |
---|
[4357be8] | 367 | int ret2; |
---|
[a0a5179] | 368 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
---|
[4357be8] | 369 | |
---|
| 370 | /* load default subscriptions */ |
---|
| 371 | ret=owl_zephyr_loaddefaultsubs(); |
---|
[252a5c2] | 372 | |
---|
[4357be8] | 373 | /* load subscriptions from subs file */ |
---|
[95474d7] | 374 | ret2=owl_zephyr_loadsubs(NULL, 0); |
---|
[4357be8] | 375 | |
---|
| 376 | if (ret || ret2) { |
---|
| 377 | owl_function_adminmsg("", "Error loading zephyr subscriptions"); |
---|
| 378 | } else if (ret2!=-1) { |
---|
[bd3f232] | 379 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
| 380 | } |
---|
[7d4fbcd] | 381 | |
---|
[bd3f232] | 382 | /* load login subscriptions */ |
---|
| 383 | if (owl_global_is_loginsubs(&g)) { |
---|
[a0a5179] | 384 | owl_function_debugmsg("startup: loading login subs"); |
---|
[bd3f232] | 385 | owl_function_loadloginsubs(NULL); |
---|
| 386 | } |
---|
[7d4fbcd] | 387 | } |
---|
| 388 | |
---|
[5a95b69] | 389 | /* First buddy check to sync the list without notifications */ |
---|
| 390 | owl_function_debugmsg("startup: doing initial zephyr buddy check"); |
---|
[4357be8] | 391 | /* owl_function_zephyr_buddy_check(0); */ |
---|
[5a95b69] | 392 | |
---|
[f933403] | 393 | /* set the startup and default style, based on userclue and presence of a |
---|
| 394 | * formatting function */ |
---|
[a0a5179] | 395 | owl_function_debugmsg("startup: setting startup and default style"); |
---|
[27c3a93] | 396 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
| 397 | /* the style was set by the user: leave it alone */ |
---|
| 398 | } else if (owl_global_is_config_format(&g)) { |
---|
[f933403] | 399 | owl_global_set_default_style(&g, "perl"); |
---|
| 400 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
| 401 | owl_global_set_default_style(&g, "default"); |
---|
| 402 | } else { |
---|
| 403 | owl_global_set_default_style(&g, "basic"); |
---|
| 404 | } |
---|
| 405 | |
---|
[7d4fbcd] | 406 | /* zlog in if we need to */ |
---|
| 407 | if (owl_global_is_startuplogin(&g)) { |
---|
[a0a5179] | 408 | owl_function_debugmsg("startup: doing zlog in"); |
---|
[31e48a3] | 409 | owl_zephyr_zlog_in(); |
---|
[7d4fbcd] | 410 | } |
---|
| 411 | |
---|
[a0a5179] | 412 | owl_function_debugmsg("startup: set style for the view"); |
---|
[f1e629d] | 413 | owl_view_set_style(owl_global_get_current_view(&g), |
---|
| 414 | owl_global_get_style_by_name(&g, owl_global_get_default_style(&g))); |
---|
[a0a5179] | 415 | |
---|
| 416 | owl_function_debugmsg("startup: setting context interactive"); |
---|
[7d4fbcd] | 417 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
| 418 | |
---|
[a15a84f] | 419 | nexttimediff=10; |
---|
[4b464a4] | 420 | nexttime=time(NULL); |
---|
| 421 | |
---|
[a0a5179] | 422 | owl_function_debugmsg("startup: entering main loop"); |
---|
[7d4fbcd] | 423 | /* main loop */ |
---|
| 424 | while (1) { |
---|
| 425 | |
---|
| 426 | /* if a resize has been scheduled, deal with it */ |
---|
| 427 | owl_global_resize(&g, 0, 0); |
---|
| 428 | |
---|
| 429 | /* these are here in case a resize changes the windows */ |
---|
| 430 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
| 431 | recwin=owl_global_get_curs_recwin(&g); |
---|
| 432 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
| 433 | typwin=owl_global_get_curs_typwin(&g); |
---|
| 434 | |
---|
| 435 | followlast=owl_global_should_followlast(&g); |
---|
[5789230] | 436 | |
---|
[a352335c] | 437 | /* Do AIM stuff */ |
---|
| 438 | if (owl_global_is_doaimevents(&g)) { |
---|
[5789230] | 439 | owl_aim_process_events(); |
---|
[de03334] | 440 | |
---|
[a352335c] | 441 | if (owl_global_is_aimloggedin(&g)) { |
---|
| 442 | if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) { |
---|
[f4d0975] | 443 | /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */ |
---|
[a352335c] | 444 | owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g)); |
---|
| 445 | } |
---|
[de03334] | 446 | } |
---|
[5789230] | 447 | } |
---|
| 448 | |
---|
[bc220b2] | 449 | /* Hook perl into the loop */ |
---|
| 450 | perl_mainloop_hook = owl_global_get_perl_mainloop_hook(&g); |
---|
| 451 | if (perl_mainloop_hook) { |
---|
| 452 | if (owl_perlconfig_is_function(perl_mainloop_hook)) { |
---|
| 453 | owl_function_debugmsg("startup: executing perl mainloop hook"); |
---|
| 454 | perlout = owl_perlconfig_execute(perl_mainloop_hook); |
---|
| 455 | if (perlout) owl_free(perlout); |
---|
| 456 | } |
---|
| 457 | } |
---|
| 458 | |
---|
[4b464a4] | 459 | /* little hack */ |
---|
[a15a84f] | 460 | now=time(NULL); |
---|
| 461 | today=localtime(&now); |
---|
| 462 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
[4b464a4] | 463 | if (time(NULL)>nexttime) { |
---|
| 464 | if (nexttimediff==1) { |
---|
[a15a84f] | 465 | nexttimediff=10; |
---|
[4b464a4] | 466 | } else { |
---|
| 467 | nexttimediff=1; |
---|
| 468 | } |
---|
| 469 | nexttime+=nexttimediff; |
---|
| 470 | owl_hack_animate(); |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
[aac889a] | 474 | /* Grab incoming messages. */ |
---|
[d09e5a1] | 475 | newmsgs=0; |
---|
[ced25d1] | 476 | zpendcount=0; |
---|
[be0a79f] | 477 | while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) { |
---|
[09489b89] | 478 | #ifdef HAVE_LIBZEPHYR |
---|
[7d4fbcd] | 479 | struct sockaddr_in from; |
---|
[09489b89] | 480 | #endif |
---|
[e6449bc] | 481 | owl_message *m=NULL; |
---|
[ecd5dc5] | 482 | owl_filter *f; |
---|
[09489b89] | 483 | |
---|
[bd3f232] | 484 | /* grab the new message, stick it in 'm' */ |
---|
[be0a79f] | 485 | if (owl_zephyr_zpending()) { |
---|
[09489b89] | 486 | #ifdef HAVE_LIBZEPHYR |
---|
[d09e5a1] | 487 | /* grab a zephyr notice, but if we've done 20 without stopping, |
---|
| 488 | take a break to process keystrokes etc. */ |
---|
| 489 | if (zpendcount>20) break; |
---|
| 490 | ZReceiveNotice(¬ice, &from); |
---|
| 491 | zpendcount++; |
---|
| 492 | |
---|
| 493 | /* is this an ack from a zephyr we sent? */ |
---|
| 494 | if (owl_zephyr_notice_is_ack(¬ice)) { |
---|
| 495 | owl_zephyr_handle_ack(¬ice); |
---|
| 496 | continue; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | /* if it's a ping and we're not viewing pings then skip it */ |
---|
| 500 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
---|
| 501 | continue; |
---|
| 502 | } |
---|
[7d4fbcd] | 503 | |
---|
[d09e5a1] | 504 | /* create the new message */ |
---|
| 505 | m=owl_malloc(sizeof(owl_message)); |
---|
| 506 | owl_message_create_from_znotice(m, ¬ice); |
---|
[09489b89] | 507 | #endif |
---|
[d09e5a1] | 508 | } else if (owl_global_messagequeue_pending(&g)) { |
---|
[74037d9] | 509 | /* pick up the non-zephyr message in the message queue */ |
---|
[d09e5a1] | 510 | m=owl_global_messageuque_popmsg(&g); |
---|
[74037d9] | 511 | } else { |
---|
| 512 | /* Not supposed to happen, but we seem to get here on resizes */ |
---|
| 513 | owl_function_debugmsg("Bottomed out looking for zephyr"); |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | /* If we didn't pick up a message for some reason, don't go on */ |
---|
| 517 | if (m==NULL) { |
---|
| 518 | owl_function_debugmsg("m is null in main loop"); |
---|
| 519 | continue; |
---|
[7d4fbcd] | 520 | } |
---|
| 521 | |
---|
[bd3f232] | 522 | /* if this message it on the puntlist, nuke it and continue */ |
---|
[7d4fbcd] | 523 | if (owl_global_message_is_puntable(&g, m)) { |
---|
| 524 | owl_message_free(m); |
---|
| 525 | continue; |
---|
| 526 | } |
---|
| 527 | |
---|
[280ddc6] | 528 | /* login or logout that should be ignored? */ |
---|
| 529 | if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) { |
---|
| 530 | owl_message_free(m); |
---|
| 531 | continue; |
---|
| 532 | } |
---|
| 533 | |
---|
[7d4fbcd] | 534 | /* otherwise add it to the global list */ |
---|
| 535 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
[d09e5a1] | 536 | newmsgs=1; |
---|
[7d4fbcd] | 537 | |
---|
| 538 | /* let the config know the new message has been received */ |
---|
[f1e629d] | 539 | owl_perlconfig_getmsg(m, 0, NULL); |
---|
[7d4fbcd] | 540 | |
---|
| 541 | /* add it to any necessary views; right now there's only the current view */ |
---|
| 542 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
| 543 | |
---|
| 544 | /* do we need to autoreply? */ |
---|
[9854278] | 545 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
[1077753b] | 546 | if (owl_message_is_type_zephyr(m)) { |
---|
| 547 | owl_zephyr_zaway(m); |
---|
| 548 | } else if (owl_message_is_type_aim(m)) { |
---|
[e9f239b] | 549 | if (owl_message_is_private(m)) { |
---|
| 550 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
| 551 | } |
---|
[1077753b] | 552 | } |
---|
[7d4fbcd] | 553 | } |
---|
| 554 | |
---|
| 555 | /* ring the bell if it's a personal */ |
---|
[5d365f6] | 556 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
| 557 | if (!owl_message_is_loginout(m) && |
---|
| 558 | !owl_message_is_mail(m) && |
---|
| 559 | owl_message_is_private(m)) { |
---|
| 560 | owl_function_beep(); |
---|
| 561 | } |
---|
| 562 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
| 563 | /* do nothing */ |
---|
| 564 | } else { |
---|
| 565 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
| 566 | if (f && owl_filter_message_match(f, m)) { |
---|
| 567 | owl_function_beep(); |
---|
| 568 | } |
---|
[7d4fbcd] | 569 | } |
---|
| 570 | |
---|
[5d365f6] | 571 | |
---|
[ecd5dc5] | 572 | /* if it matches the alert filter, do the alert action */ |
---|
| 573 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
| 574 | if (f && owl_filter_message_match(f, m)) { |
---|
| 575 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
| 576 | } |
---|
| 577 | |
---|
[5a95b69] | 578 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
| 579 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
| 580 | if (owl_message_is_login(m)) { |
---|
| 581 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 582 | } else if (owl_message_is_logout(m)) { |
---|
| 583 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 584 | } else { |
---|
| 585 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
[7d4fbcd] | 589 | /* check for burning ears message */ |
---|
| 590 | /* this is an unsupported feature */ |
---|
| 591 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
[1c6c4d3] | 592 | char *buff; |
---|
| 593 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
[7d4fbcd] | 594 | owl_function_adminmsg(buff, ""); |
---|
[1c6c4d3] | 595 | owl_free(buff); |
---|
[7d4fbcd] | 596 | owl_function_beep(); |
---|
| 597 | } |
---|
| 598 | |
---|
[d09e5a1] | 599 | /* log the message if we need to */ |
---|
[15b34fd] | 600 | owl_log_message(m); |
---|
[7d4fbcd] | 601 | } |
---|
| 602 | |
---|
[5a95b69] | 603 | /* is it time to check zbuddies? */ |
---|
| 604 | if (owl_global_is_pseudologins(&g)) { |
---|
| 605 | if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) { |
---|
| 606 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
| 607 | owl_function_zephyr_buddy_check(1); |
---|
| 608 | owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g)); |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
[afbf668] | 612 | /* dispatch any muxevents */ |
---|
| 613 | owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0); |
---|
| 614 | |
---|
[7d4fbcd] | 615 | /* follow the last message if we're supposed to */ |
---|
[d09e5a1] | 616 | if (newmsgs && followlast) { |
---|
[7d4fbcd] | 617 | owl_function_lastmsg_noredisplay(); |
---|
| 618 | } |
---|
[700c712] | 619 | |
---|
[ced25d1] | 620 | /* do the newmsgproc thing */ |
---|
[d09e5a1] | 621 | if (newmsgs) { |
---|
[8f44c6b] | 622 | owl_function_do_newmsgproc(); |
---|
[700c712] | 623 | } |
---|
[7d4fbcd] | 624 | |
---|
| 625 | /* redisplay if necessary */ |
---|
[d09e5a1] | 626 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
| 627 | if (newmsgs) { |
---|
[7d4fbcd] | 628 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
| 629 | sepbar(NULL); |
---|
| 630 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
| 631 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
| 632 | /* TODO: this is a broken kludge */ |
---|
| 633 | if (owl_global_get_viewwin(&g)) { |
---|
| 634 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | owl_global_set_needrefresh(&g); |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | /* if a popwin just came up, refresh it */ |
---|
| 641 | pw=owl_global_get_popwin(&g); |
---|
| 642 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
| 643 | owl_popwin_refresh(pw); |
---|
| 644 | owl_popwin_no_needs_first_refresh(pw); |
---|
| 645 | owl_global_set_needrefresh(&g); |
---|
| 646 | /* TODO: this is a broken kludge */ |
---|
| 647 | if (owl_global_get_viewwin(&g)) { |
---|
| 648 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | /* update the terminal if we need to */ |
---|
| 653 | if (owl_global_is_needrefresh(&g)) { |
---|
| 654 | /* leave the cursor in the appropriate window */ |
---|
| 655 | if (owl_global_is_typwin_active(&g)) { |
---|
| 656 | owl_function_set_cursor(typwin); |
---|
| 657 | } else { |
---|
| 658 | owl_function_set_cursor(sepwin); |
---|
| 659 | } |
---|
| 660 | doupdate(); |
---|
| 661 | owl_global_set_noneedrefresh(&g); |
---|
| 662 | } |
---|
| 663 | |
---|
[bd3f232] | 664 | /* Handle all keypresses. If no key has been pressed, sleep for a |
---|
| 665 | * little bit, but otherwise do not. This lets input be grabbed |
---|
| 666 | * as quickly as possbile */ |
---|
[7d4fbcd] | 667 | j=wgetch(typwin); |
---|
| 668 | if (j==ERR) { |
---|
| 669 | usleep(10); |
---|
| 670 | } else { |
---|
[1fd4cd3e] | 671 | /* find and activate the current keymap. |
---|
| 672 | * TODO: this should really get fixed by activating |
---|
| 673 | * keymaps as we switch between windows... |
---|
| 674 | */ |
---|
| 675 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
| 676 | owl_context_set_popless(owl_global_get_context(&g), |
---|
| 677 | owl_global_get_viewwin(&g)); |
---|
| 678 | owl_function_activate_keymap("popless"); |
---|
| 679 | } else if (owl_global_is_typwin_active(&g) |
---|
| 680 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
| 681 | /* |
---|
| 682 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
| 683 | owl_function_activate_keymap("editline"); |
---|
| 684 | */ |
---|
| 685 | } else if (owl_global_is_typwin_active(&g) |
---|
| 686 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
| 687 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
| 688 | owl_function_activate_keymap("editmulti"); |
---|
| 689 | } else { |
---|
| 690 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
| 691 | owl_function_activate_keymap("recv"); |
---|
| 692 | } |
---|
| 693 | /* now actually handle the keypress */ |
---|
| 694 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
| 695 | if (ret!=0 && ret!=1) { |
---|
| 696 | owl_function_makemsg("Unable to handle keypress"); |
---|
| 697 | } |
---|
[7d4fbcd] | 698 | } |
---|
[2a2bb60] | 699 | |
---|
[c9e72d1] | 700 | /* Log any error signals */ |
---|
| 701 | { |
---|
| 702 | siginfo_t si; |
---|
| 703 | int signum; |
---|
| 704 | if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) { |
---|
[81634b6] | 705 | owl_function_error("Got unexpected signal: %d %s (code: %d band: %d errno: %d)", |
---|
[afbf668] | 706 | signum, signum==SIGPIPE?"SIGPIPE":"SIG????", |
---|
[81634b6] | 707 | si.si_code, si.si_band, si.si_errno); |
---|
[c9e72d1] | 708 | } |
---|
| 709 | } |
---|
| 710 | |
---|
[7d4fbcd] | 711 | } |
---|
| 712 | } |
---|
| 713 | |
---|
[de22c3d] | 714 | void sig_handler(int sig, siginfo_t *si, void *data) |
---|
| 715 | { |
---|
[7d4fbcd] | 716 | if (sig==SIGWINCH) { |
---|
[bd3f232] | 717 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
| 718 | * schedulding a resize for later |
---|
| 719 | */ |
---|
[7d4fbcd] | 720 | owl_function_resize(); |
---|
[afbf668] | 721 | } else if (sig==SIGPIPE || sig==SIGCHLD) { |
---|
[c9e72d1] | 722 | /* Set a flag and some info that we got the sigpipe |
---|
| 723 | * so we can record that we got it and why... */ |
---|
| 724 | owl_global_set_errsignal(&g, sig, si); |
---|
[fe1f605] | 725 | } else if (sig==SIGTERM || sig==SIGHUP) { |
---|
| 726 | owl_function_quit(); |
---|
[7d4fbcd] | 727 | } |
---|
[c9e72d1] | 728 | |
---|
[7d4fbcd] | 729 | } |
---|
| 730 | |
---|
[de22c3d] | 731 | void usage() |
---|
| 732 | { |
---|
[e7cc1c3] | 733 | fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING); |
---|
[a68f05d] | 734 | fprintf(stderr, "Usage: owl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n"); |
---|
[e7cc1c3] | 735 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
| 736 | fprintf(stderr, " -d enable debugging\n"); |
---|
[a68f05d] | 737 | fprintf(stderr, " -D enable debugging and delete previous debug file\n"); |
---|
[e7cc1c3] | 738 | fprintf(stderr, " -v print the Owl version number and exit\n"); |
---|
| 739 | fprintf(stderr, " -h print this help message\n"); |
---|
| 740 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
| 741 | fprintf(stderr, " -t set the tty name\n"); |
---|
[7d4fbcd] | 742 | } |
---|
[2a2bb60] | 743 | |
---|
| 744 | #if OWL_STDERR_REDIR |
---|
| 745 | |
---|
| 746 | /* Replaces stderr with a pipe so that we can read from it. |
---|
| 747 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
[de22c3d] | 748 | int stderr_replace(void) |
---|
| 749 | { |
---|
[2a2bb60] | 750 | int pipefds[2]; |
---|
| 751 | if (0 != pipe(pipefds)) { |
---|
| 752 | perror("pipe"); |
---|
| 753 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
| 754 | return -1; |
---|
| 755 | } |
---|
| 756 | owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]); |
---|
| 757 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
| 758 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno)); |
---|
| 759 | perror("dup2"); |
---|
| 760 | return -1; |
---|
| 761 | } |
---|
| 762 | return pipefds[0]; |
---|
| 763 | } |
---|
| 764 | |
---|
[afbf668] | 765 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
| 766 | void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) |
---|
[de22c3d] | 767 | { |
---|
[2a2bb60] | 768 | int navail, bread; |
---|
| 769 | char *buf; |
---|
| 770 | /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/ |
---|
| 771 | if (rfd<0) return; |
---|
| 772 | if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) { |
---|
| 773 | return; |
---|
| 774 | } |
---|
| 775 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
| 776 | if (navail<=0) return; |
---|
| 777 | if (navail>256) { navail = 256; } |
---|
| 778 | buf = owl_malloc(navail+1); |
---|
| 779 | bread = read(rfd, buf, navail); |
---|
| 780 | if (buf[navail-1] != '\0') { |
---|
| 781 | buf[navail] = '\0'; |
---|
| 782 | } |
---|
| 783 | owl_function_error("Err: %s", buf); |
---|
| 784 | owl_free(buf); |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | #endif /* OWL_STDERR_REDIR */ |
---|