home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume27 / trn-3.3 / part04 / ngstuff.c < prev    next >
C/C++ Source or Header  |  1993-11-27  |  10KB  |  476 lines

  1. /* $Id: ngstuff.c,v 3.0 1991/09/09 20:23:31 davison Trn $
  2.  */
  3. /* This software is Copyright 1991 by Stan Barber. 
  4.  *
  5.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  6.  * use this software as long as: there is no monetary profit gained
  7.  * specifically from the use or reproduction of this software, it is not
  8.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  9.  * included prominently in any copy made. 
  10.  *
  11.  * The authors make no claims as to the fitness or correctness of this software
  12.  * for any use whatsoever, and it is provided as is. Any use of this software
  13.  * is at the user's own risk. 
  14.  */
  15.  
  16. #include "EXTERN.h"
  17. #include "common.h"
  18. #include "term.h"
  19. #include "util.h"
  20. #include "cache.h"
  21. #include "bits.h"
  22. #include "ngdata.h"
  23. #include "ng.h"
  24. #include "intrp.h"
  25. #include "head.h"
  26. #include "final.h"
  27. #include "sw.h"
  28. #include "rthread.h"
  29. #include "rt-select.h"
  30. #include "rt-wumpus.h"
  31. #include "trn.h"
  32. #include "rcstuff.h"
  33. #include "respond.h"
  34. #include "kfile.h"
  35. #include "decode.h"
  36. #include "INTERN.h"
  37. #include "ngstuff.h"
  38.  
  39. void
  40. ngstuff_init()
  41. {
  42.     ;
  43. }
  44.  
  45. /* do a shell escape */
  46.  
  47. int
  48. escapade()
  49. {
  50.     register char *s;
  51.     bool interactive = (buf[1] == FINISHCMD);
  52.     bool docd;
  53.     char whereiam[512];
  54.  
  55.     if (!finish_command(interactive))    /* get remainder of command */
  56.     return -1;
  57.     s = buf+1;
  58.     docd = *s != '!';
  59.     if (!docd) {
  60.     s++;
  61.     }
  62.     else {
  63.     getwd(whereiam);
  64.     if (chdir(cwd)) {
  65.         printf(nocd,cwd) FLUSH;
  66.         sig_catcher(0);
  67.     }
  68.     }
  69.     while (*s == ' ') s++;
  70.                     /* skip leading spaces */
  71.     interp(cmd_buf, (sizeof cmd_buf), s);/* interpret any % escapes */
  72.     resetty();                /* make sure tty is friendly */
  73.     doshell(Nullch,cmd_buf);    /* invoke the shell */
  74.     noecho();                /* and make terminal */
  75.     crmode();                /*   unfriendly again */
  76.     if (docd) {
  77.     if (chdir(whereiam)) {
  78.         printf(nocd,whereiam) FLUSH;
  79.         sig_catcher(0);
  80.     }
  81.     }
  82. #ifdef MAILCALL
  83.     mailcount = 0;            /* force recheck */
  84. #endif
  85.     return 0;
  86. }
  87.  
  88. /* process & command */
  89.  
  90. int
  91. switcheroo()
  92. {
  93.     if (!finish_command(TRUE)) /* get rest of command */
  94.     return -1;    /* if rubbed out, try something else */
  95.     if (!buf[1])
  96.     pr_switches();
  97.     else if (buf[1] == '&') {
  98.     if (!buf[2]) {
  99.         page_init();
  100.         show_macros();
  101.     }
  102.     else {
  103.         char tmpbuf[LBUFLEN];
  104.         register char *s;
  105.  
  106.         for (s=buf+2; isspace(*s); s++);
  107.         mac_line(s,tmpbuf,(sizeof tmpbuf));
  108.     }
  109.     }
  110.     else {
  111.     bool docd = (instr(buf,"-d", TRUE) != Nullch);
  112.      char whereami[512];
  113.  
  114.     if (docd)
  115.         getwd(whereami);
  116.     sw_list(buf+1);
  117.     if (docd) {
  118.         cwd_check();
  119.         if (chdir(whereami)) {        /* -d does chdirs */
  120.         printf(nocd,whereami) FLUSH;
  121.         sig_catcher(0);
  122.         }
  123.     }
  124.     }
  125.     return 0;
  126. }
  127.  
  128. /* process range commands */
  129.  
  130. int
  131. numnum()
  132. {
  133.     ART_NUM min, max;
  134.     char *cmdlst = Nullch;
  135.     register char *s, *c;
  136.     ART_NUM oldart = art;
  137.     char tmpbuf[LBUFLEN];
  138.     bool justone = TRUE;        /* assume only one article */
  139.  
  140.     perform_cnt = 0;
  141.     if (!finish_command(TRUE))    /* get rest of command */
  142.     return NN_INP;
  143.     if (lastart < 1) {
  144.         fputs("\nNo articles\n",stdout) FLUSH;
  145.         return NN_ASK;
  146.     }
  147. #ifdef ARTSEARCH
  148.     if (srchahead)
  149.     srchahead = -1;
  150. #endif
  151.     for (s=buf; *s && (isdigit(*s) || index(" ,-.$",*s)); s++)
  152.     if (!isdigit(*s))
  153.         justone = FALSE;
  154.     if (*s) {
  155.     cmdlst = savestr(s);
  156.     justone = FALSE;
  157.     }
  158.     else if (!justone)
  159.     cmdlst = savestr("m");
  160.     *s++ = ',';
  161.     *s = '\0';
  162.     safecpy(tmpbuf,buf,LBUFLEN);
  163.     for (s = tmpbuf; c = index(s,','); s = ++c) {
  164.     *c = '\0';
  165.     if (*s == '.')
  166.         min = oldart;
  167.     else
  168.         min = atol(s);
  169.     if (min < absfirst) {
  170.         min = absfirst;
  171.         printf("(First article is %ld)\n",(long)absfirst) FLUSH;
  172.         pad(just_a_sec/3);
  173.     }
  174.     if ((s=index(s,'-')) != Nullch) {
  175.         s++;
  176.         if (*s == '$')
  177.         max = lastart;
  178.         else if (*s == '.')
  179.         max = oldart;
  180.         else
  181.         max = atol(s);
  182.     }
  183.     else
  184.         max = min;
  185.     if (max>lastart) {
  186.         max = lastart;
  187.         if (min > max)
  188.         min = max;
  189.         printf("(Last article is %ld)\n",(long)lastart) FLUSH;
  190.         pad(just_a_sec/3);
  191.     }
  192.     if (max < min) {
  193.         fputs("\nBad range\n",stdout) FLUSH;
  194.         if (cmdlst)
  195.         free(cmdlst);
  196.         return NN_ASK;
  197.     }
  198.     if (justone) {
  199.         art = min;
  200.         return NN_REREAD;
  201.     }
  202.     for (art=min, artp=article_ptr(min); art<=max; art++, artp++) {
  203.         if (perform(cmdlst,TRUE)) {
  204. #ifdef VERBOSE
  205.         IF(verbose)
  206.             printf("\n(Interrupted at article %ld)\n",(long)art)
  207.               FLUSH;
  208.         ELSE
  209. #endif
  210. #ifdef TERSE
  211.             printf("\n(Intr at %ld)\n",(long)art) FLUSH;
  212. #endif
  213.         if (cmdlst)
  214.             free(cmdlst);
  215.         return NN_ASK;
  216.         }
  217.     }
  218.     }
  219.     art = oldart;
  220.     if (cmdlst)
  221.     free(cmdlst);
  222.     return NN_NORM;
  223. }
  224.  
  225. int
  226. use_selected()
  227. {
  228.     register SUBJECT *sp;
  229.     register ARTICLE *ap;
  230.     bool want_read;
  231.     char *cmdstr;
  232.     int len, ret = 1;
  233.     int subj_mask = (sel_mode == SM_THREAD? (SF_THREAD|SF_VISIT) : SF_VISIT);
  234.  
  235.     if (!finish_command(TRUE))    /* get rest of command */
  236.     return 0;
  237.     if (!buf[1])
  238.     return -1;
  239.     cmdstr = savestr(buf+1);
  240.     want_read = (sel_rereading || *cmdstr =='m');
  241.  
  242.     perform_cnt = 0;
  243.     page_line = 1;
  244.     len = strlen(cmdstr);
  245.  
  246.     /* A few commands can just loop through the subjects. */
  247.     if ((len == 1 && (*cmdstr == 't' || *cmdstr == 'J'))
  248.      || (len == 2
  249.       && (((*cmdstr == '+' || *cmdstr == '-') && cmdstr[0] == cmdstr[1])
  250.        || *cmdstr == 'T'))) {
  251.     for (sp = first_subject; sp; sp = sp->next) {
  252.         if ((sp->flags & subj_mask) == subj_mask) {
  253.         artp = first_art(sp);
  254.         if (artp) {
  255.             art = article_num(artp);
  256.             if (perform(cmdstr, FALSE)) {
  257.             fputs("\nInterrupted\n", stdout) FLUSH;
  258.             goto break_out;
  259.             }
  260.         }
  261. #ifdef VERBOSE
  262.         IF(verbose)
  263.             if (mode != 't' && *cmdstr != 't' && *cmdstr != 'T')
  264.             putchar('\n') FLUSH;
  265. #endif
  266.         }
  267.     }
  268.     } else if (strEQ(cmdstr, "E")) {
  269.     /* The 'E'nd-decode command doesn't do any looping at all. */
  270.     if (decode_fp)
  271.         decode_end();
  272.     else
  273.         ret = 2;
  274.     } else {
  275.     /* The rest loop through all (selected) articles. */
  276.     /* Use the explicit article-order if it exists */
  277.     if (artptr_list) {
  278.         ARTICLE **app, **limit = artptr_list + artptr_list_size;
  279.         for (app = artptr_list; app < limit; app++)
  280.         if ((!((ap = *app)->flags & AF_READ) ^ want_read)
  281.          && (ap->flags & sel_mask)) {
  282.             art = article_num(ap);
  283.             artp = ap;
  284.             if (perform(cmdstr, TRUE)) {
  285.             fputs("\nInterrupted\n", stdout) FLUSH;
  286.             goto break_out;
  287.             }
  288.         }
  289.     } else {
  290.         for (sp = first_subject; sp; sp = sp->next)
  291.         if ((sp->flags & subj_mask) == subj_mask)
  292.             for (ap = first_art(sp); ap; ap = next_art(ap))
  293.             if ((!(ap->flags & AF_READ) ^ want_read)
  294.              && (ap->flags & sel_mask)) {
  295.                 art = article_num(ap);
  296.                 artp = ap;
  297.                 if (perform(cmdstr, TRUE)) {
  298.                 fputs("\nInterrupted\n", stdout) FLUSH;
  299.                 goto break_out;
  300.                 }
  301.             }
  302.     }
  303.     }
  304.   break_out:
  305.     free(cmdstr);
  306.     return ret;
  307. }
  308.  
  309. int
  310. perform(cmdlst,toplevel)
  311. register char *cmdlst;
  312. int toplevel;
  313. {
  314.     register int ch;
  315.     bool saveit = FALSE;
  316.     
  317.     if (toplevel) {
  318.     printf("%-6ld ",art);
  319.     fflush(stdout);
  320.     }
  321.     perform_cnt++;
  322.     for (; ch = *cmdlst; cmdlst++) {
  323.     if (isspace(ch) || ch == ':')
  324.         continue;
  325.     if (ch == 'j' && !saveit) {
  326.         if (!was_read(art)) {
  327.         mark_as_read();
  328. #ifdef VERBOSE
  329.         IF(verbose)
  330.             fputs("\tJunked",stdout);
  331. #endif
  332.         }
  333.         if (sel_rereading)
  334.         deselect_article(artp);
  335.     } else if (ch == '+') {
  336.         if ((saveit || cmdlst[1] == '+') && artp->subj) {
  337.         if (sel_mode == SM_THREAD)
  338.             select_thread(artp->subj->thread,
  339.                   saveit? AF_AUTOSELECTALL : 0);
  340.         else
  341.             select_subject(artp->subj, saveit? AF_AUTOSELECTALL : 0);
  342.         cmdlst++;
  343.         } else
  344.         select_article(artp, (saveit? AF_AUTOSELECTALL : 0) | AF_ECHO);
  345.     } else if (ch == '.') {
  346.         select_subthread(artp, saveit? AF_AUTOSELECT : 0);
  347.     } else if (ch == '-') {
  348.         if (cmdlst[1] == '-' && artp->subj) {
  349.         if (sel_mode == SM_THREAD)
  350.             deselect_thread(artp->subj->thread);
  351.         else
  352.             deselect_subject(artp->subj);
  353.         cmdlst++;
  354.         } else
  355.         deselect_article(artp);
  356.     } else if (ch == ',') {
  357.         kill_subthread(artp, saveit? (KF_ALL|KF_KILLFILE) : KF_ALL);
  358.     } else if (ch == 'J' || ch == 'j') {
  359.         if (!artp->subj) {
  360.         set_read(artp);
  361.         artp->flags |= AF_AUTOKILLALL;
  362.         } else if (sel_mode == SM_THREAD)
  363.         kill_thread(artp->subj->thread,
  364.             saveit? (KF_ALL|KF_KILLFILE) : KF_ALL);
  365.         else
  366.         kill_subject(artp->subj,
  367.             saveit? (KF_ALL|KF_KILLFILE) : KF_ALL);
  368.     } else if (ch == 't') {
  369.         entire_tree(artp);
  370.     } else if (ch == 'T') {
  371.         saveit = TRUE;
  372.     } else if (ch == 'm') {
  373.         if ((article_ptr(art)->flags & (AF_READ|AF_MISSING)) == AF_READ) {
  374.         unmark_as_read();
  375. #ifdef VERBOSE
  376.         IF(verbose)
  377.             fputs("\tMarked unread",stdout);
  378. #endif
  379.         }
  380.     }
  381.     else if (ch == 'M') {
  382.         delay_unmark(artp);
  383.         oneless(artp);
  384. #ifdef VERBOSE
  385.         IF(verbose)
  386.         fputs("\tWill return",stdout);
  387. #endif
  388.     }
  389.     else if (ch == '=') {
  390.         printf("\t%s",fetchsubj(art,FALSE));
  391. #ifdef VERBOSE
  392.         IF(verbose)
  393.         ;
  394.         ELSE
  395. #endif
  396.         putchar('\n') FLUSH;        /* ghad! */
  397.     }
  398.     else if (ch == 'C') {
  399. #ifdef ASYNC_PARSE
  400.         printf("\t%sancelled",(cancel_article() ? "Not c" : "C"));
  401. #else
  402.         notincl("C");
  403.         return -1;
  404. #endif
  405.     }
  406.     else if (ch == '%') {
  407. #ifdef ASYNC_PARSE
  408.         char tmpbuf[512];
  409.  
  410.         if (one_command)
  411.         interp(tmpbuf, (sizeof tmpbuf), cmdlst);
  412.         else
  413.         cmdlst = dointerp(tmpbuf, (sizeof tmpbuf), cmdlst, ":") - 1;
  414.         perform_cnt--;
  415.         if (perform(tmpbuf,FALSE))
  416.         return -1;
  417. #else
  418.         notincl("%");
  419.         return -1;
  420. #endif
  421.     }
  422.     else if (index("!&sSwWe|",ch)) {
  423.         if (one_command)
  424.         strcpy(buf,cmdlst);
  425.         else
  426.         cmdlst = cpytill(buf,cmdlst,':') - 1;
  427.         /* we now have the command in buf */
  428.         if (ch == '!') {
  429.         escapade();
  430. #ifdef VERBOSE
  431.         IF(verbose)
  432.             fputs("\tShell escaped",stdout);
  433. #endif
  434.         }
  435.         else if (ch == '&') {
  436.         switcheroo();
  437. #ifdef VERBOSE
  438.         IF(verbose)
  439.             if (buf[1] && buf[1] != '&')
  440.             fputs("\tSwitched",stdout);
  441. #endif
  442.         }
  443.         else {
  444.         putchar('\t');
  445.         save_article();
  446. #ifdef VERBOSE
  447.         IF(verbose)
  448.             ;
  449.         ELSE
  450. #endif
  451.             putchar('\n') FLUSH;
  452.         }
  453.     }
  454.     else {
  455.         printf("\t???%s\n",cmdlst);
  456.         return -1;
  457.     }
  458. #ifdef VERBOSE
  459.     fflush(stdout);
  460. #endif
  461.     if (one_command)
  462.         break;
  463.     }
  464.     if (toplevel) {
  465. #ifdef VERBOSE
  466.     IF(verbose)
  467.         putchar('\n') FLUSH;
  468. #endif
  469.     }
  470.     if (int_count) {
  471.     int_count = 0;
  472.     return -1;
  473.     }
  474.     return 0;
  475. }
  476.