3 * msh.c -- The nmh shell
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 * Keep more status information in maildrop map
19 #include <h/signals.h>
20 #include <h/dropsbr.h>
21 #include <h/fmt_scan.h>
22 #include <h/scansbr.h>
43 #define QUOTE '\\' /* sigh */
45 static struct swit switches[] = {
47 { "idstart number", -7 }, /* interface from bbc */
49 { "idstop number", -6 }, /* .. */
51 { "idquit number", -6 }, /* .. */
53 { "idname BBoard", -6 }, /* .. */
55 { "prompt string", 0 },
63 { "vmhwrite fd", -8 },
67 { "popwrite fd", -8 },
79 static int mbx_style = MMDF_FORMAT;
84 char*fmsh = NULL; /* folder instead of file */
85 int modified; /* command modified folder */
86 struct msgs *mp; /* used a lot */
88 struct Msg *Msgs = NULL; /* Msgs[0] not used */
89 static FILE *fp; /* input file */
90 static FILE *yp = NULL; /* temporary file */
91 static int mode; /* mode of file */
92 static int numfds = 0; /* number of files cached */
93 static int maxfds = 0; /* number of files cached to be cached */
94 static time_t mtime = (time_t) 0; /* mtime of file */
99 #define ALARM ((unsigned int) 10)
100 #define ttyN(c) ttyNaux ((c), NULL)
104 static int vmhpid = OK;
109 static int vmhtty = NOTOK;
116 static int topcur = 0;
118 static int numwins = 0;
119 static int windows[NWIN + 1];
121 static jmp_buf peerenv;
124 int pmsh = 0; /* BPOP enabled */
125 extern char response[];
131 static int pfd = NOTOK; /* fd parent is reading from */
132 static int ppid = 0; /* pid of parent */
137 int interactive; /* running from a /dev/tty */
138 int redirected; /* re-directing output */
139 FILE *sp = NULL; /* original stdout */
141 char *cmd_name; /* command being run */
142 char myfilter[BUFSIZ]; /* path to mhl.forward */
144 static char *myprompt = "(%s) ";/* prompting string */
149 static int gap; /* gap in BBoard-ID:s */
150 static char *myname = NULL; /* BBoard name */
151 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
156 SIGNAL_HANDLER istat; /* original SIGINT */
157 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
158 SIGNAL_HANDLER qstat; /* original SIGQUIT */
161 SIGNAL_HANDLER tstat; /* original SIGTSTP */
164 int interrupted; /* SIGINT detected */
165 int broken_pipe; /* SIGPIPE detected */
166 int told_to_quit; /* SIGQUIT detected */
169 int should_intr; /* signal handler should interrupt call */
170 jmp_buf sigenv; /* the environment pointer */
176 int SOprintf (char *, ...); /* from termsbr.c */
177 int sc_width (void); /* from termsbr.c */
178 void fsetup (char *);
180 FILE *msh_ready (int, int);
183 void display_info (int);
186 void seq_setcur (struct msgs *, int);
187 void padios (char *, char *, ...);
188 void padvise (char *, char *, ...);
194 static void msh (int);
195 static int read_map (char *, long);
196 static int read_file (long, int);
200 static int pop_statmsg (char *);
202 static int read_pop (void);
203 static int pop_action (char *);
206 static void m_gMsgs (int);
207 FILE *msh_ready (int, int);
208 static int check_folder (int);
209 static void scanrange (int, int);
210 static void scanstring (char *);
211 static void write_ids (void);
212 static void quit (void);
213 static int getargs (char *, struct swit *, struct Cmd *);
214 static int getcmds (struct swit *, struct Cmd *, int);
215 static int parse (char *, struct Cmd *);
216 static int init_io (struct Cmd *, int);
217 static int initaux_io (struct Cmd *);
218 static void fin_io (struct Cmd *, int);
219 static void finaux_io (struct Cmd *);
220 static void m_init (void);
221 static RETSIGTYPE intrser (int);
222 static RETSIGTYPE pipeser (int);
223 static RETSIGTYPE quitser (int);
224 static RETSIGTYPE alrmser (int);
225 static int pINI (void);
226 static int pQRY (char *, int);
227 static int pQRY1 (int);
228 static int pQRY2 (void);
229 static int pCMD (char *, struct swit *, struct Cmd *);
230 static int pFIN (void);
231 static int peerwait (void);
232 static int ttyNaux (struct Cmd *, char *);
233 static int ttyR (struct Cmd *);
234 static int winN (struct Cmd *, int, int);
235 static int winR (struct Cmd *);
236 static int winX (int);
240 main (int argc, char **argv)
242 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
243 char *cp, *file = NULL, *folder = NULL;
244 char **argp, **arguments, buf[BUFSIZ];
246 int pmsh1 = 0, pmsh2 = 0;
250 setlocale(LC_ALL, "");
252 invo_name = r1bindex (argv[0], '/');
254 /* read user profile/context */
257 mts_init (invo_name);
258 arguments = getarguments (invo_name, argc,argv, 1);
261 while ((cp = *argp++)) {
263 switch (smatch (++cp, switches)) {
265 ambigsw (cp, switches);
268 adios (NULL, "-%s unknown", cp);
271 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
272 print_help (buf, switches, 1);
275 print_version(invo_name);
279 if (!(cp = *argp++) || *cp == '-')
280 adios (NULL, "missing argument to %s", argp[-2]);
281 if ((id = atoi (cp)) < 1)
282 adios (NULL, "bad argument %s %s", argp[-2], cp);
285 if (!(cp = *argp++) || *cp == '-')
286 adios (NULL, "missing argument to %s", argp[-2]);
287 if ((pfd = atoi (cp)) <= 1)
288 adios (NULL, "bad argument %s %s", argp[-2], cp);
291 if (!(cp = *argp++) || *cp == '-')
292 adios (NULL, "missing argument to %s", argp[-2]);
293 if ((ppid = atoi (cp)) <= 1)
294 adios (NULL, "bad argument %s %s", argp[-2], cp);
297 if (!(myname = *argp++) || *myname == '-')
298 adios (NULL, "missing argument to %s", argp[-2]);
309 if (!(myprompt = *argp++) || *myprompt == '-')
310 adios (NULL, "missing argument to %s", argp[-2]);
314 if (!(cp = *argp++) || *cp == '-')
315 adios (NULL, "missing argument to %s", argp[-2]);
316 if ((vmh1 = atoi (cp)) < 1)
317 adios (NULL, "bad argument %s %s", argp[-2], cp);
320 if (!(cp = *argp++) || *cp == '-')
321 adios (NULL, "missing argument to %s", argp[-2]);
322 if ((vmh2 = atoi (cp)) < 1)
323 adios (NULL, "bad argument %s %s", argp[-2], cp);
327 if (!(cp = *argp++) || *cp == '-')
328 adios (NULL, "missing argument to %s", argp[-2]);
330 if ((pmsh1 = atoi (cp)) < 1)
331 adios (NULL, "bad argument %s %s", argp[-2], cp);
335 if (!(cp = *argp++) || *cp == '-')
336 adios (NULL, "missing argument to %s", argp[-2]);
338 if ((pmsh2 = atoi (cp)) < 1)
339 adios (NULL, "bad argument %s %s", argp[-2], cp);
350 if (*cp == '+' || *cp == '@') {
352 adios (NULL, "only one folder at a time!");
354 folder = pluspath (cp);
358 adios (NULL, "only one file at a time!");
363 if (!file && !folder)
366 adios (NULL, "use a file or a folder, not both");
367 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
370 ioctl (pfd, FIOCLEX, NULL);
376 istat = SIGNAL2 (SIGINT, intrser);
377 qstat = SIGNAL2 (SIGQUIT, quitser);
379 sc_width (); /* MAGIC... */
381 if ((vmh = vmh1 && vmh2)) {
384 SIGNAL (SIGINT, SIG_IGN);
385 SIGNAL (SIGQUIT, SIG_IGN);
387 tstat = SIGNAL (SIGTSTP, SIG_IGN);
392 if (pmsh = pmsh1 && pmsh2) {
393 cp = getenv ("MHPOPDEBUG");
395 if (pop_set (pmsh1, pmsh2, cp && *cp, myname) == NOTOK)
397 if (pop_set (pmsh1, pmsh2, cp && *cp) == NOTOK)
399 padios (NULL, "%s", response);
401 file = folder, folder = NULL;
410 display_info (id > 0 ? scansw : 0);
412 msh (id > 0 ? scansw : 0);
420 static struct swit mshcmds[] = {
485 register char *cp, **ap;
486 char prompt[BUFSIZ], *vec[MAXARGS];
488 register struct Cmd *cmdp;
489 static int once_only = ADVCMD;
491 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
500 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
505 check_folder (scansw);
506 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
511 cmd_name = mshcmds[i].sw;
519 if (once_only == ADVCMD)
520 once_only = i = SHOWCMD;
522 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
523 cmd_name = mshcmds[i].sw;
529 case FORWCMD: /* sigh */
539 if ((cp = context_find (cmd_name))) {
541 ap = brkstring (cp, " ", "\n");
542 ap = copyip (ap, vec, MAXARGS);
553 copyip (cmdp->args + 1, ap, MAXARGS);
557 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
563 redirected = vmh || cmdp->direction != STDIO;
574 if (!vmh || ttyN (cmdp) != NOTOK)
575 forkcmd (vec, cmd_name);
579 if (!vmh || ttyN (cmdp) != NOTOK)
584 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
590 || (filehak (vec) == OK ? ttyN (cmdp)
591 : winN (cmdp, DISPLAY, 1)) != NOTOK)
596 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
601 if (!vmh || ttyN (cmdp) != NOTOK)
606 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
612 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
617 if (!vmh || ttyN (cmdp) != NOTOK)
624 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
630 || (packhak (vec) == OK ? ttyN (cmdp)
631 : winN (cmdp, DISPLAY, 1)) != NOTOK)
636 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
641 if (!vmh || ttyN (cmdp) != NOTOK)
646 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
651 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
656 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
661 padios (NULL, "no dispatch for %s", cmd_name);
683 fsetup (char *folder)
689 maildir = m_maildir (folder);
690 if (chdir (maildir) == NOTOK)
691 padios (maildir, "unable to change directory to");
693 /* read folder and create message structure */
694 if (!(mp = folder_read (folder)))
695 padios (NULL, "unable to read folder %s", folder);
697 /* check for empty folder */
699 padios (NULL, "no messages in %s", folder);
702 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
704 m_gMsgs (mp->hghmsg);
706 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
707 Msgs[msgnum].m_bboard_id = 0;
708 Msgs[msgnum].m_top = NOTOK;
709 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
710 Msgs[msgnum].m_scanl = NULL;
715 fmsh = getcpy (folder);
717 maxfds = OPEN_MAX / 2;
719 if ((maxfds -= 2) < 1)
735 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
736 if ((fp = fopen (tmpfil, "w+")) == NULL)
737 padios (tmpfil, "unable to create");
742 if ((fp = fopen (file, "r")) == NULL)
743 padios (file, "unable to read");
745 ioctl (fileno (fp), FIOCLEX, NULL);
747 if (fstat (fileno (fp), &st) != NOTOK) {
748 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
749 msgp = read_map (file, (long) st.st_size);
752 mode = m_gmprot (), mtime = 0;
756 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
757 padios (NULL, "no messages in %s", myname ? myname : file);
759 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
760 padios (NULL, "unable to allocate folder storage");
762 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
763 padios (NULL, "unable to allocate message status storage");
769 mp->foldpath = getcpy (myname ? myname : file);
770 clear_folder_flags (mp);
778 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
785 mp->hghoff = mp->hghmsg + 1;
790 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
792 clear_msg_flags (mp, i);
797 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
798 if (Msgs[i].m_top) /* set in read_pop() */
799 clear_msg_flags (mp, i);
807 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
808 clear_msg_flags (mp, i);
813 mp->msgattrs[0] = getcpy ("unseen");
814 mp->msgattrs[1] = NULL;
816 m_unknown (fp); /* the MAGIC invocation */
825 read_map (char *file, long size)
827 register int i, msgp;
828 register struct drop *dp, *mp;
836 if ((i = map_read (file, size, &rp, 1)) == 0)
842 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
843 mp = &Msgs[msgp].m_drop;
845 mp->d_size = dp->d_size;
846 mp->d_start = dp->d_start;
847 mp->d_stop = dp->d_stop;
848 Msgs[msgp].m_scanl = NULL;
857 read_file (long pos, int msgp)
860 register struct drop *dp, *mp;
868 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
871 m_gMsgs ((msgp - 1) + i);
873 for (dp = rp; i-- > 0; msgp++, dp++) {
874 mp = &Msgs[msgp].m_drop;
876 mp->d_size = dp->d_size;
877 mp->d_start = dp->d_start;
878 mp->d_stop = dp->d_stop;
879 Msgs[msgp].m_scanl = NULL;
889 static int pop_base = 0;
892 pop_statmsg (char *s)
896 n = (i = atoi (s)) - pop_base; /* s="nnn header-line..." */
897 Msgs[n].m_top = Msgs[n].m_bboard_id = i;
907 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
908 padios (NULL, "%s", response);
912 #ifdef NNTP /* this makes read_pop() do some real work... */
913 pop_base = nbytes - 1; /* nmsgs=last-first+1, nbytes=first */
914 pop_exists (pop_statmsg);
923 fprintf (yp, "%s\n", s);
934 nMsgs = n + MAXFOLDER / 2;
935 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
937 padios (NULL, "unable to allocate Msgs structure");
944 nmsgs = nMsgs + n + MAXFOLDER / 2;
945 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
946 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
953 msh_ready (int msgnum, int full)
969 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
970 if (numfds >= maxfds)
971 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
972 if (Msgs[msgp].m_top != NOTOK) {
973 close (Msgs[msgp].m_top);
974 Msgs[msgp].m_top = NOTOK;
979 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
980 padios (cp, "unable to open message");
981 Msgs[msgnum].m_top = fd;
985 if ((fd = dup (fd)) == NOTOK)
986 padios ("cached message", "unable to dup");
987 if ((yp = fdopen (fd, "r")) == NULL)
988 padios (NULL, "unable to fdopen cached message");
989 fseek (yp, 0L, SEEK_SET);
994 if (pmsh && is_virtual (mp, msgnum)) {
995 if (Msgs[msgnum].m_top == 0)
996 padios (NULL, "msh_ready (%d, %d) botch", msgnum, full);
998 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
999 if ((yp = fopen (tmpfil, "w+")) == NULL)
1000 padios (tmpfil, "unable to create");
1003 if (pop_top (Msgs[msgnum].m_top, 4, pop_action) == NOTOK)
1004 padios (NULL, "%s", response);
1006 m_eomsbr ((int (*)()) 0); /* XXX */
1007 msg_style = MS_DEFAULT; /* .. */
1008 fseek (yp, 0L, SEEK_SET);
1012 fseek (fp, 0L, SEEK_END);
1013 fwrite (mmdlm1, 1, strlen (mmdlm1), fp);
1015 padios ("temporary file", "write error on");
1016 fseek (fp, 0L, SEEK_END);
1020 if (pop_retr (Msgs[msgnum].m_top, pop_action) == NOTOK)
1021 padios (NULL, "%s", response);
1024 fseek (fp, 0L, SEEK_END);
1026 fwrite (mmdlm2, 1, strlen (mmdlm2), fp);
1028 padios ("temporary file", "write error on");
1030 Msgs[msgnum].m_start = pos1;
1031 Msgs[msgnum].m_stop = pos2;
1033 unset_virtual (mp, msgnum);
1037 m_eomsbr ((int (*)()) 0); /* XXX */
1038 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
1044 check_folder (int scansw)
1046 int seqnum, i, low, hgh, msgp;
1055 if (stat (mp->foldpath, &st) == NOTOK)
1056 padios (mp->foldpath, "unable to stat");
1057 if (mtime == st.st_mtime)
1059 mtime = st.st_mtime;
1061 low = mp->hghmsg + 1;
1062 folder_free (mp); /* free folder/message structure */
1064 if (!(mp = folder_read (fmsh)))
1065 padios (NULL, "unable to re-read folder %s", fmsh);
1069 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
1070 if (Msgs[msgp].m_top != NOTOK) {
1071 close (Msgs[msgp].m_top);
1072 Msgs[msgp].m_top = NOTOK;
1075 if (Msgs[msgp].m_scanl) {
1076 free (Msgs[msgp].m_scanl);
1077 Msgs[msgp].m_scanl = NULL;
1083 if (modified || low > hgh)
1087 if (fstat (fileno (fp), &st) == NOTOK)
1088 padios (mp->foldpath, "unable to fstat");
1089 if (mtime == st.st_mtime)
1091 mode = (int) (st.st_mode & 0777);
1092 mtime = st.st_mtime;
1094 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
1095 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
1096 if (msgp >= MAXFOLDER)
1097 padios (NULL, "more than %d messages in %s", MAXFOLDER,
1099 if (msgp <= mp->hghmsg)
1102 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
1103 padios (NULL, "unable to allocate folder storage");
1105 low = mp->hghmsg + 1, hgh = msgp;
1106 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
1107 for (i = mp->hghmsg + 1; i <= msgp; i++) {
1110 add_sequence(mp, seqnum, i);
1120 advise (NULL, "new messages have arrived!\007");
1122 scanrange (low, hgh);
1129 scanrange (int low, int hgh)
1131 char buffer[BUFSIZ];
1133 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
1134 scanstring (buffer);
1139 scanstring (char *arg)
1141 char *cp, **ap, *vec[MAXARGS];
1144 * This should be replace with a call to getarguments()
1146 if ((cp = context_find (cmd_name = "scan"))) {
1148 ap = brkstring (cp, " ", "\n");
1149 ap = copyip (ap, vec, MAXARGS);
1165 register int cur, seqnum, i, msgnum;
1167 if (mp->curmsg == 0)
1168 seq_setcur (mp, mp->lowmsg);
1169 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
1172 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1173 add_sequence(mp, seqnum, msgnum);
1178 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1179 if (does_exist(mp, msgnum)) /* FIX */
1180 if ((i = readid (msgnum)) > 0 && i < id) {
1182 clear_sequence(mp, seqnum, msgnum);
1185 for (i = mp->lowmsg; i < msgnum; i++)
1186 clear_sequence(mp, seqnum, i);
1188 if (cur > mp->hghmsg)
1191 seq_setcur (mp, cur);
1194 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
1195 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
1204 char *bp, buf[BUFSIZ], name[NAMESZ];
1207 int arg1, arg2, arg3;
1210 if (Msgs[msgnum].m_bboard_id)
1211 return Msgs[msgnum].m_bboard_id;
1214 if (Msgs[msgnum].m_top == 0)
1215 padios (NULL, "readid (%d) botch", msgnum);
1216 if (pop_list (Msgs[msgnum].m_top, (int *) 0, &arg1, &arg2, &arg3) == OK
1218 return (Msgs[msgnum].m_bboard_id = arg3);
1222 zp = msh_ready (msgnum, 0);
1224 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1228 if (!mh_strcasecmp (name, BBoard_ID)) {
1230 while (state == FLDPLUS) {
1231 state = m_getfld (state, name, buf, sizeof(buf), zp);
1237 return (Msgs[msgnum].m_bboard_id = i);
1241 while (state == FLDPLUS)
1242 state = m_getfld (state, name, buf, sizeof(buf), zp);
1243 if (state != FLDEOF)
1253 display_info (int scansw)
1257 interactive = isatty (fileno (stdout));
1259 if ((sd = dup (fileno (stdout))) == NOTOK)
1260 padios ("standard output", "unable to dup");
1261 #ifndef BSD42 /* XXX */
1263 ioctl (sd, FIOCLEX, NULL);
1264 #endif /* FIOCLEX */
1265 #endif /* not BSD42 */
1266 if ((sp = fdopen (sd, "w")) == NULL)
1267 padios ("standard output", "unable to fdopen");
1270 m_putenv ("mhfolder", mp->foldpath);
1275 printf ("Reading ");
1276 if (SOprintf ("%s", myname))
1277 printf ("%s", myname);
1278 printf (", currently at message %d of %d\n",
1279 mp->curmsg, mp->hghmsg);
1282 printf ("Reading ");
1284 printf ("+%s", fmsh);
1286 printf ("%s", mp->foldpath);
1287 printf (", currently at message %d of %d\n",
1288 mp->curmsg, mp->hghmsg);
1291 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1293 && in_sequence(mp, seqnum, mp->hghmsg))
1294 scanstring ("unseen");
1301 int i = 0, seqnum, msgnum;
1307 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1308 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1309 if (!in_sequence(mp, seqnum, msgnum)) {
1310 if (Msgs[msgnum].m_bboard_id == 0)
1312 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1316 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1317 write (pfd, buffer, sizeof(buffer));
1327 char *cp, tmpfil[BUFSIZ];
1328 char map1[BUFSIZ], map2[BUFSIZ];
1332 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1334 rc2peer (RC_FIN, 0, NULL);
1339 ttyNaux (NULLCMD, "FAST");
1341 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1342 advise (mp->foldpath, "unable to lock");
1349 if (fstat (fileno (dp), &st) == NOTOK) {
1350 advise (mp->foldpath, "unable to stat");
1353 if (mtime != st.st_mtime) {
1354 advise (NULL, "new messages have arrived, no update");
1357 mode = (int) (st.st_mode & 0777);
1359 if (mp->nummsg == 0) {
1360 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1361 if (getanswer (cp)) {
1362 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1365 advise (mp->foldpath, "error zero'ing");
1366 unlink (map_name (mp->foldpath));/* XXX */
1371 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1372 if (!getanswer (cp))
1374 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1375 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1376 advise (tmpfil, "unable to open");
1380 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1381 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1382 mbx_close (tmpfil, md);
1384 unlink (map_name (tmpfil));
1387 mbx_close (tmpfil, md);
1389 if (rename (tmpfil, mp->foldpath) == NOTOK)
1390 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1392 strncpy (map1, map_name (tmpfil), sizeof(map1));
1393 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1395 if (rename (map1, map2) == NOTOK) {
1396 admonish (map2, "unable to rename %s to", map1);
1405 lkfclose (dp, mp->foldpath);
1414 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1418 static char buffer[BUFSIZ];
1424 switch (setjmp (sigenv)) {
1431 if (interrupted && !told_to_quit) {
1437 kill (ppid, SIGEMT);
1439 kill (ppid, SIGTERM);
1445 printf ("%s", prompt);
1448 for (cp = buffer; (i = getchar ()) != '\n';) {
1450 if (interrupted && !told_to_quit) {
1455 if (told_to_quit || i == EOF) {
1458 kill (ppid, SIGEMT);
1460 kill (ppid, SIGTERM);
1466 longjmp (sigenv, DONE);
1468 if (cp < &buffer[sizeof buffer - 2])
1475 if (buffer[0] == '?') {
1476 printf ("commands:\n");
1477 print_sw (ALL, sw, "");
1478 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1483 if (parse (buffer, cmdp) == NOTOK)
1486 switch (i = smatch (cmdp->args[0], sw)) {
1488 ambigsw (cmdp->args[0], sw);
1491 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1505 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1508 struct record rcs, *rc;
1514 switch (peer2rc (rc)) {
1516 pQRY (rc->rc_data, scansw);
1520 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1527 kill (ppid, SIGEMT);
1529 kill (ppid, SIGTERM);
1534 padios (NULL, "%s", rc->rc_data);
1537 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1544 parse (char *buffer, struct Cmd *cmdp)
1550 pp = cmdp->args[argp++] = cmdp->line;
1551 cmdp->redirect = NULL;
1552 cmdp->direction = STDIO;
1553 cmdp->stream = NULL;
1555 for (cp = buffer; (c = *cp); cp++) {
1561 fmt2peer (RC_EOF, "null command");
1565 while ((c = *cp++)) {
1572 cmdp->args[argp++] = pp;
1579 switch (c = *cp++) {
1581 padvise (NULL, "unmatched \"");
1586 if ((c = *cp++) == 0)
1597 if ((c = *cp++) == 0) {
1599 padvise (NULL, "the newline character can not be quoted");
1609 if (pp == cmdp->line) {
1610 padvise (NULL, "invalid null command");
1613 if (*cmdp->args[argp - 1] == 0)
1615 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1616 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1617 cmdp->direction = APPIO;
1620 cmdp->redirect = pp + 1;/* sigh */
1621 for (; (c = *cp); cp++)
1625 padvise (NULL, cmdp->direction != PIPIO
1626 ? "missing name for redirect"
1627 : "invalid null command");
1630 strcpy (cmdp->redirect, cp);
1631 if (cmdp->direction != PIPIO) {
1633 if (isspace (*cp)) {
1634 padvise (NULL, "bad name for redirect");
1637 if (expand (cmdp->redirect) == NOTOK)
1646 cmdp->args[argp] = NULL;
1653 expand (char *redirect)
1659 if (*redirect != '~')
1662 if ((cp = strchr(pp = redirect + 1, '/')))
1667 if ((pw = getpwnam (pp)))
1670 padvise (NULL, "unknown user: %s", pp);
1674 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1675 strcpy (redirect, path);
1681 init_io (struct Cmd *cmdp, int vio)
1688 result = initaux_io (cmdp);
1696 initaux_io (struct Cmd *cmdp)
1700 switch (cmdp->direction) {
1706 mode = cmdp->direction == CRTIO ? "write" : "append";
1707 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1708 padvise (cmdp->redirect, "unable to %s ", mode);
1709 cmdp->direction = STDIO;
1715 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1716 padvise (cmdp->redirect, "unable to pipe");
1717 cmdp->direction = STDIO;
1720 SIGNAL (SIGPIPE, pipeser);
1725 padios (NULL, "unknown redirection for command");
1729 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1730 padios ("standard output", "unable to dup2");
1738 fin_io (struct Cmd *cmdp, int vio)
1750 finaux_io (struct Cmd *cmdp)
1752 switch (cmdp->direction) {
1759 close (fileno (stdout));
1760 if (ferror (stdout))
1761 padvise (NULL, "problems writing %s", cmdp->redirect);
1762 fclose (cmdp->stream);
1767 close (fileno (stdout));
1768 pclose (cmdp->stream);
1769 SIGNAL (SIGPIPE, SIG_DFL);
1773 padios (NULL, "unknown redirection for command");
1776 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1777 padios ("standard output", "unable to dup2");
1780 cmdp->direction = STDIO;
1789 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1790 unset_selected (mp, msgnum);
1791 mp->lowsel = mp->hghsel = mp->numsel = 0;
1799 folder_free (mp); /* free folder/message structure */
1811 seq_setcur (struct msgs *mp, int msgnum)
1813 if (mp->curmsg == msgnum)
1816 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1817 free (Msgs[mp->curmsg].m_scanl);
1818 Msgs[mp->curmsg].m_scanl = NULL;
1820 if (Msgs[msgnum].m_scanl) {
1821 free (Msgs[msgnum].m_scanl);
1822 Msgs[msgnum].m_scanl = NULL;
1825 mp->curmsg = msgnum;
1833 #ifndef RELIABLE_SIGNALS
1834 SIGNAL (SIGINT, intrser);
1842 longjmp (sigenv, NOTOK);
1850 #ifndef RELIABLE_SIGNALS
1851 SIGNAL (SIGPIPE, pipeser);
1854 if (broken_pipe++ == 0)
1855 fprintf (stderr, "broken pipe\n");
1861 longjmp (sigenv, NOTOK);
1869 #ifndef RELIABLE_SIGNALS
1870 SIGNAL (SIGQUIT, quitser);
1878 longjmp (sigenv, NOTOK);
1886 longjmp (peerenv, DONE);
1895 struct record rcs, *rc;
1900 switch (peer2rc (rc)) {
1903 while (isspace (*bp))
1905 if (sscanf (bp, "%d", &vrsn) != 1) {
1907 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1910 if (vrsn != RC_VRSN) {
1911 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1915 while (*bp && !isspace (*bp))
1917 while (isspace (*bp))
1919 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1924 for (i = 1; i <= numwins; i++) {
1925 while (*bp && !isspace (*bp))
1927 while (isspace (*bp))
1929 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1932 rc2peer (RC_ACK, 0, NULL);
1936 padios (NULL, "%s", rc->rc_data);
1939 fmt2peer (RC_ERR, "pINI protocol screw-up");
1940 done (1); /* NOTREACHED */
1943 return 1; /* dead code to satisfy the compiler */
1948 pQRY (char *str, int scansw)
1950 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1953 rc2peer (RC_EOF, 0, NULL);
1962 static int lastlow = 0,
1967 oldhgh = mp->hghmsg;
1968 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1969 switch (winX (STATUS)) {
1974 printf ("new messages have arrived!");
1977 _exit (0); /* NOTREACHED */
1980 lastlow = lastcur = lasthgh = lastnum = 0;
1984 switch (winX (DISPLAY)) {
1989 scanrange (oldhgh + 1, mp->hghmsg);
1992 _exit (0); /* NOTREACHED */
2001 switch (winX (STATUS)) {
2006 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
2007 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
2008 readid (mp->lowmsg));
2011 _exit (0); /* NOTREACHED */
2018 if (mp->lowmsg != lastlow
2019 || mp->curmsg != lastcur
2020 || mp->hghmsg != lasthgh
2021 || mp->nummsg != lastnum)
2022 switch (winX (STATUS)) {
2030 _exit (0); /* NOTREACHED */
2033 lastlow = mp->lowmsg;
2034 lastcur = mp->curmsg;
2035 lasthgh = mp->hghmsg;
2036 lastnum = mp->nummsg;
2047 int i, j, k, msgnum, n;
2053 if (mp->nummsg == 0 && mp->nummsg != num)
2054 switch (winX (SCAN)) {
2062 _exit (0); /* NOTREACHED */
2071 j = (k = windows[SCAN]) / 2;
2072 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
2073 if (does_exist (mp, msgnum))
2077 k = i >= k ? 1 : k - i;
2084 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
2085 if (does_exist (mp, msgnum)) {
2092 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
2093 if (does_exist (mp, msgnum)) {
2097 if (n++ >= windows[SCAN])
2103 && does_exist (mp, lo)
2104 && does_exist (mp, hi)
2106 || (lo == mp->curmsg && lo == mp->lowmsg))
2108 || (hi == mp->curmsg && hi == mp->hghmsg))
2109 && hi - lo == j - i)
2112 if (mp->curmsg != cur || modified)
2113 switch (winN (NULLCMD, SCAN, 0)) {
2121 scanrange (lo = i, hi = j);
2132 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
2137 switch (winX (DISPLAY)) {
2142 printf ("commands:\n");
2143 print_sw (ALL, sw, "");
2144 printf ("type ``quit'' to leave %s\n", invo_name);
2147 _exit (0); /* NOTREACHED */
2150 rc2peer (RC_EOF, 0, NULL);
2154 if (parse (str, cmdp) == NOTOK)
2157 switch (i = smatch (cmdp->args[0], sw)) {
2159 switch (winX (DISPLAY)) {
2164 ambigsw (cmdp->args[0], sw);
2167 _exit (0); /* NOTREACHED */
2170 rc2peer (RC_EOF, 0, NULL);
2176 "say what: ``%s'' -- type ? (or help) for help",
2191 switch (setjmp (peerenv)) {
2193 SIGNAL (SIGALRM, alrmser);
2196 status = peerwait ();
2210 struct record rcs, *rc;
2215 switch (peer2rc (rc)) {
2218 rc2peer (RC_FIN, 0, NULL);
2222 advise (NULL, "%s", rc->rc_data);
2226 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
2233 ttyNaux (struct Cmd *cmdp, char *s)
2235 struct record rcs, *rc;
2240 if (cmdp && init_io (cmdp, vmh) == NOTOK)
2243 /* XXX: fseek() too tricky for our own good */
2245 fseek (fp, 0L, SEEK_SET);
2248 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
2250 vmhtty = OK; /* fall */
2255 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2258 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2259 done (1); /* NOTREACHED */
2263 SIGNAL (SIGTSTP, tstat);
2270 ttyR (struct Cmd *cmdp)
2272 struct record rcs, *rc;
2277 SIGNAL (SIGTSTP, SIG_IGN);
2289 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2291 rc2peer (RC_EOF, 0, NULL);
2295 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2298 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2299 done (1); /* NOTREACHED */
2302 return 1; /* dead code to satisfy compiler */
2307 winN (struct Cmd *cmdp, int n, int eof)
2310 char buffer[BUFSIZ];
2311 struct record rcs, *rc;
2314 if (vmhpid == NOTOK)
2319 /* XXX: fseek() too tricky for our own good */
2321 fseek (fp, 0L, SEEK_SET);
2325 snprintf (buffer, sizeof(buffer), "%d", n);
2326 switch (str2rc (RC_WIN, buffer, rc)) {
2334 padios (NULL, "%s", rc->rc_data);
2337 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2341 if (pipe (pd) == NOTOK) {
2342 err2peer (RC_ERR, "pipe", "unable to");
2346 switch (vmhpid = fork()) {
2348 err2peer (RC_ERR, "fork", "unable to");
2355 SIGNAL (SIGPIPE, SIG_IGN);
2356 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2357 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2365 advise (NULL, "%s", rc->rc_data);
2369 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2373 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2376 rc2peer (RC_EOF, 0, NULL);
2381 advise (NULL, "%s", rc->rc_data);
2386 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2391 err2peer (RC_ERR, "pipe", "error reading from");
2393 _exit (i != NOTOK ? i : 1);
2396 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2397 padios ("standard input", "unable to dup");
2398 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2399 padios ("standard output", "unable to dup");
2400 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2401 padios ("diagnostic output", "unable to dup");
2404 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2405 dup2 (i, fileno (stdin));
2410 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2411 padios ("standard output", "unable to dup2");
2415 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2416 padios ("diagnostic output", "unable to dup2");
2419 if (cmdp && init_io (cmdp, 0) == NOTOK)
2421 pstat = SIGNAL (SIGPIPE, pipeser);
2433 winR (struct Cmd *cmdp)
2443 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2444 padios ("standard input", "unable to dup2");
2449 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2450 padios ("standard output", "unable to dup2");
2455 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2456 padios ("diagnostic output", "unable to dup2");
2460 SIGNAL (SIGPIPE, pstat);
2462 if ((status = pidwait (vmhpid, OK)) == 2)
2466 return (status == 0 ? OK : NOTOK);
2474 char buffer[BUFSIZ];
2475 struct record rcs, *rc;
2480 /* XXX: fseek() too tricky for our own good */
2482 fseek (fp, 0L, SEEK_SET);
2484 snprintf (buffer, sizeof(buffer), "%d", n);
2485 switch (str2rc (RC_WIN, buffer, rc)) {
2493 padios (NULL, "%s", rc->rc_data);
2496 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2500 if (pipe (pd) == NOTOK) {
2501 err2peer (RC_ERR, "pipe", "unable to");
2505 switch (pid = fork ()) {
2507 err2peer (RC_ERR, "fork", "unable to");
2513 close (fileno (stdin));
2514 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2515 dup2 (i, fileno (stdin));
2518 dup2 (pd[1], fileno (stdout));
2519 dup2 (pd[1], fileno (stderr));
2527 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2528 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2538 padios (NULL, "%s", rc->rc_data);
2541 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2545 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2550 padios (NULL, "%s", rc->rc_data);
2553 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2557 err2peer (RC_ERR, "pipe", "error reading from");
2561 return (i != NOTOK ? pid : NOTOK);
2567 padios (char *what, char *fmt, ...)
2573 verr2peer (RC_FIN, what, fmt, ap);
2576 advertise (what, NULL, fmt, ap);
2585 padvise (char *what, char *fmt, ...)
2591 verr2peer (RC_ERR, what, fmt, ap);
2593 advertise (what, NULL, fmt, ap);