home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rn_4_3_blars.lzh / init.c < prev    next >
C/C++ Source or Header  |  1990-12-02  |  10KB  |  425 lines

  1. /* $Header: init.c,v 4.3.2.6 90/05/08 22:05:55 sob Exp $
  2.  *
  3.  * $Log:    init.c,v $
  4.  * Revision 4.3.2.6  90/05/08  22:05:55  sob
  5.  * Added quick startup (-q) flag.
  6.  * 
  7.  * Revision 4.3.2.5  90/05/04  23:10:01  sob
  8.  * Fix for exiting "second" rn such that tty will be left in correct state.
  9.  * Provided by glenn@mathcs.emory.edu
  10.  * 
  11.  * Revision 4.3.2.4  90/03/22  23:04:32  sob
  12.  * Fixes provided by Wayne Davison <drivax!davison>
  13.  * 
  14.  * Revision 4.3.2.3  90/03/17  21:34:04  sob
  15.  * Cleaned up a bit.
  16.  * 
  17.  * Revision 4.3.2.2  89/11/08  01:17:48  sob
  18.  * Added changes to insure that this will compile for RN or RRN with no
  19.  * changes to the source code.
  20.  * 
  21.  * Revision 4.3.2.1  89/11/06  00:39:14  sob
  22.  * Added RRN support from NNTP 1.5
  23.  * 
  24.  * Revision 4.3.1.4  86/09/05  14:24:02  lwall
  25.  * Removed net.announce dependency.
  26.  * 
  27.  * Revision 4.3.1.3  85/07/23  18:08:36  lwall
  28.  * Fixed up NOLINEBUF option to work.
  29.  * 
  30.  * Revision 4.3.1.2  85/05/21  14:22:46  lwall
  31.  * Sped up "rn -c" by avoiding unnecessary initialization.
  32.  * 
  33.  * Revision 4.3.1.1  85/05/10  11:33:39  lwall
  34.  * Branch for patches.
  35.  * 
  36.  * Revision 4.3  85/05/01  16:16:13  lwall
  37.  * Baseline for release with 4.3bsd.
  38.  * 
  39.  */
  40.  
  41. #include "EXTERN.h"
  42. #include "common.h"
  43. #include "util.h"
  44. #include "final.h"
  45. #include "term.h"
  46. #include "last.h"
  47. #include "rn.h"
  48. #include "rcstuff.h"
  49. #include "ngdata.h"
  50. #include "only.h"
  51. #include "intrp.h"
  52. #include "addng.h"
  53. #include "sw.h"
  54. #include "art.h"
  55. #include "artsrch.h"
  56. #include "artio.h"
  57. #include "backpage.h"
  58. #include "bits.h"
  59. #include "cheat.h"
  60. #include "head.h"
  61. #include "help.h"
  62. #include "kfile.h"
  63. #include "ngsrch.h"
  64. #include "ngstuff.h"
  65. #include "rcln.h"
  66. #include "respond.h"
  67. #ifdef SERVER
  68. #include "server.h"
  69. #endif
  70. #include "ng.h"
  71. #include "INTERN.h"
  72. #include "init.h"
  73.  
  74. bool
  75. initialize(argc,argv)
  76. int argc;
  77. char *argv[];
  78. {
  79.     char *tcbuf;
  80.     register bool foundany = FALSE;
  81.     long time();
  82. #ifdef SERVER
  83.     char *server;
  84.     int response;
  85. #endif SERVER
  86. #ifdef NOLINEBUF
  87.     static char std_out_buf[BUFSIZ];    /* must be static or malloced */
  88.  
  89.     setbuf(stdout, std_out_buf);
  90. #endif
  91.  
  92.     tcbuf = safemalloc(1024);        /* make temp buffer for termcap and */
  93.                     /* other initialization stuff */
  94.     
  95.     /* init terminal */
  96.     
  97.     term_init();            /* must precede sw_init() so that */
  98.                     /* ospeed is set for baud-rate */
  99.                     /* switches.  Actually terminal */
  100.                     /* mode setting is in term_set() */
  101.  
  102.     /* we have to know rnlib to look up global switches in %X/INIT */
  103.  
  104.     lib = savestr(filexp(LIB));
  105.     rnlib = savestr(filexp(RNLIB));
  106.  
  107.     /* decode switches */
  108.  
  109.     sw_init(argc,argv,&tcbuf);          /* must not do % interps! */
  110.                     /* (but may mung environment) */
  111.  
  112.     /* init signals, status flags */
  113.  
  114.     final_init();
  115.     
  116.     /* start up file expansion and the % interpreter */
  117.  
  118.     intrp_init(tcbuf);
  119.     
  120.     /* now make sure we have a current working directory */
  121.  
  122.     if (!checkflag)
  123.     cwd_check();
  124.     
  125.     /* now that we know where to save things, cd to news directory */
  126.  
  127.     if (chdir(spool)) {
  128.     printf(nocd,spool) FLUSH;
  129.     finalize(1);
  130.     }
  131.  
  132.     /* if we aren't just checking, turn off echo */
  133.  
  134.     if (!checkflag)
  135.     term_set(tcbuf);
  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.     server = getserverbyfile(SERVER_FILE);
  159.     if (server == NULL) {
  160.     fprintf(stderr, "Can't get the name of the news server from %s\n",
  161.         SERVER_FILE);
  162.     fprintf(stderr,
  163.       "Either fix this file, or put NNTPSERVER in your environment.\n");
  164.     finalize(1);
  165.     }
  166.  
  167.     response = server_init(server);
  168.     if (response < 0) {
  169.     fprintf(stderr,
  170.         "Couldn't connect to %s news server, try again later.\n",
  171.         server);
  172.     finalize(1);
  173.     }
  174.  
  175.     if (handle_server_response(response, server) < 0)
  176.     finalize(1);
  177.  
  178. #endif SERVER
  179.  
  180.     /* open active file, etc. */
  181.  
  182.     ngdata_init();
  183.  
  184.     /* now read in the .newsrc file */
  185.  
  186.     foundany = rcstuff_init();
  187.  
  188.     /* it looks like we will actually read something, so init everything */
  189.  
  190.     addng_init();
  191.     art_init();
  192.     artio_init();
  193.     artsrch_init();
  194.     backpage_init();
  195.     bits_init();
  196.     cheat_init();
  197.     head_init();
  198.     help_init();
  199.     kfile_init();
  200.     ng_init();
  201.     ngsrch_init();
  202.     ngstuff_init();
  203.     only_init();
  204.     rcln_init();
  205.     respond_init();
  206.     rn_init();
  207.     search_init();
  208.     util_init();
  209.  
  210. #ifdef FINDNEWNG
  211.     fstat(fileno(actfp),&filestat);    /* did active file grow? */
  212.     /*
  213.      * Skip this check if the -q flag was given.
  214.      */
  215.     
  216.     if (!quickstart && filestat.st_size != lastactsiz) {
  217.     long actsiz = filestat.st_size;    /* remember new size */
  218.     NG_NUM oldnext = nextrcline;    /* remember # lines in newsrc */
  219. #ifdef FASTNEW
  220.     bool munged = writesoft || !lastactsiz;
  221.                     /* bad soft ptrs -> edited active */
  222. #else
  223.     bool munged = TRUE;        /* just assume .newsrc munged */
  224. #endif
  225.  
  226. #ifdef VERBOSE
  227.     IF(verbose)
  228.         fputs("\nChecking active list for new newsgroups...\n",stdout)
  229.           FLUSH;
  230.     ELSE
  231. #endif
  232. #ifdef TERSE
  233.         fputs("\nNew newsgroups:\n",stdout) FLUSH;
  234. #endif
  235. #ifdef FASTNEW
  236.     if (!munged) {            /* maybe just do tail of file? */
  237.         fseek(actfp,lastactsiz-1,0);
  238.         fgets(buf,LBUFLEN,actfp);
  239.         munged = (*buf != '\n');
  240.         if (!munged)
  241.         munged = newlist(munged,FALSE);
  242.     }
  243. #endif
  244.     if (munged) {            /* must we scan entire file? */
  245.         fseek(actfp,0L,0);        /* rewind active file */
  246.         newlist(munged,FALSE);      /* sure hope they use hashing... */
  247.     }
  248.     lastactsiz = actsiz;        /* remember for .rnlast */
  249.     if (nextrcline != oldnext) {    /* did we add any new groups? */
  250.         foundany = TRUE;        /* let main() know */
  251.         starthere = 0;              /* and start ng scan from the top */
  252.     }
  253.     }
  254. #endif
  255.     time(&lasttime);            /* remember when we inited-- */
  256.                     /* ends up back in .rnlast */
  257.     writelast();                       /* in fact, put it there now */
  258.     
  259. #ifdef FINDNEWNG
  260. # ifdef ONLY
  261.     if (maxngtodo)            /* patterns on command line? */
  262.     foundany |= scanactive();
  263. # endif
  264. #endif
  265.  
  266.     return foundany;
  267. }
  268.  
  269. /* make sure there is no rn out there already */
  270.  
  271. #ifndef OSK
  272. void
  273. lock_check()
  274. {
  275.     lockname = savestr(filexp(LOCKNAME));
  276.     if (!checkflag) {
  277.     tmpfp = fopen(lockname,"r");
  278.     if (tmpfp != Nullfp) {
  279.         int processnum;
  280.     
  281.         fgets(buf,LBUFLEN,tmpfp);
  282.         fclose(tmpfp);
  283.         processnum = atoi(buf);
  284. #ifdef VERBOSE
  285.         IF(verbose)
  286.         printf("You seem to have left an rn running, process %d.\n",
  287.             processnum) FLUSH;
  288.         ELSE
  289. #endif
  290. #ifdef TERSE
  291.         printf("Rn left running, #%d.\n", processnum) FLUSH;
  292. #endif
  293.         if (kill(processnum, SIGEMT)) {
  294.                     /* does process not exist? */
  295.                     /* (rn ignores SIGEMT) */
  296.         sleep(2);
  297. #ifdef VERBOSE
  298.         IF(verbose)
  299.             fputs("\n\
  300. That process does not seem to exist anymore.  The count of read articles\n\
  301. may be incorrect in the last newsgroup accessed by that other (defunct)\n\
  302. process.\n\n",stdout) FLUSH;
  303.         ELSE
  304. #endif
  305. #ifdef TERSE
  306.             fputs("\nProcess crashed.\n",stdout) FLUSH;
  307. #endif
  308.         if (*lastngname) {
  309. #ifdef VERBOSE
  310.             IF(verbose)
  311.             printf("(The last newsgroup accessed was %s.)\n\n",
  312.             lastngname) FLUSH;
  313.             ELSE
  314. #endif
  315. #ifdef TERSE
  316.             printf("(In %s.)\n\n",lastngname) FLUSH;
  317. #endif
  318.         }
  319.         get_anything();
  320.         putchar('\n') FLUSH;
  321.         }
  322.         else {
  323. #ifdef VERBOSE
  324.         IF(verbose)
  325.             fputs("\n\
  326. You may not have two copies of rn running simultaneously.  Goodbye.\n\
  327. ",stdout) FLUSH;
  328.         ELSE
  329. #endif
  330. #ifdef TERSE
  331.             fputs("\nCan't start another.\n",stdout) FLUSH;
  332. #endif
  333.                if (bizarre)
  334.                  resetty();
  335.         exit(0);
  336.         }
  337.     }
  338.     tmpfp = fopen(lockname,"w");
  339.     if (tmpfp == Nullfp) {
  340.         printf(cantcreate,lockname) FLUSH;
  341.         sig_catcher(0);
  342.     }
  343.     fprintf(tmpfp,"%d\n",getpid());
  344.     fclose(tmpfp);
  345.     }
  346. }
  347. #else    /* OSK */
  348. void
  349. lock_check()
  350. {
  351.     lockname = savestr(filexp(LOCKNAME));
  352.     if (!checkflag) {
  353.         if((lockunit = create(lockname, S_IWRITE | S_ISHARE | S_ISIZE,
  354.         S_IREAD | S_IWRITE, 8)) < 0) {
  355.         if(errno == E_CEF) {
  356.             if((unlink(lockname) >= 0) && (lockunit =  create(lockname,
  357.             S_IWRITE | S_ISHARE | S_ISIZE, S_IREAD | S_IWRITE,
  358.             8)) >= 0) {
  359. #ifdef VERBOSE
  360.             IF(verbose)
  361.             fputs("\n\
  362. Rn appears to have exited prematurely.  The count of read articles\n\
  363. may be incorrect in the last newsgroup accessed by that other (defunct)\n\
  364. process.\n\n",stdout) FLUSH;
  365.             ELSE
  366. #endif
  367. #ifdef TERSE
  368.                 fputs("\nProcess crashed.\n",stdout) FLUSH;
  369. #endif
  370.             if (*lastngname) {
  371. #ifdef VERBOSE
  372.             IF(verbose)
  373.                 printf("(The last newsgroup accessed was %s.)\n\n",
  374.                     lastngname) FLUSH;
  375.                 ELSE
  376. #endif
  377. #ifdef TERSE
  378.                 printf("(In %s.)\n\n",lastngname) FLUSH;
  379. #endif
  380.             }
  381.             errno = 0;
  382.         }
  383.         }
  384.         if(errno == E_SHARE) {
  385. #ifdef VERBOSE
  386.         IF(verbose)
  387.             fputs("\n\
  388. You may not have two copies of rn running simultaneously.  Goodbye.\n\
  389. ",stdout) FLUSH;
  390.         ELSE
  391. #endif
  392. #ifdef TERSE
  393.             fputs("\nCan't start another.\n",stdout) FLUSH;
  394. #endif
  395.             if (bizarre) resetty();
  396.         exit(0);
  397.         } else if(errno != 0) {
  398.         fprintf(stderr, "\nCan't open %s error %d\n", lockname, errno);
  399.         FLUSH;
  400.             if (bizarre) resetty();
  401.         exit(1);
  402.         }
  403.     }
  404.     /* lockunit will be closed in final.c */
  405.     }
  406. }
  407. #endif    /* OSK */
  408.  
  409. void
  410. newsnews_check()
  411. {
  412.     char *newsnewsname = filexp(NEWSNEWSNAME);
  413.  
  414.     if ((tmpfp = fopen(newsnewsname,"r")) != Nullfp) {
  415.     fstat(fileno(tmpfp),&filestat);
  416.     if (filestat.st_mtime > lasttime) {
  417.         while (fgets(buf,sizeof(buf),tmpfp) != Nullch)
  418.         fputs(buf,stdout) FLUSH;
  419.         get_anything();
  420.         putchar('\n') FLUSH;
  421.     }
  422.     fclose(tmpfp);
  423.     }
  424. }
  425.