]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/cal.c
Merge branch 'lsfd--close-all' of https://github.com/masatake/util-linux
[thirdparty/util-linux.git] / misc-utils / cal.c
index 21eecae9a7fded8cbede034dc0760b1a44a21839..89bce405151a4c8f721adf96cb32140c23db9c0d 100644 (file)
@@ -85,15 +85,17 @@ enum {
        CAL_COLOR_TODAY,
        CAL_COLOR_HEADER,
        CAL_COLOR_WEEKNUMBER,
+       CAL_COLOR_WEEKS,
        CAL_COLOR_WORKDAY,
        CAL_COLOR_WEEKEND
 };
 
 static const struct { const char * const scheme; const char * dflt; } colors[] =
 {
-        [CAL_COLOR_TODAY]      = { "today",      UL_COLOR_REVERSE },
-        [CAL_COLOR_WEEKNUMBER] = { "weeknumber", UL_COLOR_REVERSE },
-        [CAL_COLOR_HEADER]     = { "header",     ""               },
+       [CAL_COLOR_TODAY]      = { "today",      UL_COLOR_REVERSE },
+       [CAL_COLOR_WEEKNUMBER] = { "weeknumber", UL_COLOR_REVERSE },    /* requested week */
+       [CAL_COLOR_WEEKS]      = { "weeks",      ""               },    /* week numbers */
+       [CAL_COLOR_HEADER]     = { "header",     ""               },
        [CAL_COLOR_WORKDAY]    = { "workday",    ""               },
        [CAL_COLOR_WEEKEND]    = { "weekend",    ""               }
 };
@@ -320,7 +322,7 @@ int main(int argc, char **argv)
                {"twelve", no_argument, NULL, 'Y'},
                {"help", no_argument, NULL, 'h'},
                {"vertical", no_argument, NULL,'v'},
-               {"column", required_argument, NULL,'c'},
+               {"columns", required_argument, NULL,'c'},
                {NULL, 0, NULL, 0}
        };
 
@@ -360,7 +362,7 @@ int main(int argc, char **argv)
         * POSIX:  19971201 + 7 -1 = 0
         */
        {
-               int wfd;
+               unsigned int wfd;
                union { unsigned int word; char *string; } val;
                val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
 
@@ -407,6 +409,8 @@ int main(int argc, char **argv)
                        break;
                case 'w':
                        if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
                                ctl.req.week = strtos32_or_err(optarg,
                                                _("invalid week argument"));
                                if (ctl.req.week < 1 || 54 < ctl.req.week)
@@ -517,10 +521,13 @@ int main(int argc, char **argv)
                }
                break;
        case 0:
-               ctl.req.day = local_time.tm_yday + 1;
+               if (!ctl.req.week) {
+                       ctl.req.day = local_time.tm_yday + 1;
+                       if (!ctl.req.month)
+                               ctl.req.month = local_time.tm_mon + 1;
+               }
                ctl.req.year = local_time.tm_year + 1900;
-               if (!ctl.req.month)
-                       ctl.req.month = local_time.tm_mon + 1;
+
                break;
        default:
                warnx(_("bad usage"));
@@ -692,9 +699,9 @@ static void headers_init(struct cal_control *ctl)
        for (i = 0; i < DAYS_IN_WEEK; i++) {
                size_t space_left;
 
-               if (i)
-                       strcat(cur_dh++, " ");
                space_left = sizeof(day_headings) - (cur_dh - day_headings);
+               if (i && space_left)
+                       strncat(cur_dh++, " ", space_left--);
 
                if (space_left <= (ctl->day_width - 1))
                        break;
@@ -815,6 +822,8 @@ static void cal_vert_output_header(struct cal_month *month,
        struct cal_month *m;
        int month_width;
 
+       cal_enable_color(CAL_COLOR_HEADER);
+
        month_width = ctl->day_width * (MAXDAYS / DAYS_IN_WEEK);
 
        /* Padding for the weekdays */
@@ -841,6 +850,8 @@ static void cal_vert_output_header(struct cal_month *month,
                        left(out, month_width, ctl->gutter_width);
                }
        }
+
+       cal_disable_color(CAL_COLOR_HEADER);
        fputc('\n', stdout);
 }
 
@@ -859,6 +870,13 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
        const char *seq_we_start = cal_get_color_sequence(CAL_COLOR_WEEKEND);
        const char *seq_we_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKEND);
 
+       const char *seq_ws_start = NULL, *seq_ws_end = NULL;
+
+       if (ctl->weektype) {
+               seq_ws_start = cal_get_color_sequence(CAL_COLOR_WEEKS);
+               seq_ws_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKS);
+       }
+
        for (week_line = 0; week_line < MAXDAYS / DAYS_IN_WEEK; week_line++) {
                for (i = month; i; i = i->next) {
                        /* Determine the day that should be highlighted. */
@@ -874,11 +892,21 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
 
                        if (ctl->weektype) {
                                if (0 < i->weeks[week_line]) {
-                                       if ((ctl->weektype & WEEK_NUM_MASK) == i->weeks[week_line])
-                                               printf("%s%2d%s",
-                                                      cal_get_color_sequence(CAL_COLOR_WEEKNUMBER),
-                                                      i->weeks[week_line],
-                                                      cal_get_color_disable_sequence(CAL_COLOR_WEEKNUMBER));
+                                       const char *seq_start, *seq_end;
+
+                                       /* colorize by requested week-number or generic weeks color */
+                                       if (ctl->req.week &&
+                                           ctl->req.week == i->weeks[week_line]) {
+                                               seq_start = cal_get_color_sequence(CAL_COLOR_WEEKNUMBER);
+                                               seq_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKNUMBER);
+                                       } else {
+                                               seq_start = seq_ws_start;
+                                               seq_end = seq_ws_end;
+                                       }
+
+                                       if (seq_start && *seq_start)
+                                               printf("%s%2d%s", seq_start,
+                                                      i->weeks[week_line], seq_end);
                                        else
                                                printf("%2d", i->weeks[week_line]);
                                } else
@@ -927,10 +955,32 @@ cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
        int i, reqday, week, d;
        int skip;
        struct cal_month *m;
+       int firstwork = ctl->weekstart == SUNDAY ? 1 : 0;       /* first workday in week */
+
+       const char *seq_wo_start = cal_get_color_sequence(CAL_COLOR_WORKDAY);
+       const char *seq_wo_end = cal_get_color_disable_sequence(CAL_COLOR_WORKDAY);
+       const char *seq_we_start = cal_get_color_sequence(CAL_COLOR_WEEKEND);
+       const char *seq_we_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKEND);
+       const char *seq_hd_start = cal_get_color_sequence(CAL_COLOR_HEADER);
+       const char *seq_hd_end = cal_get_color_disable_sequence(CAL_COLOR_HEADER);
 
        skip = ctl->day_width;
        for (i = 0; i < DAYS_IN_WEEK; i++) {
+               const char *seq_start = seq_wo_start,
+                          *seq_end = seq_wo_end;
+
+               /* Day name */
+               fput_seq(seq_hd_start);
                left(ctl->weekdays[i], ctl->day_width - 1, 0);
+               fput_seq(seq_hd_end);
+
+               /* Workday/Weekend color */
+               if (i < firstwork || i > firstwork + 4) {
+                       seq_start = seq_we_start;
+                       seq_end = seq_we_end;
+               }
+
+               /* Day digits */
                for (m = month; m; m = m->next) {
                        reqday = 0;
                        if (m->month == ctl->req.month && m->year == ctl->req.year) {
@@ -943,7 +993,9 @@ cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
                        }
                        for (week = 0; week < MAXDAYS / DAYS_IN_WEEK; week++) {
                                d = i + DAYS_IN_WEEK * week;
+
                                if (0 < m->days[d]) {
+                                       fput_seq(seq_start);
                                        if (reqday == m->days[d]) {
                                                printf("%*s%s%*d%s",
                                                       skip - (ctl->julian ? 3 : 2),
@@ -955,6 +1007,7 @@ cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
                                        } else {
                                                printf("%*d",  skip, m->days[d]);
                                        }
+                                       fput_seq(seq_end);
                                } else {
                                        printf("%*s", skip, "");
                                }
@@ -968,16 +1021,32 @@ cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
        if (!ctl->weektype)
                return;
 
+       /* Week numbers */
+       const char *seq_ws_start = cal_get_color_sequence(CAL_COLOR_WEEKS);
+       const char *seq_ws_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKS);
+
        printf("%*s", (int)ctl->day_width - 1, "");
        for (m = month; m; m = m->next) {
                for (week = 0; week < MAXDAYS / DAYS_IN_WEEK; week++) {
                        if (0 < m->weeks[week]) {
-                               if ((ctl->weektype & WEEK_NUM_MASK) == m->weeks[week])
-                                       printf("%s%*d%s",
-                                                cal_get_color_sequence(CAL_COLOR_WEEKNUMBER),
-                                                skip - (ctl->julian ? 3 : 2),
-                                                m->weeks[week],
-                                                cal_get_color_disable_sequence(CAL_COLOR_WEEKNUMBER));
+                               const char *seq_start = NULL, *seq_end = NULL;
+
+                               /* colorize by requested week-number or generic weeks color */
+                               if (ctl->req.week &&
+                                   ctl->req.week == m->weeks[week]) {
+                                       seq_start = cal_get_color_sequence(CAL_COLOR_WEEKNUMBER);
+                                       seq_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKNUMBER);
+                               } else {
+                                       seq_start = seq_ws_start;
+                                       seq_end = seq_ws_end;
+                               }
+
+                               if (seq_start && *seq_start)
+                                       printf("%*s%s%*d%s",
+                                                skip - (ctl->julian ? 3 : 2), "",
+                                                seq_start,
+                                                (ctl->julian ? 3 : 2), m->weeks[week],
+                                                seq_end);
                                else
                                        printf("%*d", skip, m->weeks[week]);
                        } else
@@ -1296,8 +1365,8 @@ static void __attribute__((__noreturn__)) usage(void)
                "                         %s\n", USAGE_COLORS_DEFAULT);
 
        fputs(USAGE_SEPARATOR, out);
-       printf(USAGE_HELP_OPTIONS(23));
-       printf(USAGE_MAN_TAIL("cal(1)"));
+       fprintf(out, USAGE_HELP_OPTIONS(23));
+       fprintf(out, USAGE_MAN_TAIL("cal(1)"));
 
        exit(EXIT_SUCCESS);
 }