home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / RN / SW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-21  |  14.2 KB  |  663 lines

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