]>
diplodocus.org Git - nmh/blob - uip/msh.c
3 * msh.c -- The nmh shell
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 * Keep more status information in maildrop map
17 #include <h/signals.h>
18 #include <h/dropsbr.h>
19 #include <h/fmt_scan.h>
20 #include <h/scansbr.h>
32 #define QUOTE '\\' /* sigh */
34 #define MSH_SWITCHES \
35 X("idstart number", -7, IDSW) /* interface from bbc */ \
36 X("idstop number", -6, FDSW) /* .. */ \
37 X("idquit number", -6, QDSW) /* .. */ \
38 X("idname BBoard", -6, NMSW) /* .. */ \
39 X("prompt string", 0, PRMPTSW) \
40 X("scan", 0, SCANSW) \
41 X("noscan", 0, NSCANSW) \
42 X("vmhread fd", -7, READSW) \
43 X("vmhwrite fd", -8, WRITESW) \
44 X("popread fd", -7, PREADSW) \
45 X("popwrite fd", -8, PWRITSW) \
46 X("topcur", 0, TCURSW) \
47 X("notopcur", 0, NTCURSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MSH
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MSH
, switches
);
59 static int mbx_style
= MMDF_FORMAT
;
64 char*fmsh
= NULL
; /* folder instead of file */
65 int modified
; /* command modified folder */
66 struct msgs
*mp
; /* used a lot */
68 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
69 static FILE *fp
; /* input file */
70 static FILE *yp
= NULL
; /* temporary file */
71 static int mode
; /* mode of file */
72 static int numfds
= 0; /* number of files cached */
73 static int maxfds
= 0; /* number of files cached to be cached */
74 static time_t mtime
= (time_t) 0; /* mtime of file */
79 #define ALARM ((unsigned int) 10)
80 #define ttyN(c) ttyNaux ((c), NULL)
84 static int vmhpid
= OK
;
89 static int vmhtty
= NOTOK
;
96 static int topcur
= 0;
98 static int numwins
= 0;
99 static int windows
[NWIN
+ 1];
101 static jmp_buf peerenv
;
106 static int pfd
= NOTOK
; /* fd parent is reading from */
107 static int ppid
= 0; /* pid of parent */
112 int interactive
; /* running from a /dev/tty */
113 int redirected
; /* re-directing output */
114 FILE *sp
= NULL
; /* original stdout */
116 char *cmd_name
; /* command being run */
117 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
119 static char *myprompt
= "(%s) ";/* prompting string */
124 static int gap
; /* gap in BBoard-ID:s */
125 static char *myname
= NULL
; /* BBoard name */
126 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
131 SIGNAL_HANDLER istat
; /* original SIGINT */
132 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
133 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
136 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
139 int interrupted
; /* SIGINT detected */
140 int broken_pipe
; /* SIGPIPE detected */
141 int told_to_quit
; /* SIGQUIT detected */
146 void fsetup (char *);
150 void display_info (int);
153 void seq_setcur (struct msgs
*, int);
154 void padios (char *, char *, ...);
155 void padvise (char *, char *, ...);
157 extern m_getfld_state_t gstate
; /* use the gstate in scansbr.c */
163 static void msh (int);
164 static int read_map (char *, long);
165 static int read_file (long, int);
167 static void m_gMsgs (int);
168 static int check_folder (int);
169 static void scanrange (int, int);
170 static void scanstring (char *);
171 static void write_ids (void);
172 static void quit (void);
173 static int getargs (char *, struct swit
*, struct Cmd
*);
174 static int getcmds (struct swit
*, struct Cmd
*, int);
175 static int parse (char *, struct Cmd
*);
176 static int init_io (struct Cmd
*, int);
177 static int initaux_io (struct Cmd
*);
178 static void fin_io (struct Cmd
*, int);
179 static void finaux_io (struct Cmd
*);
180 static void m_init (void);
181 static void intrser (int);
182 static void pipeser (int);
183 static void quitser (int);
184 static void alrmser (int);
185 static int pINI (void);
186 static int pQRY (char *, int);
187 static int pQRY1 (int);
188 static int pQRY2 (void);
189 static int pCMD (char *, struct swit
*, struct Cmd
*);
190 static int pFIN (void);
191 static int peerwait (void);
192 static int ttyNaux (struct Cmd
*, char *);
193 static int ttyR (struct Cmd
*);
194 static int winN (struct Cmd
*, int, int);
195 static int winR (struct Cmd
*);
196 static int winX (int);
200 main (int argc
, char **argv
)
202 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
203 char *cp
, *file
= NULL
, *folder
= NULL
;
204 char **argp
, **arguments
, buf
[BUFSIZ
];
207 setlocale(LC_ALL
, "");
209 invo_name
= r1bindex (argv
[0], '/');
211 /* read user profile/context */
214 mts_init (invo_name
);
215 arguments
= getarguments (invo_name
, argc
,argv
, 1);
218 while ((cp
= *argp
++)) {
220 switch (smatch (++cp
, switches
)) {
222 ambigsw (cp
, switches
);
225 adios (NULL
, "-%s unknown", cp
);
228 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
229 print_help (buf
, switches
, 1);
232 print_version(invo_name
);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
238 if ((id
= atoi (cp
)) < 1)
239 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 adios (NULL
, "missing argument to %s", argp
[-2]);
244 if ((pfd
= atoi (cp
)) <= 1)
245 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
250 if ((ppid
= atoi (cp
)) <= 1)
251 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
254 if (!(myname
= *argp
++) || *myname
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
266 if (!(myprompt
= *argp
++) || *myprompt
== '-')
267 adios (NULL
, "missing argument to %s", argp
[-2]);
271 if (!(cp
= *argp
++) || *cp
== '-')
272 adios (NULL
, "missing argument to %s", argp
[-2]);
273 if ((vmh1
= atoi (cp
)) < 1)
274 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 adios (NULL
, "missing argument to %s", argp
[-2]);
279 if ((vmh2
= atoi (cp
)) < 1)
280 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
284 if (!(cp
= *argp
++) || *cp
== '-')
285 adios (NULL
, "missing argument to %s", argp
[-2]);
288 if (!(cp
= *argp
++) || *cp
== '-')
289 adios (NULL
, "missing argument to %s", argp
[-2]);
299 if (*cp
== '+' || *cp
== '@') {
301 adios (NULL
, "only one folder at a time!");
303 folder
= pluspath (cp
);
307 adios (NULL
, "only one file at a time!");
312 if (!file
&& !folder
)
315 adios (NULL
, "use a file or a folder, not both");
316 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
319 ioctl (pfd
, FIOCLEX
, NULL
);
322 istat
= SIGNAL2 (SIGINT
, intrser
);
323 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
325 sc_width (); /* MAGIC... */
327 if ((vmh
= vmh1
&& vmh2
)) {
330 SIGNAL (SIGINT
, SIG_IGN
);
331 SIGNAL (SIGQUIT
, SIG_IGN
);
333 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
342 display_info (id
> 0 ? scansw
: 0);
344 msh (id
> 0 ? scansw
: 0);
354 #define MSHCMDS_SWITCHES \
355 X("advance", -7, ADVCMD) \
356 X("ali", 0, ALICMD) \
357 X("burst", 0, EXPLCMD) \
358 X("comp", 0, COMPCMD) \
359 X("dist", 0, DISTCMD) \
360 X("exit", 0, EXITCMD) \
361 X("folder", 0, FOLDCMD) \
362 X("forw", 0, FORWCMD) \
363 X("help", 0, HELPCMD) \
365 X("mark", 0, MARKCMD) \
366 X("mhmail", 0, MAILCMD) \
367 X("mhn", 0, MHNCMD) \
368 X("msgchk", 0, MSGKCMD) \
369 X("next", 0, NEXTCMD) \
370 X("packf", 0, PACKCMD) \
371 X("pick", 0, PICKCMD) \
372 X("prev", 0, PREVCMD) \
373 X("quit", 0, QUITCMD) \
374 X("refile", 0, FILECMD) \
375 X("repl", 0, REPLCMD) \
376 X("rmm", 0, RMMCMD) \
377 X("scan", 0, SCANCMD) \
378 X("send", 0, SENDCMD) \
379 X("show", 0, SHOWCMD) \
380 X("sortm", 0, SORTCMD) \
381 X("whatnow", 0, WHATCMD) \
382 X("whom", 0, WHOMCMD) \
384 #define X(sw, minchars, id) id,
385 DEFINE_SWITCH_ENUM(MSHCMDS
);
388 #define X(sw, minchars, id) { sw, minchars, id },
389 DEFINE_SWITCH_ARRAY(MSHCMDS
, mshcmds
);
397 register char *cp
, **ap
;
398 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
400 register struct Cmd
*cmdp
;
401 static int once_only
= ADVCMD
;
403 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
412 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
417 check_folder (scansw
);
418 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
423 cmd_name
= mshcmds
[i
].sw
;
431 if (once_only
== ADVCMD
)
432 once_only
= i
= SHOWCMD
;
434 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
435 cmd_name
= mshcmds
[i
].sw
;
441 case FORWCMD
: /* sigh */
451 if ((cp
= context_find (cmd_name
))) {
453 ap
= brkstring (cp
, " ", "\n");
454 ap
= copyip (ap
, vec
, MAXARGS
);
465 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
469 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
475 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
486 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
487 forkcmd (vec
, cmd_name
);
491 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
496 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
502 || (filehak (vec
) == OK
? ttyN (cmdp
)
503 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
508 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
513 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
518 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
524 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
529 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
536 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
542 || (packhak (vec
) == OK
? ttyN (cmdp
)
543 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
548 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
553 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
558 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
563 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
568 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
573 padios (NULL
, "no dispatch for %s", cmd_name
);
595 fsetup (char *folder
)
601 maildir
= m_maildir (folder
);
602 if (chdir (maildir
) == NOTOK
)
603 padios (maildir
, "unable to change directory to");
605 /* read folder and create message structure */
606 if (!(mp
= folder_read (folder
, 0)))
607 padios (NULL
, "unable to read folder %s", folder
);
609 /* check for empty folder */
611 padios (NULL
, "no messages in %s", folder
);
614 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
616 m_gMsgs (mp
->hghmsg
);
618 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
619 Msgs
[msgnum
].m_bboard_id
= 0;
620 Msgs
[msgnum
].m_top
= NOTOK
;
621 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
622 Msgs
[msgnum
].m_scanl
= NULL
;
627 fmsh
= getcpy (folder
);
629 maxfds
= OPEN_MAX
/ 2;
631 if ((maxfds
-= 2) < 1)
641 if ((fp
= fopen (file
, "r")) == NULL
)
642 padios (file
, "unable to read");
644 ioctl (fileno (fp
), FIOCLEX
, NULL
);
646 if (fstat (fileno (fp
), &st
) != NOTOK
) {
647 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
648 msgp
= read_map (file
, (long) st
.st_size
);
651 mode
= m_gmprot (), mtime
= 0;
655 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
656 padios (NULL
, "no messages in %s", myname
? myname
: file
);
658 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
659 padios (NULL
, "unable to allocate folder storage");
661 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
662 padios (NULL
, "unable to allocate message status storage");
668 mp
->foldpath
= getcpy (myname
? myname
: file
);
669 clear_folder_flags (mp
);
672 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
676 mp
->hghoff
= mp
->hghmsg
+ 1;
678 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
679 clear_msg_flags (mp
, i
);
684 svector_push_back (mp
->msgattrs
, getcpy ("unseen"));
686 scan_detect_mbox_style (fp
); /* the MAGIC invocation */
695 read_map (char *file
, long size
)
697 register int i
, msgp
;
698 register struct drop
*dp
, *mp
;
701 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
707 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
708 mp
= &Msgs
[msgp
].m_drop
;
710 mp
->d_size
= dp
->d_size
;
711 mp
->d_start
= dp
->d_start
;
712 mp
->d_stop
= dp
->d_stop
;
713 Msgs
[msgp
].m_scanl
= NULL
;
722 read_file (long pos
, int msgp
)
725 register struct drop
*dp
, *mp
;
728 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
731 m_gMsgs ((msgp
- 1) + i
);
733 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
734 mp
= &Msgs
[msgp
].m_drop
;
736 mp
->d_size
= dp
->d_size
;
737 mp
->d_start
= dp
->d_start
;
738 mp
->d_stop
= dp
->d_stop
;
739 Msgs
[msgp
].m_scanl
= NULL
;
753 nMsgs
= n
+ MAXFOLDER
/ 2;
754 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
756 padios (NULL
, "unable to allocate Msgs structure");
763 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
764 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
765 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
772 msh_ready (int msgnum
, int full
)
785 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
786 if (numfds
>= maxfds
)
787 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
788 if (Msgs
[msgp
].m_top
!= NOTOK
) {
789 close (Msgs
[msgp
].m_top
);
790 Msgs
[msgp
].m_top
= NOTOK
;
795 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
796 padios (cp
, "unable to open message");
797 Msgs
[msgnum
].m_top
= fd
;
801 if ((fd
= dup (fd
)) == NOTOK
)
802 padios ("cached message", "unable to dup");
803 if ((yp
= fdopen (fd
, "r")) == NULL
)
804 padios (NULL
, "unable to fdopen cached message");
805 fseek (yp
, 0L, SEEK_SET
);
809 scan_reset_m_getfld_state ();
810 scan_eom_action ((int (*)()) 0); /* XXX */
811 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
817 check_folder (int scansw
)
819 int seqnum
, i
, low
, hgh
, msgp
;
823 if (stat (mp
->foldpath
, &st
) == NOTOK
)
824 padios (mp
->foldpath
, "unable to stat");
825 if (mtime
== st
.st_mtime
)
829 low
= mp
->hghmsg
+ 1;
830 folder_free (mp
); /* free folder/message structure */
832 if (!(mp
= folder_read (fmsh
, 0)))
833 padios (NULL
, "unable to re-read folder %s", fmsh
);
837 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
838 if (Msgs
[msgp
].m_top
!= NOTOK
) {
839 close (Msgs
[msgp
].m_top
);
840 Msgs
[msgp
].m_top
= NOTOK
;
843 if (Msgs
[msgp
].m_scanl
) {
844 free (Msgs
[msgp
].m_scanl
);
845 Msgs
[msgp
].m_scanl
= NULL
;
851 if (modified
|| low
> hgh
)
855 if (fstat (fileno (fp
), &st
) == NOTOK
)
856 padios (mp
->foldpath
, "unable to fstat");
857 if (mtime
== st
.st_mtime
)
859 mode
= (int) (st
.st_mode
& 0777);
862 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
863 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
864 if (msgp
>= MAXFOLDER
)
865 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
867 if (msgp
<= mp
->hghmsg
)
870 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
871 padios (NULL
, "unable to allocate folder storage");
873 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
874 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
875 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
878 add_sequence(mp
, seqnum
, i
);
888 advise (NULL
, "new messages have arrived!\007");
890 scanrange (low
, hgh
);
897 scanrange (int low
, int hgh
)
901 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
907 scanstring (char *arg
)
909 char *cp
, **ap
, *vec
[MAXARGS
];
912 * This should be replace with a call to getarguments()
914 if ((cp
= context_find (cmd_name
= "scan"))) {
916 ap
= brkstring (cp
, " ", "\n");
917 ap
= copyip (ap
, vec
, MAXARGS
);
933 register int cur
, seqnum
, i
=0, msgnum
;
936 seq_setcur (mp
, mp
->lowmsg
);
937 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
940 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
941 add_sequence(mp
, seqnum
, msgnum
);
946 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
947 if (does_exist(mp
, msgnum
)) /* FIX */
948 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
950 clear_sequence(mp
, seqnum
, msgnum
);
953 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
954 clear_sequence(mp
, seqnum
, i
);
956 if (cur
> mp
->hghmsg
)
959 seq_setcur (mp
, cur
);
962 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
963 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
972 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
975 if (Msgs
[msgnum
].m_bboard_id
)
976 return Msgs
[msgnum
].m_bboard_id
;
978 zp
= msh_ready (msgnum
, 0);
980 int bufsz
= sizeof buf
;
981 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
984 if (!strcasecmp (name
, BBoard_ID
)) {
986 while (state
== FLDPLUS
) {
988 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
994 return (Msgs
[msgnum
].m_bboard_id
= i
);
998 while (state
== FLDPLUS
) {
1000 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
1012 display_info (int scansw
)
1016 interactive
= isatty (fileno (stdout
));
1018 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1019 padios ("standard output", "unable to dup");
1021 ioctl (sd
, FIOCLEX
, NULL
);
1022 #endif /* FIOCLEX */
1023 if ((sp
= fdopen (sd
, "w")) == NULL
)
1024 padios ("standard output", "unable to fdopen");
1027 m_putenv ("mhfolder", mp
->foldpath
);
1032 printf ("Reading ");
1033 if (SOprintf ("%s", myname
))
1034 printf ("%s", myname
);
1035 printf (", currently at message %d of %d\n",
1036 mp
->curmsg
, mp
->hghmsg
);
1039 printf ("Reading ");
1041 printf ("+%s", fmsh
);
1043 printf ("%s", mp
->foldpath
);
1044 printf (", currently at message %d of %d\n",
1045 mp
->curmsg
, mp
->hghmsg
);
1048 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1050 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1051 scanstring ("unseen");
1058 int i
= 0, seqnum
, msgnum
;
1064 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1065 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1066 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1067 if (Msgs
[msgnum
].m_bboard_id
== 0)
1069 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1073 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1074 write (pfd
, buffer
, sizeof(buffer
));
1084 char *cp
, tmpfil
[BUFSIZ
];
1085 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1089 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1091 rc2peer (RC_FIN
, 0, NULL
);
1096 ttyNaux (NULLCMD
, "FAST");
1098 if ((dp
= lkfopendata (mp
->foldpath
, "r")) == NULL
) {
1099 advise (mp
->foldpath
, "unable to lock");
1106 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1107 advise (mp
->foldpath
, "unable to stat");
1110 if (mtime
!= st
.st_mtime
) {
1111 advise (NULL
, "new messages have arrived, no update");
1114 mode
= (int) (st
.st_mode
& 0777);
1116 if (mp
->nummsg
== 0) {
1117 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1118 if (getanswer (cp
)) {
1119 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1122 advise (mp
->foldpath
, "error zero'ing");
1123 unlink (map_name (mp
->foldpath
));/* XXX */
1128 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1129 if (!getanswer (cp
))
1131 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1132 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1133 advise (tmpfil
, "unable to open");
1137 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1138 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1139 mbx_close (tmpfil
, md
);
1141 unlink (map_name (tmpfil
));
1144 mbx_close (tmpfil
, md
);
1146 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1147 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1149 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1150 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1152 if (rename (map1
, map2
) == NOTOK
) {
1153 admonish (map2
, "unable to rename %s to", map1
);
1162 lkfclosedata (dp
, mp
->foldpath
);
1171 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1175 static char buffer
[BUFSIZ
];
1181 printf ("%s", prompt
);
1184 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1185 if (interrupted
&& !told_to_quit
) {
1190 if (told_to_quit
|| i
== EOF
) {
1193 kill (ppid
, SIGEMT
);
1195 kill (ppid
, SIGTERM
);
1199 if (cp
< &buffer
[sizeof buffer
- 2])
1206 if (buffer
[0] == '?') {
1207 printf ("commands:\n");
1208 print_sw (ALL
, sw
, "", stdout
);
1209 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1214 if (parse (buffer
, cmdp
) == NOTOK
)
1217 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1219 ambigsw (cmdp
->args
[0], sw
);
1222 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1233 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1236 struct record rcs
, *rc
;
1242 switch (peer2rc (rc
)) {
1244 pQRY (rc
->rc_data
, scansw
);
1248 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1255 kill (ppid
, SIGEMT
);
1257 kill (ppid
, SIGTERM
);
1262 padios (NULL
, "%s", rc
->rc_data
);
1265 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1272 parse (char *buffer
, struct Cmd
*cmdp
)
1278 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1279 cmdp
->redirect
= NULL
;
1280 cmdp
->direction
= STDIO
;
1281 cmdp
->stream
= NULL
;
1283 for (cp
= buffer
; (c
= *cp
); cp
++) {
1284 if (!isspace ((unsigned char) c
))
1289 fmt2peer (RC_EOF
, "null command");
1293 while ((c
= *cp
++)) {
1294 if (isspace ((unsigned char) c
)) {
1295 while (isspace ((unsigned char) c
))
1300 cmdp
->args
[argp
++] = pp
;
1307 switch (c
= *cp
++) {
1309 padvise (NULL
, "unmatched \"");
1314 if ((c
= *cp
++) == 0)
1325 if ((c
= *cp
++) == 0) {
1327 padvise (NULL
, "the newline character can not be quoted");
1337 if (pp
== cmdp
->line
) {
1338 padvise (NULL
, "invalid null command");
1341 if (*cmdp
->args
[argp
- 1] == 0)
1343 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1344 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1345 cmdp
->direction
= APPIO
;
1348 cmdp
->redirect
= pp
+ 1;/* sigh */
1349 for (; (c
= *cp
); cp
++)
1350 if (!isspace ((unsigned char) c
))
1353 padvise (NULL
, cmdp
->direction
!= PIPIO
1354 ? "missing name for redirect"
1355 : "invalid null command");
1358 strcpy (cmdp
->redirect
, cp
);
1359 if (cmdp
->direction
!= PIPIO
) {
1361 if (isspace ((unsigned char) *cp
)) {
1362 padvise (NULL
, "bad name for redirect");
1365 if (expand (cmdp
->redirect
) == NOTOK
)
1374 cmdp
->args
[argp
] = NULL
;
1381 expand (char *redirect
)
1387 if (*redirect
!= '~')
1390 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1395 if ((pw
= getpwnam (pp
)))
1398 padvise (NULL
, "unknown user: %s", pp
);
1402 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1403 strcpy (redirect
, path
);
1409 init_io (struct Cmd
*cmdp
, int vio
)
1416 result
= initaux_io (cmdp
);
1424 initaux_io (struct Cmd
*cmdp
)
1428 switch (cmdp
->direction
) {
1434 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1435 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1436 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1437 cmdp
->direction
= STDIO
;
1443 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1444 padvise (cmdp
->redirect
, "unable to pipe");
1445 cmdp
->direction
= STDIO
;
1448 SIGNAL (SIGPIPE
, pipeser
);
1453 padios (NULL
, "unknown redirection for command");
1457 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1458 padios ("standard output", "unable to dup2");
1466 fin_io (struct Cmd
*cmdp
, int vio
)
1478 finaux_io (struct Cmd
*cmdp
)
1480 switch (cmdp
->direction
) {
1487 close (fileno (stdout
));
1488 if (ferror (stdout
))
1489 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1490 fclose (cmdp
->stream
);
1495 close (fileno (stdout
));
1496 pclose (cmdp
->stream
);
1497 SIGNAL (SIGPIPE
, SIG_DFL
);
1501 padios (NULL
, "unknown redirection for command");
1504 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1505 padios ("standard output", "unable to dup2");
1508 cmdp
->direction
= STDIO
;
1517 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1518 unset_selected (mp
, msgnum
);
1519 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1527 folder_free (mp
); /* free folder/message structure */
1533 seq_setcur (struct msgs
*mp
, int msgnum
)
1535 if (mp
->curmsg
== msgnum
)
1538 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1539 free (Msgs
[mp
->curmsg
].m_scanl
);
1540 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1542 if (Msgs
[msgnum
].m_scanl
) {
1543 free (Msgs
[msgnum
].m_scanl
);
1544 Msgs
[msgnum
].m_scanl
= NULL
;
1547 mp
->curmsg
= msgnum
;
1565 if (broken_pipe
++ == 0)
1566 fprintf (stderr
, "broken pipe\n");
1585 longjmp (peerenv
, DONE
);
1594 struct record rcs
, *rc
;
1599 switch (peer2rc (rc
)) {
1602 while (isspace ((unsigned char) *bp
))
1604 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1606 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1609 if (vrsn
!= RC_VRSN
) {
1610 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1614 while (*bp
&& !isspace ((unsigned char) *bp
))
1616 while (isspace ((unsigned char) *bp
))
1618 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1623 for (i
= 1; i
<= numwins
; i
++) {
1624 while (*bp
&& !isspace ((unsigned char) *bp
))
1626 while (isspace ((unsigned char) *bp
))
1628 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1631 rc2peer (RC_ACK
, 0, NULL
);
1635 padios (NULL
, "%s", rc
->rc_data
);
1638 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1639 done (1); /* NOTREACHED */
1642 return 1; /* dead code to satisfy the compiler */
1647 pQRY (char *str
, int scansw
)
1650 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1653 rc2peer (RC_EOF
, 0, NULL
);
1662 static int lastlow
= 0,
1667 oldhgh
= mp
->hghmsg
;
1668 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1669 switch (winX (STATUS
)) {
1674 printf ("new messages have arrived!");
1677 _exit (0); /* NOTREACHED */
1680 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1684 switch (winX (DISPLAY
)) {
1689 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1692 _exit (0); /* NOTREACHED */
1701 switch (winX (STATUS
)) {
1706 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1707 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1708 readid (mp
->lowmsg
));
1711 _exit (0); /* NOTREACHED */
1718 if (mp
->lowmsg
!= lastlow
1719 || mp
->curmsg
!= lastcur
1720 || mp
->hghmsg
!= lasthgh
1721 || mp
->nummsg
!= lastnum
)
1722 switch (winX (STATUS
)) {
1730 _exit (0); /* NOTREACHED */
1733 lastlow
= mp
->lowmsg
;
1734 lastcur
= mp
->curmsg
;
1735 lasthgh
= mp
->hghmsg
;
1736 lastnum
= mp
->nummsg
;
1747 int i
, j
, k
, msgnum
, n
;
1753 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1754 switch (winX (SCAN
)) {
1762 _exit (0); /* NOTREACHED */
1771 j
= (k
= windows
[SCAN
]) / 2;
1772 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1773 if (does_exist (mp
, msgnum
))
1777 k
= i
>= k
? 1 : k
- i
;
1784 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1785 if (does_exist (mp
, msgnum
)) {
1792 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1793 if (does_exist (mp
, msgnum
)) {
1797 if (n
++ >= windows
[SCAN
])
1803 && does_exist (mp
, lo
)
1804 && does_exist (mp
, hi
)
1806 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1808 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1809 && hi
- lo
== j
- i
)
1812 if (mp
->curmsg
!= cur
|| modified
)
1813 switch (winN (NULLCMD
, SCAN
, 0)) {
1821 scanrange (lo
= i
, hi
= j
);
1832 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1837 switch (winX (DISPLAY
)) {
1842 printf ("commands:\n");
1843 print_sw (ALL
, sw
, "", stdout
);
1844 printf ("type ``quit'' to leave %s\n", invo_name
);
1847 _exit (0); /* NOTREACHED */
1850 rc2peer (RC_EOF
, 0, NULL
);
1854 if (parse (str
, cmdp
) == NOTOK
)
1857 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1859 switch (winX (DISPLAY
)) {
1864 ambigsw (cmdp
->args
[0], sw
);
1867 _exit (0); /* NOTREACHED */
1870 rc2peer (RC_EOF
, 0, NULL
);
1876 "say what: ``%s'' -- type ? (or help) for help",
1891 switch (setjmp (peerenv
)) {
1893 SIGNAL (SIGALRM
, alrmser
);
1896 status
= peerwait ();
1910 struct record rcs
, *rc
;
1915 switch (peer2rc (rc
)) {
1918 rc2peer (RC_FIN
, 0, NULL
);
1922 advise (NULL
, "%s", rc
->rc_data
);
1926 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1933 ttyNaux (struct Cmd
*cmdp
, char *s
)
1935 struct record rcs
, *rc
;
1940 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1943 /* XXX: fseek() too tricky for our own good */
1945 fseek (fp
, 0L, SEEK_SET
);
1948 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1950 vmhtty
= OK
; /* fall */
1955 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1958 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1959 done (1); /* NOTREACHED */
1963 SIGNAL (SIGTSTP
, tstat
);
1970 ttyR (struct Cmd
*cmdp
)
1972 struct record rcs
, *rc
;
1977 SIGNAL (SIGTSTP
, SIG_IGN
);
1989 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
1991 rc2peer (RC_EOF
, 0, NULL
);
1995 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1998 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1999 done (1); /* NOTREACHED */
2002 return 1; /* dead code to satisfy compiler */
2007 winN (struct Cmd
*cmdp
, int n
, int eof
)
2010 char buffer
[BUFSIZ
];
2011 struct record rcs
, *rc
;
2014 if (vmhpid
== NOTOK
)
2019 /* XXX: fseek() too tricky for our own good */
2021 fseek (fp
, 0L, SEEK_SET
);
2025 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2026 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2034 padios (NULL
, "%s", rc
->rc_data
);
2037 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2041 if (pipe (pd
) == NOTOK
) {
2042 err2peer (RC_ERR
, "pipe", "unable to");
2046 switch (vmhpid
= fork()) {
2048 err2peer (RC_ERR
, "fork", "unable to");
2055 SIGNAL (SIGPIPE
, SIG_IGN
);
2056 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2057 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2065 advise (NULL
, "%s", rc
->rc_data
);
2069 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2073 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2076 rc2peer (RC_EOF
, 0, NULL
);
2081 advise (NULL
, "%s", rc
->rc_data
);
2086 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2091 err2peer (RC_ERR
, "pipe", "error reading from");
2093 _exit (i
!= NOTOK
? i
: 1);
2096 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2097 padios ("standard input", "unable to dup");
2098 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2099 padios ("standard output", "unable to dup");
2100 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2101 padios ("diagnostic output", "unable to dup");
2104 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2105 dup2 (i
, fileno (stdin
));
2110 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2111 padios ("standard output", "unable to dup2");
2115 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2116 padios ("diagnostic output", "unable to dup2");
2119 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2121 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2133 winR (struct Cmd
*cmdp
)
2143 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2144 padios ("standard input", "unable to dup2");
2149 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2150 padios ("standard output", "unable to dup2");
2155 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2156 padios ("diagnostic output", "unable to dup2");
2160 SIGNAL (SIGPIPE
, pstat
);
2162 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2166 return (status
== 0 ? OK
: NOTOK
);
2174 char buffer
[BUFSIZ
];
2175 struct record rcs
, *rc
;
2180 /* XXX: fseek() too tricky for our own good */
2182 fseek (fp
, 0L, SEEK_SET
);
2184 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2185 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2193 padios (NULL
, "%s", rc
->rc_data
);
2196 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2200 if (pipe (pd
) == NOTOK
) {
2201 err2peer (RC_ERR
, "pipe", "unable to");
2205 switch (pid
= fork ()) {
2207 err2peer (RC_ERR
, "fork", "unable to");
2213 close (fileno (stdin
));
2214 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2215 dup2 (i
, fileno (stdin
));
2218 dup2 (pd
[1], fileno (stdout
));
2219 dup2 (pd
[1], fileno (stderr
));
2227 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2228 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2238 padios (NULL
, "%s", rc
->rc_data
);
2241 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2245 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2250 padios (NULL
, "%s", rc
->rc_data
);
2253 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2257 err2peer (RC_ERR
, "pipe", "error reading from");
2261 return (i
!= NOTOK
? pid
: NOTOK
);
2267 padios (char *what
, char *fmt
, ...)
2273 verr2peer (RC_FIN
, what
, fmt
, ap
);
2276 advertise (what
, NULL
, fmt
, ap
);
2285 padvise (char *what
, char *fmt
, ...)
2291 verr2peer (RC_ERR
, what
, fmt
, ap
);
2293 advertise (what
, NULL
, fmt
, ap
);