]>
diplodocus.org Git - nmh/blob - uip/msh.c
3144a4932b80e0eef4e76f4d22baa1c0ecb9e426
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>
33 #define QUOTE '\\' /* sigh */
35 #define MSH_SWITCHES \
36 X("idstart number", -7, IDSW) /* interface from bbc */ \
37 X("idstop number", -6, FDSW) /* .. */ \
38 X("idquit number", -6, QDSW) /* .. */ \
39 X("idname BBoard", -6, NMSW) /* .. */ \
40 X("prompt string", 0, PRMPTSW) \
41 X("scan", 0, SCANSW) \
42 X("noscan", 0, NSCANSW) \
43 X("vmhread fd", -7, READSW) \
44 X("vmhwrite fd", -8, WRITESW) \
45 X("popread fd", -7, PREADSW) \
46 X("popwrite fd", -8, PWRITSW) \
47 X("topcur", 0, TCURSW) \
48 X("notopcur", 0, NTCURSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MSH
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MSH
, switches
);
60 static int mbx_style
= MMDF_FORMAT
;
65 char*fmsh
= NULL
; /* folder instead of file */
66 int modified
; /* command modified folder */
67 struct msgs
*mp
; /* used a lot */
69 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
70 static FILE *fp
; /* input file */
71 static FILE *yp
= NULL
; /* temporary file */
72 static int mode
; /* mode of file */
73 static int numfds
= 0; /* number of files cached */
74 static int maxfds
= 0; /* number of files cached to be cached */
75 static time_t mtime
= (time_t) 0; /* mtime of file */
80 #define ALARM ((unsigned int) 10)
81 #define ttyN(c) ttyNaux ((c), NULL)
85 static int vmhpid
= OK
;
90 static int vmhtty
= NOTOK
;
97 static int topcur
= 0;
99 static int numwins
= 0;
100 static int windows
[NWIN
+ 1];
102 static jmp_buf peerenv
;
107 static int pfd
= NOTOK
; /* fd parent is reading from */
108 static int ppid
= 0; /* pid of parent */
113 int interactive
; /* running from a /dev/tty */
114 int redirected
; /* re-directing output */
115 FILE *sp
= NULL
; /* original stdout */
117 char *cmd_name
; /* command being run */
118 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
120 static char *myprompt
= "(%s) ";/* prompting string */
125 static int gap
; /* gap in BBoard-ID:s */
126 static char *myname
= NULL
; /* BBoard name */
127 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
132 SIGNAL_HANDLER istat
; /* original SIGINT */
133 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
134 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
137 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
140 int interrupted
; /* SIGINT detected */
141 int broken_pipe
; /* SIGPIPE detected */
142 int told_to_quit
; /* SIGQUIT detected */
147 void fsetup (char *);
151 void display_info (int);
154 void seq_setcur (struct msgs
*, int);
155 void padios (char *, char *, ...);
156 void padvise (char *, char *, ...);
158 extern m_getfld_state_t gstate
; /* use the gstate in scansbr.c */
164 static void msh (int);
165 static int read_map (char *, long);
166 static int read_file (long, int);
168 static void m_gMsgs (int);
169 static int check_folder (int);
170 static void scanrange (int, int);
171 static void scanstring (char *);
172 static void write_ids (void);
173 static void quit (void);
174 static int getargs (char *, struct swit
*, struct Cmd
*);
175 static int getcmds (struct swit
*, struct Cmd
*, int);
176 static int parse (char *, struct Cmd
*);
177 static int init_io (struct Cmd
*, int);
178 static int initaux_io (struct Cmd
*);
179 static void fin_io (struct Cmd
*, int);
180 static void finaux_io (struct Cmd
*);
181 static void m_init (void);
182 static void intrser (int);
183 static void pipeser (int);
184 static void quitser (int);
185 static void alrmser (int);
186 static int pINI (void);
187 static int pQRY (char *, int);
188 static int pQRY1 (int);
189 static int pQRY2 (void);
190 static int pCMD (char *, struct swit
*, struct Cmd
*);
191 static int pFIN (void);
192 static int peerwait (void);
193 static int ttyNaux (struct Cmd
*, char *);
194 static int ttyR (struct Cmd
*);
195 static int winN (struct Cmd
*, int, int);
196 static int winR (struct Cmd
*);
197 static int winX (int);
201 main (int argc
, char **argv
)
203 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
204 char *cp
, *file
= NULL
, *folder
= NULL
;
205 char **argp
, **arguments
, buf
[BUFSIZ
];
208 setlocale(LC_ALL
, "");
210 invo_name
= r1bindex (argv
[0], '/');
212 /* read user profile/context */
215 mts_init (invo_name
);
216 arguments
= getarguments (invo_name
, argc
,argv
, 1);
219 while ((cp
= *argp
++)) {
221 switch (smatch (++cp
, switches
)) {
223 ambigsw (cp
, switches
);
226 adios (NULL
, "-%s unknown", cp
);
229 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
230 print_help (buf
, switches
, 1);
233 print_version(invo_name
);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 adios (NULL
, "missing argument to %s", argp
[-2]);
239 if ((id
= atoi (cp
)) < 1)
240 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
243 if (!(cp
= *argp
++) || *cp
== '-')
244 adios (NULL
, "missing argument to %s", argp
[-2]);
245 if ((pfd
= atoi (cp
)) <= 1)
246 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
251 if ((ppid
= atoi (cp
)) <= 1)
252 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
255 if (!(myname
= *argp
++) || *myname
== '-')
256 adios (NULL
, "missing argument to %s", argp
[-2]);
267 if (!(myprompt
= *argp
++) || *myprompt
== '-')
268 adios (NULL
, "missing argument to %s", argp
[-2]);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 adios (NULL
, "missing argument to %s", argp
[-2]);
274 if ((vmh1
= atoi (cp
)) < 1)
275 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 adios (NULL
, "missing argument to %s", argp
[-2]);
280 if ((vmh2
= atoi (cp
)) < 1)
281 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 adios (NULL
, "missing argument to %s", argp
[-2]);
289 if (!(cp
= *argp
++) || *cp
== '-')
290 adios (NULL
, "missing argument to %s", argp
[-2]);
300 if (*cp
== '+' || *cp
== '@') {
302 adios (NULL
, "only one folder at a time!");
304 folder
= pluspath (cp
);
308 adios (NULL
, "only one file at a time!");
313 if (!file
&& !folder
)
316 adios (NULL
, "use a file or a folder, not both");
317 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
320 ioctl (pfd
, FIOCLEX
, NULL
);
323 istat
= SIGNAL2 (SIGINT
, intrser
);
324 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
326 sc_width (); /* MAGIC... */
328 if ((vmh
= vmh1
&& vmh2
)) {
331 SIGNAL (SIGINT
, SIG_IGN
);
332 SIGNAL (SIGQUIT
, SIG_IGN
);
334 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
343 display_info (id
> 0 ? scansw
: 0);
345 msh (id
> 0 ? scansw
: 0);
355 #define MSHCMDS_SWITCHES \
356 X("advance", -7, ADVCMD) \
357 X("ali", 0, ALICMD) \
358 X("burst", 0, EXPLCMD) \
359 X("comp", 0, COMPCMD) \
360 X("dist", 0, DISTCMD) \
361 X("exit", 0, EXITCMD) \
362 X("folder", 0, FOLDCMD) \
363 X("forw", 0, FORWCMD) \
364 X("help", 0, HELPCMD) \
366 X("mark", 0, MARKCMD) \
367 X("mhmail", 0, MAILCMD) \
368 X("mhn", 0, MHNCMD) \
369 X("msgchk", 0, MSGKCMD) \
370 X("next", 0, NEXTCMD) \
371 X("packf", 0, PACKCMD) \
372 X("pick", 0, PICKCMD) \
373 X("prev", 0, PREVCMD) \
374 X("quit", 0, QUITCMD) \
375 X("refile", 0, FILECMD) \
376 X("repl", 0, REPLCMD) \
377 X("rmm", 0, RMMCMD) \
378 X("scan", 0, SCANCMD) \
379 X("send", 0, SENDCMD) \
380 X("show", 0, SHOWCMD) \
381 X("sortm", 0, SORTCMD) \
382 X("whatnow", 0, WHATCMD) \
383 X("whom", 0, WHOMCMD) \
385 #define X(sw, minchars, id) id,
386 DEFINE_SWITCH_ENUM(MSHCMDS
);
389 #define X(sw, minchars, id) { sw, minchars, id },
390 DEFINE_SWITCH_ARRAY(MSHCMDS
, mshcmds
);
398 register char *cp
, **ap
;
399 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
401 register struct Cmd
*cmdp
;
402 static int once_only
= ADVCMD
;
404 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
413 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
418 check_folder (scansw
);
419 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
424 cmd_name
= mshcmds
[i
].sw
;
432 if (once_only
== ADVCMD
)
433 once_only
= i
= SHOWCMD
;
435 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
436 cmd_name
= mshcmds
[i
].sw
;
442 case FORWCMD
: /* sigh */
452 if ((cp
= context_find (cmd_name
))) {
454 ap
= brkstring (cp
, " ", "\n");
455 ap
= copyip (ap
, vec
, MAXARGS
);
466 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
470 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
476 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
487 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
488 forkcmd (vec
, cmd_name
);
492 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
497 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
503 || (filehak (vec
) == OK
? ttyN (cmdp
)
504 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
509 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
514 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
519 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
525 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
530 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
537 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
543 || (packhak (vec
) == OK
? ttyN (cmdp
)
544 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
549 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
554 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
559 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
564 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
569 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
574 padios (NULL
, "no dispatch for %s", cmd_name
);
596 fsetup (char *folder
)
602 maildir
= m_maildir (folder
);
603 if (chdir (maildir
) == NOTOK
)
604 padios (maildir
, "unable to change directory to");
606 /* read folder and create message structure */
607 if (!(mp
= folder_read (folder
, 0)))
608 padios (NULL
, "unable to read folder %s", folder
);
610 /* check for empty folder */
612 padios (NULL
, "no messages in %s", folder
);
615 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
617 m_gMsgs (mp
->hghmsg
);
619 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
620 Msgs
[msgnum
].m_bboard_id
= 0;
621 Msgs
[msgnum
].m_top
= NOTOK
;
622 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
623 Msgs
[msgnum
].m_scanl
= NULL
;
628 fmsh
= getcpy (folder
);
630 maxfds
= OPEN_MAX
/ 2;
632 if ((maxfds
-= 2) < 1)
642 if ((fp
= fopen (file
, "r")) == NULL
)
643 padios (file
, "unable to read");
645 ioctl (fileno (fp
), FIOCLEX
, NULL
);
647 if (fstat (fileno (fp
), &st
) != NOTOK
) {
648 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
649 msgp
= read_map (file
, (long) st
.st_size
);
652 mode
= m_gmprot (), mtime
= 0;
656 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
657 padios (NULL
, "no messages in %s", myname
? myname
: file
);
659 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
660 padios (NULL
, "unable to allocate folder storage");
662 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
663 padios (NULL
, "unable to allocate message status storage");
669 mp
->foldpath
= getcpy (myname
? myname
: file
);
670 clear_folder_flags (mp
);
673 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
677 mp
->hghoff
= mp
->hghmsg
+ 1;
679 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
680 clear_msg_flags (mp
, i
);
685 svector_push_back (mp
->msgattrs
, getcpy ("unseen"));
687 scan_detect_mbox_style (fp
); /* the MAGIC invocation */
696 read_map (char *file
, long size
)
698 register int i
, msgp
;
699 register struct drop
*dp
, *mp
;
702 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
708 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
709 mp
= &Msgs
[msgp
].m_drop
;
711 mp
->d_size
= dp
->d_size
;
712 mp
->d_start
= dp
->d_start
;
713 mp
->d_stop
= dp
->d_stop
;
714 Msgs
[msgp
].m_scanl
= NULL
;
723 read_file (long pos
, int msgp
)
726 register struct drop
*dp
, *mp
;
729 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
732 m_gMsgs ((msgp
- 1) + i
);
734 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
735 mp
= &Msgs
[msgp
].m_drop
;
737 mp
->d_size
= dp
->d_size
;
738 mp
->d_start
= dp
->d_start
;
739 mp
->d_stop
= dp
->d_stop
;
740 Msgs
[msgp
].m_scanl
= NULL
;
754 nMsgs
= n
+ MAXFOLDER
/ 2;
755 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
757 padios (NULL
, "unable to allocate Msgs structure");
764 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
765 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
766 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
773 msh_ready (int msgnum
, int full
)
786 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
787 if (numfds
>= maxfds
)
788 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
789 if (Msgs
[msgp
].m_top
!= NOTOK
) {
790 close (Msgs
[msgp
].m_top
);
791 Msgs
[msgp
].m_top
= NOTOK
;
796 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
797 padios (cp
, "unable to open message");
798 Msgs
[msgnum
].m_top
= fd
;
802 if ((fd
= dup (fd
)) == NOTOK
)
803 padios ("cached message", "unable to dup");
804 if ((yp
= fdopen (fd
, "r")) == NULL
)
805 padios (NULL
, "unable to fdopen cached message");
806 fseek (yp
, 0L, SEEK_SET
);
810 scan_reset_m_getfld_state ();
811 scan_eom_action ((int (*)()) 0); /* XXX */
812 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
818 check_folder (int scansw
)
820 int seqnum
, i
, low
, hgh
, msgp
;
824 if (stat (mp
->foldpath
, &st
) == NOTOK
)
825 padios (mp
->foldpath
, "unable to stat");
826 if (mtime
== st
.st_mtime
)
830 low
= mp
->hghmsg
+ 1;
831 folder_free (mp
); /* free folder/message structure */
833 if (!(mp
= folder_read (fmsh
, 0)))
834 padios (NULL
, "unable to re-read folder %s", fmsh
);
838 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
839 if (Msgs
[msgp
].m_top
!= NOTOK
) {
840 close (Msgs
[msgp
].m_top
);
841 Msgs
[msgp
].m_top
= NOTOK
;
844 if (Msgs
[msgp
].m_scanl
) {
845 free (Msgs
[msgp
].m_scanl
);
846 Msgs
[msgp
].m_scanl
= NULL
;
852 if (modified
|| low
> hgh
)
856 if (fstat (fileno (fp
), &st
) == NOTOK
)
857 padios (mp
->foldpath
, "unable to fstat");
858 if (mtime
== st
.st_mtime
)
860 mode
= (int) (st
.st_mode
& 0777);
863 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
864 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
865 if (msgp
>= MAXFOLDER
)
866 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
868 if (msgp
<= mp
->hghmsg
)
871 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
872 padios (NULL
, "unable to allocate folder storage");
874 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
875 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
876 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
879 add_sequence(mp
, seqnum
, i
);
889 advise (NULL
, "new messages have arrived!\007");
891 scanrange (low
, hgh
);
898 scanrange (int low
, int hgh
)
902 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
908 scanstring (char *arg
)
910 char *cp
, **ap
, *vec
[MAXARGS
];
913 * This should be replace with a call to getarguments()
915 if ((cp
= context_find (cmd_name
= "scan"))) {
917 ap
= brkstring (cp
, " ", "\n");
918 ap
= copyip (ap
, vec
, MAXARGS
);
934 register int cur
, seqnum
, i
=0, msgnum
;
937 seq_setcur (mp
, mp
->lowmsg
);
938 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
941 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
942 add_sequence(mp
, seqnum
, msgnum
);
947 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
948 if (does_exist(mp
, msgnum
)) /* FIX */
949 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
951 clear_sequence(mp
, seqnum
, msgnum
);
954 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
955 clear_sequence(mp
, seqnum
, i
);
957 if (cur
> mp
->hghmsg
)
960 seq_setcur (mp
, cur
);
963 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
964 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
973 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
976 if (Msgs
[msgnum
].m_bboard_id
)
977 return Msgs
[msgnum
].m_bboard_id
;
979 zp
= msh_ready (msgnum
, 0);
981 int bufsz
= sizeof buf
;
982 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
985 if (!strcasecmp (name
, BBoard_ID
)) {
987 while (state
== FLDPLUS
) {
989 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
995 return (Msgs
[msgnum
].m_bboard_id
= i
);
999 while (state
== FLDPLUS
) {
1001 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
1013 display_info (int scansw
)
1017 interactive
= isatty (fileno (stdout
));
1019 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1020 padios ("standard output", "unable to dup");
1022 ioctl (sd
, FIOCLEX
, NULL
);
1023 #endif /* FIOCLEX */
1024 if ((sp
= fdopen (sd
, "w")) == NULL
)
1025 padios ("standard output", "unable to fdopen");
1028 m_putenv ("mhfolder", mp
->foldpath
);
1033 printf ("Reading ");
1034 if (SOprintf ("%s", myname
))
1035 printf ("%s", myname
);
1036 printf (", currently at message %d of %d\n",
1037 mp
->curmsg
, mp
->hghmsg
);
1040 printf ("Reading ");
1042 printf ("+%s", fmsh
);
1044 printf ("%s", mp
->foldpath
);
1045 printf (", currently at message %d of %d\n",
1046 mp
->curmsg
, mp
->hghmsg
);
1049 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1051 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1052 scanstring ("unseen");
1059 int i
= 0, seqnum
, msgnum
;
1065 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1066 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1067 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1068 if (Msgs
[msgnum
].m_bboard_id
== 0)
1070 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1074 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1075 write (pfd
, buffer
, sizeof(buffer
));
1085 char *cp
, tmpfil
[BUFSIZ
];
1086 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1090 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1092 rc2peer (RC_FIN
, 0, NULL
);
1097 ttyNaux (NULLCMD
, "FAST");
1099 if ((dp
= lkfopendata (mp
->foldpath
, "r")) == NULL
) {
1100 advise (mp
->foldpath
, "unable to lock");
1107 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1108 advise (mp
->foldpath
, "unable to stat");
1111 if (mtime
!= st
.st_mtime
) {
1112 advise (NULL
, "new messages have arrived, no update");
1115 mode
= (int) (st
.st_mode
& 0777);
1117 if (mp
->nummsg
== 0) {
1118 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1119 if (getanswer (cp
)) {
1120 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1123 advise (mp
->foldpath
, "error zero'ing");
1124 unlink (map_name (mp
->foldpath
));/* XXX */
1129 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1130 if (!getanswer (cp
))
1132 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1133 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1134 advise (tmpfil
, "unable to open");
1138 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1139 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1140 mbx_close (tmpfil
, md
);
1142 unlink (map_name (tmpfil
));
1145 mbx_close (tmpfil
, md
);
1147 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1148 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1150 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1151 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1153 if (rename (map1
, map2
) == NOTOK
) {
1154 admonish (map2
, "unable to rename %s to", map1
);
1163 lkfclosedata (dp
, mp
->foldpath
);
1172 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1176 static char buffer
[BUFSIZ
];
1182 printf ("%s", prompt
);
1185 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1186 if (interrupted
&& !told_to_quit
) {
1191 if (told_to_quit
|| i
== EOF
) {
1194 kill (ppid
, SIGEMT
);
1196 kill (ppid
, SIGTERM
);
1200 if (cp
< &buffer
[sizeof buffer
- 2])
1207 if (buffer
[0] == '?') {
1208 printf ("commands:\n");
1209 print_sw (ALL
, sw
, "", stdout
);
1210 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1215 if (parse (buffer
, cmdp
) == NOTOK
)
1218 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1220 ambigsw (cmdp
->args
[0], sw
);
1223 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1234 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1237 struct record rcs
, *rc
;
1243 switch (peer2rc (rc
)) {
1245 pQRY (rc
->rc_data
, scansw
);
1249 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1256 kill (ppid
, SIGEMT
);
1258 kill (ppid
, SIGTERM
);
1263 padios (NULL
, "%s", rc
->rc_data
);
1266 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1273 parse (char *buffer
, struct Cmd
*cmdp
)
1279 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1280 cmdp
->redirect
= NULL
;
1281 cmdp
->direction
= STDIO
;
1282 cmdp
->stream
= NULL
;
1284 for (cp
= buffer
; (c
= *cp
); cp
++) {
1285 if (!isspace ((unsigned char) c
))
1290 fmt2peer (RC_EOF
, "null command");
1294 while ((c
= *cp
++)) {
1295 if (isspace ((unsigned char) c
)) {
1296 while (isspace ((unsigned char) c
))
1301 cmdp
->args
[argp
++] = pp
;
1308 switch (c
= *cp
++) {
1310 padvise (NULL
, "unmatched \"");
1315 if ((c
= *cp
++) == 0)
1326 if ((c
= *cp
++) == 0) {
1328 padvise (NULL
, "the newline character can not be quoted");
1338 if (pp
== cmdp
->line
) {
1339 padvise (NULL
, "invalid null command");
1342 if (*cmdp
->args
[argp
- 1] == 0)
1344 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1345 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1346 cmdp
->direction
= APPIO
;
1349 cmdp
->redirect
= pp
+ 1;/* sigh */
1350 for (; (c
= *cp
); cp
++)
1351 if (!isspace ((unsigned char) c
))
1354 padvise (NULL
, cmdp
->direction
!= PIPIO
1355 ? "missing name for redirect"
1356 : "invalid null command");
1359 strcpy (cmdp
->redirect
, cp
);
1360 if (cmdp
->direction
!= PIPIO
) {
1362 if (isspace ((unsigned char) *cp
)) {
1363 padvise (NULL
, "bad name for redirect");
1366 if (expand (cmdp
->redirect
) == NOTOK
)
1375 cmdp
->args
[argp
] = NULL
;
1382 expand (char *redirect
)
1388 if (*redirect
!= '~')
1391 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1396 if ((pw
= getpwnam (pp
)))
1399 padvise (NULL
, "unknown user: %s", pp
);
1403 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1404 strcpy (redirect
, path
);
1410 init_io (struct Cmd
*cmdp
, int vio
)
1417 result
= initaux_io (cmdp
);
1425 initaux_io (struct Cmd
*cmdp
)
1429 switch (cmdp
->direction
) {
1435 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1436 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1437 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1438 cmdp
->direction
= STDIO
;
1444 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1445 padvise (cmdp
->redirect
, "unable to pipe");
1446 cmdp
->direction
= STDIO
;
1449 SIGNAL (SIGPIPE
, pipeser
);
1454 padios (NULL
, "unknown redirection for command");
1458 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1459 padios ("standard output", "unable to dup2");
1467 fin_io (struct Cmd
*cmdp
, int vio
)
1479 finaux_io (struct Cmd
*cmdp
)
1481 switch (cmdp
->direction
) {
1488 close (fileno (stdout
));
1489 if (ferror (stdout
))
1490 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1491 fclose (cmdp
->stream
);
1496 close (fileno (stdout
));
1497 pclose (cmdp
->stream
);
1498 SIGNAL (SIGPIPE
, SIG_DFL
);
1502 padios (NULL
, "unknown redirection for command");
1505 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1506 padios ("standard output", "unable to dup2");
1509 cmdp
->direction
= STDIO
;
1518 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1519 unset_selected (mp
, msgnum
);
1520 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1528 folder_free (mp
); /* free folder/message structure */
1534 seq_setcur (struct msgs
*mp
, int msgnum
)
1536 if (mp
->curmsg
== msgnum
)
1539 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1540 free (Msgs
[mp
->curmsg
].m_scanl
);
1541 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1543 if (Msgs
[msgnum
].m_scanl
) {
1544 free (Msgs
[msgnum
].m_scanl
);
1545 Msgs
[msgnum
].m_scanl
= NULL
;
1548 mp
->curmsg
= msgnum
;
1566 if (broken_pipe
++ == 0)
1567 fprintf (stderr
, "broken pipe\n");
1586 longjmp (peerenv
, DONE
);
1595 struct record rcs
, *rc
;
1600 switch (peer2rc (rc
)) {
1603 while (isspace ((unsigned char) *bp
))
1605 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1607 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1610 if (vrsn
!= RC_VRSN
) {
1611 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1615 while (*bp
&& !isspace ((unsigned char) *bp
))
1617 while (isspace ((unsigned char) *bp
))
1619 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1624 for (i
= 1; i
<= numwins
; i
++) {
1625 while (*bp
&& !isspace ((unsigned char) *bp
))
1627 while (isspace ((unsigned char) *bp
))
1629 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1632 rc2peer (RC_ACK
, 0, NULL
);
1636 padios (NULL
, "%s", rc
->rc_data
);
1639 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1640 done (1); /* NOTREACHED */
1643 return 1; /* dead code to satisfy the compiler */
1648 pQRY (char *str
, int scansw
)
1651 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1654 rc2peer (RC_EOF
, 0, NULL
);
1663 static int lastlow
= 0,
1668 oldhgh
= mp
->hghmsg
;
1669 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1670 switch (winX (STATUS
)) {
1675 printf ("new messages have arrived!");
1678 _exit (0); /* NOTREACHED */
1681 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1685 switch (winX (DISPLAY
)) {
1690 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1693 _exit (0); /* NOTREACHED */
1702 switch (winX (STATUS
)) {
1707 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1708 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1709 readid (mp
->lowmsg
));
1712 _exit (0); /* NOTREACHED */
1719 if (mp
->lowmsg
!= lastlow
1720 || mp
->curmsg
!= lastcur
1721 || mp
->hghmsg
!= lasthgh
1722 || mp
->nummsg
!= lastnum
)
1723 switch (winX (STATUS
)) {
1731 _exit (0); /* NOTREACHED */
1734 lastlow
= mp
->lowmsg
;
1735 lastcur
= mp
->curmsg
;
1736 lasthgh
= mp
->hghmsg
;
1737 lastnum
= mp
->nummsg
;
1748 int i
, j
, k
, msgnum
, n
;
1754 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1755 switch (winX (SCAN
)) {
1763 _exit (0); /* NOTREACHED */
1772 j
= (k
= windows
[SCAN
]) / 2;
1773 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1774 if (does_exist (mp
, msgnum
))
1778 k
= i
>= k
? 1 : k
- i
;
1785 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1786 if (does_exist (mp
, msgnum
)) {
1793 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1794 if (does_exist (mp
, msgnum
)) {
1798 if (n
++ >= windows
[SCAN
])
1804 && does_exist (mp
, lo
)
1805 && does_exist (mp
, hi
)
1807 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1809 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1810 && hi
- lo
== j
- i
)
1813 if (mp
->curmsg
!= cur
|| modified
)
1814 switch (winN (NULLCMD
, SCAN
, 0)) {
1822 scanrange (lo
= i
, hi
= j
);
1833 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1838 switch (winX (DISPLAY
)) {
1843 printf ("commands:\n");
1844 print_sw (ALL
, sw
, "", stdout
);
1845 printf ("type ``quit'' to leave %s\n", invo_name
);
1848 _exit (0); /* NOTREACHED */
1851 rc2peer (RC_EOF
, 0, NULL
);
1855 if (parse (str
, cmdp
) == NOTOK
)
1858 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1860 switch (winX (DISPLAY
)) {
1865 ambigsw (cmdp
->args
[0], sw
);
1868 _exit (0); /* NOTREACHED */
1871 rc2peer (RC_EOF
, 0, NULL
);
1877 "say what: ``%s'' -- type ? (or help) for help",
1892 switch (setjmp (peerenv
)) {
1894 SIGNAL (SIGALRM
, alrmser
);
1897 status
= peerwait ();
1911 struct record rcs
, *rc
;
1916 switch (peer2rc (rc
)) {
1919 rc2peer (RC_FIN
, 0, NULL
);
1923 advise (NULL
, "%s", rc
->rc_data
);
1927 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1934 ttyNaux (struct Cmd
*cmdp
, char *s
)
1936 struct record rcs
, *rc
;
1941 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1944 /* XXX: fseek() too tricky for our own good */
1946 fseek (fp
, 0L, SEEK_SET
);
1949 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1951 vmhtty
= OK
; /* fall */
1956 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1959 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1960 done (1); /* NOTREACHED */
1964 SIGNAL (SIGTSTP
, tstat
);
1971 ttyR (struct Cmd
*cmdp
)
1973 struct record rcs
, *rc
;
1978 SIGNAL (SIGTSTP
, SIG_IGN
);
1990 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
1992 rc2peer (RC_EOF
, 0, NULL
);
1996 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1999 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2000 done (1); /* NOTREACHED */
2003 return 1; /* dead code to satisfy compiler */
2008 winN (struct Cmd
*cmdp
, int n
, int eof
)
2011 char buffer
[BUFSIZ
];
2012 struct record rcs
, *rc
;
2015 if (vmhpid
== NOTOK
)
2020 /* XXX: fseek() too tricky for our own good */
2022 fseek (fp
, 0L, SEEK_SET
);
2026 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2027 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2035 padios (NULL
, "%s", rc
->rc_data
);
2038 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2042 if (pipe (pd
) == NOTOK
) {
2043 err2peer (RC_ERR
, "pipe", "unable to");
2047 switch (vmhpid
= fork()) {
2049 err2peer (RC_ERR
, "fork", "unable to");
2056 SIGNAL (SIGPIPE
, SIG_IGN
);
2057 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2058 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2066 advise (NULL
, "%s", rc
->rc_data
);
2070 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2074 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2077 rc2peer (RC_EOF
, 0, NULL
);
2082 advise (NULL
, "%s", rc
->rc_data
);
2087 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2092 err2peer (RC_ERR
, "pipe", "error reading from");
2094 _exit (i
!= NOTOK
? i
: 1);
2097 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2098 padios ("standard input", "unable to dup");
2099 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2100 padios ("standard output", "unable to dup");
2101 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2102 padios ("diagnostic output", "unable to dup");
2105 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2106 dup2 (i
, fileno (stdin
));
2111 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2112 padios ("standard output", "unable to dup2");
2116 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2117 padios ("diagnostic output", "unable to dup2");
2120 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2122 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2134 winR (struct Cmd
*cmdp
)
2144 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2145 padios ("standard input", "unable to dup2");
2150 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2151 padios ("standard output", "unable to dup2");
2156 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2157 padios ("diagnostic output", "unable to dup2");
2161 SIGNAL (SIGPIPE
, pstat
);
2163 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2167 return (status
== 0 ? OK
: NOTOK
);
2175 char buffer
[BUFSIZ
];
2176 struct record rcs
, *rc
;
2181 /* XXX: fseek() too tricky for our own good */
2183 fseek (fp
, 0L, SEEK_SET
);
2185 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2186 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2194 padios (NULL
, "%s", rc
->rc_data
);
2197 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2201 if (pipe (pd
) == NOTOK
) {
2202 err2peer (RC_ERR
, "pipe", "unable to");
2206 switch (pid
= fork ()) {
2208 err2peer (RC_ERR
, "fork", "unable to");
2214 close (fileno (stdin
));
2215 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2216 dup2 (i
, fileno (stdin
));
2219 dup2 (pd
[1], fileno (stdout
));
2220 dup2 (pd
[1], fileno (stderr
));
2228 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2229 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2239 padios (NULL
, "%s", rc
->rc_data
);
2242 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2246 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2251 padios (NULL
, "%s", rc
->rc_data
);
2254 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2258 err2peer (RC_ERR
, "pipe", "error reading from");
2262 return (i
!= NOTOK
? pid
: NOTOK
);
2268 padios (char *what
, char *fmt
, ...)
2274 verr2peer (RC_FIN
, what
, fmt
, ap
);
2277 advertise (what
, NULL
, fmt
, ap
);
2286 padvise (char *what
, char *fmt
, ...)
2292 verr2peer (RC_ERR
, what
, fmt
, ap
);
2294 advertise (what
, NULL
, fmt
, ap
);