]>
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
];
206 if (nmh_init(argv
[0], 1)) { return 1; }
208 mts_init (invo_name
);
209 arguments
= getarguments (invo_name
, argc
,argv
, 1);
212 while ((cp
= *argp
++)) {
214 switch (smatch (++cp
, switches
)) {
216 ambigsw (cp
, switches
);
219 adios (NULL
, "-%s unknown", cp
);
222 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
223 print_help (buf
, switches
, 1);
226 print_version(invo_name
);
230 if (!(cp
= *argp
++) || *cp
== '-')
231 adios (NULL
, "missing argument to %s", argp
[-2]);
232 if ((id
= atoi (cp
)) < 1)
233 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
238 if ((pfd
= 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 ((ppid
= atoi (cp
)) <= 1)
245 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
248 if (!(myname
= *argp
++) || *myname
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
260 if (!(myprompt
= *argp
++) || *myprompt
== '-')
261 adios (NULL
, "missing argument to %s", argp
[-2]);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 adios (NULL
, "missing argument to %s", argp
[-2]);
267 if ((vmh1
= atoi (cp
)) < 1)
268 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
271 if (!(cp
= *argp
++) || *cp
== '-')
272 adios (NULL
, "missing argument to %s", argp
[-2]);
273 if ((vmh2
= atoi (cp
)) < 1)
274 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 adios (NULL
, "missing argument to %s", argp
[-2]);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 adios (NULL
, "missing argument to %s", argp
[-2]);
293 if (*cp
== '+' || *cp
== '@') {
295 adios (NULL
, "only one folder at a time!");
297 folder
= pluspath (cp
);
301 adios (NULL
, "only one file at a time!");
306 if (!file
&& !folder
)
309 adios (NULL
, "use a file or a folder, not both");
310 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
313 ioctl (pfd
, FIOCLEX
, NULL
);
316 istat
= SIGNAL2 (SIGINT
, intrser
);
317 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
319 sc_width (); /* MAGIC... */
321 if ((vmh
= vmh1
&& vmh2
)) {
324 SIGNAL (SIGINT
, SIG_IGN
);
325 SIGNAL (SIGQUIT
, SIG_IGN
);
327 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
336 display_info (id
> 0 ? scansw
: 0);
338 msh (id
> 0 ? scansw
: 0);
348 #define MSHCMDS_SWITCHES \
349 X("advance", -7, ADVCMD) \
350 X("ali", 0, ALICMD) \
351 X("burst", 0, EXPLCMD) \
352 X("comp", 0, COMPCMD) \
353 X("dist", 0, DISTCMD) \
354 X("exit", 0, EXITCMD) \
355 X("folder", 0, FOLDCMD) \
356 X("forw", 0, FORWCMD) \
357 X("help", 0, HELPCMD) \
359 X("mark", 0, MARKCMD) \
360 X("mhmail", 0, MAILCMD) \
361 X("mhn", 0, MHNCMD) \
362 X("msgchk", 0, MSGKCMD) \
363 X("next", 0, NEXTCMD) \
364 X("packf", 0, PACKCMD) \
365 X("pick", 0, PICKCMD) \
366 X("prev", 0, PREVCMD) \
367 X("quit", 0, QUITCMD) \
368 X("refile", 0, FILECMD) \
369 X("repl", 0, REPLCMD) \
370 X("rmm", 0, RMMCMD) \
371 X("scan", 0, SCANCMD) \
372 X("send", 0, SENDCMD) \
373 X("show", 0, SHOWCMD) \
374 X("sortm", 0, SORTCMD) \
375 X("whatnow", 0, WHATCMD) \
376 X("whom", 0, WHOMCMD) \
378 #define X(sw, minchars, id) id,
379 DEFINE_SWITCH_ENUM(MSHCMDS
);
382 #define X(sw, minchars, id) { sw, minchars, id },
383 DEFINE_SWITCH_ARRAY(MSHCMDS
, mshcmds
);
391 register char *cp
, **ap
;
392 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
394 register struct Cmd
*cmdp
;
395 static int once_only
= ADVCMD
;
397 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
406 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
411 check_folder (scansw
);
412 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
417 cmd_name
= mshcmds
[i
].sw
;
425 if (once_only
== ADVCMD
)
426 once_only
= i
= SHOWCMD
;
428 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
429 cmd_name
= mshcmds
[i
].sw
;
435 case FORWCMD
: /* sigh */
445 if ((cp
= context_find (cmd_name
))) {
447 ap
= brkstring (cp
, " ", "\n");
448 ap
= copyip (ap
, vec
, MAXARGS
);
459 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
463 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
469 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
480 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
481 forkcmd (vec
, cmd_name
);
485 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
490 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
496 || (filehak (vec
) == OK
? ttyN (cmdp
)
497 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
502 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
507 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
512 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
518 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
523 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
530 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
536 || (packhak (vec
) == OK
? ttyN (cmdp
)
537 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
542 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
547 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
552 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
557 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
562 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
567 padios (NULL
, "no dispatch for %s", cmd_name
);
589 fsetup (char *folder
)
595 maildir
= m_maildir (folder
);
596 if (chdir (maildir
) == NOTOK
)
597 padios (maildir
, "unable to change directory to");
599 /* read folder and create message structure */
600 if (!(mp
= folder_read (folder
, 0)))
601 padios (NULL
, "unable to read folder %s", folder
);
603 /* check for empty folder */
605 padios (NULL
, "no messages in %s", folder
);
608 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
610 m_gMsgs (mp
->hghmsg
);
612 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
613 Msgs
[msgnum
].m_bboard_id
= 0;
614 Msgs
[msgnum
].m_top
= NOTOK
;
615 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
616 Msgs
[msgnum
].m_scanl
= NULL
;
621 fmsh
= getcpy (folder
);
623 maxfds
= OPEN_MAX
/ 2;
625 if ((maxfds
-= 2) < 1)
635 if ((fp
= fopen (file
, "r")) == NULL
)
636 padios (file
, "unable to read");
638 ioctl (fileno (fp
), FIOCLEX
, NULL
);
640 if (fstat (fileno (fp
), &st
) != NOTOK
) {
641 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
642 msgp
= read_map (file
, (long) st
.st_size
);
645 mode
= m_gmprot (), mtime
= 0;
649 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
650 padios (NULL
, "no messages in %s", myname
? myname
: file
);
652 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
653 padios (NULL
, "unable to allocate folder storage");
655 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
656 padios (NULL
, "unable to allocate message status storage");
662 mp
->foldpath
= getcpy (myname
? myname
: file
);
663 clear_folder_flags (mp
);
666 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
670 mp
->hghoff
= mp
->hghmsg
+ 1;
672 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
673 clear_msg_flags (mp
, i
);
678 svector_push_back (mp
->msgattrs
, getcpy ("unseen"));
680 scan_detect_mbox_style (fp
); /* the MAGIC invocation */
689 read_map (char *file
, long size
)
691 register int i
, msgp
;
692 register struct drop
*dp
, *mp
;
695 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
701 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
702 mp
= &Msgs
[msgp
].m_drop
;
704 mp
->d_size
= dp
->d_size
;
705 mp
->d_start
= dp
->d_start
;
706 mp
->d_stop
= dp
->d_stop
;
707 Msgs
[msgp
].m_scanl
= NULL
;
716 read_file (long pos
, int msgp
)
719 register struct drop
*dp
, *mp
;
722 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
725 m_gMsgs ((msgp
- 1) + i
);
727 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
728 mp
= &Msgs
[msgp
].m_drop
;
730 mp
->d_size
= dp
->d_size
;
731 mp
->d_start
= dp
->d_start
;
732 mp
->d_stop
= dp
->d_stop
;
733 Msgs
[msgp
].m_scanl
= NULL
;
747 nMsgs
= n
+ MAXFOLDER
/ 2;
748 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
750 padios (NULL
, "unable to allocate Msgs structure");
757 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
758 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
759 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
766 msh_ready (int msgnum
, int full
)
779 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
780 if (numfds
>= maxfds
)
781 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
782 if (Msgs
[msgp
].m_top
!= NOTOK
) {
783 close (Msgs
[msgp
].m_top
);
784 Msgs
[msgp
].m_top
= NOTOK
;
789 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
790 padios (cp
, "unable to open message");
791 Msgs
[msgnum
].m_top
= fd
;
795 if ((fd
= dup (fd
)) == NOTOK
)
796 padios ("cached message", "unable to dup");
797 if ((yp
= fdopen (fd
, "r")) == NULL
)
798 padios (NULL
, "unable to fdopen cached message");
799 fseek (yp
, 0L, SEEK_SET
);
803 scan_reset_m_getfld_state ();
804 scan_eom_action ((int (*)()) 0); /* XXX */
805 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
811 check_folder (int scansw
)
813 int seqnum
, i
, low
, hgh
, msgp
;
817 if (stat (mp
->foldpath
, &st
) == NOTOK
)
818 padios (mp
->foldpath
, "unable to stat");
819 if (mtime
== st
.st_mtime
)
823 low
= mp
->hghmsg
+ 1;
824 folder_free (mp
); /* free folder/message structure */
826 if (!(mp
= folder_read (fmsh
, 0)))
827 padios (NULL
, "unable to re-read folder %s", fmsh
);
831 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
832 if (Msgs
[msgp
].m_top
!= NOTOK
) {
833 close (Msgs
[msgp
].m_top
);
834 Msgs
[msgp
].m_top
= NOTOK
;
837 if (Msgs
[msgp
].m_scanl
) {
838 free (Msgs
[msgp
].m_scanl
);
839 Msgs
[msgp
].m_scanl
= NULL
;
845 if (modified
|| low
> hgh
)
849 if (fstat (fileno (fp
), &st
) == NOTOK
)
850 padios (mp
->foldpath
, "unable to fstat");
851 if (mtime
== st
.st_mtime
)
853 mode
= (int) (st
.st_mode
& 0777);
856 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
857 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
858 if (msgp
>= MAXFOLDER
)
859 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
861 if (msgp
<= mp
->hghmsg
)
864 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
865 padios (NULL
, "unable to allocate folder storage");
867 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
868 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
869 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
872 add_sequence(mp
, seqnum
, i
);
882 advise (NULL
, "new messages have arrived!\007");
884 scanrange (low
, hgh
);
891 scanrange (int low
, int hgh
)
895 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
901 scanstring (char *arg
)
903 char *cp
, **ap
, *vec
[MAXARGS
];
906 * This should be replace with a call to getarguments()
908 if ((cp
= context_find (cmd_name
= "scan"))) {
910 ap
= brkstring (cp
, " ", "\n");
911 ap
= copyip (ap
, vec
, MAXARGS
);
927 register int cur
, seqnum
, i
=0, msgnum
;
930 seq_setcur (mp
, mp
->lowmsg
);
931 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
934 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
935 add_sequence(mp
, seqnum
, msgnum
);
940 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
941 if (does_exist(mp
, msgnum
)) /* FIX */
942 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
944 clear_sequence(mp
, seqnum
, msgnum
);
947 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
948 clear_sequence(mp
, seqnum
, i
);
950 if (cur
> mp
->hghmsg
)
953 seq_setcur (mp
, cur
);
956 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
957 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
966 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
969 if (Msgs
[msgnum
].m_bboard_id
)
970 return Msgs
[msgnum
].m_bboard_id
;
972 zp
= msh_ready (msgnum
, 0);
974 int bufsz
= sizeof buf
;
975 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
978 if (!strcasecmp (name
, BBoard_ID
)) {
980 while (state
== FLDPLUS
) {
982 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
988 return (Msgs
[msgnum
].m_bboard_id
= i
);
992 while (state
== FLDPLUS
) {
994 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
1006 display_info (int scansw
)
1010 interactive
= isatty (fileno (stdout
));
1012 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1013 padios ("standard output", "unable to dup");
1015 ioctl (sd
, FIOCLEX
, NULL
);
1016 #endif /* FIOCLEX */
1017 if ((sp
= fdopen (sd
, "w")) == NULL
)
1018 padios ("standard output", "unable to fdopen");
1021 m_putenv ("mhfolder", mp
->foldpath
);
1026 printf ("Reading ");
1027 if (SOprintf ("%s", myname
))
1028 printf ("%s", myname
);
1029 printf (", currently at message %d of %d\n",
1030 mp
->curmsg
, mp
->hghmsg
);
1033 printf ("Reading ");
1035 printf ("+%s", fmsh
);
1037 printf ("%s", mp
->foldpath
);
1038 printf (", currently at message %d of %d\n",
1039 mp
->curmsg
, mp
->hghmsg
);
1042 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1044 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1045 scanstring ("unseen");
1052 int i
= 0, seqnum
, msgnum
;
1058 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1059 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1060 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1061 if (Msgs
[msgnum
].m_bboard_id
== 0)
1063 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1067 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1068 write (pfd
, buffer
, sizeof(buffer
));
1078 char *cp
, tmpfil
[BUFSIZ
];
1079 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1082 int failed_to_lock
= 0;
1084 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1086 rc2peer (RC_FIN
, 0, NULL
);
1091 ttyNaux (NULLCMD
, "FAST");
1093 if ((dp
= lkfopendata (mp
->foldpath
, "r", &failed_to_lock
)) == NULL
) {
1094 advise (mp
->foldpath
, "unable to lock");
1101 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1102 advise (mp
->foldpath
, "unable to stat");
1105 if (mtime
!= st
.st_mtime
) {
1106 advise (NULL
, "new messages have arrived, no update");
1109 mode
= (int) (st
.st_mode
& 0777);
1111 if (mp
->nummsg
== 0) {
1112 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1113 if (getanswer (cp
)) {
1114 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1117 advise (mp
->foldpath
, "error zero'ing");
1118 (void) m_unlink (map_name (mp
->foldpath
));/* XXX */
1123 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1124 if (!getanswer (cp
))
1126 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1127 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1128 advise (tmpfil
, "unable to open");
1132 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1133 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1134 mbx_close (tmpfil
, md
);
1135 (void) m_unlink (tmpfil
);
1136 (void) m_unlink (map_name (tmpfil
));
1139 mbx_close (tmpfil
, md
);
1141 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1142 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1144 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1145 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1147 if (rename (map1
, map2
) == NOTOK
) {
1148 admonish (map2
, "unable to rename %s to", map1
);
1149 (void) m_unlink (map1
);
1150 (void) m_unlink (map2
);
1157 lkfclosedata (dp
, mp
->foldpath
);
1166 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1170 static char buffer
[BUFSIZ
];
1176 printf ("%s", prompt
);
1179 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1180 if (interrupted
&& !told_to_quit
) {
1185 if (told_to_quit
|| i
== EOF
) {
1188 kill (ppid
, SIGEMT
);
1190 kill (ppid
, SIGTERM
);
1194 if (cp
< &buffer
[sizeof buffer
- 2])
1201 if (buffer
[0] == '?') {
1202 printf ("commands:\n");
1203 print_sw (ALL
, sw
, "", stdout
);
1204 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1209 if (parse (buffer
, cmdp
) == NOTOK
)
1212 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1214 ambigsw (cmdp
->args
[0], sw
);
1217 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1228 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1231 struct record rcs
, *rc
;
1237 switch (peer2rc (rc
)) {
1239 pQRY (rc
->rc_data
, scansw
);
1243 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1250 kill (ppid
, SIGEMT
);
1252 kill (ppid
, SIGTERM
);
1257 padios (NULL
, "%s", rc
->rc_data
);
1260 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1267 parse (char *buffer
, struct Cmd
*cmdp
)
1273 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1274 cmdp
->redirect
= NULL
;
1275 cmdp
->direction
= STDIO
;
1276 cmdp
->stream
= NULL
;
1278 for (cp
= buffer
; (c
= *cp
); cp
++) {
1279 if (!isspace ((unsigned char) c
))
1284 fmt2peer (RC_EOF
, "null command");
1288 while ((c
= *cp
++)) {
1289 if (isspace ((unsigned char) c
)) {
1290 while (isspace ((unsigned char) c
))
1295 cmdp
->args
[argp
++] = pp
;
1302 switch (c
= *cp
++) {
1304 padvise (NULL
, "unmatched \"");
1309 if ((c
= *cp
++) == 0)
1320 if ((c
= *cp
++) == 0) {
1322 padvise (NULL
, "the newline character can not be quoted");
1332 if (pp
== cmdp
->line
) {
1333 padvise (NULL
, "invalid null command");
1336 if (*cmdp
->args
[argp
- 1] == 0)
1338 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1339 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1340 cmdp
->direction
= APPIO
;
1343 cmdp
->redirect
= pp
+ 1;/* sigh */
1344 for (; (c
= *cp
); cp
++)
1345 if (!isspace ((unsigned char) c
))
1348 padvise (NULL
, cmdp
->direction
!= PIPIO
1349 ? "missing name for redirect"
1350 : "invalid null command");
1353 strcpy (cmdp
->redirect
, cp
);
1354 if (cmdp
->direction
!= PIPIO
) {
1356 if (isspace ((unsigned char) *cp
)) {
1357 padvise (NULL
, "bad name for redirect");
1360 if (expand (cmdp
->redirect
) == NOTOK
)
1369 cmdp
->args
[argp
] = NULL
;
1376 expand (char *redirect
)
1382 if (*redirect
!= '~')
1385 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1390 if ((pw
= getpwnam (pp
)))
1393 padvise (NULL
, "unknown user: %s", pp
);
1397 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1398 strcpy (redirect
, path
);
1404 init_io (struct Cmd
*cmdp
, int vio
)
1411 result
= initaux_io (cmdp
);
1419 initaux_io (struct Cmd
*cmdp
)
1423 switch (cmdp
->direction
) {
1429 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1430 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1431 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1432 cmdp
->direction
= STDIO
;
1438 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1439 padvise (cmdp
->redirect
, "unable to pipe");
1440 cmdp
->direction
= STDIO
;
1443 SIGNAL (SIGPIPE
, pipeser
);
1448 padios (NULL
, "unknown redirection for command");
1452 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1453 padios ("standard output", "unable to dup2");
1461 fin_io (struct Cmd
*cmdp
, int vio
)
1473 finaux_io (struct Cmd
*cmdp
)
1475 switch (cmdp
->direction
) {
1482 close (fileno (stdout
));
1483 if (ferror (stdout
))
1484 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1485 fclose (cmdp
->stream
);
1490 close (fileno (stdout
));
1491 pclose (cmdp
->stream
);
1492 SIGNAL (SIGPIPE
, SIG_DFL
);
1496 padios (NULL
, "unknown redirection for command");
1499 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1500 padios ("standard output", "unable to dup2");
1503 cmdp
->direction
= STDIO
;
1512 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1513 unset_selected (mp
, msgnum
);
1514 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1522 folder_free (mp
); /* free folder/message structure */
1528 seq_setcur (struct msgs
*mp
, int msgnum
)
1530 if (mp
->curmsg
== msgnum
)
1533 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1534 free (Msgs
[mp
->curmsg
].m_scanl
);
1535 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1537 if (Msgs
[msgnum
].m_scanl
) {
1538 free (Msgs
[msgnum
].m_scanl
);
1539 Msgs
[msgnum
].m_scanl
= NULL
;
1542 mp
->curmsg
= msgnum
;
1560 if (broken_pipe
++ == 0)
1561 fprintf (stderr
, "broken pipe\n");
1580 longjmp (peerenv
, DONE
);
1589 struct record rcs
, *rc
;
1594 switch (peer2rc (rc
)) {
1597 while (isspace ((unsigned char) *bp
))
1599 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1601 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1604 if (vrsn
!= RC_VRSN
) {
1605 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1609 while (*bp
&& !isspace ((unsigned char) *bp
))
1611 while (isspace ((unsigned char) *bp
))
1613 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1618 for (i
= 1; i
<= numwins
; i
++) {
1619 while (*bp
&& !isspace ((unsigned char) *bp
))
1621 while (isspace ((unsigned char) *bp
))
1623 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1626 rc2peer (RC_ACK
, 0, NULL
);
1630 padios (NULL
, "%s", rc
->rc_data
);
1633 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1634 done (1); /* NOTREACHED */
1637 return 1; /* dead code to satisfy the compiler */
1642 pQRY (char *str
, int scansw
)
1645 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1648 rc2peer (RC_EOF
, 0, NULL
);
1657 static int lastlow
= 0,
1662 oldhgh
= mp
->hghmsg
;
1663 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1664 switch (winX (STATUS
)) {
1669 printf ("new messages have arrived!");
1672 _exit (0); /* NOTREACHED */
1675 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1679 switch (winX (DISPLAY
)) {
1684 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1687 _exit (0); /* NOTREACHED */
1696 switch (winX (STATUS
)) {
1701 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1702 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1703 readid (mp
->lowmsg
));
1706 _exit (0); /* NOTREACHED */
1713 if (mp
->lowmsg
!= lastlow
1714 || mp
->curmsg
!= lastcur
1715 || mp
->hghmsg
!= lasthgh
1716 || mp
->nummsg
!= lastnum
)
1717 switch (winX (STATUS
)) {
1725 _exit (0); /* NOTREACHED */
1728 lastlow
= mp
->lowmsg
;
1729 lastcur
= mp
->curmsg
;
1730 lasthgh
= mp
->hghmsg
;
1731 lastnum
= mp
->nummsg
;
1742 int i
, j
, k
, msgnum
, n
;
1748 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1749 switch (winX (SCAN
)) {
1757 _exit (0); /* NOTREACHED */
1766 j
= (k
= windows
[SCAN
]) / 2;
1767 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1768 if (does_exist (mp
, msgnum
))
1772 k
= i
>= k
? 1 : k
- i
;
1779 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1780 if (does_exist (mp
, msgnum
)) {
1787 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1788 if (does_exist (mp
, msgnum
)) {
1792 if (n
++ >= windows
[SCAN
])
1798 && does_exist (mp
, lo
)
1799 && does_exist (mp
, hi
)
1801 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1803 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1804 && hi
- lo
== j
- i
)
1807 if (mp
->curmsg
!= cur
|| modified
)
1808 switch (winN (NULLCMD
, SCAN
, 0)) {
1816 scanrange (lo
= i
, hi
= j
);
1827 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1832 switch (winX (DISPLAY
)) {
1837 printf ("commands:\n");
1838 print_sw (ALL
, sw
, "", stdout
);
1839 printf ("type ``quit'' to leave %s\n", invo_name
);
1842 _exit (0); /* NOTREACHED */
1845 rc2peer (RC_EOF
, 0, NULL
);
1849 if (parse (str
, cmdp
) == NOTOK
)
1852 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1854 switch (winX (DISPLAY
)) {
1859 ambigsw (cmdp
->args
[0], sw
);
1862 _exit (0); /* NOTREACHED */
1865 rc2peer (RC_EOF
, 0, NULL
);
1871 "say what: ``%s'' -- type ? (or help) for help",
1886 switch (setjmp (peerenv
)) {
1888 SIGNAL (SIGALRM
, alrmser
);
1891 status
= peerwait ();
1905 struct record rcs
, *rc
;
1910 switch (peer2rc (rc
)) {
1913 rc2peer (RC_FIN
, 0, NULL
);
1917 advise (NULL
, "%s", rc
->rc_data
);
1921 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1928 ttyNaux (struct Cmd
*cmdp
, char *s
)
1930 struct record rcs
, *rc
;
1935 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1938 /* XXX: fseek() too tricky for our own good */
1940 fseek (fp
, 0L, SEEK_SET
);
1943 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1945 vmhtty
= OK
; /* fall */
1950 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1953 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1954 done (1); /* NOTREACHED */
1958 SIGNAL (SIGTSTP
, tstat
);
1965 ttyR (struct Cmd
*cmdp
)
1967 struct record rcs
, *rc
;
1972 SIGNAL (SIGTSTP
, SIG_IGN
);
1984 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
1986 rc2peer (RC_EOF
, 0, NULL
);
1990 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1993 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1994 done (1); /* NOTREACHED */
1997 return 1; /* dead code to satisfy compiler */
2002 winN (struct Cmd
*cmdp
, int n
, int eof
)
2005 char buffer
[BUFSIZ
];
2006 struct record rcs
, *rc
;
2009 if (vmhpid
== NOTOK
)
2014 /* XXX: fseek() too tricky for our own good */
2016 fseek (fp
, 0L, SEEK_SET
);
2020 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2021 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2029 padios (NULL
, "%s", rc
->rc_data
);
2032 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2036 if (pipe (pd
) == NOTOK
) {
2037 err2peer (RC_ERR
, "pipe", "unable to");
2041 switch (vmhpid
= fork()) {
2043 err2peer (RC_ERR
, "fork", "unable to");
2050 SIGNAL (SIGPIPE
, SIG_IGN
);
2051 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2052 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2060 advise (NULL
, "%s", rc
->rc_data
);
2064 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2068 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2071 rc2peer (RC_EOF
, 0, NULL
);
2076 advise (NULL
, "%s", rc
->rc_data
);
2081 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2086 err2peer (RC_ERR
, "pipe", "error reading from");
2088 _exit (i
!= NOTOK
? i
: 1);
2091 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2092 padios ("standard input", "unable to dup");
2093 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2094 padios ("standard output", "unable to dup");
2095 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2096 padios ("diagnostic output", "unable to dup");
2099 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2100 dup2 (i
, fileno (stdin
));
2105 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2106 padios ("standard output", "unable to dup2");
2110 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2111 padios ("diagnostic output", "unable to dup2");
2114 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2116 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2128 winR (struct Cmd
*cmdp
)
2138 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2139 padios ("standard input", "unable to dup2");
2144 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2145 padios ("standard output", "unable to dup2");
2150 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2151 padios ("diagnostic output", "unable to dup2");
2155 SIGNAL (SIGPIPE
, pstat
);
2157 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2161 return (status
== 0 ? OK
: NOTOK
);
2169 char buffer
[BUFSIZ
];
2170 struct record rcs
, *rc
;
2175 /* XXX: fseek() too tricky for our own good */
2177 fseek (fp
, 0L, SEEK_SET
);
2179 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2180 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2188 padios (NULL
, "%s", rc
->rc_data
);
2191 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2195 if (pipe (pd
) == NOTOK
) {
2196 err2peer (RC_ERR
, "pipe", "unable to");
2200 switch (pid
= fork ()) {
2202 err2peer (RC_ERR
, "fork", "unable to");
2208 close (fileno (stdin
));
2209 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2210 dup2 (i
, fileno (stdin
));
2213 dup2 (pd
[1], fileno (stdout
));
2214 dup2 (pd
[1], fileno (stderr
));
2222 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2223 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2233 padios (NULL
, "%s", rc
->rc_data
);
2236 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2240 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2245 padios (NULL
, "%s", rc
->rc_data
);
2248 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2252 err2peer (RC_ERR
, "pipe", "error reading from");
2256 return (i
!= NOTOK
? pid
: NOTOK
);
2262 padios (char *what
, char *fmt
, ...)
2268 verr2peer (RC_FIN
, what
, fmt
, ap
);
2271 advertise (what
, NULL
, fmt
, ap
);
2280 padvise (char *what
, char *fmt
, ...)
2286 verr2peer (RC_ERR
, what
, fmt
, ap
);
2288 advertise (what
, NULL
, fmt
, ap
);