home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part05 / sw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  11.7 KB  |  595 lines

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