: [pull] cal: second review round
Subsystems (2)
cover : [pull] cal: second review round
Hello all,
Here comes cal(1) patch series II. There is a lot of similarities, and some changes since last submission. The previous patch series is still available in my git (branch 2013wk16), which should make comparing every single small detail quite trivial. IMHO the most significant changes in between the two submissions are:
o Copy argmatch.h and argmatch.c from gnulib to util-linux.
o Add --highlight={never,auto,always} option.
o Add new option to usage(), and clean it up.
o Remove trailing space trimming.
All the changes are based on feedback from P�draig, Thomas, Dave, and Karel. Thank you for pointing me to correct direction, and please have look if I'm still a bit lost.
The following changes since commit a48c321dd175d043fb4572bab625631b2055075e:
textual: fixing typos in a warning and an error message (2013-04-26 13:55:29 +0200)
are available in the git repository at:
git://github.com/kerolasa/lelux-utiliteetit.git cal-new
for you to fetch changes up to 2b491019173dc2eda3a0d014e9adb3dec54e41bc:
cal: simplify day_in_week() (2013-05-02 19:30:16 +0100)
----------------------------------------------------------------
Sami Kerola (16):
cal: fix preprocessor directive indendation
cal: convert function like definitions to functions
cal: clean up use of constants
tests: add calendar reformation check
cal: simplify calendar reformat calculations
cal: remove unnecessary initializations
cal: de-duplicate julian specific functions
lib: copy argmatch from gnulib
cal: add --highligth option which uses argmatch
cal: add --highlight to usage()
docs: cal: add --highlight option description
tests: add cal day highlight corner cases
cal: stop trimming whitespaces
cal: move global variables to local scope
cal: mark all functions static
cal: simplify day_in_week()
include/Makemodule.am | 1 + include/argmatch.h | 100 ++++++++ lib/Makemodule.am | 1 + lib/argmatch.c | 272 ++++++++++++++++++++ misc-utils/Makemodule.am | 2 +- misc-utils/cal.1 | 13 + misc-utils/cal.c | 597 ++++++++++++++++++++++--------------------- tests/expected/cal/1m | 48 ++-- tests/expected/cal/3m | 48 ++-- tests/expected/cal/highlight | 55 ++++ tests/expected/cal/sep1752 | 198 ++++++++++++++ tests/expected/cal/year | 240 ++++++++--------- tests/ts/cal/highlight | 70 +++++ tests/ts/cal/sep1752 | 95 +++++++ 14 files changed, 1274 insertions(+), 466 deletions(-) create mode 100644 include/argmatch.h create mode 100644 lib/argmatch.c create mode 100644 tests/expected/cal/highlight create mode 100644 tests/expected/cal/sep1752 create mode 100755 tests/ts/cal/highlight create mode 100755 tests/ts/cal/sep1752
PATCH 1/16 cal: fix preprocessor directive indendation
And code within the blocks selected by preprocessor to be live code.
--- misc-utils/cal.c | 81 +++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 42 deletions(-)
misc-utils/cal.c +39 -42
@@ -73,69 +73,66 @@#include "strutils.h" #if defined(HAVE_LIBNCURSES ) || defined(HAVE_LIBNCURSESW ) - -#ifdef HAVE_NCURSES_H -#include <ncurses.h> -#elif defined(HAVE_NCURSES_NCURSES_H ) -#include <ncurses/ncurses.h> -#endif - -#include <term.h> /* include after <curses.h> */ - -static void -my_setupterm (const char *term,int fildes,int *errret) { -setupterm ((char *)term, fildes, errret); +# ifdef HAVE_NCURSES_H +# include <ncurses.h> +# elif defined(HAVE_NCURSES_NCURSES_H ) +# include <ncurses/ncurses.h> +# endif +# include <term.h> + +static void my_setupterm (const char *term,int fildes,int *errret) +{ +setupterm ((char *)term, fildes, errret); } -static void -my_putstring (char *s) { -putp (s); +static void my_putstring (char *s) +{ +putp (s); } -static const char * -my_tgetstr (char *s __attribute__ ((__unused__)),char *ss) { -const char * ret =tigetstr (ss); -if (!ret || ret==(char *)-1 ) -return "" ; -else -return ret; +static const char *my_tgetstr (char *s __attribute__((__unused__)),char *ss) +{ +const char *ret =tigetstr (ss); +if (!ret || ret == (char *)-1 ) +return "" ; +else +return ret; }#elif defined(HAVE_LIBTERMCAP ) - -#include <termcap.h> +# include <termcap.h> char termbuffer[4096 ];char tcbuffer[4096 ];char *strbuf = termbuffer; -static void -my_setupterm (const char *term,int fildes,int *errret) { - *errret =tgetent (tcbuffer, term); +static void my_setupterm (const char *term,int fildes,int *errret) +{ + *errret =tgetent (tcbuffer, term); } -static void -my_putstring (char *s) { -tputs (s,1 , putchar); +static void my_putstring (char *s) +{ +tputs (s,1 , putchar); } -static const char * -my_tgetstr (char *s,char *ss __attribute__ ((__unused__))) { -const char * ret =tgetstr (s, &strbuf); -if (!ret) -return "" ; -else -return ret; +static const char *my_tgetstr (char *s,char *ss __attribute__((__unused__))) +{ +const char *ret =tgetstr (s, &strbuf); +if (!ret) +return "" ; +else +return ret; } -#else /* ! (HAVE_LIBTERMCAP || HAVE_LIBNCURSES || HAVE_LIBNCURSESW) */ +#else /* ! (HAVE_LIBTERMCAP || HAVE_LIBNCURSES || HAVE_LIBNCURSESW) */ -static void -my_putstring (char *s) { -fputs (s, stdout); +static void my_putstring (char *s) +{ +fputs (s, stdout); } -#endif +#endif /* end of LIBTERMCAP / NCURSES */ const char *term="" ;
Signature · 1 line
1.8.2.2
PATCH 2/16 cal: convert function like definitions to functions
In reply to Sami Kerola--- misc-utils/cal.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-)
misc-utils/cal.c +28 -17
@@ -209,23 +209,6 @@ char j_day_headings[J_WEEK_LEN*6+1];/* weekstart = 1 => " M Tu W Th F S S " */ const char *full_month[12 ]; -/* leap year -- account for gregorian reformation in 1752 */ -#define leap_year (yr) \ - ((yr) <= 1752 ? !((yr) % 4) : \ - (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) - -/* number of centuries since 1700, not inclusive */ -#define centuries_since_1700 (yr) \ - ((yr) > 1700 ? (yr) / 100 - 17 : 0) - -/* number of centuries since 1700 whose modulo of 400 is 0 */ -#define quad_centuries_since_1700 (yr) \ - ((yr) > 1600 ? ((yr) - 1600) / 400 : 0) - -/* number of leap years between year 1 and this year, not inclusive */ -#define leap_years_since_year_1 (yr) \ - ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr)) -/* 0 => sunday, 1 => monday */ int weekstart=0 ;int julian;
@@ -239,6 +222,9 @@ struct fmt_stchar s[FMT_ST_LINES ][FMT_ST_CHARS ]; }; +static int leap_year (int year); +static int centuries_since_1700 (int year,int centuries); +static int leap_years_since_year_1 (int year);char *ascii_day (char *,int );int center_str (const char * src,char * dest,size_t dest_size,size_t width);void center (const char *,size_t ,int );
@@ -413,6 +399,31 @@ main(int argc, char **argv) {return EXIT_SUCCESS ; } +/* leap year -- account for gregorian reformation in 1752 */ +static int leap_year (int year) +{ +if (year <=1752 ) +return !(year %4 ); +else +return ( !(year %4 ) && (year %100 ) ) || !(year %400 ); +} + +/* number of centuries since 1700 */ +static int centuries_since_1700 (int year,int n) +{ +if (year <1700 ) +return 0 ; +else +return ((year / (100 * n)) - (17 / n)); +} + +/* number of leap years between year 1 and this year, not inclusive */ +static int leap_years_since_year_1 (int year) +{ +return (year /4 -centuries_since_1700 (year,1 ) + +centuries_since_1700 (year,4 )); +} +void headers_init (void ) {int i, wd;
Signature · 1 line
1.8.2.2
PATCH 3/16 cal: clean up use of constants
In reply to Sami KerolaThe commit also adds few new symbolic names, such as DAYS_IN_WEEK. While these definitions may look a little too trivial to have I am sure they ease understanding what is going on in code where they are used.
--- misc-utils/cal.c | 141 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 81 insertions(+), 60 deletions(-)
misc-utils/cal.c +81 -60
@@ -151,15 +151,50 @@ char *Hrow; /* pointer to highlighted row in month */#error NUM_MONTHS must be 1 or 3#endif -#define THURSDAY 4/* for reformation */ -#define SATURDAY 6/* 1 Jan 1 was a Saturday */ +enum { +SUNDAY =0 , +MONDAY , +TUESDAY , +WEDNESDAY , +THURSDAY , +FRIDAY , +SATURDAY , +DAYS_IN_WEEK +}; +#define FIRST_WEEKDAY SATURDAY/* Jan 1st, 1 was a Saturday */ +#define REFORMATION_WEEKDAY THURSDAY/* after reformation it was Thursday */ +#define REFORMATION_YEAR 1752/* Signed-off-by: Lord Chesterfield */ +#define REFORMATION_MONTH 9/* September */ #define FIRST_MISSING_DAY 639799/* 3 Sep 1752 */ #define NUMBER_MISSING_DAYS 11/* 11 day correction */ +#define DAYS_IN_YEAR 365/* the common case, leap years are calculated */ +#define MONTHS_IN_YEAR 12 +#define DAYS_IN_MONTH 31#define MAXDAYS 42/* slots in a month array */ #define SPACE -1/* used in day array */ +#define SMALLEST_YEAR 1 +#define GREATEST_YEAR 9999 + +#define DAY_LEN 3/* 3 spaces per day */ +#define WEEK_LEN (DAYS_IN_WEEK * DAY_LEN) +#define HEAD_SEP 2 + +#define J_DAY_LEN 4/* 4 spaces per day */ +#define J_WEEK_LEN (DAYS_IN_WEEK * J_DAY_LEN) +#define J_HEAD_SEP 2 + +#define TODAY_FLAG 0x400/* flag day for highlighting */ + +#define FMT_ST_LINES 8 +#define FMT_ST_CHARS 300/* 90 suffices in most locales */ +struct fmt_st +{ +char s[FMT_ST_LINES ][FMT_ST_CHARS ]; +}; +static int days_in_month[2 ][13 ] = { {0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31 }, {0 ,31 ,29 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31 },
@@ -195,33 +230,19 @@ int sep1752[MAXDAYS+6] = { SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE }; -#define DAY_LEN 3/* 3 spaces per day */ -#define J_DAY_LEN 4/* 4 spaces per day */ -#define WEEK_LEN 21/* 7 days * 3 characters */ -#define J_WEEK_LEN 28/* 7 days * 4 characters */ -#define HEAD_SEP 2/* spaces between day headings */ -#define J_HEAD_SEP 2/* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */ char day_headings[WEEK_LEN *6 +1 ];/* weekstart = 1 => " M Tu W Th F S S " */ char j_day_headings[J_WEEK_LEN *6 +1 ];/* weekstart = 1 => " M Tu W Th F S S " */ -const char *full_month[12 ]; +const char *full_month[MONTHS_IN_YEAR ];/* 0 => sunday, 1 => monday */ -int weekstart=0 ; +int weekstart =SUNDAY ;int julian; -#define TODAY_FLAG 0x400/* flag day for highlighting */ - -#define FMT_ST_LINES 8 -#define FMT_ST_CHARS 300/* 90 suffices in most locales */ -struct fmt_st -{ -char s[FMT_ST_LINES ][FMT_ST_CHARS ]; -}; - +/* function prototypes */ static int leap_year (int year);static int centuries_since_1700 (int year,int centuries);static int leap_years_since_year_1 (int year);
@@ -244,7 +265,7 @@ int main(int argc, char **argv) { struct tm *local_time;time_t now; -int ch, day, month, year, yflag; +int ch, day =0 , month =0 , year =0 , yflag =0 ;int num_months =NUM_MONTHS ; static const struct option longopts[] = {
@@ -267,7 +288,7 @@ main(int argc, char **argv) {#if defined(HAVE_LIBNCURSES ) || defined(HAVE_LIBNCURSESW ) || defined(HAVE_LIBTERMCAP )if ((term =getenv ("TERM" ))) {int ret; -my_setupterm (term,1 , &ret); +my_setupterm (term,STDOUT_FILENO , &ret);if (ret >0 ) { Senter =my_tgetstr ("so" ,"smso" ); Sexit =my_tgetstr ("se" ,"rmso" );
@@ -307,11 +328,10 @@ main(int argc, char **argv) { wfd = val.word; wfd =day_in_week (wfd %100 , (wfd /100 ) %100 , wfd / (100 *100 )); - weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7; + weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK; }#endif - yflag =0 ;while ((ch =getopt_long (argc, argv,"13mjsyVh" , longopts,NULL )) !=-1 )switch (ch) {case '1' :
@@ -321,10 +341,10 @@ main(int argc, char **argv) { num_months =3 ;break ;case 's' : - weekstart =0 ;/* default */ + weekstart =SUNDAY ;/* default */ break ;case 'm' : - weekstart =1 ; + weekstart =MONDAY ;break ;case 'j' : julian =1 ;
@@ -347,21 +367,20 @@ main(int argc, char **argv) {time (&now); local_time =localtime (&now); - day = month = year =0 ;switch (argc) {case 3 : day =strtos32_or_err (*argv++,_ ("illegal day value" )); -if (day <1 ||31 < day) -errx (EXIT_FAILURE ,_ ("illegal day value: use 1-%d" ),31 ); +if (day <1 ||DAYS_IN_MONTH < day) +errx (EXIT_FAILURE ,_ ("illegal day value: use 1-%d" ),DAYS_IN_MONTH );/* FALLTHROUGH */ case 2 : month =strtos32_or_err (*argv++,_ ("illegal month value: use 1-12" )); -if (month <1 ||12 < month) +if (month <1 ||MONTHS_IN_YEAR < month)errx (EXIT_FAILURE ,_ ("illegal month value: use 1-12" ));/* FALLTHROUGH */ case 1 : year =strtos32_or_err (*argv++,_ ("illegal year value: use 1-9999" )); -if (year <1 ||9999 < year) +if (year <SMALLEST_YEAR ||GREATEST_YEAR < year)errx (EXIT_FAILURE ,_ ("illegal year value: use 1-9999" ));if (day) {int dm = days_in_month[leap_year (year)][month];
@@ -384,7 +403,7 @@ main(int argc, char **argv) { }headers_init (); -if (!isatty (1 )) +if (!isatty (STDOUT_FILENO )) day =0 ;/* don't highlight */ if (yflag && julian)
@@ -402,7 +421,7 @@ main(int argc, char **argv) {/* leap year -- account for gregorian reformation in 1752 */ static int leap_year (int year) { -if (year <=1752 ) +if (year <=REFORMATION_YEAR )return !(year %4 );else return ( !(year %4 ) && (year %100 ) ) || !(year %400 );
@@ -411,10 +430,10 @@ static int leap_year(int year)/* number of centuries since 1700 */ static int centuries_since_1700 (int year,int n) { -if (year <1700 ) +if (year <REFORMATION_YEAR )return 0 ;else -return ((year / (100 * n)) - (17 / n)); +return ((year / (100 * n)) - ((REFORMATION_YEAR /100 ) / n)); }/* number of leap years between year 1 and this year, not inclusive */
@@ -432,9 +451,9 @@ void headers_init(void)strcpy (day_headings,"" );strcpy (j_day_headings,"" ); -for (i =0 ; i <7 ; i++) { +for (i =0 ; i <DAYS_IN_WEEK ; i++) {ssize_t space_left; - wd = (i + weekstart) %7 ; + wd = (i + weekstart) %DAYS_IN_WEEK ;if (i)strcat (cur_dh++," " );
@@ -457,7 +476,7 @@ void headers_init(void) space_left, 3); } -for (i =0 ; i <12 ; i++) +for (i =0 ; i <MONTHS_IN_YEAR ; i++) full_month[i] =nl_langinfo (MON_1 + i); }
@@ -481,10 +500,10 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {snprintf (out->s[1 ],FMT_ST_CHARS ,"%s" , julian ? j_day_headings : day_headings); -for (row =0 ; row <6 ; row++) { +for (row =0 ; row <DAYS_IN_WEEK -1 ; row++) {int has_hl =0 ; -for (col =0 , p = lineout; col <7 ; col++) { -int xd = days[row *7 + col]; +for (col =0 , p = lineout; col <DAYS_IN_WEEK ; col++) { +int xd = days[row *DAYS_IN_WEEK + col];if (xd !=SPACE && (xd &TODAY_FLAG )) has_hl =1 ; p =ascii_day (p, xd);
@@ -521,13 +540,13 @@ monthly3(int day, int month, int year) {int next_month, next_year;if (month ==1 ) { - prev_month =12 ; + prev_month =MONTHS_IN_YEAR ; prev_year = year -1 ; }else { prev_month = month -1 ; prev_year = year; } -if (month ==12 ) { +if (month ==MONTHS_IN_YEAR ) { next_month =1 ; next_year = year +1 ; }else {
@@ -567,29 +586,29 @@ monthly3(int day, int month, int year) {void j_yearly (int day,int year) {int col, *dp, i, month, row, which_cal; -int days[12 ][MAXDAYS ]; +int days[MONTHS_IN_YEAR ][MAXDAYS ];char *p, lineout[80 ];snprintf (lineout,sizeof (lineout),"%d" , year);center (lineout,J_WEEK_LEN *2 +J_HEAD_SEP -1 ,0 );my_putstring ("\n\n" ); -for (i =0 ; i <12 ; i++) +for (i =0 ; i <MONTHS_IN_YEAR ; i++)day_array (day, i +1 , year, days[i]);memset (lineout,' ' ,sizeof (lineout) -1 ); lineout[sizeof (lineout) -1 ] ='\0' ; -for (month =0 ; month <12 ; month +=2 ) { +for (month =0 ; month <MONTHS_IN_YEAR ; month +=2 ) {center (full_month[month],J_WEEK_LEN -1 ,J_HEAD_SEP +1 );center (full_month[month +1 ],J_WEEK_LEN -1 ,0 );snprintf (lineout,sizeof (lineout),"\n%s%*s %s\n" , j_day_headings,J_HEAD_SEP ,"" , j_day_headings);my_putstring (lineout); -for (row =0 ; row <6 ; row++) { +for (row =0 ; row <DAYS_IN_WEEK -1 ; row++) { p = lineout;for (which_cal =0 ; which_cal <2 ; which_cal++) { - dp = &days[month + which_cal][row *7 ]; -for (col =0 ; col <7 ; col++) + dp = &days[month + which_cal][row *DAYS_IN_WEEK ]; +for (col =0 ; col <DAYS_IN_WEEK ; col++) p =ascii_day (p, *dp++); p +=sprintf (p," " ); }
@@ -605,18 +624,18 @@ j_yearly(int day, int year) {void yearly (int day,int year) {int col, *dp, i, month, row, which_cal; -int days[12 ][MAXDAYS ]; +int days[MONTHS_IN_YEAR ][MAXDAYS ];char *p, lineout[100 ];snprintf (lineout,sizeof (lineout),"%d" , year);center (lineout,WEEK_LEN *3 +HEAD_SEP *2 -1 ,0 );my_putstring ("\n\n" ); -for (i =0 ; i <12 ; i++) +for (i =0 ; i <MONTHS_IN_YEAR ; i++)day_array (day, i +1 , year, days[i]);memset (lineout,' ' ,sizeof (lineout) -1 ); lineout[sizeof (lineout) -1 ] ='\0' ; -for (month =0 ; month <12 ; month +=3 ) { +for (month =0 ; month <MONTHS_IN_YEAR ; month +=3 ) {center (full_month[month],WEEK_LEN -1 ,HEAD_SEP +1 );center (full_month[month +1 ],WEEK_LEN -1 ,HEAD_SEP +1 );center (full_month[month +2 ],WEEK_LEN -1 ,0 );
@@ -624,11 +643,11 @@ yearly(int day, int year) {"\n%s%*s %s%*s %s\n" , day_headings,HEAD_SEP , "", day_headings, HEAD_SEP, "", day_headings);my_putstring (lineout); -for (row =0 ; row <6 ; row++) { +for (row =0 ; row <DAYS_IN_WEEK -1 ; row++) { p = lineout;for (which_cal =0 ; which_cal <3 ; which_cal++) { - dp = &days[month + which_cal][row *7 ]; -for (col =0 ; col <7 ; col++) + dp = &days[month + which_cal][row *DAYS_IN_WEEK ]; +for (col =0 ; col <DAYS_IN_WEEK ; col++) p =ascii_day (p, *dp++); p +=sprintf (p," " ); }
@@ -653,7 +672,7 @@ day_array(int day, int month, int year, int *days) {int julday, daynum, dw, dm;int *d_sep1752; -if (month ==9 && year ==1752 ) { +if (month ==REFORMATION_MONTH && year ==REFORMATION_YEAR ) {int sep1752_ofs = (weekstart +SEP1752_OFS ) %7 ; d_sep1752 = julian ? j_sep1752 : sep1752;memcpy (days, d_sep1752 + sep1752_ofs,MAXDAYS *sizeof (int ));
@@ -664,7 +683,7 @@ day_array(int day, int month, int year, int *days) { }memcpy (days, empty,MAXDAYS *sizeof (int )); dm = days_in_month[leap_year (year)][month]; - dw = (day_in_week (1 , month, year) - weekstart +7 ) %7 ; + dw = (day_in_week (1 , month, year) - weekstart +DAYS_IN_WEEK ) %DAYS_IN_WEEK ; julday =day_in_year (1 , month, year); daynum = julian ? julday :1 ;while (dm--) {
@@ -700,13 +719,15 @@ int day_in_week(int day, int month, int year) { long temp; - temp = (long )(year -1 ) *365 +leap_years_since_year_1 (year -1 ) + temp = + (long )(year -SMALLEST_YEAR ) *DAYS_IN_YEAR + +leap_years_since_year_1 (year -SMALLEST_YEAR ) +day_in_year (day, month, year);if (temp <FIRST_MISSING_DAY ) -return ((temp -1 +SATURDAY ) %7 ); +return ((temp -1 +FIRST_WEEKDAY ) %DAYS_IN_WEEK );if (temp >= (FIRST_MISSING_DAY +NUMBER_MISSING_DAYS )) -return (((temp -1 +SATURDAY ) -NUMBER_MISSING_DAYS ) %7 ); -return (THURSDAY ); +return (((temp -1 +FIRST_WEEKDAY ) -NUMBER_MISSING_DAYS ) %DAYS_IN_WEEK ); +return (REFORMATION_WEEKDAY ); } char *
Signature · 1 line
1.8.2.2
PATCH 4/16 tests: add calendar reformation check
In reply to Sami Kerola--- tests/expected/cal/sep1752 | 198 +++++++++++++++++++++++++++++++++++++++++++++ tests/ts/cal/sep1752 | 95 ++++++++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 tests/expected/cal/sep1752 create mode 100755 tests/ts/cal/sep1752
tests/expected/cal/sep1752 new file +198 -0
@@ -0,0 +1,198 @@ + +Gregorian - Monday-based month + September 1752 +Mo Tu We Th Fr Sa Su + 1 2 14 15 16 17 +18 19 20 21 22 23 24 +25 26 27 28 29 30 + + + +Gregorian - Sunday-based month + September 1752 +Su Mo Tu We Th Fr Sa + 1 2 14 15 16 +17 18 19 20 21 22 23 +24 25 26 27 28 29 30 + + + +Julian - Monday-based month + September 1752 +Mon Tue Wed Thu Fri Sat Sun + 245 246 258 259 260 261 +262 263 264 265 266 267 268 +269 270 271 272 273 274 + + + +Julian - Sunday-based month + September 1752 +Sun Mon Tue Wed Thu Fri Sat + 245 246 258 259 260 +261 262 263 264 265 266 267 +268 269 270 271 272 273 274 + + + +Gregorian - Monday-based three months + August 1752 September 1752 October 1752 +Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su + 1 2 1 2 14 15 16 17 1 + 3 4 5 6 7 8 9 18 19 20 21 22 23 24 2 3 4 5 6 7 8 +10 11 12 13 14 15 16 25 26 27 28 29 30 9 10 11 12 13 14 15 +17 18 19 20 21 22 23 16 17 18 19 20 21 22 +24 25 26 27 28 29 30 23 24 25 26 27 28 29 +31 30 31 +Gregorian - Sunday-based three months + August 1752 September 1752 October 1752 +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 1 2 14 15 16 1 2 3 4 5 6 7 + 2 3 4 5 6 7 8 17 18 19 20 21 22 23 8 9 10 11 12 13 14 + 9 10 11 12 13 14 15 24 25 26 27 28 29 30 15 16 17 18 19 20 21 +16 17 18 19 20 21 22 22 23 24 25 26 27 28 +23 24 25 26 27 28 29 29 30 31 +30 31 +Julian - Monday-based three months + August 1752 September 1752 October 1752 +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 214 215 245 246 258 259 260 261 275 +216 217 218 219 220 221 222 262 263 264 265 266 267 268 276 277 278 279 280 281 282 +223 224 225 226 227 228 229 269 270 271 272 273 274 283 284 285 286 287 288 289 +230 231 232 233 234 235 236 290 291 292 293 294 295 296 +237 238 239 240 241 242 243 297 298 299 300 301 302 303 +244 304 305 +Julian - Sunday-based three months + August 1752 September 1752 October 1752 +Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat + 214 245 246 258 259 260 275 276 277 278 279 280 281 +215 216 217 218 219 220 221 261 262 263 264 265 266 267 282 283 284 285 286 287 288 +222 223 224 225 226 227 228 268 269 270 271 272 273 274 289 290 291 292 293 294 295 +229 230 231 232 233 234 235 296 297 298 299 300 301 302 +236 237 238 239 240 241 242 303 304 305 +243 244 +Gregorian - Monday-based year + 1752 + + January February March +Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su + 1 2 3 4 5 1 2 1 + 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8 +13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15 +20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22 +27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29 + 30 31 + April May June +Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su + 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7 + 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14 +13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21 +20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28 +27 28 29 30 25 26 27 28 29 30 31 29 30 + + July August September +Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su + 1 2 3 4 5 1 2 1 2 14 15 16 17 + 6 7 8 9 10 11 12 3 4 5 6 7 8 9 18 19 20 21 22 23 24 +13 14 15 16 17 18 19 10 11 12 13 14 15 16 25 26 27 28 29 30 +20 21 22 23 24 25 26 17 18 19 20 21 22 23 +27 28 29 30 31 24 25 26 27 28 29 30 + 31 + October November December +Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su + 1 1 2 3 4 5 1 2 3 + 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10 + 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17 +16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24 +23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31 +30 31 + +Gregorian - Sunday-based year + 1752 + + January February March +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 1 1 2 3 4 5 6 7 + 5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14 +12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21 +19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28 +26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31 + + April May June +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 1 2 1 2 3 4 5 6 + 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 +12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 +19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 +26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 + 31 + July August September +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 1 1 2 14 15 16 + 5 6 7 8 9 10 11 2 3 4 5 6 7 8 17 18 19 20 21 22 23 +12 13 14 15 16 17 18 9 10 11 12 13 14 15 24 25 26 27 28 29 30 +19 20 21 22 23 24 25 16 17 18 19 20 21 22 +26 27 28 29 30 31 23 24 25 26 27 28 29 + 30 31 + October November December +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 7 1 2 3 4 1 2 + 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 +15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 +22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 +29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 + 31 + +Julian - Monday-based year + 1752 + + January February +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 1 2 3 4 5 32 33 + 6 7 8 9 10 11 12 34 35 36 37 38 39 40 + 13 14 15 16 17 18 19 41 42 43 44 45 46 47 + 20 21 22 23 24 25 26 48 49 50 51 52 53 54 + 27 28 29 30 31 55 56 57 58 59 60 + + March April +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 61 92 93 94 95 96 + 62 63 64 65 66 67 68 97 98 99 100 101 102 103 + 69 70 71 72 73 74 75 104 105 106 107 108 109 110 + 76 77 78 79 80 81 82 111 112 113 114 115 116 117 + 83 84 85 86 87 88 89 118 119 120 121 + 90 91 + May June +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 122 123 124 153 154 155 156 157 158 159 +125 126 127 128 129 130 131 160 161 162 163 164 165 166 +132 133 134 135 136 137 138 167 168 169 170 171 172 173 +139 140 141 142 143 144 145 174 175 176 177 178 179 180 +146 147 148 149 150 151 152 181 182 + + July August +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 183 184 185 186 187 214 215 +188 189 190 191 192 193 194 216 217 218 219 220 221 222 +195 196 197 198 199 200 201 223 224 225 226 227 228 229 +202 203 204 205 206 207 208 230 231 232 233 234 235 236 +209 210 211 212 213 237 238 239 240 241 242 243 + 244 + September October +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 245 246 258 259 260 261 275 +262 263 264 265 266 267 268 276 277 278 279 280 281 282 +269 270 271 272 273 274 283 284 285 286 287 288 289 + 290 291 292 293 294 295 296 + 297 298 299 300 301 302 303 + 304 305 + November December +Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun + 306 307 308 309 310 336 337 338 +311 312 313 314 315 316 317 339 340 341 342 343 344 345 +318 319 320 321 322 323 324 346 347 348 349 350 351 352 +325 326 327 328 329 330 331 353 354 355 356 357 358 359 +332 333 334 335 360 361 362 363 364 365 366 + + +Julian - Sunday-based year
tests/ts/cal/sep1752 new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
1.8.2.2
PATCH 5/16 cal: simplify calendar reformat calculations
In reply to Sami KerolaThe only September 1752 offset calculation that is necessary is whether Sun or Mon is the first day of the week.
--- misc-utils/cal.c | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-)
misc-utils/cal.c +15 -26
@@ -200,27 +200,16 @@ static int days_in_month[2][13] = { {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; -#define SEP1752_OFS 4/* sep1752[4] is a Sunday */ - -/* 1 Sep 1752 is represented by sep1752[6] and j_sep1752[6] */ -int sep1752[MAXDAYS +6 ] = { -SPACE ,SPACE ,SPACE ,SPACE , +/* September 1752 is special, and has static assignments for both date +* and Julian representations. */ +int d_sep1752[MAXDAYS /2 ] = {SPACE ,SPACE ,1 ,2 ,14 ,15 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 , -24 ,25 ,26 ,27 ,28 ,29 ,30 , -SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , -SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , -SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , -SPACE ,SPACE -}, j_sep1752[MAXDAYS +6 ] = { -SPACE ,SPACE ,SPACE ,SPACE , +24 ,25 ,26 ,27 ,28 ,29 ,30 +}, j_sep1752[MAXDAYS /2 ] = {SPACE ,SPACE ,245 ,246 ,258 ,259 ,260 ,261 ,262 ,263 ,264 ,265 ,266 ,267 , -268 ,269 ,270 ,271 ,272 ,273 ,274 , -SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , -SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , -SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , -SPACE ,SPACE +268 ,269 ,270 ,271 ,272 ,273 ,274 }, empty[MAXDAYS ] = {SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE ,SPACE , SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
@@ -670,18 +659,18 @@ yearly(int day, int year) {void day_array (int day,int month,int year,int *days) {int julday, daynum, dw, dm; -int *d_sep1752; - -if (month ==REFORMATION_MONTH && year ==REFORMATION_YEAR ) { -int sep1752_ofs = (weekstart +SEP1752_OFS ) %7 ; - d_sep1752 = julian ? j_sep1752 : sep1752; -memcpy (days, d_sep1752 + sep1752_ofs,MAXDAYS *sizeof (int )); -for (dm=0 ; dm<MAXDAYS ; dm++) -if (j_sep1752[dm + sep1752_ofs] == day) +int *sep1752; + +memcpy (days, empty,MAXDAYS *sizeof (int )); +if (year ==REFORMATION_YEAR && month ==REFORMATION_MONTH ) { + sep1752 = julian ? j_sep1752 : d_sep1752; +memcpy (days, sep1752 + weekstart, + ((MAXDAYS /2 ) - weekstart) *sizeof (int )); +for (dm =0 ; dm <MAXDAYS /2 ; dm++) +if (j_sep1752[dm] == day) days[dm] |=TODAY_FLAG ;return ; } -memcpy (days, empty,MAXDAYS *sizeof (int )); dm = days_in_month[leap_year (year)][month]; dw = (day_in_week (1 , month, year) - weekstart +DAYS_IN_WEEK ) %DAYS_IN_WEEK ; julday =day_in_year (1 , month, year);
Signature · 1 line
1.8.2.2
PATCH 6/16 cal: remove unnecessary initializations
In reply to Sami Kerola--- misc-utils/cal.c | 7 ------- 1 file changed, 7 deletions(-)
misc-utils/cal.c +0 -7
@@ -437,9 +437,6 @@ void headers_init(void)int i, wd;char *cur_dh = day_headings, *cur_j_dh = j_day_headings; -strcpy (day_headings,"" ); -strcpy (j_day_headings,"" ); -for (i =0 ; i <DAYS_IN_WEEK ; i++) {ssize_t space_left; wd = (i + weekstart) %DAYS_IN_WEEK ;
@@ -584,8 +581,6 @@ j_yearly(int day, int year) {for (i =0 ; i <MONTHS_IN_YEAR ; i++)day_array (day, i +1 , year, days[i]); -memset (lineout,' ' ,sizeof (lineout) -1 ); - lineout[sizeof (lineout) -1 ] ='\0' ;for (month =0 ; month <MONTHS_IN_YEAR ; month +=2 ) {center (full_month[month],J_WEEK_LEN -1 ,J_HEAD_SEP +1 );center (full_month[month +1 ],J_WEEK_LEN -1 ,0 );
@@ -622,8 +617,6 @@ yearly(int day, int year) {for (i =0 ; i <MONTHS_IN_YEAR ; i++)day_array (day, i +1 , year, days[i]); -memset (lineout,' ' ,sizeof (lineout) -1 ); - lineout[sizeof (lineout) -1 ] ='\0' ;for (month =0 ; month <MONTHS_IN_YEAR ; month +=3 ) {center (full_month[month],WEEK_LEN -1 ,HEAD_SEP +1 );center (full_month[month +1 ],WEEK_LEN -1 ,HEAD_SEP +1 );
Signature · 1 line
1.8.2.2
PATCH 7/16 cal: de-duplicate julian specific functions
In reply to Sami KerolaThe cost is a little bit more complex functions, while the benefit is couple data flows fewer to mind about.
--- misc-utils/cal.c | 119 +++++++++++++++++++++---------------------------------- 1 file changed, 45 insertions(+), 74 deletions(-)
misc-utils/cal.c +45 -74
@@ -181,10 +181,12 @@ enum {#define DAY_LEN 3/* 3 spaces per day */ #define WEEK_LEN (DAYS_IN_WEEK * DAY_LEN)#define HEAD_SEP 2 +#define MONTH_COLS 3/* month columns in year view */ #define J_DAY_LEN 4/* 4 spaces per day */ #define J_WEEK_LEN (DAYS_IN_WEEK * J_DAY_LEN)#define J_HEAD_SEP 2 +#define J_MONTH_COLS 2#define TODAY_FLAG 0x400/* flag day for highlighting */
@@ -221,9 +223,7 @@ static int days_in_month[2][13] = {/* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */ -char day_headings[WEEK_LEN *6 +1 ]; -/* weekstart = 1 => " M Tu W Th F S S " */ -char j_day_headings[J_WEEK_LEN *6 +1 ]; +char day_headings[J_WEEK_LEN *6 +1 ];/* weekstart = 1 => " M Tu W Th F S S " */ const char *full_month[MONTHS_IN_YEAR ];
@@ -241,14 +241,13 @@ void center(const char *, size_t, int);void day_array (int ,int ,int ,int *);int day_in_week (int ,int ,int );int day_in_year (int ,int ,int ); -void yearly (int ,int ); -void j_yearly (int ,int ); +void yearly (int ,int ,int );void do_monthly (int ,int ,int ,struct fmt_st *);void monthly (int ,int ,int );void monthly3 (int ,int ,int );void trim_trailing_spaces (char *);static void __attribute__ ((__noreturn__))usage (FILE * out); -void headers_init (void ); +void headers_init (int );int main (int argc,char **argv) {
@@ -390,15 +389,13 @@ main(int argc, char **argv) {default :usage (stderr); } -headers_init (); +headers_init (julian);if (!isatty (STDOUT_FILENO )) day =0 ;/* don't highlight */ -if (yflag && julian) -j_yearly (day, year); -else if (yflag) -yearly (day, year); +if (yflag) +yearly (day, year, julian);else if (num_months ==1 )monthly (day, month, year);else if (num_months ==3 )
@@ -432,10 +429,10 @@ static int leap_years_since_year_1(int year) centuries_since_1700(year, 4)); } -void headers_init (void ) +void headers_init (int julian) { -int i, wd; -char *cur_dh = day_headings, *cur_j_dh = j_day_headings; +int i, wd, spaces = julian ?J_DAY_LEN -1 :DAY_LEN -1 ; +char *cur_dh = day_headings;for (i =0 ; i <DAYS_IN_WEEK ; i++) {ssize_t space_left;
@@ -445,21 +442,11 @@ void headers_init(void)strcat (cur_dh++," " ); space_left =sizeof (day_headings) - (cur_dh - day_headings); -if (space_left <=2 ) +if (space_left <= spaces)break ; cur_dh +=center_str (nl_langinfo (ABDAY_1 + wd), cur_dh, - space_left,2 ); - -if (i) -strcat (cur_j_dh++," " ); - space_left = -sizeof (j_day_headings) - (cur_j_dh - j_day_headings); -if (space_left <=3 ) -break ; - cur_j_dh += -center_str (nl_langinfo (ABDAY_1 + wd), cur_j_dh, - space_left,3 ); + space_left, spaces); }for (i =0 ; i <MONTHS_IN_YEAR ; i++)
@@ -484,8 +471,7 @@ do_monthly(int day, int month, int year, struct fmt_st *out) { full_month[month - 1], year);center_str (lineout, out->s[0 ],ARRAY_SIZE (out->s[0 ]), width); -snprintf (out->s[1 ],FMT_ST_CHARS ,"%s" , - julian ? j_day_headings : day_headings); +snprintf (out->s[1 ],FMT_ST_CHARS ,"%s" , day_headings);for (row =0 ; row <DAYS_IN_WEEK -1 ; row++) {int has_hl =0 ;for (col =0 , p = lineout; col <DAYS_IN_WEEK ; col++) {
@@ -570,64 +556,49 @@ monthly3(int day, int month, int year) { }void -j_yearly (int day,int year) { -int col, *dp, i, month, row, which_cal; -int days[MONTHS_IN_YEAR ][MAXDAYS ]; -char *p, lineout[80 ]; - -snprintf (lineout,sizeof (lineout),"%d" , year); -center (lineout,J_WEEK_LEN *2 +J_HEAD_SEP -1 ,0 ); -my_putstring ("\n\n" ); - -for (i =0 ; i <MONTHS_IN_YEAR ; i++) -day_array (day, i +1 , year, days[i]); -for (month =0 ; month <MONTHS_IN_YEAR ; month +=2 ) { -center (full_month[month],J_WEEK_LEN -1 ,J_HEAD_SEP +1 ); -center (full_month[month +1 ],J_WEEK_LEN -1 ,0 ); -snprintf (lineout,sizeof (lineout), -"\n%s%*s %s\n" , j_day_headings,J_HEAD_SEP ,"" , - j_day_headings); -my_putstring (lineout); -for (row =0 ; row <DAYS_IN_WEEK -1 ; row++) { - p = lineout; -for (which_cal =0 ; which_cal <2 ; which_cal++) { - dp = &days[month + which_cal][row *DAYS_IN_WEEK ]; -for (col =0 ; col <DAYS_IN_WEEK ; col++) - p =ascii_day (p, *dp++); - p +=sprintf (p," " ); - } - *p ='\0' ; -trim_trailing_spaces (lineout); -my_putstring (lineout); -my_putstring ("\n" ); - } - } -my_putstring ("\n" ); -} - -void -yearly (int day,int year) { +yearly (int day,int year,int julian) {int col, *dp, i, month, row, which_cal; +int maxrow, sep_len, week_len;int days[MONTHS_IN_YEAR ][MAXDAYS ];char *p, lineout[100 ]; +if (julian) { + maxrow =J_MONTH_COLS ; + sep_len =J_HEAD_SEP ; + week_len =J_WEEK_LEN ; + }else { + maxrow =MONTH_COLS ; + sep_len =HEAD_SEP ; + week_len =WEEK_LEN ; + }snprintf (lineout,sizeof (lineout),"%d" , year); -center (lineout,WEEK_LEN *3 +HEAD_SEP *2 -1 ,0 ); +/* 2013-04-28: The -1 near sep_len makes year header to be +* aligned exactly how it has been aligned for long time, but it +* is unexplainable. */ +center (lineout, (week_len + sep_len) * maxrow - sep_len -1 ,0 );my_putstring ("\n\n" );for (i =0 ; i <MONTHS_IN_YEAR ; i++)day_array (day, i +1 , year, days[i]); -for (month =0 ; month <MONTHS_IN_YEAR ; month +=3 ) { -center (full_month[month],WEEK_LEN -1 ,HEAD_SEP +1 ); -center (full_month[month +1 ],WEEK_LEN -1 ,HEAD_SEP +1 ); -center (full_month[month +2 ],WEEK_LEN -1 ,0 ); -snprintf (lineout,sizeof (lineout), -"\n%s%*s %s%*s %s\n" , day_headings,HEAD_SEP , -"" , day_headings,HEAD_SEP ,"" , day_headings); +for (month =0 ; month <MONTHS_IN_YEAR ; month += maxrow) { +center (full_month[month], week_len -1 , sep_len +1 ); +if (julian) { +center (full_month[month +1 ], week_len -1 ,0 ); + }else { +center (full_month[month +1 ], week_len -1 , sep_len +1 ); +center (full_month[month +2 ], week_len -1 ,0 ); + } +if (julian) +snprintf (lineout,sizeof (lineout), +"\n%s%*s %s\n" , day_headings, sep_len,"" , day_headings); +else +snprintf (lineout,sizeof (lineout), +"\n%s%*s %s%*s %s\n" , day_headings, sep_len, +"" , day_headings, sep_len,"" , day_headings);my_putstring (lineout);for (row =0 ; row <DAYS_IN_WEEK -1 ; row++) { p = lineout; -for (which_cal =0 ; which_cal <3 ; which_cal++) { +for (which_cal =0 ; which_cal < maxrow; which_cal++) { dp = &days[month + which_cal][row *DAYS_IN_WEEK ];for (col =0 ; col <DAYS_IN_WEEK ; col++) p =ascii_day (p, *dp++);
Signature · 1 line
1.8.2.2
PATCH 8/16 lib: copy argmatch from gnulib
In reply to Sami KerolaBoth argmatch.h and argmatch.c are copied from gnulib. Biggest difference is that messaging does not use same quoting as the gnulib does. Other than that code is nearly untouched.
gnulib-commit: 92f3a4c8e52e64c233e260431d095dbf88554c14
--- include/Makemodule.am | 1 + include/argmatch.h | 100 +++++++++++++++++++ lib/Makemodule.am | 1 + lib/argmatch.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 374 insertions(+) create mode 100644 include/argmatch.h create mode 100644 lib/argmatch.c
include/Makemodule.am +1 -0
@@ -1,6 +1,7 @@ dist_noinst_HEADERS += \ include/all-io.h \ + include/argmatch.h \ include/at.h \ include/bitops.h \ include/blkdev.h \
include/argmatch.h new file +100 -0
100 lines not on this page · raw message
lib/Makemodule.am +1 -0
@@ -2,6 +2,7 @@ noinst_LTLIBRARIES += libcommon.la libcommon_la_CFLAGS = $(AM_CFLAGS) libcommon_la_SOURCES = \ + lib/argmatch.c \ lib/at.c \ lib/blkdev.c \ lib/canonicalize.c \
lib/argmatch.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
1.8.2.2
Karel Zak
maintainerRe: [PATCH 08/16] lib: copy argmatch from gnulib
Quoted 1 line · Sami Kerola
Both
argmatch.handargmatch.care copied from gnulib.
Ah no... this is overkill, all we need is to parse three words.
I have updated lib/colors.c and dmesg.c to support color modes 'auto', 'never' and 'always'. Please, use it also in cal.c.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Re: [PATCH 08/16] lib: copy argmatch from gnulib
Quoted 7 lines · Karel Zak
Quoted 1 line · Sami Kerola
Both
argmatch.handargmatch.care copied from gnulib.Ah no... this is overkill, all we need is to parse three words.
I have updated
lib/colors.canddmesg.cto support color modes 'auto', 'never' and 'always'. Please, use it also incal.c.
I thought there might be other uses to argmatch, but perhaps there isn't. I changed the cal to use lib/colors.c argument parsing in 'cal-again' branch.
https://github.com/kerolasa/lelux-utiliteetit/commit/a33225e623b389e49d05b665104dcdef17e01502
--
Sami Kerola
http://www.iki.fi/kerolasa/
PATCH 9/16 cal: add --highligth option which uses argmatch
In reply to Sami KerolaThis switch will allow an user to choose when highlighting is outputed. The default remains the same as it has been, e.g., highlighting is used depending on whether stdout is a terminal or not.
--- misc-utils/Makemodule.am | 2 +- misc-utils/cal.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-)
misc-utils/Makemodule.am +1 -1
@@ -10,7 +10,7 @@ if !HAVE_LANGINFO cal_SOURCES += lib/langinfo.c endif -cal_LDADD = $(LDADD) +cal_LDADD = $(LDADD) libcommon.la if HAVE_TINFO cal_LDADD += -ltinfo @NCURSES_LIBS@
misc-utils/cal.c +38 -2
@@ -66,6 +66,7 @@#include <unistd.h> #include <errno.h> +#include "argmatch.h" #include "c.h" #include "closestream.h" #include "nls.h"
@@ -256,6 +257,24 @@ main(int argc, char **argv) {int ch, day =0 , month =0 , year =0 , yflag =0 ;int num_months =NUM_MONTHS ; +enum Highlight_type { +HIGHLIGHT_UNUSED , +HIGHLIGHT_NEVER , +HIGHLIGHT_AUTO , +HIGHLIGHT_ALWAYS + }; +int highlight =HIGHLIGHT_AUTO ; +static char const *const highlight_type_string[] = { +"never" ,"auto" ,"always" ,NULL + }; +static enum Highlight_type const highlight_type[] = { +HIGHLIGHT_NEVER ,HIGHLIGHT_AUTO ,HIGHLIGHT_ALWAYS + }; + +enum { +OPT_HIGHLIGHT =CHAR_MAX +1 + }; +static const struct option longopts[] = { {"one" , no_argument,NULL ,'1' }, {"three" , no_argument,NULL ,'3' },
@@ -263,6 +282,7 @@ main(int argc, char **argv) { {"monday", no_argument, NULL, 'm'}, {"julian", no_argument, NULL, 'j'}, {"year", no_argument, NULL, 'y'}, + {"highlight", required_argument, NULL, OPT_HIGHLIGHT}, {"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}
@@ -340,6 +360,9 @@ main(int argc, char **argv) {case 'y' : yflag =1 ;break ; +case OPT_HIGHLIGHT : + highlight =XARGMATCH ("--highlight" , optarg, highlight_type_string, highlight_type); +break ;case 'V' :printf (UTIL_LINUX_VERSION );return EXIT_SUCCESS ;
@@ -391,8 +414,21 @@ main(int argc, char **argv) { }headers_init (julian); -if (!isatty (STDOUT_FILENO )) - day =0 ;/* don't highlight */ +switch (highlight) { +case HIGHLIGHT_NEVER : + day =0 ; +break ; +case HIGHLIGHT_AUTO : +if (!isatty (STDOUT_FILENO )) + day =0 ;/* don't highlight */ +break ; +case HIGHLIGHT_ALWAYS : +if (day ==0 ) + day = local_time->tm_yday +1 ;; +break ; +default : +abort (); + }if (yflag)yearly (day, year, julian);
Signature · 1 line
1.8.2.2
Re: [PATCH 09/16] cal: add --highligth option which uses argmatch
Quoted 62 lines · Sami Kerola
This switch will allow an user to choose when highlighting is outputed. The default remains the same as it has been, e.g., highlighting is used depending on whether stdout is a terminal or not.
CC Pádraig Brady <[email protected]>Signed-off-by Sami Kerola <[email protected]>--- misc-utils/Makemodule.am | 2 +- misc-utils/cal.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-)misc-utils/Makemodule.am +1
-1@@ -10,7 +10,7 @@ if !HAVE_LANGINFO cal_SOURCES += lib/langinfo.c endif -cal_LDADD = $(LDADD) +cal_LDADD = $(LDADD) libcommon.la if HAVE_TINFO cal_LDADD += -ltinfo @NCURSES_LIBS@misc-utils/cal.c +19
-0@@ -66,6 +66,7 @@#include <unistd.h> #include <errno.h> +#include "argmatch.h" #include "c.h" #include "closestream.h" #include "nls.h" @@ -256,6 +257,24 @@ main(int argc, char **argv) {int ch, day =0 , month =0 , year =0 , yflag =0 ;int num_months =NUM_MONTHS ; +enum Highlight_type { +HIGHLIGHT_UNUSED , +HIGHLIGHT_NEVER , +HIGHLIGHT_AUTO , +HIGHLIGHT_ALWAYS + }; +int highlight =HIGHLIGHT_AUTO ; +static char const *const highlight_type_string[] = { +"never" ,"auto" ,"always" ,NULL + }; +static enum Highlight_type const highlight_type[] = { +HIGHLIGHT_NEVER ,HIGHLIGHT_AUTO ,HIGHLIGHT_ALWAYS + }; + +enum { +OPT_HIGHLIGHT =CHAR_MAX +1 + }; +static const struct option longopts[] = { {"one" , no_argument,NULL ,'1' }, {"three" , no_argument,NULL ,'3' },
misc-utils/cal.c main(int argc, char **argv) { · line 285
Quoted 4 lines
misc-utils/cal.c +1
-0@@ -263,6 +282,7 @@ main(int argc, char **argv) { {"monday", no_argument, NULL, 'm'}, {"julian", no_argument, NULL, 'j'}, {"year", no_argument, NULL, 'y'}, + {"highlight", required_argument, NULL, OPT_HIGHLIGHT},
I'd have it as an optional arg, defaulting to auto.
Quoted 20 lines
misc-utils/cal.c +4
-2{"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}@@ -340,6 +360,9 @@ main(int argc, char **argv) {case 'y' : yflag =1 ;break ; +case OPT_HIGHLIGHT : + highlight =XARGMATCH ("--highlight" , optarg, highlight_type_string, highlight_type); +break ;case 'V' :printf (UTIL_LINUX_VERSION );return EXIT_SUCCESS ;@@ -391,8 +414,21 @@ main(int argc, char **argv) { }headers_init (julian); -if (!isatty (STDOUT_FILENO )) - day =0 ;/* don't highlight */ +switch (highlight) {
misc-utils/cal.c main(int argc, char **argv) { · line 427
Quoted 10 lines
misc-utils/cal.c +10
-0+case HIGHLIGHT_NEVER : + day =0 ; +break ; +case HIGHLIGHT_AUTO : +if (!isatty (STDOUT_FILENO )) + day =0 ;/* don't highlight */ +break ; +case HIGHLIGHT_ALWAYS : +if (day ==0 ) + day = local_time->tm_yday +1 ;;
s/;;/;/ shellism :)
misc-utils/cal.c main(int argc, char **argv) { · line 434
Quoted 8 lines
misc-utils/cal.c +4
-0+break ; +default : +abort (); + }if (yflag)yearly (day, year, julian);
s/highligth/highlight/ in the subject line
Also I'd s/--highlight/--color/ to align with dmesg and other utils. Only highlighting is done at present, but that could be set up to use color in the user's environment. Also in future other parts of the output could be colored etc.
BTW, dmesg could get the same support for specifying "always" and "never".
thanks!
Pádraig.
Re: [PATCH 09/16] cal: add --highligth option which uses argmatch
On 6 May 2013 01:11, Pádraig Brady <[email protected]> wrote:
Hi Pádraig,
Quoted 1 line
I'd have it as an optional arg, defaulting to auto.
Good point. That would result to more consistent interface, which is a virtue in itself.
Quoted 5 lines
misc-utils/cal.c main(int argc, char **argv) { · line 427
Quoted 3 lines
misc-utils/cal.c +3
-0+case HIGHLIGHT_ALWAYS : +if (day ==0 ) + day = local_time->tm_yday +1 ;;s/;;/;/ shellism :)
Oops, I will push a fix to my git within next 48 hours. I have slight problem with my programming environment at the moment which I need to sort out first.
Quoted 10 lines
misc-utils/cal.c main(int argc, char **argv) { · line 434
Quoted 8 lines
misc-utils/cal.c +4
-0+break ; +default : +abort (); + }if (yflag)yearly (day, year, julian);s/highligth/highlight/ in the subject line
Oh dear, me and my finglish.
Quoted 6 lines
Also I'd s/--highlight/--color/ to align with dmesg and other utils. Only highlighting is done at present, but that could be set up to use color in the user's environment. Also in future other parts of the output could be colored etc.
BTW, dmesg could get the same support for specifying "always" and "never".
Karel earlier mentioned the same, and I wrote some sort of change which will add --when to dmesg. Allowing user to choose dmesg colors etc highlights using facilities & priorities as criteria would probably be quite neat feature.
--
Sami Kerola
http://www.iki.fi/kerolasa/
Karel Zak
maintainerRe: [PATCH 09/16] cal: add --highligth option which uses argmatch
In reply to Sami Kerolamisc-utils/cal.c main(int argc, char **argv) { · line 285
Quoted 8 lines · Sami Kerola
misc-utils/cal.c +1
-0static const struct option longopts[] = { {"one" , no_argument,NULL ,'1' }, {"three" , no_argument,NULL ,'3' },@@ -263,6 +282,7 @@ main(int argc, char **argv) { {"monday", no_argument, NULL, 'm'}, {"julian", no_argument, NULL, 'j'}, {"year", no_argument, NULL, 'y'}, + {"highlight", required_argument, NULL, OPT_HIGHLIGHT},
I agree with Pádraig, --color seems better and extendible.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
PATCH 10/16 cal: add --highlight to usage()
In reply to Sami KerolaAnd clean up the usage() function.
--- misc-utils/cal.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
misc-utils/cal.c +14 -15
@@ -809,20 +809,19 @@ center(const char *str, size_t len, int separate)static void __attribute__ ((__noreturn__))usage (FILE * out) { -fputs (_ ("\nUsage:\n" ), out); -fprintf (out, -_ (" %s [options] [[[day] month] year]\n" ), - program_invocation_short_name); - -fputs (_ ("\nOptions:\n" ), out); -fputs (_ (" -1, --one show only current month (default)\n" -" -3, --three show previous, current and next month\n" -" -s, --sunday Sunday as first day of week\n" -" -m, --monday Monday as first day of week\n" -" -j, --julian output Julian dates\n" -" -y, --year show whole current year\n" -" -V, --version display version information and exit\n" -" -h, --help display this help text and exit\n\n" ), out); - +fputs (USAGE_HEADER , out); +fprintf (out,_ (" %s [options] [[[day] month] year]\n" ), program_invocation_short_name); +fputs (USAGE_OPTIONS , out); +fputs (_ (" -1, --one show only current month (default)\n" ), out); +fputs (_ (" -3, --three show previous, current and next month\n" ), out); +fputs (_ (" -s, --sunday Sunday as first day of week\n" ), out); +fputs (_ (" -m, --monday Monday as first day of week\n" ), out); +fputs (_ (" -j, --julian output Julian dates\n" ), out); +fputs (_ (" -y, --year show whole current year\n" ), out); +fputs (_ (" --highlight <when> advice when to highlight; never, auto, always\n" ), out); +fputs (USAGE_SEPARATOR , out); +fputs (USAGE_HELP , out); +fputs (USAGE_VERSION , out); +fprintf (out,USAGE_MAN_TAIL ("cal(1)" ));exit (out == stderr ?EXIT_FAILURE :EXIT_SUCCESS ); }
Signature · 1 line
1.8.2.2
PATCH 11/16 docs: cal: add --highlight option description
In reply to Sami Kerola--- misc-utils/cal.1 | 13 +++++++++++++ 1 file changed, 13 insertions(+)
misc-utils/cal.1 +13 -0
@@ -65,6 +65,19 @@ Display Julian dates (days one-based, numbered from January 1). \fB\-y\fR, \fB\-\-year\fR Display a calendar for the current year. .TP +\fB\-\-highlight\fR \fIwhen\fR +Highlight output. The +.I when +can be one +.IR never , +.IR auto , +or +.IR always . +Never will turn off highlighting in all situations. Auto is default, and +it will make highlight to be in use if output is done to termina. Always +will allow highlight to be outputed when cal outputs to pipe, or is +called from a script. +.TP \fB\-V\fR, \fB\-\-version\fR Display version information and exit. .TP
Signature · 1 line
1.8.2.2
PATCH 12/16 tests: add cal day highlight corner cases
In reply to Sami KerolaCheck that the first, andn last, possible dates and strange Sep 1752 dates are highlighted correctly.
--- tests/expected/cal/highlight | 55 ++++++++++++++++++++++++++++++++++ tests/ts/cal/highlight | 70 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tests/expected/cal/highlight create mode 100755 tests/ts/cal/highlight
tests/expected/cal/highlight new file +55 -0
@@ -0,0 +1,55 @@ + +First day + January 1 +Su Mo Tu We Th Fr Sa + [7m 1[27m + 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 31 +Reformation corner cases 1 + September 1752 +Su Mo Tu We Th Fr Sa + 1 [7m 2[27m 14 15 16 +17 18 19 20 21 22 23 +24 25 26 27 28 29 30 + + + +Reformation corner cases 2 + September 1752 +Su Mo Tu We Th Fr Sa + 1 2 14 15 16 +17 18 19 20 21 22 23 +24 25 26 27 28 29 30 + + + +Reformation corner cases 3 + September 1752 +Su Mo Tu We Th Fr Sa + 1 2 14 15 16 +17 18 19 20 21 22 23 +24 25 26 27 28 29 30 + + + +Reformation corner cases 4 + September 1752 +Su Mo Tu We Th Fr Sa + 1 2 [7m14[27m 15 16 +17 18 19 20 21 22 23 +24 25 26 27 28 29 30 + + + +Last day + November 9999 December 9999 January 10000 +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 1 2 3 4 1 + 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8 +14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15 +21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22 +28 29 30 26 27 28 29 30 [7m31[27m 23 24 25 26 27 28 29 + 30 31
tests/ts/cal/highlight new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
1.8.2.2
PATCH 13/16 cal: stop trimming whitespaces
In reply to Sami KerolaRemove trailing spaces from output it is trivial
cal | sed 's/ *$//'
but padding them back is difficult.
--- misc-utils/cal.c | 17 --- tests/expected/cal/1m | 48 ++++---- tests/expected/cal/3m | 48 ++++---- tests/expected/cal/highlight | 72 ++++++------ tests/expected/cal/sep1752 | 264 +++++++++++++++++++++---------------------- tests/expected/cal/year | 240 +++++++++++++++++++-------------------- 6 files changed, 336 insertions(+), 353 deletions(-)
misc-utils/cal.c +0 -17
@@ -246,7 +246,6 @@ void yearly(int, int, int);void do_monthly (int ,int ,int ,struct fmt_st *);void monthly (int ,int ,int );void monthly3 (int ,int ,int ); -void trim_trailing_spaces (char *);static void __attribute__ ((__noreturn__))usage (FILE * out);void headers_init (int );
@@ -517,7 +516,6 @@ do_monthly(int day, int month, int year, struct fmt_st *out) { p =ascii_day (p, xd); } *p ='\0' ; -trim_trailing_spaces (lineout);snprintf (out->s[row+2 ],FMT_ST_CHARS ,"%s" , lineout);if (has_hl) Hrow = out->s[row+2 ];
@@ -641,7 +639,6 @@ yearly(int day, int year, int julian) { p +=sprintf (p," " ); } *p ='\0' ; -trim_trailing_spaces (lineout);my_putstring (lineout);my_putstring ("\n" ); }
@@ -767,20 +764,6 @@ ascii_day(char *p, int day) {return p; } -void -trim_trailing_spaces (char *s) -{ -char *p; - -for (p = s; *p; ++p) -continue ; -while (p > s &&isspace (*--p)) -continue ; -if (p > s) - ++p; - *p ='\0' ; -} -/* * Center string, handling multibyte characters appropriately. * In addition if the string is too large for the width it's truncated.
tests/expected/cal/1m +24 -24
60 lines not on this page · raw message
tests/expected/cal/3m +24 -24
60 lines not on this page · raw message
tests/expected/cal/highlight +36 -36
90 lines not on this page · raw message
tests/expected/cal/sep1752 +132 -132
329 lines not on this page · raw message
tests/expected/cal/year +120 -120
294 lines not on this page · raw message
Signature · 1 line
1.8.2.2
Re: [PATCH 13/16] cal: stop trimming whitespaces
Quoted 8 lines · Sami Kerola
Remove trailing spaces from output it is trivial
cal | sed 's/ *$//'
but padding them back is difficult.
CC Pádraig Brady <[email protected]>Signed-off-by Sami Kerola <[email protected]>
Looks good.
thanks,
Pádraig.
Karel Zak
maintainerRe: [PATCH 13/16] cal: stop trimming whitespaces
In reply to Sami KerolaQuoted 1 line · Sami Kerola
Remove trailing spaces from output it is trivial
and introduce a regression and ugly output, see "cal -3":
old version:
April 2013 May 2013 June 2013
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30 new version:
April 2013 May 2013 June 2013
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30 btw, it's pretty obvious from
https://github.com/kerolasa/lelux-utiliteetit/commit/5b472fb7f4d8b855756a249633250e818a560a66
I guess we have regression test just to check for regressions.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Re: [PATCH 13/16] cal: stop trimming whitespaces
Quoted 4 lines · Karel Zak
Quoted 1 line · Sami Kerola
Remove trailing spaces from output it is trivial
and introduce a regression and ugly output, see "cal -3":
[d'oh]
Quoted 1 line
I guess we have regression test just to check for regressions.
Fixed here.
https://github.com/kerolasa/lelux-utiliteetit/commit/a0a150f8c14ac6b62483d03ec446f07e751903bf#L0L564
As all can see removal of couple white spaces removed regression.
I also dropped the global variables patch (earlier nr 14) from the fourth branch. There are no other changes in 'cal-fourth' branch.
--
Sami Kerola
http://www.iki.fi/kerolasa/
PATCH 14/16 cal: move global variables to local scope
In reply to Sami KerolaAlso include small change to function parameter coding style. The braces are from function line to new line, and return value is in same line with the function.
--- misc-utils/cal.c | 103 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 51 deletions(-)
misc-utils/cal.c +52 -51
@@ -223,31 +223,22 @@ static int days_in_month[2][13] = { }; -/* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */ -char day_headings[J_WEEK_LEN *6 +1 ]; -/* weekstart = 1 => " M Tu W Th F S S " */ -const char *full_month[MONTHS_IN_YEAR ]; - -/* 0 => sunday, 1 => monday */ -int weekstart =SUNDAY ; -int julian; -/* function prototypes */ static int leap_year (int year);static int centuries_since_1700 (int year,int centuries);static int leap_years_since_year_1 (int year); -char *ascii_day (char *,int ); +char *ascii_day (const int ,char *,int );int center_str (const char * src,char * dest,size_t dest_size,size_t width);void center (const char *,size_t ,int ); -void day_array (int ,int ,int ,int *); +void day_array (const int ,const int ,int ,int ,int ,int *);int day_in_week (int ,int ,int );int day_in_year (int ,int ,int ); -void yearly (int ,int ,int ); -void do_monthly (int ,int ,int ,struct fmt_st *); -void monthly (int ,int ,int ); -void monthly3 (int ,int ,int ); +void yearly (int ,int ,const int ,const int ,const char *,const char **); +void do_monthly (const int ,const int ,const char *,const char **,int ,int ,int ,struct fmt_st *); +void monthly (const int ,const int ,const char *,const char **,int ,int ,int ); +void monthly3 (const int ,const int ,const char *,const char **,int ,int ,int );static void __attribute__ ((__noreturn__))usage (FILE * out); -void headers_init (int ); +void headers_init (const int ,const int ,char *,const char **);int main (int argc,char **argv) {
@@ -255,6 +246,12 @@ main(int argc, char **argv) {time_t now;int ch, day =0 , month =0 , year =0 , yflag =0 ;int num_months =NUM_MONTHS ; +/* UTF-8 can have up to 6 bytes per char; and an extra byte for +* ending \0 */ +char day_headings[J_WEEK_LEN *6 +1 ]; +const char *full_month[MONTHS_IN_YEAR ]; +int julian =0 ; +int weekstart =SUNDAY ;enum Highlight_type {HIGHLIGHT_UNUSED ,
@@ -411,7 +408,7 @@ main(int argc, char **argv) {default :usage (stderr); } -headers_init (julian); +headers_init (julian, weekstart, day_headings, full_month);switch (highlight) {case HIGHLIGHT_NEVER :
@@ -430,11 +427,11 @@ main(int argc, char **argv) { }if (yflag) -yearly (day, year, julian); +yearly (day, year, julian, weekstart, day_headings, full_month);else if (num_months ==1 ) -monthly (day, month, year); +monthly (julian, weekstart, day_headings, full_month, day, month, year);else if (num_months ==3 ) -monthly3 (day, month, year); +monthly3 (julian, weekstart, day_headings, full_month, day, month, year);return EXIT_SUCCESS ; }
@@ -464,7 +461,8 @@ static int leap_years_since_year_1(int year) centuries_since_1700(year, 4)); } -void headers_init (int julian) +void headers_init (const int julian,const int weekstart,char *day_headings, +const char **full_month) {int i, wd, spaces = julian ?J_DAY_LEN -1 :DAY_LEN -1 ;char *cur_dh = day_headings;
@@ -475,8 +473,7 @@ void headers_init(int julian)if (i)strcat (cur_dh++," " ); - space_left = -sizeof (day_headings) - (cur_dh - day_headings); + space_left = (J_WEEK_LEN *6 +1 ) - (cur_dh - day_headings);if (space_left <= spaces)break ; cur_dh +=
@@ -488,13 +485,15 @@ void headers_init(int julian) full_month[i] =nl_langinfo (MON_1 + i); } -void -do_monthly (int day,int month,int year,struct fmt_st *out) { +void do_monthly (const int julian,const int weekstart,const char *day_headings, +const char **full_month,int day,int month,int year, +struct fmt_st *out) +{int col, row, days[MAXDAYS ];char *p, lineout[FMT_ST_CHARS ];int width = (julian ?J_WEEK_LEN :WEEK_LEN ) -1 ; -day_array (day, month, year, days); +day_array (julian, weekstart, day, month, year, days);/* * %s is the month name, %d the year number.
@@ -513,7 +512,7 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {int xd = days[row *DAYS_IN_WEEK + col];if (xd !=SPACE && (xd &TODAY_FLAG )) has_hl =1 ; - p =ascii_day (p, xd); + p =ascii_day (julian, p, xd); } *p ='\0' ;snprintf (out->s[row+2 ],FMT_ST_CHARS ,"%s" , lineout);
@@ -522,20 +521,22 @@ do_monthly(int day, int month, int year, struct fmt_st *out) { } } -void -monthly (int day,int month,int year) { +void monthly (const int julian,const int weekstart,const char *day_headings, +const char **full_month,int day,int month,int year) +{int i;struct fmt_st out; -do_monthly (day, month, year, &out); +do_monthly (julian, weekstart, day_headings, full_month, day, month, year, &out);for (i =0 ; i <FMT_ST_LINES ; i++) {my_putstring (out.s[i]);my_putstring ("\n" ); } } -void -monthly3 (int day,int month,int year) { +void monthly3 (const int julian,const int weekstart,const char *day_headings, +const char **full_month,int day,int month,int year) +{char lineout[FMT_ST_CHARS ];int i;int width;
@@ -560,9 +561,9 @@ monthly3(int day, int month, int year) { next_year = year; } -do_monthly (day, prev_month, prev_year, &out_prev); -do_monthly (day, month, year, &out_curm); -do_monthly (day, next_month, next_year, &out_next); +do_monthly (julian, weekstart, day_headings, full_month, day, prev_month, prev_year, &out_prev); +do_monthly (julian, weekstart, day_headings, full_month, day, month, year, &out_curm); +do_monthly (julian, weekstart, day_headings, full_month, day, next_month, next_year, &out_next); width = (julian ?J_WEEK_LEN :WEEK_LEN ) -1 ;for (i =0 ; i <2 ; i++) {
@@ -589,8 +590,9 @@ monthly3(int day, int month, int year) { } } -void -yearly (int day,int year,int julian) { +void yearly (int day,int year,const int julian,const int weekstart, +const char *day_headings,const char **full_month) +{int col, *dp, i, month, row, which_cal;int maxrow, sep_len, week_len;int days[MONTHS_IN_YEAR ][MAXDAYS ];
@@ -613,7 +615,7 @@ yearly(int day, int year, int julian) {my_putstring ("\n\n" );for (i =0 ; i <MONTHS_IN_YEAR ; i++) -day_array (day, i +1 , year, days[i]); +day_array (julian, weekstart, day, i +1 , year, days[i]);for (month =0 ; month <MONTHS_IN_YEAR ; month += maxrow) {center (full_month[month], week_len -1 , sep_len +1 );if (julian) {
@@ -635,7 +637,7 @@ yearly(int day, int year, int julian) {for (which_cal =0 ; which_cal < maxrow; which_cal++) { dp = &days[month + which_cal][row *DAYS_IN_WEEK ];for (col =0 ; col <DAYS_IN_WEEK ; col++) - p =ascii_day (p, *dp++); + p =ascii_day (julian, p, *dp++); p +=sprintf (p," " ); } *p ='\0' ;
@@ -653,8 +655,9 @@ yearly(int day, int year, int julian) {* out end to end. You would have 42 numbers or spaces. This routine * builds that array for any month from Jan. 1 through Dec. 9999. */ -void -day_array (int day,int month,int year,int *days) { +void day_array (const int julian,const int weekstart,int day,int month, +int year,int *days) +{int julday, daynum, dw, dm;int *sep1752;
@@ -684,8 +687,8 @@ day_array(int day, int month, int year, int *days) {* day_in_year -- * return the 1 based day number within the year */ -int -day_in_year (int day,int month,int year) { +int day_in_year (int day,int month,int year) +{int i, leap; leap =leap_year (year);
@@ -701,8 +704,8 @@ day_in_year(int day, int month, int year) {* 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all * missing days. */ -int -day_in_week (int day,int month,int year) { +int day_in_week (int day,int month,int year) +{long temp; temp =
@@ -716,8 +719,8 @@ day_in_week(int day, int month, int year) {return (REFORMATION_WEEKDAY ); } -char * -ascii_day (char *p,int day) { +char *ascii_day (const int julian,char *p,int day) +{int display, val;int highlight =0 ;static char *aday[] = {
@@ -769,15 +772,13 @@ ascii_day(char *p, int day) {* In addition if the string is too large for the width it's truncated. * The number of trailing spaces may be 1 less than the number of leading spaces. */ -int -center_str (const char * src,char * dest,size_t dest_size,size_t width) +int center_str (const char *src,char *dest,size_t dest_size,size_t width) {return mbsalign (src, dest, dest_size, &width,MBS_ALIGN_CENTER ,MBA_UNIBYTE_FALLBACK ); } -void -center (const char *str,size_t len,int separate) +void center (const char *str,size_t len,int separate) {char lineout[FMT_ST_CHARS ];
Signature · 1 line
1.8.2.2
Karel Zak
maintainerRe: [PATCH 14/16] cal: move global variables to local scope
Quoted 3 lines · Sami Kerola
Also include small change to function parameter coding style. The braces are from function line to new line, and return value is in same line with the function.
The version you send to mailing list seems correct, but the version in your cal-again branch is completely broken. It's obvious that nobody has tried "make cal" for the branch :-(
Anyway, I'm really not sure if this change:
misc-utils/cal.c static int days_in_month[2][13] = { · line 238
Quoted 2 lines
misc-utils/cal.c +1
-1-void monthly (int ,int ,int ); +void monthly (const int ,const int ,const char *,const char **,int ,int ,int );
makes the code more readable. Maybe you can drop the patch at all for now.
From long term point of view it would be better to introduce 'cal_context' struct for all the stuff. It's a way how to make the code more readable and extendible.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
PATCH 15/16 cal: mark all functions static
In reply to Sami KerolaReference: http://marc.info/?l=util-linux-ng&m=136717012419551&w=2
--- misc-utils/cal.c | 61 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 29 deletions(-)
misc-utils/cal.c +32 -29
@@ -227,18 +227,18 @@ static int days_in_month[2][13] = {static int leap_year (int year);static int centuries_since_1700 (int year,int centuries);static int leap_years_since_year_1 (int year); -char *ascii_day (const int ,char *,int ); -int center_str (const char * src,char * dest,size_t dest_size,size_t width); -void center (const char *,size_t ,int ); -void day_array (const int ,const int ,int ,int ,int ,int *); -int day_in_week (int ,int ,int ); -int day_in_year (int ,int ,int ); -void yearly (int ,int ,const int ,const int ,const char *,const char **); -void do_monthly (const int ,const int ,const char *,const char **,int ,int ,int ,struct fmt_st *); -void monthly (const int ,const int ,const char *,const char **,int ,int ,int ); -void monthly3 (const int ,const int ,const char *,const char **,int ,int ,int ); +static char *ascii_day (const int ,char *,int ); +static int center_str (const char * src,char * dest,size_t dest_size,size_t width); +static void center (const char *,size_t ,int ); +static void day_array (const int ,const int ,int ,int ,int ,int *); +static int day_in_week (int ,int ,int ); +static int day_in_year (int ,int ,int ); +static void yearly (int ,int ,const int ,const int ,const char *,const char **); +static void do_monthly (const int ,const int ,const char *,const char **,int ,int ,int ,struct fmt_st *); +static void monthly (const int ,const int ,const char *,const char **,int ,int ,int ); +static void monthly3 (const int ,const int ,const char *,const char **,int ,int ,int );static void __attribute__ ((__noreturn__))usage (FILE * out); -void headers_init (const int ,const int ,char *,const char **); +static void headers_init (const int ,const int ,char *,const char **);int main (int argc,char **argv) {
@@ -461,8 +461,8 @@ static int leap_years_since_year_1(int year) centuries_since_1700(year, 4)); } -void headers_init (const int julian,const int weekstart,char *day_headings, -const char **full_month) +static void headers_init (const int julian,const int weekstart, +char *day_headings,const char **full_month) {int i, wd, spaces = julian ?J_DAY_LEN -1 :DAY_LEN -1 ;char *cur_dh = day_headings;
@@ -485,9 +485,9 @@ void headers_init(const int julian, const int weekstart, char *day_headings, full_month[i] =nl_langinfo (MON_1 + i); } -void do_monthly (const int julian,const int weekstart,const char *day_headings, -const char **full_month,int day,int month,int year, -struct fmt_st *out) +static void do_monthly (const int julian,const int weekstart, +const char *day_headings,const char **full_month, +int day,int month,int year,struct fmt_st *out) {int col, row, days[MAXDAYS ];char *p, lineout[FMT_ST_CHARS ];
@@ -521,8 +521,9 @@ void do_monthly(const int julian, const int weekstart, const char *day_headings, } } -void monthly (const int julian,const int weekstart,const char *day_headings, -const char **full_month,int day,int month,int year) +static void monthly (const int julian,const int weekstart, +const char *day_headings,const char **full_month,int day, +int month,int year) {int i;struct fmt_st out;
@@ -534,8 +535,9 @@ void monthly(const int julian, const int weekstart, const char *day_headings, } } -void monthly3 (const int julian,const int weekstart,const char *day_headings, -const char **full_month,int day,int month,int year) +static void monthly3 (const int julian,const int weekstart, +const char *day_headings,const char **full_month,int day, +int month,int year) {char lineout[FMT_ST_CHARS ];int i;
@@ -590,8 +592,8 @@ void monthly3(const int julian, const int weekstart, const char *day_headings, } } -void yearly (int day,int year,const int julian,const int weekstart, -const char *day_headings,const char **full_month) +static void yearly (int day,int year,const int julian,const int weekstart, +const char *day_headings,const char **full_month) {int col, *dp, i, month, row, which_cal;int maxrow, sep_len, week_len;
@@ -655,8 +657,8 @@ void yearly(int day, int year, const int julian, const int weekstart,* out end to end. You would have 42 numbers or spaces. This routine * builds that array for any month from Jan. 1 through Dec. 9999. */ -void day_array (const int julian,const int weekstart,int day,int month, -int year,int *days) +static void day_array (const int julian,const int weekstart,int day,int month, +int year,int *days) {int julday, daynum, dw, dm;int *sep1752;
@@ -687,7 +689,7 @@ void day_array(const int julian, const int weekstart, int day, int month,* day_in_year -- * return the 1 based day number within the year */ -int day_in_year (int day,int month,int year) +static int day_in_year (int day,int month,int year) {int i, leap;
@@ -704,7 +706,7 @@ int day_in_year(int day, int month, int year)* 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all * missing days. */ -int day_in_week (int day,int month,int year) +static int day_in_week (int day,int month,int year) {long temp;
@@ -719,7 +721,7 @@ int day_in_week(int day, int month, int year)return (REFORMATION_WEEKDAY ); } -char *ascii_day (const int julian,char *p,int day) +static char *ascii_day (const int julian,char *p,int day) {int display, val;int highlight =0 ;
@@ -772,13 +774,14 @@ char *ascii_day(const int julian, char *p, int day)* In addition if the string is too large for the width it's truncated. * The number of trailing spaces may be 1 less than the number of leading spaces. */ -int center_str (const char *src,char *dest,size_t dest_size,size_t width) +static int center_str (const char *src,char *dest,size_t dest_size, +size_t width) {return mbsalign (src, dest, dest_size, &width,MBS_ALIGN_CENTER ,MBA_UNIBYTE_FALLBACK ); } -void center (const char *str,size_t len,int separate) +static void center (const char *str,size_t len,int separate) {char lineout[FMT_ST_CHARS ];
Signature · 1 line
1.8.2.2
PATCH 16/16 cal: simplify day_in_week()
In reply to Sami KerolaThe day_in_week() does not need to perform fullfledged September 1752 calculations, that month is dealt as static assignment.
References: http://www.spinics.net/lists/util-linux-ng/msg07910.html
--- misc-utils/cal.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
misc-utils/cal.c +7 -8
@@ -164,7 +164,6 @@ enum { };#define FIRST_WEEKDAY SATURDAY/* Jan 1st, 1 was a Saturday */ -#define REFORMATION_WEEKDAY THURSDAY/* after reformation it was Thursday */ #define REFORMATION_YEAR 1752/* Signed-off-by: Lord Chesterfield */ #define REFORMATION_MONTH 9/* September */ #define FIRST_MISSING_DAY 639799/* 3 Sep 1752 */
@@ -702,9 +701,9 @@ static int day_in_year(int day, int month, int year)/* * day_in_week * return the 0 based day number for any date from 1 Jan. 1 to -* 31 Dec. 9999. Assumes the Gregorian reformation eliminates -* 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all -* missing days. +* 31 Dec. 9999. Assumes the Gregorian reformation dates +* 3 Sep. 1752 through 13 Sep. 1752 are handled as statically +* assigned array, see day_array(). */ static int day_in_week (int day,int month,int year) {
@@ -715,10 +714,10 @@ static int day_in_week(int day, int month, int year)leap_years_since_year_1 (year -SMALLEST_YEAR ) +day_in_year (day, month, year);if (temp <FIRST_MISSING_DAY ) -return ((temp -1 +FIRST_WEEKDAY ) %DAYS_IN_WEEK ); -if (temp >= (FIRST_MISSING_DAY +NUMBER_MISSING_DAYS )) -return (((temp -1 +FIRST_WEEKDAY ) -NUMBER_MISSING_DAYS ) %DAYS_IN_WEEK ); -return (REFORMATION_WEEKDAY ); +return ((temp + (FIRST_WEEKDAY -1 )) %DAYS_IN_WEEK ); +if (temp >FIRST_MISSING_DAY ) +return ((temp + (FIRST_WEEKDAY -1 -NUMBER_MISSING_DAYS )) %DAYS_IN_WEEK ); +abort (); }static char *ascii_day (const int julian,char *p,int day)
Signature · 1 line
1.8.2.2
Re: [PATCH 16/16] cal: simplify day_in_week()
Quoted 2 lines · Sami Kerola
The
day_in_week()does not need to perform fullfledged September 1752 calculations, that month is dealt as static assignment.
Hi Karel, and others,
Please drop this patch. Having abort() at the end of the function is bad choice, if something like weekday name highlighting is implemented[1]. When I reconsider that bit the function it should return for the eleven days at September 1752 a day out of bounds, which basically some sort of NaN equivalent.
[1] http://www.spinics.net/lists/util-linux-ng/msg07904.html
--
Sami Kerola
http://www.iki.fi/kerolasa/