home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / bss / pup.arc / PUP.C < prev    next >
C/C++ Source or Header  |  1987-11-17  |  9KB  |  350 lines

  1. #include <puppy.h>
  2. #include <pupmem.h>
  3. #include <ascii.h>
  4.  
  5. /*
  6.     Puppy's Main Section. 
  7.  
  8. This handles one caller then returns. This source contains the
  9. command prompt and the message base commands below it.
  10.  
  11. */
  12.  
  13. puppy() {
  14. char *cp;
  15.  
  16.     limit= pup.nlimit;
  17.     klimit= pup.klimit;            /* set limits */
  18.  
  19.     clr_clk();                /* start counting */
  20.     lmtstate= 0;                /* initialize the warning msg */
  21.     *name= NUL;                /* dont know their name */
  22.  
  23.     set_abort(0);                /* set carrier loss trap */
  24.     if (was_abort()) {            /* return to main */
  25.         printf("\r\n\r\n");
  26.         closemsg();            /* close the message base, */
  27.         putsys();            /* write system file */
  28.         close_up();            /* close aborted files */
  29.         return(0);            /* not TSYNC */
  30.     }
  31.     openmsg();            /* open message base, */
  32.  
  33.     mconflush();            /* flush interrupt buffers */
  34.     cmdflush();            /* flush command buffers */
  35.  
  36.     nulls= 0;            /* no nulls, */
  37.     width= 80; rows= 25;        /* std. screen */
  38.     column= line= 0;        /* sync up "more" stuff, */
  39.     mprintf("\r\nPup says: \"Hello!\"\r\n");
  40.     hello(1);            /* first time stuff, */
  41.     quote();            /* do a quotation */
  42.     mprintf("\r\nYou are caller #%,lu\r\n\r\n",++pup.callers);
  43.  
  44. /* Now execute commands forever. */
  45.  
  46.     topic= -1;            /* default to all topics */
  47.     msgnbr= 1;            /* first time through */
  48.  
  49.     while (1) {
  50.         cp= getarg("Command: M)essages H)ello! D)ownload U)pload B)ye (?=help): ");
  51.         switch (tolower(*cp)) {
  52.             case 'm': msg_base(); break;
  53.             case 'h': hello(0); break;
  54.             case 'd': download(); break;
  55.             case 'u': upload(); break;
  56.             case 'b': goodbye(); break;
  57.             case '?': dspfile("main.hlp"); break;
  58.             case NUL: break;
  59.             default:
  60.                 mprintf("eh?\r\n");
  61.                 cmdflush();
  62.                 break;
  63.         }
  64.     }
  65. }
  66.  
  67. /* Just say Hello. */
  68.  
  69. hello(a)
  70. int a;
  71. {
  72. struct _time t;
  73. char hour,ampm;
  74.  
  75.     userset();            /* set screen width, */
  76.     if (! a) a= ask("Want to see the \"Hello\" display again");
  77.     if (a) dspfile("welcome.pup");
  78.  
  79.     gtod(&t);            /* get time, */
  80.     mprintf("It is now: %d-%d-%d",t.day,t.month,t.year);
  81.  
  82.     hour= t.hour % 12; if (!hour) hour= 12;
  83.     mprintf(", %d:%02d",hour,t.minute);
  84.     if (t.hour >= 12) mputs("pm\r\n"); else mputs("am\r\n");
  85.  
  86.     mprintf("You have been on for %d minutes, %d left\r\n",minutes,limit - minutes);
  87. }
  88.  
  89. /* Get user screen settings, etc */
  90.  
  91. userset() {
  92. int n;
  93.  
  94.     n= atoi(getarg("How wide is your screen? (CR=80): "));
  95.     if ((n >= 20) && (n <= 80)) width= n;
  96.     else width= 80;
  97.  
  98.     n= atoi(getarg("How many lines on your screen? (CR=24): "));
  99.     if ((n >= 4) && (n <= 66)) rows= n;
  100.     else rows= 24;
  101.  
  102.     pause= ask("Pause every page");
  103. }
  104.  
  105. /* Me, I disconnect from you. */
  106.  
  107. goodbye() {
  108.  
  109.     if (ask("Want to disconnect now")) logoff(0,1);
  110. }
  111.  
  112. /*
  113.     Message Base commands
  114.  
  115. */
  116.  
  117. msg_base() {
  118. char *cp;
  119. FLAG again;
  120. int n;
  121.  
  122.     while (1) {
  123.         mputs("\r\n");
  124.         if (ismsg(msgnbr,topic)) {        /* (msgnbr set elsewere) */
  125.             listhdr(msgnbr);        /* display current message */
  126.             n= line;            /* remember current line */
  127.             again= ques("Read it? ");    /* ask the question, */
  128.             mputs("\r");
  129.             if (again) {            /* if it was Yes, */
  130.                 line= n;        /* keep header on screen */
  131.                 listmsg(msgnbr);    /* maybe read it */
  132.             }
  133.         }
  134.         do {
  135.             again= 0;            /* command error flag */
  136.             cp= getarg("S)tart-Where T)opic F)ind E)nter Q)uit (?=help CR=Next-Msg): ");
  137.             switch (tolower(*cp)) {
  138.                 case 't': topic= gettopic("What topic(s) you wanna see? (A=ALL)\r\n"); break;
  139.                 case 's': setmsg(); break;
  140.                 case NUL: nxtnbr(); break;
  141.                 case 'q': return;
  142.  
  143.                 case 'f': again= !mfind(); break;
  144.     
  145.                 case '?': again= 1; dspfile("message.hlp"); break;
  146.                 case 'e': again= 1; msend(); break;
  147.                 default: again= 1; mprintf("eh?\r\n"); break;
  148.             }
  149.         } while (again);
  150.     }
  151. }
  152.  
  153. /* Select a specific message. Message numbers run: 0 non-existent, 
  154. 1 is the top, 2 is "1 down from the top", etc. */
  155.  
  156. setmsg() {
  157. char *cp;
  158. int n;
  159.  
  160.     cp= getarg("Start how far down: (T=TOP) #");
  161.     if (! *cp) return;            /* nothing entered */
  162.  
  163.     n= 0;                    /* assume its garbage */
  164.     if (tolower(*cp) == 't') n= 1;        /* T == top */
  165.     else n= atoi(cp) + 1;            /* else a number, */
  166.  
  167.     if (n && ismsg(n,-1)) msgnbr= n;    /* check if a valid msg */
  168.     else mprintf("\r\nNO SUCH MESSAGE\r\n");
  169. }
  170.  
  171. /* Pick the next message number that exists and is in the right topic.
  172. It sets 0 if the bottom of the pile is reached, and prevents 0 from 
  173. "wrapping around" to 1. */
  174.  
  175. nxtnbr() {
  176.  
  177.     if (msgnbr) {
  178.         while (++msgnbr < highmsg()) {
  179.             if (ismsg(msgnbr,topic)) return(msgnbr);
  180.         }
  181.         msgnbr= 0;
  182.     }
  183.     mprintf("\r\nBOTTOM OF THE PILE\r\n");
  184.     return(0);
  185. }
  186.  
  187. /* Make the user select a topic; return the bit fields set. */
  188.  
  189. gettopic(prompt)
  190. char *prompt;        /* initial prompt */
  191. {
  192. int n;
  193. char *cp;
  194. WORD topic;
  195.  
  196.     while (1) {
  197.         if (! isargs()) {
  198.             mputs(prompt);             /* say what */
  199.             for (n= 0; n < 16; n++) {    /* list them all, */
  200.                 if (*pup.topic[n].name) 
  201.                     mprintf("#%d %-8s %s\r\n",n + 1,pup.topic[n].name,pup.topic[n].desc);
  202.             }
  203.         }
  204.         topic= 0;                /* start with this */
  205.         cp= getarg("Choose topic #");        /* ask for them, */
  206.         while (*cp) {                /* check each one */
  207.             if (tolower(*cp) == 'a') topic |= -1;
  208.             n= atoi(cp);            /* ALL or a number */    
  209.             if ((n > 0) && (n < 16)) {    /* if in range, */
  210.                 --n;
  211.                 if (*pup.topic[n].name) topic |= (1 << n);
  212.             }
  213.             cp= next_arg(cp);        /* ptr to next arg (if any) */
  214.         }
  215.         cmdflush();                /* pitch input line we just ate */
  216.         if (topic) return(topic);        /* return 'em when we get 'em */
  217.     }    
  218. }
  219.  
  220. /* Search the message base for content. Return true if the thing was found. */
  221.  
  222. mfind() {
  223. char pattern[SS];
  224. struct _msg *m;
  225.  
  226. char *strfnd();
  227.  
  228.     while (1) {
  229.         cpyarg(pattern,getarg("Find what? (CR=Quit, ?=Help): "));
  230.         if (*pattern == '?') dspfile("find.hlp"); /* ? is help */
  231.         else if (! *pattern) return(0);        /* just a CR */
  232.         else break;                /* anything else */
  233.     }
  234.     mputs("Wait ... (Control-C to abort)\r\n");
  235.     while (nxtnbr()) {                /* find a message, */
  236.         pollkbd();                /* check the keyboard */
  237.         if (abort) break;            /* allow stopping */
  238.         m= getmsg(msgnbr);            /* get this msg */
  239.         if (strfnd(m-> to,pattern)) return(1);    /* check each field */
  240.         if (strfnd(m-> from,pattern)) return(1);
  241.         if (strfnd(m-> subj,pattern)) return(1);
  242.         loadmsg(msgnbr);            /* get the msg body, */
  243.         if (strfnd(text,pattern)) return(1);    /* search that */
  244.     }
  245.     return(0);
  246. }
  247.  
  248. /* Edit-message commands. Returns true if the message should be saved. */
  249.  
  250. msend() {
  251. int lineno;        /* last used line number */
  252. char *cp;
  253. int i;
  254. struct _msg *msg;    /* ptr to new message */
  255.  
  256.     msg= newmsg();                /* ptr to our new message */
  257.     gtod(&msg-> time);            /* get the current time, */
  258.     while (! *name) {            /* if we dont have the guys name */
  259.         cmdflush();            /* sorry, no command-ahead */
  260.         input("I didn't get your name: ",name,sizeof(msg-> from));
  261.         if (! *name) mprintf("\r\nAHEM: ");
  262.     }
  263.     strcpy(msg-> from,name);        /* set From: */
  264.     mprintf("FROM: %s\r\n",name);
  265.     input("TO: ",msg-> to,sizeof(msg-> to));
  266.     if (! *msg-> to) return;
  267.     msg-> topic= gettopic("Choose a TOPIC:\r\n");
  268.     input("TITLE: ",msg-> subj,sizeof(msg-> subj));
  269.     if (! *msg-> subj) return;
  270.  
  271.     cp= text; i= pup.msgsize;        /* clear out the text buffer */
  272.     while (i--) *cp++= NUL;
  273.  
  274.     mprintf("Enter your message,\r\nControl-C or Control-K to end\r\n");
  275.     lineno= edit(0);            /* get some new text */
  276.  
  277.     while (1) {
  278.         cp= "\r\nEdit: C)ancel A)dd L)ist I)ns D)el S)ave (?=help): ";
  279.         cp= getarg(cp);
  280.         switch (tolower(*cp)) { 
  281.             case '?': dspfile("edit.hlp"); break;
  282.             case NUL: break;
  283.             case 'c': if (ask("Cancel this message")) return(0); break;
  284.             case 'a': lineno= edit(lineno); break;
  285.             case 'l': listhdr(highmsg());
  286.                 for (i= 0; i < lineno; i++) {
  287.                     msgline(i);
  288.                     if (abort) break;
  289.                 }
  290.                 break;
  291.  
  292.             case 'i':
  293.                 i= getlno("Insert before line #",lineno);
  294.                 if (i < 0) break;
  295.                 lineno= edit(i);
  296.                 break;
  297.  
  298.             case 'd':
  299.                 i= getlno("Delete line #",lineno);
  300.                 if (i < 0) break;
  301.                 lineno= del_line(i,lineno);
  302.                 break;
  303.  
  304.             case 's':
  305.                 mprintf("Saving new message\r\n");
  306.                 savemsg(lineno);
  307.                 ++msgnbr;        /* new top means all shift down */
  308.                 return(1);
  309.  
  310.             default:
  311.                 mprintf("eh?\r\n");
  312.                 cmdflush();
  313.                 break;
  314.         }
  315.     } 
  316. }
  317.  
  318. /* Display a message line. */
  319.  
  320. msgline(n)
  321. int n;
  322. {
  323. char *s;
  324.  
  325.     s= &text[n * width];            /* s == ptr to line to display */
  326.     mprintf("%2u: ",n + 1);            /* display line number, */
  327.     while (*s && !abort) {            /* until done or Control-K */
  328.         if (*s >= ' ') mconout(*s);    /* output them */
  329.         ++s;
  330.     }
  331.     mputs("\r\n");
  332. }
  333.  
  334. /* Get a line number; return -1 if invalid number. */
  335.  
  336. getlno(ps,lim)
  337. char *ps;
  338. int lim;
  339. {
  340. char *cp;
  341. int n;
  342.  
  343.     n= atoi(getarg(ps)) - 1;        /* get one, (garbage becomes -1) */
  344.     if (n >= lim) {
  345.         mprintf("Must be 1 to %d\r\n",lim);
  346.         n= -1;
  347.     }
  348.     return(n);
  349. }
  350.