home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / trn / src / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-04  |  7.4 KB  |  340 lines

  1. /* $Id: init.c,v 4.4.3.1 1992/02/01 03:09:32 sob PATCH_3 sob $
  2.  *
  3.  * $Log: init.c,v $
  4.  * Revision 4.4.3.1  1992/02/01  03:09:32  sob
  5.  * Release 4.4 Patchlevel 3
  6.  *
  7.  * Revision 4.4  1991/09/09  20:18:23  sob
  8.  * release 4.4
  9.  *
  10.  *
  11.  * 
  12.  */
  13. /* This software is Copyright 1991 by Stan Barber. 
  14.  *
  15.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  16.  * use this software as long as: there is no monetary profit gained
  17.  * specifically from the use or reproduction of this software, it is not
  18.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  19.  * included prominently in any copy made. 
  20.  *
  21.  * The author make no claims as to the fitness or correctness of this software
  22.  * for any use whatsoever, and it is provided as is. Any use of this software
  23.  * is at the user's own risk. 
  24.  */
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #include "util.h"
  29. #include "final.h"
  30. #include "term.h"
  31. #include "last.h"
  32. #include "rn.h"
  33. #include "rcstuff.h"
  34. #include "ngdata.h"
  35. #include "only.h"
  36. #include "intrp.h"
  37. #include "addng.h"
  38. #include "sw.h"
  39. #include "art.h"
  40. #include "artsrch.h"
  41. #include "artio.h"
  42. #include "backpage.h"
  43. #include "bits.h"
  44. #include "cheat.h"
  45. #include "head.h"
  46. #include "help.h"
  47. #include "kfile.h"
  48. #include "ngsrch.h"
  49. #include "ngstuff.h"
  50. #include "rcln.h"
  51. #include "respond.h"
  52. #ifdef SERVER
  53. #include "server.h"
  54. #endif
  55. #ifdef USETHREADS
  56. #include "threads.h"
  57. #include "rthreads.h"
  58. #endif
  59. #include "ng.h"
  60. #include "decode.h"
  61. #include "INTERN.h"
  62. #include "init.h"
  63.  
  64. bool
  65. initialize(argc,argv)
  66. int argc;
  67. char *argv[];
  68. {
  69.     char *tcbuf;
  70.     register bool foundany = FALSE;
  71. /*** OS2: we don't need a prototype for time() */
  72. /*    long time(); */
  73. #ifdef SERVER
  74.     char *server;
  75.     int response;
  76. #endif
  77. #ifdef NOLINEBUF
  78.     static char std_out_buf[BUFSIZ];    /* must be static or malloced */
  79.  
  80.     setbuf(stdout, std_out_buf);
  81. #endif
  82.  
  83.     tcbuf = safemalloc(1024);        /* make temp buffer for termcap and */
  84.                     /* other initialization stuff */
  85.     
  86.     /* init terminal */
  87.     
  88.     term_init();            /* must precede sw_init() so that */
  89.                     /* ospeed is set for baud-rate */
  90.                     /* switches.  Actually terminal */
  91.                     /* mode setting is in term_set() */
  92.  
  93.     /* we have to know rnlib to look up global switches in %X/INIT */
  94.  
  95. /*** OS2: don't filexp settings which come out of uupc-rc-files ***/
  96. /*    lib = savestr(filexp(LIB));         */
  97. /*    rnlib = savestr(filexp(RNLIB));     */
  98.     lib = savestr(LIB);
  99.     rnlib = savestr(RNLIB);
  100.     change_bsl2sl(lib);
  101.     change_bsl2sl(rnlib);
  102.  
  103.     /* decode switches */
  104.  
  105.     sw_init(argc,argv,&tcbuf);          /* must not do % interps! */
  106.                     /* (but may mung environment) */
  107.  
  108.     /* init signals, status flags */
  109.  
  110.     final_init();
  111.     
  112.     /* start up file expansion and the % interpreter */
  113.  
  114.     intrp_init(tcbuf);
  115.     
  116.     /* now make sure we have a current working directory */
  117.  
  118.     if (!checkflag)
  119.     cwd_check();
  120.     
  121.     /* now that we know where to save things, cd to news directory */
  122.  
  123.     if (chdir(spool)) {
  124.     printf(nocd,spool) ; FLUSH;
  125.     finalize(1);
  126.     }
  127.  
  128.     /* if we aren't just checking, turn off echo */
  129.  
  130.     if (!checkflag)
  131.     term_set(tcbuf);
  132.  
  133. /*** OS2: initialize screen  ***/
  134.     un_standout();
  135.     clear();
  136.  
  137.     /* get info on last rn run, if any */
  138.  
  139.     if (!checkflag)
  140.     last_init(tcbuf);
  141.  
  142.     free(tcbuf);            /* recover 1024 bytes */
  143.  
  144.     /* make sure we are the sole possessors of .newsrc */
  145.  
  146.     if (!checkflag)
  147.     lock_check();
  148.  
  149.     /* check for news news */
  150.  
  151.     if (!checkflag)
  152.     newsnews_check();
  153.  
  154. #ifdef SERVER
  155.  
  156.     /* open connection to server if appropriate */
  157.  
  158.     if ((server = get_server_name(1)) == NULL)
  159.     finalize(1);
  160.  
  161.     response = server_init(server);
  162.     if (response < 0) {
  163.     fprintf(stderr,
  164.         "Couldn't connect to %s news server, try again later.\n",
  165.         server);
  166.     finalize(1);
  167.     }
  168.  
  169.     if (handle_server_response(response, server) < 0)
  170.     finalize(1);
  171.  
  172. #endif
  173.  
  174.     /* open active file, etc. */
  175.  
  176.     ngdata_init();
  177.  
  178.     /* now read in the .newsrc file */
  179.  
  180.     foundany = rcstuff_init();
  181.  
  182.     /* it looks like we will actually read something, so init everything */
  183.  
  184.     addng_init();
  185.     art_init();
  186.     artio_init();
  187.     artsrch_init();
  188.     backpage_init();
  189.     bits_init();
  190.     cheat_init();
  191.     head_init();
  192.     help_init();
  193.     kfile_init();
  194.     ng_init();
  195.     ngsrch_init();
  196.     ngstuff_init();
  197.     only_init();
  198.     rcln_init();
  199.     respond_init();
  200.     rn_init();
  201.     search_init();
  202.     decode_init();
  203. #ifdef USETHREADS
  204.     thread_init();
  205. #endif
  206.     util_init();
  207.  
  208. #ifdef FINDNEWNG
  209.     /*
  210.      * Skip this check if the -q flag was given.
  211.      */
  212.     if (!quickstart) {
  213.     if (find_new_groups()) {    /* did we add any new groups? */
  214.         foundany = TRUE;        /* let main() know */
  215.         starthere = 0;        /* start ng scan from the top */
  216.     }
  217.     }
  218. #endif
  219.     time(&lasttime);            /* remember when we inited-- */
  220.                     /* ends up back in .rnlast */
  221.     writelast();            /* in fact, put it there now */
  222.  
  223. #ifdef FINDNEWNG
  224. # ifdef ONLY
  225.     if (maxngtodo)            /* patterns on command line? */
  226.     foundany |= scanactive();
  227. # endif
  228. #endif
  229.  
  230.     return foundany;
  231. }
  232.  
  233. #ifndef SIGEMT
  234. #define SIGEMT 0
  235. #endif
  236.  
  237. /* make sure there is no rn out there already */
  238.  
  239. void
  240. lock_check()
  241. {
  242.     lockname = savestr(filexp(LOCKNAME));
  243.     if (!checkflag) {
  244.     tmpfp = fos2open(lockname,"r");
  245.     if (tmpfp != Nullfp) {
  246.         int processnum;
  247.     
  248.         fgets(buf,LBUFLEN,tmpfp);
  249.         fclose(tmpfp);
  250.         processnum = atoi(buf);
  251. #ifdef VERBOSE
  252.         IF(verbose) {
  253.         printf("You seem to have left a trn running, process %d.\n",
  254.             processnum) ; FLUSH;
  255.         }
  256.         ELSE
  257. #endif
  258. #ifdef TERSE
  259.         printf("Trn left running, #%d.\n", processnum) ; FLUSH;
  260. #endif
  261.         if (kill(processnum, SIGEMT)) {
  262.                     /* does process not exist? */
  263.                     /* (rn ignores SIGEMT) */
  264.         sleep(2);
  265. #ifdef VERBOSE
  266.         IF(verbose) {
  267.             fputs("\n\
  268. That process does not seem to exist anymore.  The count of read articles\n\
  269. may be incorrect in the last newsgroup accessed by that other (defunct)\n\
  270. process.\n\n",stdout) ; FLUSH;
  271.         }
  272.         ELSE
  273. #endif
  274. #ifdef TERSE
  275.             fputs("\nProcess crashed.\n",stdout) ; FLUSH;
  276. #endif
  277.         if (*lastngname) {
  278. #ifdef VERBOSE
  279.             IF(verbose) {
  280.             printf("(The last newsgroup accessed was %s.)\n\n",
  281.             lastngname) ; FLUSH;
  282.             }
  283.             ELSE
  284. #endif
  285. #ifdef TERSE
  286.             printf("(In %s.)\n\n",lastngname) ; FLUSH;
  287. #endif
  288.         }
  289.         get_anything();
  290.         putchar('\n') ; FLUSH;
  291.         }
  292.         else {
  293. #ifdef VERBOSE
  294.         IF(verbose) {
  295.             fputs("\n\
  296. You may not have two copies of [t]rn running simultaneously.  Goodbye.\n\
  297. ",stdout) ; FLUSH;
  298.         }
  299.         ELSE
  300. #endif
  301. #ifdef TERSE
  302.             fputs("\nCan't start another.\n",stdout) ; FLUSH;
  303. #endif
  304.                if (bizarre)
  305.                  resetty();
  306.  
  307. /*** OS2: reset display before exiting ***/
  308.         termlib_reset();
  309.         clear_rest();
  310.  
  311.         exit(0);
  312.         }
  313.     }
  314.     tmpfp = fos2open(lockname,"w");
  315.     if (tmpfp == Nullfp) {
  316.         printf(cantcreate,lockname) ; FLUSH;
  317.         sig_catcher(0);
  318.     }
  319.     fprintf(tmpfp,"%d\n",getpid());
  320.     fclose(tmpfp);
  321.     }
  322. }
  323.  
  324. void
  325. newsnews_check()
  326. {
  327.     char *newsnewsname = filexp(NEWSNEWSNAME);
  328.  
  329.     if ((tmpfp = fos2open(newsnewsname,"r")) != Nullfp) {
  330.     fstat(fileno(tmpfp),&filestat);
  331.     if (filestat.st_mtime > lasttime) {
  332.         while (fgets(buf,sizeof(buf),tmpfp) != Nullch)
  333.         fputs(buf,stdout) ; FLUSH;
  334.         get_anything();
  335.         putchar('\n') ; FLUSH;
  336.     }
  337.     fclose(tmpfp);
  338.     }
  339. }
  340.