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 / sw.c < prev    next >
Text File  |  1990-08-25  |  10KB  |  521 lines

  1. /* $Header: sw.c,v 4.3.2.3 90/05/08 22:06:00 sob Exp $
  2.  *
  3.  * $Log:    sw.c,v $
  4.  * Revision 4.3.2.3  90/05/08  22:06:00  sob
  5.  * Added quick startup (-q) flag.
  6.  * 
  7.  * Revision 4.3.2.2  90/03/22  23:05:34  sob
  8.  * Fixes provided by Wayne Davison <drivax!davison>
  9.  * 
  10.  * Revision 4.3.2.1  89/12/09  00:52:40  sob
  11.  * Now handles SIGWINCH correctly.
  12.  * 
  13.  * Revision 4.3.1.2  85/05/21  13:36:23  lwall
  14.  * Sped up "rn -c" by not doing unnecessary initialization.
  15.  * 
  16.  * Revision 4.3.1.1  85/05/10  11:40:38  lwall
  17.  * Branch for patches.
  18.  * 
  19.  * Revision 4.3  85/05/01  11:50:54  lwall
  20.  * Baseline for release with 4.3bsd.
  21.  * 
  22.  */
  23.  
  24. #include "EXTERN.h"
  25. #include "common.h"
  26. #include "util.h"
  27. #include "head.h"
  28. #include "only.h"
  29. #include "term.h"
  30. #include "ng.h"
  31. #include "intrp.h"
  32. #include "INTERN.h"
  33. #include "sw.h"
  34. #include "fileinfo.h"
  35.  
  36. void
  37. sw_init(argc,argv,tcbufptr)
  38. int argc;
  39. char *argv[];
  40. char **tcbufptr;
  41. {
  42.     register int i;
  43.  
  44.     if (argc >= 2 && strEQ(argv[1],"-c"))
  45.     checkflag=TRUE;            /* so we can optimize for -c */
  46.     interp(*tcbufptr,1024,GLOBINIT);
  47.     sw_file(tcbufptr,FALSE);
  48.     safecpy(*tcbufptr,getenv("RNINIT"),1024);
  49.     if (**tcbufptr) {
  50.     if (**tcbufptr == '/') {
  51.         sw_file(tcbufptr,TRUE);
  52.     }
  53.     else
  54.         sw_list(*tcbufptr);
  55.     }
  56.  
  57.     for (i = 1; i < argc; i++)
  58.     decode_switch(argv[i]);
  59. }
  60.  
  61. void
  62. sw_file(tcbufptr,bleat)
  63. char **tcbufptr;
  64. bool bleat;
  65. {
  66.     int initfd = open(*tcbufptr, OPEN_READ);
  67.     
  68.     if (initfd >= 0) {
  69.     fstat(initfd,&filestat);
  70.     if (filestat.st_size > 1024)
  71.         *tcbufptr = saferealloc(*tcbufptr,(MEM_SIZE)filestat.st_size);
  72.     if (filestat.st_size) {
  73.         read(initfd,*tcbufptr,(int)filestat.st_size);
  74.         (*tcbufptr)[filestat.st_size-1] = '\0';
  75.                 /* wipe out last newline */
  76.         sw_list(*tcbufptr);
  77.     }
  78.     else
  79.         **tcbufptr = '\0';
  80.     close(initfd);
  81.     }
  82.     else {
  83.     if (bleat)
  84.         printf(cantopen,*tcbufptr) FLUSH;
  85.     **tcbufptr = '\0';
  86.     }
  87. }
  88.  
  89. /* decode a list of space separated switches */
  90.  
  91. void
  92. sw_list(swlist)
  93. char *swlist;
  94. {
  95.     char *tmplist = safemalloc((MEM_SIZE) strlen(swlist) + 2);
  96.                     /* semi-automatic string */
  97.     register char *p, inquote = 0;
  98.  
  99.     strcpy(tmplist,swlist);
  100.     for (p=tmplist; isspace(*p); p++) ;    /* skip any initial spaces */
  101.     while (*p) {            /* "String, or nothing" */
  102.     if (!inquote && isspace(*p)) {    /* word delimiter? */
  103.         *p++ = '\0';        /* chop here */
  104.         while (isspace(*p))        /* these will be ignored later */
  105.         p++;
  106.     }
  107.     else if (inquote == *p) {
  108.         strcpy(p,p+1);        /* delete trailing quote */
  109.         inquote = 0;        /* no longer quoting */
  110.     }
  111.     else if (!inquote && *p == '"' || *p == '\'') {
  112.                     /* OK, I know when I am not wanted */
  113.         inquote = *p;        /* remember single or double */
  114.         strcpy(p,p+1);        /* delete the quote */
  115.     }                /* (crude, but effective) */
  116.     else if (*p == '\\') {        /* quoted something? */
  117.         if (p[1] == '\n')        /* newline? */
  118.         strcpy(p,p+2);        /* "I didn't see anything" */
  119.         else {
  120.         strcpy(p,p+1);        /* delete the backwhack */
  121.         p++;            /* leave the whatever alone */
  122.         }
  123.     }
  124.     else
  125.         p++;            /* normal char, leave it alone */
  126.     }
  127.     *++p = '\0';            /* put an extra null on the end */
  128.     if (inquote)
  129.     printf("Unmatched %c in switch\n",inquote) FLUSH;
  130.     for (p = tmplist; *p; /* p += strlen(p)+1 */ ) {
  131.     decode_switch(p);
  132.     while (*p++) ;            /* point at null + 1 */
  133.     }
  134.     free(tmplist);            /* this oughta be in Ada */
  135. }
  136.  
  137. /* decode a single switch */
  138.  
  139. void
  140. decode_switch(s)
  141. register char *s;
  142. {
  143.     while (isspace(*s))            /* ignore leading spaces */
  144.     s++;
  145. #ifdef DEBUGGING
  146.     if (debug)
  147.     printf("Switch: %s\n",s) FLUSH;
  148. #endif
  149.     if (*s != '-' && *s != '+') {    /* newsgroup pattern */
  150.     setngtodo(s);
  151.     }
  152.     else {                /* normal switch */
  153.     bool upordown = *s == '-' ? TRUE : FALSE;
  154.     char tmpbuf[LBUFLEN];
  155.  
  156.     s++;
  157.     switch (*s) {
  158. #ifdef TERMMOD
  159.     case '=': {
  160.         char *beg = s+1;
  161.  
  162.         while (*s && *s != '-' && *s != '+') s++;
  163.         cpytill(tmpbuf,beg,*s);
  164.         if (upordown ? strEQ(getenv("TERM"),tmpbuf)
  165.                  : strNE(getenv("TERM"),tmpbuf) ) {
  166.         decode_switch(s);
  167.         }
  168.         break;
  169.     }
  170. #endif
  171. #ifdef BAUDMOD
  172.     case '0': case '1': case '2': case '3': case '4':
  173.     case '5': case '6': case '7': case '8': case '9':
  174.         if (upordown ? (just_a_sec*10 <= atoi(s))
  175.                  : (just_a_sec*10 >= atoi(s)) ) {
  176.         while (isdigit(*s)) s++;
  177.         decode_switch(s);
  178.         }
  179.         break;
  180. #endif
  181.     case '/':
  182.         if (checkflag)
  183.         break;
  184. #ifdef SETENV
  185.         setenv("SAVEDIR",  upordown ? "%p/%c" : "%p" );
  186.         setenv("SAVENAME", upordown ? "%a"    : "%^C");
  187. #else
  188.         notincl("-/");
  189. #endif
  190.         break;
  191.     case 'c':
  192.         checkflag = upordown;
  193.         break;
  194.     case 'C':
  195.         s++;
  196.         if (*s == '=') s++;
  197.         docheckwhen = atoi(s);
  198.         break;
  199.     case 'd': {
  200.         if (checkflag)
  201.         break;
  202.         s++;
  203.         if (*s == '=') s++;
  204.         if (cwd) {
  205.         chdir(cwd);
  206.         free(cwd);
  207.         }
  208.         cwd = savestr(s);
  209.         break;
  210.     }
  211. #ifdef DEBUGGING
  212.     case 'D':
  213.         s++;
  214.         if (*s == '=') s++;
  215.         if (*s)
  216.         if (upordown)
  217.             debug |= atoi(s);
  218.         else
  219.             debug &= ~atoi(s);
  220.         else
  221.         if (upordown)
  222.             debug |= 1;
  223.         else
  224.             debug = 0;
  225.         break;
  226. #endif
  227.     case 'e':
  228.         erase_screen = upordown;
  229.         break;
  230.     case 'E':
  231. #ifdef SETENV
  232.         s++;
  233.         if (*s == '=')
  234.         s++;
  235.         strcpy(tmpbuf,s);
  236.         s = index(tmpbuf,'=');
  237.         if (s) {
  238.         *s++ = '\0';
  239.         setenv(tmpbuf,s);
  240.         }
  241.         else
  242.         setenv(tmpbuf,nullstr);
  243. #else
  244.         notincl("-E");
  245. #endif
  246.         break;
  247.     case 'F':
  248.         s++;
  249.         indstr = savestr(s);
  250.         break;
  251. #ifdef INNERSEARCH
  252.     case 'g':
  253.         gline = atoi(s+1)-1;
  254.         break;
  255. #endif
  256.     case 'H':
  257.     case 'h': {
  258.         register int len, i;
  259.         char *t;
  260.         int flag = (*s == 'h' ? HT_HIDE : HT_MAGIC);
  261.         
  262.         if (checkflag)
  263.         break;
  264.         s++;
  265.         len = strlen(s);
  266.         for (t=s; *t; t++)
  267.         if (isupper(*t))
  268.            *t = tolower(*t);
  269.         for (i=HEAD_FIRST; i<HEAD_LAST; i++)
  270.         if (!len || strnEQ(s,htype[i].ht_name,len))
  271.             if (upordown)
  272.             htype[i].ht_flags |= flag;
  273.             else
  274.             htype[i].ht_flags &= ~flag;
  275.         break;
  276.     }
  277.     case 'i':
  278.         s++;
  279.         if (*s == '=') s++;
  280.         initlines = atoi(s);
  281.         initlines_specified = TRUE;
  282.         break;
  283.     case 'l':
  284.         muck_up_clear = upordown;
  285.         break;
  286.     case 'L':
  287. #ifdef CLEAREOL
  288.         can_home_clear = upordown;
  289. #else
  290.         notincl("-L");
  291. #endif
  292.         break;
  293.     case 'M':
  294.         mbox_always = upordown;
  295.         break;
  296.     case 'm':
  297.         s++;
  298.         if (*s == '=') s++;
  299.         if (!upordown)
  300.         marking = NOMARKING;
  301.         else if (*s == 'u')
  302.         marking = UNDERLINE;
  303.         else {
  304.         marking = STANDOUT;
  305.         }
  306.         break;
  307.     case 'N':
  308.         norm_always = upordown;
  309.         break;
  310. #ifdef VERBOSE
  311.     case 'n':
  312.         fputs("This isn't readnews.  Don't use -n.\n\n",stdout) FLUSH;
  313.         break;
  314. #endif
  315.     case 'r':
  316.         findlast = upordown;
  317.         break;
  318.     case 's':
  319.         s++;
  320.         if (*s == '=') s++;
  321.         if (*s) {
  322.         countdown = atoi(s);
  323.         suppress_cn = FALSE;
  324.         }
  325.         else {
  326.         if (!upordown)
  327.             countdown = 5;
  328.         suppress_cn = upordown;
  329.         }
  330.         break;
  331.     case 'S':
  332. #ifdef ARTSEARCH
  333.         s++;
  334.         if (*s == '=') s++;
  335.         if (*s)
  336.         scanon = atoi(s);
  337.         else
  338.         scanon = upordown*3;
  339. #else
  340.         notincl("-S");
  341. #endif
  342.         break;
  343.     case 't':
  344. #ifdef VERBOSE
  345. #ifdef TERSE
  346.         verbose = !upordown;
  347. #else
  348.         notincl("+t");
  349. #endif
  350. #else
  351.         notincl("+t");
  352. #endif
  353.         break;
  354.     case 'T':
  355.         typeahead = upordown;
  356.         break;
  357.     case 'v':
  358. #ifdef VERIFY
  359.         verify = upordown;
  360. #else
  361.         notincl("-v");
  362. #endif
  363.         break;
  364.     /*
  365.      * People want a way to avoid checking for new newsgroups on startup.
  366.      */
  367.     case 'q':
  368.         quickstart = upordown;
  369.         break;
  370.     default:
  371. #ifdef VERBOSE
  372.         IF(verbose)
  373.         printf("\nIgnoring unrecognized switch: -%c\n", *s) FLUSH;
  374.         ELSE
  375. #endif
  376. #ifdef TERSE
  377.         printf("\nIgnoring -%c\n", *s) FLUSH;
  378. #endif
  379.         break;
  380.     }
  381.     }
  382. }
  383.  
  384. /* print current switch values */
  385.  
  386. void
  387. pr_switches()
  388. {
  389.     static char mp[2] = {'+','-'};
  390.     register int i;
  391.     
  392.     fputs("\nCurrent switch settings:\n",stdout);
  393.     printf("%c/ ", mp[strEQ(getval("SAVEDIR",SAVEDIR),"%p/%c")]);
  394.     printf("%cc ", mp[checkflag]);
  395.     printf("-C%d ", docheckwhen);
  396.     printf("-d%s ", cwd);
  397. #ifdef DEBUGGING
  398.     if (debug)
  399.     printf("-D%d ", debug);
  400. #endif
  401.     printf("%ce ", mp[erase_screen]);
  402.     printf("-F\"%s\" ", indstr);
  403. #ifdef INNERSEARCH
  404.     printf("-g%d", gline);
  405. #endif
  406.     putchar('\n');
  407. #ifdef VERBOSE
  408.     if (verbose) {
  409.     for (i=HEAD_FIRST; i<HEAD_LAST; i++)
  410.         printf("%ch%s%c",
  411.         mp[htype[i].ht_flags & HT_HIDE], htype[i].ht_name,
  412.         (! (i % 5) ? '\n' : ' ') );
  413.     }
  414. #endif
  415.     printf("-i%d ", initlines);
  416.     printf("%cl ", mp[muck_up_clear]);
  417. #ifdef CLEAREOL
  418.     printf("%cL ", mp[can_home_clear]);
  419. #endif CLEAREOL
  420.     if (marking)
  421.     printf("-m%c ",marking==UNDERLINE?'u':'s');
  422.     else
  423.     printf("+m ");
  424.     printf("%cM ", mp[mbox_always]);
  425.     printf("%cN ", mp[norm_always]);
  426.     printf("%cr ", mp[findlast]);
  427.     if (countdown)
  428.     printf("-s%d ", countdown);
  429.     else
  430.     printf("%cs ", mp[suppress_cn]);
  431. #ifdef ARTSEARCH
  432.     if (scanon)
  433.     printf("-S%d ",scanon);
  434.     else
  435.     printf("+S ");
  436. #endif
  437. #ifdef VERBOSE
  438. #ifdef TERSE
  439.     printf("%ct ", mp[!verbose]);
  440. #endif
  441. #endif
  442.     printf("%cT ", mp[typeahead]);
  443. #ifdef VERIFY
  444.     printf("%cv ", mp[verify]);
  445. #endif
  446.     fputs("\n\n",stdout) FLUSH;
  447. #ifdef ONLY
  448.     if (maxngtodo) {
  449. #ifdef VERBOSE
  450.     IF(verbose)
  451.         fputs("Current restriction:",stdout);
  452.     ELSE
  453. #endif
  454. #ifdef TERSE
  455.         fputs("Only:",stdout);
  456. #endif
  457.     for (i=0; i<maxngtodo; i++)
  458.         printf(" %s",ngtodo[i]);
  459.     fputs("\n\n",stdout) FLUSH;
  460.     }
  461. #ifdef VERBOSE
  462.     else if (verbose)
  463.     fputs("No restriction.\n\n",stdout) FLUSH;
  464. #endif
  465. #endif
  466. }
  467.  
  468. void
  469. cwd_check()
  470. {
  471.     char tmpbuf[LBUFLEN];
  472.  
  473.     if (!cwd)
  474.     cwd = savestr(filexp("~/News"));
  475.     strcpy(tmpbuf,cwd);
  476.     if (chdir(cwd)) {
  477.     safecpy(tmpbuf,filexp(cwd),sizeof tmpbuf);
  478.     if (makedir(tmpbuf,MD_DIR) < 0 || chdir(tmpbuf) < 0) {
  479.         interp(cmd_buf, (sizeof cmd_buf), "%~/News");
  480.         if (makedir(cmd_buf,MD_DIR) < 0)
  481.         strcpy(tmpbuf,homedir);
  482.         else
  483.         strcpy(tmpbuf,cmd_buf);
  484.         chdir(tmpbuf);
  485. #ifdef VERBOSE
  486.         IF(verbose)
  487.         printf("\
  488. Cannot make directory %s--\n\
  489.     articles will be saved to %s\n\
  490. \n\
  491. ",cwd,tmpbuf) FLUSH;
  492.         ELSE
  493. #endif
  494. #ifdef TERSE
  495.         printf("\
  496. Can't make %s--\n\
  497.     using %s\n\
  498. \n\
  499. ",cwd,tmpbuf) FLUSH;
  500. #endif
  501.     }
  502.     }
  503.     free(cwd);
  504.     getwd(tmpbuf);
  505.     if (eaccess(tmpbuf,ACCESS_DIRREADWRITE)) {
  506. #ifdef VERBOSE
  507.     IF(verbose)
  508.         printf("\
  509. Current directory %s is not writeable--\n\
  510.     articles will be saved to home directory\n\n\
  511. ",tmpbuf) FLUSH;
  512.     ELSE
  513. #endif
  514. #ifdef TERSE
  515.         printf("%s not writeable--using ~\n\n",tmpbuf) FLUSH;
  516. #endif
  517.     strcpy(tmpbuf,homedir);
  518.     }
  519.     cwd = savestr(tmpbuf);
  520. }
  521.