]>
diplodocus.org Git - nmh/blob - uip/folder.c
3 * folder(s).c -- set/list the current message and/or folder
4 * -- push/pop a folder onto/from the folder stack
5 * -- list the folder stack
13 static struct swit switches
[] = {
67 static int fshort
= 0; /* output only folder names */
68 static int fcreat
= 0; /* should we ask to create new folders? */
69 static int fpack
= 0; /* are we packing the folder? */
70 static int fverb
= 0; /* print actions taken while packing folder */
71 static int fheader
= 0; /* should we output a header? */
72 static int frecurse
= 0; /* recurse through subfolders */
73 static int ftotal
= 0; /* should we output the totals? */
74 static int all
= 0; /* should we output all folders */
76 static int total_folders
= 0; /* total number of folders */
82 static char *stack
= "Folder-Stack";
83 static char folder
[BUFSIZ
];
85 #define NUMFOLDERS 100
88 * This is how many folders we currently can hold in the array
89 * `folds'. We increase this amount by NUMFOLDERS at a time.
91 static int maxfolders
;
95 * Structure to hold information about
96 * folders as we scan them.
104 int others
; /* others == 1 if other files in folder */
105 int error
; /* error == 1 for unreadable folder */
109 * Dynamically allocated space to hold
110 * all the folder information.
112 static struct FolderInfo
*fi
;
113 static int maxFolderInfo
;
118 static void dodir (char *);
119 static int get_folder_info (char *, char *);
120 static void print_folders (void);
121 static int num_digits (int);
122 static int sfold (struct msgs
*, char *);
123 static void addir (char *);
124 static void addfold (char *);
125 static int compare (char *, char *);
126 static void readonly_folders (void);
130 main (int argc
, char **argv
)
132 int printsw
= 0, listsw
= 0;
133 int pushsw
= 0, popsw
= 0;
134 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
135 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
139 setlocale(LC_ALL
, "");
141 invo_name
= r1bindex (argv
[0], '/');
143 /* read user profile/context */
147 * If program was invoked with name ending
148 * in `s', then add switch `-all'.
150 if (argv
[0][strlen (argv
[0]) - 1] == 's')
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
156 while ((cp
= *argp
++)) {
158 switch (smatch (++cp
, switches
)) {
160 ambigsw (cp
, switches
);
163 adios (NULL
, "-%s unknown", cp
);
166 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
168 print_help (buf
, switches
, 1);
171 print_version(invo_name
);
257 if (*cp
== '+' || *cp
== '@') {
259 adios (NULL
, "only one folder at a time!");
261 argfolder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
264 adios (NULL
, "only one (current) message at a time!");
270 if (!context_find ("path"))
271 free (path ("./", TFOLDER
));
272 nmhdir
= concat (m_maildir (""), "/", NULL
);
275 * If we aren't working with the folder stack
276 * (-push, -pop, -list) then the default is to print.
278 if (pushsw
== 0 && popsw
== 0 && listsw
== 0)
281 /* Pushing a folder onto the folder stack */
284 /* If no folder is given, the current folder and */
285 /* the top of the folder stack are swapped. */
286 if ((cp
= context_find (stack
))) {
288 ap
= brkstring (dp
, " ", "\n");
289 argfolder
= getcpy(*ap
++);
291 adios (NULL
, "no other folder");
293 for (cp
= getcpy (getfolder (1)); *ap
; ap
++)
294 cp
= add (*ap
, add (" ", cp
));
296 context_replace (stack
, cp
); /* update folder stack */
298 /* update folder stack */
299 context_replace (stack
,
300 (cp
= context_find (stack
))
301 ? concat (getfolder (1), " ", cp
, NULL
)
302 : getcpy (getfolder (1)));
306 /* Popping a folder off of the folder stack */
309 adios (NULL
, "sorry, no folders allowed with -pop");
310 if ((cp
= context_find (stack
))) {
312 ap
= brkstring (dp
, " ", "\n");
313 argfolder
= getcpy(*ap
++);
315 adios (NULL
, "folder stack empty");
318 /* if there's anything left in the stack */
321 cp
= add (*ap
, add (" ", cp
));
322 context_replace (stack
, cp
); /* update folder stack */
324 context_del (stack
); /* delete folder stack entry from context */
328 if (pushsw
|| popsw
) {
329 cp
= m_maildir(argfolder
);
330 if (access (cp
, F_OK
) == NOTOK
)
331 adios (cp
, "unable to find folder");
332 context_replace (pfolder
, argfolder
); /* update current folder */
333 context_save (); /* save the context file */
337 /* Listing the folder stack */
339 printf ("%s", argfolder
? argfolder
: getfolder (1));
340 if ((cp
= context_find (stack
))) {
342 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
352 /* Allocate initial space to record folder names */
353 maxfolders
= NUMFOLDERS
;
354 if ((folds
= malloc (maxfolders
* sizeof(char *))) == NULL
)
355 adios (NULL
, "unable to allocate storage for folder names");
357 /* Allocate initial space to record folder information */
358 maxFolderInfo
= NUMFOLDERS
;
359 if ((fi
= malloc (maxFolderInfo
* sizeof(*fi
))) == NULL
)
360 adios (NULL
, "unable to allocate storage for folder info");
365 if (all
|| ftotal
> 0) {
367 * If no folder is given, do them all
371 admonish (NULL
, "no folder given for message %s", msg
);
372 readonly_folders (); /* do any readonly folders */
373 strncpy (folder
, (cp
= context_find (pfolder
)) ? cp
: "", sizeof(folder
));
376 strncpy (folder
, argfolder
, sizeof(folder
));
377 if (get_folder_info (argfolder
, msg
)) {
378 context_replace (pfolder
, argfolder
);/* update current folder */
379 context_save (); /* save the context file */
382 * Since recurse wasn't done in get_folder_info(),
383 * we still need to list all level-1 sub-folders.
389 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
392 * Check if folder exists. If not, then see if
393 * we should create it, or just exit.
395 if (stat (strncpy (buf
, m_maildir (folder
), sizeof(buf
)), &st
) == -1) {
397 adios (buf
, "error on folder");
399 /* ask before creating folder */
400 cp
= concat ("Create folder \"", buf
, "\"? ", NULL
);
404 } else if (fcreat
== -1) {
405 /* do not create, so exit */
409 adios (NULL
, "unable to create folder %s", buf
);
412 if (get_folder_info (folder
, msg
) && argfolder
) {
413 /* update current folder */
414 context_replace (pfolder
, argfolder
);
419 * Print out folder information
423 context_save (); /* save the context file */
428 * Base routine for scanning a folder
441 /* change directory to base of nmh directory */
442 if (chdir (nmhdir
) == NOTOK
)
443 adios (nmhdir
, "unable to change directory to");
445 addir (strncpy (buffer
, dir
, sizeof(buffer
)));
447 for (i
= start
; i
< foldp
; i
++) {
448 get_folder_info (folds
[i
], NULL
);
457 get_folder_info (char *fold
, char *msg
)
461 struct msgs
*mp
= NULL
;
466 * if necessary, reallocate the space
467 * for folder information
469 if (total_folders
>= maxFolderInfo
) {
470 maxFolderInfo
+= NUMFOLDERS
;
471 if ((fi
= realloc (fi
, maxFolderInfo
* sizeof(*fi
))) == NULL
)
472 adios (NULL
, "unable to re-allocate storage for folder info");
483 mailfile
= m_maildir (fold
);
485 if (!chdir (mailfile
)) {
486 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
488 * create message structure and get folder info
490 if (!(mp
= folder_read (fold
))) {
491 admonish (NULL
, "unable to read folder %s", fold
);
495 /* set the current message */
496 if (msg
&& !sfold (mp
, msg
))
500 if (folder_pack (&mp
, fverb
) == -1)
502 seq_save (mp
); /* synchronize the sequences */
503 context_save (); /* save the context file */
506 /* record info for this folder */
507 if ((ftotal
> 0) || !fshort
) {
508 fi
[i
].nummsg
= mp
->nummsg
;
509 fi
[i
].curmsg
= mp
->curmsg
;
510 fi
[i
].lowmsg
= mp
->lowmsg
;
511 fi
[i
].hghmsg
= mp
->hghmsg
;
512 fi
[i
].others
= other_files (mp
);
515 folder_free (mp
); /* free folder/message structure */
521 if (frecurse
&& (fshort
|| fi
[i
].others
) && (fi
[i
].error
== 0))
527 * Print folder information
533 int i
, len
, hasempty
= 0, curprinted
;
534 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
535 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
536 int nummsgdigits
, lowmsgdigits
;
537 int hghmsgdigits
, curmsgdigits
;
538 char tmpname
[BUFSIZ
];
541 * compute a few values needed to for
542 * printing various fields
544 for (i
= 0; i
< total_folders
; i
++) {
545 /* length of folder name */
546 len
= strlen (fi
[i
].name
);
550 /* If folder has error, skip the rest */
554 /* calculate total number of messages */
555 total_msgs
+= fi
[i
].nummsg
;
557 /* maximum number of messages */
558 if (fi
[i
].nummsg
> maxnummsg
)
559 maxnummsg
= fi
[i
].nummsg
;
561 /* maximum low message */
562 if (fi
[i
].lowmsg
> maxlowmsg
)
563 maxlowmsg
= fi
[i
].lowmsg
;
565 /* maximum high message */
566 if (fi
[i
].hghmsg
> maxhghmsg
)
567 maxhghmsg
= fi
[i
].hghmsg
;
569 /* maximum current message */
570 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
571 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
572 fi
[i
].curmsg
> maxcurmsg
)
573 maxcurmsg
= fi
[i
].curmsg
;
575 /* check for empty folders */
576 if (fi
[i
].nummsg
== 0)
579 nummsgdigits
= num_digits (maxnummsg
);
580 lowmsgdigits
= num_digits (maxlowmsg
);
581 hghmsgdigits
= num_digits (maxhghmsg
);
582 curmsgdigits
= num_digits (maxcurmsg
);
584 if (hasempty
&& nummsgdigits
< 2)
590 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
591 printf ("%-*s %*s %-*s; %-*s %*s\n",
593 nummsgdigits
+ 13, "# MESSAGES",
594 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
595 curmsgdigits
+ 4, "CUR",
599 * Print folder information
601 if (all
|| fshort
|| ftotal
< 1) {
602 for (i
= 0; i
< total_folders
; i
++) {
604 printf ("%s\n", fi
[i
].name
);
608 /* Add `+' to end of name, if folder is current */
609 if (strcmp (folder
, fi
[i
].name
))
610 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
612 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
615 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
619 printf ("%-*s ", maxlen
+1, tmpname
);
621 curprinted
= 0; /* remember if we print cur */
622 if (fi
[i
].nummsg
== 0) {
623 printf ("has %*s messages%*s",
625 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
627 printf ("has %*d message%s (%*d-%*d)",
628 nummsgdigits
, fi
[i
].nummsg
,
629 (fi
[i
].nummsg
== 1) ? " " : "s",
630 lowmsgdigits
, fi
[i
].lowmsg
,
631 hghmsgdigits
, fi
[i
].hghmsg
);
632 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
634 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
639 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
645 * Print folder/message totals
647 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
650 printf ("TOTAL = %d message%c in %d folder%s.\n",
651 total_msgs
, total_msgs
!= 1 ? 's' : ' ',
652 total_folders
, total_folders
!= 1 ? "s" : "");
659 * Calculate the number of digits in a nonnegative integer
668 adios (NULL
, "oops, num_digits called with negative value");
682 * Set the current message and sychronize sequences
686 sfold (struct msgs
*mp
, char *msg
)
688 /* parse the message range/sequence/name and set SELECTED */
689 if (!m_convert (mp
, msg
))
692 if (mp
->numsel
> 1) {
693 admonish (NULL
, "only one message at a time!");
696 seq_setprev (mp
); /* set the previous-sequence */
697 seq_setcur (mp
, mp
->lowsel
);/* set current message */
698 seq_save (mp
); /* synchronize message sequences */
699 context_save (); /* save the context file */
714 cp
= name
+ strlen (name
);
719 * A hack to skip over a leading
720 * "./" in folder names.
722 base
= strcmp (name
, "./") ? name
: name
+ 2;
724 /* short-cut to see if directory has any sub-directories */
725 if (stat (name
, &st
) != -1 && st
.st_nlink
== 2)
728 if (!(dd
= opendir (name
))) {
729 admonish (name
, "unable to read directory ");
734 * Keep track of the number of directories we've seen
735 * so we can quit stat'ing early, if we've seen them all.
739 while (nlink
&& (dp
= readdir (dd
))) {
740 if (!strcmp (dp
->d_name
, ".") || !strcmp (dp
->d_name
, "..")) {
744 if (cp
+ NLENGTH(dp
) + 2 >= name
+ BUFSIZ
)
746 strcpy (cp
, dp
->d_name
);
747 if (stat (name
, &st
) != -1 && S_ISDIR(st
.st_mode
)) {
749 * Check if this was really a symbolic link pointing at
750 * a directory. If not, then decrement link count.
752 if (lstat (name
, &st
) == -1)
763 * Add the folder name into the
764 * list in a sorted fashion.
773 /* if necessary, reallocate the space for folder names */
774 if (foldp
>= maxfolders
) {
775 maxfolders
+= NUMFOLDERS
;
776 if ((folds
= realloc (folds
, maxfolders
* sizeof(char *))) == NULL
)
777 adios (NULL
, "unable to re-allocate storage for folder names");
781 for (i
= start
; i
< foldp
; i
++)
782 if (compare (cp
, folds
[i
]) < 0) {
783 for (j
= foldp
- 1; j
>= i
; j
--)
784 folds
[j
+ 1] = folds
[j
];
795 compare (char *s1
, char *s2
)
800 if ((i
= *s1
++ - *s2
++))
807 * Do the read only folders
811 readonly_folders (void)
815 register struct node
*np
;
817 /* sanity check - check that context has been read */
819 adios (NULL
, "oops, context hasn't been read yet");
821 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
822 atrlen
= strlen (atrcur
);
824 for (np
= m_defs
; np
; np
= np
->n_next
)
825 if (ssequal (atrcur
, np
->n_name
)
826 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
827 get_folder_info (np
->n_name
+ atrlen
, NULL
);