[CFR] ncal(1): month names on the cmdline

Peter Pentchev <[email protected]> Sat, 14 Sep 2002 20:26:02 +0300
Newsgroups gmane.os.freebsd.devel.audit
Message-ID <[email protected]>
Hi,

What do people think about the attached patch, which teaches ncal(1)
(and cal(1)) to parse month names if invoked with both a month and year
specification?  It is not terribly necessary, but it just might turn out
to be useful :)

Examples:

[roam@straylight:v2 ~]$ LANG=C ncal August 2002
    August 2002
Mo     5 12 19 26
Tu     6 13 20 27
We     7 14 21 28
Th  1  8 15 22 29
Fr  2  9 16 23 30
Sa  3 10 17 24 31
Su  4 11 18 25
[roam@straylight:v2 ~]$ LANG=C cal sep 2001
   September 2001
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
[roam@straylight:v2 ~]$

(warning: Windows-1251 character set in the following example)

[roam@straylight:v2 ~]$ cal фев 2000
   Февруари 2000
Нд Пн Вт Ср Чт Пт Сб
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29
[roam@straylight:v2 ~]$

G'luck,
Peter

-- 
Peter Pentchev	[email protected]	[email protected]
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
because I didn't think of a good beginning of it.
usr.bin-ncal.patch (text/plain, 2 KB)
Index: src/usr.bin/ncal/ncal.1
===================================================================
RCS file: /home/ncvs/src/usr.bin/ncal/ncal.1,v
retrieving revision 1.14
diff -u -r1.14 ncal.1
--- src/usr.bin/ncal/ncal.1	3 Jun 2002 15:02:02 -0000	1.14
+++ src/usr.bin/ncal/ncal.1	14 Sep 2002 10:52:43 -0000
@@ -99,7 +99,8 @@
 will
 .Em not
 display a calendar for 1989.
-Two parameters denote the month (1 - 12) and year.
+Two parameters denote the month and year; the month is either a number between
+1 and 12, or a full or abbreviated name as specified by the current locale.
 .Pp
 A year starts on Jan 1.
 .Sh SEE ALSO
Index: src/usr.bin/ncal/ncal.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/ncal/ncal.c,v
retrieving revision 1.17
diff -u -r1.17 ncal.c
--- src/usr.bin/ncal/ncal.c	28 Apr 2002 12:25:03 -0000	1.17
+++ src/usr.bin/ncal/ncal.c	14 Sep 2002 10:57:40 -0000
@@ -162,6 +162,7 @@
 void	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
 void    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
 void    mkweekdays(struct weekdays * wds);
+int	parsemonth(const char *s);
 void    printcc(void);
 void    printeaster(int year, int julian, int orthodox);
 void    printmonth(int year, int month, int jd_flag);
@@ -306,9 +307,11 @@
 	case 2:
 		if (flag_easter)
 			usage();
-		m = atoi(*argv++);
+		m = parsemonth(*argv++);
 		if (m < 1 || m > 12)
-			errx(EX_USAGE, "month %d not in range 1..12", m);
+			errx(EX_USAGE,
+			    "%s is neither a month number (1..12) or a name",
+			    argv[-1]);
 		/* FALLTHROUGH */
 	case 1:
 		y = atoi(*argv++);
@@ -847,4 +850,21 @@
 	memset(blanks, ' ', sizeof(blanks));
 	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
 	return (s);
+}
+
+int
+parsemonth(const char *s)
+{
+	int v;
+	char *cp;
+	struct tm tm;
+
+	v = (int)strtol(s, &cp, 10);
+	if (cp != s)
+		return (v);
+	if (strptime(s, "%B", &tm) != NULL)
+		return (tm.tm_mon + 1);
+	if (strptime(s, "%b", &tm) != NULL)
+		return (tm.tm_mon + 1);
+	return (0);
 }
signature.asc (application/pgp-signature, 187 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (FreeBSD)

iD8DBQE9g3Eq7Ri2jRYZRVMRAqh+AJ41AeZUEVdVkDqcqSWdz3briZEXyQCfeurU
RHBbZER+gwmD5FLUi9Jn3xE=
=2mZk
-----END PGP SIGNATURE-----