rohrpost

A commandline mail client to change the world as we see it.
git clone git://r-36.net/rohrpost
Log | Files | Refs | README | LICENSE

util.c (1213B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #include <unistd.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <strings.h>
     11 
     12 #include "ind.h"
     13 #include "arg.h"
     14 #include "mime.h"
     15 #include "llist.h"
     16 
     17 void
     18 utilusage(char *argv0)
     19 {
     20 	die("usage: %s [-h] [-e|-d] string\n", argv0);
     21 }
     22 
     23 int
     24 utilmain(int argc, char *argv[])
     25 {
     26 	int status, worklen;
     27 	llist_t *workl;
     28 	char *works, *ress, *argv0;
     29 
     30 	enum {
     31 		ENCODE = 0x04,
     32 		DECODE = 0x08,
     33 		ISBODY = 0x10,
     34 
     35 		NOARGS = 0x40
     36 	};
     37 
     38 	status = 0;
     39 
     40 	ARGBEGIN(argv0) {
     41 	case 'd':
     42 		status |= DECODE;
     43 		break;
     44 	case 'e':
     45 		status |= ENCODE;
     46 		break;
     47 	case 'o':
     48 		status |= ISBODY;
     49 		break;
     50 	case 'h':
     51 	default:
     52 		utilusage(argv0);
     53 	} ARGEND;
     54 
     55 	worklen = 0;
     56 	if (argc < 1) {
     57 		works = readtoeoffd(0, &worklen);
     58 		if (works == NULL)
     59 			edie("readtoeoffd");
     60 	} else {
     61 		workl = llist_splitargv(argc, argv);
     62 		if (workl == NULL)
     63 			utilusage(argv0);
     64 		works = llist_joinstr(workl, " ");
     65 		llist_free(workl);
     66 	}
     67 
     68 	if (status & ENCODE) {
     69 		ress = mime_encodestring(works);
     70 		printf("%s", ress);
     71 	} else if (status & DECODE) {
     72 		ress = mime_guessheader(works);
     73 		printf("%s", ress);
     74 	} else {
     75 		free(works);
     76 		utilusage(argv0);
     77 	}
     78 	free(ress);
     79 	free(works);
     80 
     81 	return 0;
     82 }
     83