return 0;
}
-static void my_putstring(char *s)
+static void my_putstring(const char *s)
{
#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
if (has_term)
struct cal_control {
const char *full_month[MONTHS_IN_YEAR]; /* month names */
const char *abbr_month[MONTHS_IN_YEAR]; /* abbreviated month names */
+ const char *weekdays[DAYS_IN_WEEK]; /* day names */
int reform_year; /* Gregorian reform year */
int colormode; /* day and week number highlight */
int weektype; /* WEEK_TYPE_{NONE,ISO,US} */
size_t day_width; /* day width in characters in printout */
size_t week_width; /* 7 * day_width + possible week num */
+ size_t month_width; /* width of a month (vertical mode) */
int gutter_width; /* spaces in between horizontal month outputs */
struct cal_request req; /* the times user is interested */
unsigned int julian:1, /* julian output */
header_year:1, /* print year number */
- header_hint:1; /* does month name + year need two lines to fit */
+ header_hint:1, /* does month name + year need two lines to fit */
+ vertical:1; /* display the output in vertical */
};
struct cal_month {
int32_t year;
struct cal_month *next;
};
-
/* function prototypes */
static int leap_year(const struct cal_control *ctl, int32_t year);
static int monthname_to_number(struct cal_control *ctl, const char *name);
+static void weekdays_init(struct cal_control *ctl);
static void headers_init(struct cal_control *ctl);
static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl);
static void cal_output_header(struct cal_month *month, const struct cal_control *ctl);
static void cal_output_months(struct cal_month *month, const struct cal_control *ctl);
+static void cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl);
static void monthly(const struct cal_control *ctl);
static void yearly(const struct cal_control *ctl);
static int day_in_year(const struct cal_control *ctl, int day,
static int week_to_day(const struct cal_control *ctl);
static int center_str(const char *src, char *dest, size_t dest_size, size_t width);
static void center(const char *str, size_t len, int separate);
+static int left_str(const char *src, char *dest, size_t dest_size, size_t width);
+static void left(const char *str, size_t len, int separate);
static int parse_reform_year(const char *reform_year);
static void __attribute__((__noreturn__)) usage(void);
int main(int argc, char **argv)
{
- struct tm *local_time;
+ struct tm local_time;
char *term;
time_t now;
int ch = 0, yflag = 0, Yflag = 0;
{"version", no_argument, NULL, 'V'},
{"twelve", no_argument, NULL, 'Y'},
{"help", no_argument, NULL, 'h'},
+ {"vertical", no_argument, NULL,'v'},
{NULL, 0, NULL, 0}
};
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
- atexit(close_stdout);
+ close_stdout_atexit();
term = getenv("TERM");
if (term) {
ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
}
#endif
- while ((ch = getopt_long(argc, argv, "13mjn:sSywYVh", longopts, NULL)) != -1) {
+ while ((ch = getopt_long(argc, argv, "13mjn:sSywYvVh", longopts, NULL)) != -1) {
err_exclusive_options(ch, longopts, excl, excl_st);
case OPT_ISO:
ctl.reform_year = ISO;
break;
+ case 'v':
+ ctl.vertical = 1;
+ break;
case 'V':
- printf(UTIL_LINUX_VERSION);
- return EXIT_SUCCESS;
+ print_version(EXIT_SUCCESS);
case 'h':
usage();
default:
ctl.week_width = (ctl.day_width * DAYS_IN_WEEK) + WNUM_LEN;
} else
ctl.week_width = ctl.day_width * DAYS_IN_WEEK;
+ /*
+ * The day_width includes the space between days,
+ * as there is no leading space, remove 1
+ * */
+ ctl.week_width -= 1;
if (argc == 1 && !isdigit_string(*argv)) {
usec_t x;
} else
cal_time(&now);
- local_time = localtime(&now);
+ localtime_r(&now, &local_time);
switch(argc) {
case 3:
errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), dm);
ctl.req.day = day_in_year(&ctl, ctl.req.day,
ctl.req.month, ctl.req.year);
- } else if ((int32_t) (local_time->tm_year + 1900) == ctl.req.year) {
- ctl.req.day = local_time->tm_yday + 1;
+ } else if ((int32_t) (local_time.tm_year + 1900) == ctl.req.year) {
+ ctl.req.day = local_time.tm_yday + 1;
}
if (!ctl.req.month && !ctl.req.week) {
- ctl.req.month = local_time->tm_mon + 1;
+ ctl.req.month = local_time.tm_mon + 1;
if (!ctl.num_months)
yflag = 1;
}
break;
case 0:
- ctl.req.day = local_time->tm_yday + 1;
- ctl.req.year = local_time->tm_year + 1900;
+ ctl.req.day = local_time.tm_yday + 1;
+ ctl.req.year = local_time.tm_year + 1900;
if (!ctl.req.month)
- ctl.req.month = local_time->tm_mon + 1;
+ ctl.req.month = local_time.tm_mon + 1;
break;
default:
warnx(_("bad usage"));
ctl.req.month = MONTHS_IN_YEAR < m ? 1 : m;
}
+ weekdays_init(&ctl);
headers_init(&ctl);
- if (!colors_init(ctl.colormode, "cal")) {
- ctl.req.day = 0;
- ctl.weektype &= ~WEEK_NUM_MASK;
+ if (colors_init(ctl.colormode, "cal") == 0) {
+ /*
+ * If standout mode available (Senter and Sexit are set) and
+ * user or terminal-colors.d do not disable colors than
+ * ignore colors_init().
+ */
+ if (*Senter && *Sexit && colors_mode() != UL_COLORMODE_NEVER) {
+ /* let use standout mode */
+ ;
+ } else {
+ /* disable */
+ Senter = ""; Sexit = "";
+ ctl.req.day = 0;
+ ctl.weektype &= ~WEEK_NUM_MASK;
+ }
}
if (yflag || Yflag) {
}
}
+ if (ctl.vertical)
+ ctl.gutter_width = 1;
+
if (ctl.num_months > 1 && ctl.months_in_row == 0) {
ctl.months_in_row = MONTHS_IN_YEAR_ROW; /* default */
if (isatty(STDOUT_FILENO)) {
- int w = get_terminal_width(STDOUT_FILENO);
- int mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH;
- int extra = ((w / mw) - 1) * ctl.gutter_width;
- int new_n = (w - extra) / mw;
+ int w, mw, extra, new_n;
+
+ w = get_terminal_width(80);
+ mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH;
+
+ if (w < mw)
+ w = mw;
+
+ extra = ((w / mw) - 1) * ctl.gutter_width;
+ new_n = (w - extra) / mw;
if (new_n < MONTHS_IN_YEAR_ROW)
- ctl.months_in_row = new_n;
+ ctl.months_in_row = new_n > 0 ? new_n : 1;
}
} else if (!ctl.months_in_row)
ctl.months_in_row = 1;
{
if (year <= ctl->reform_year)
return !(year % 4);
- else
- return ( !(year % 4) && (year % 100) ) || !(year % 400);
+
+ return ( !(year % 4) && (year % 100) ) || !(year % 400);
}
static void init_monthnames(struct cal_control *ctl)
return -EINVAL;
}
+static void weekdays_init(struct cal_control *ctl)
+{
+ size_t wd;
+ for (int i = 0; i < DAYS_IN_WEEK; i++) {
+ wd = (i + ctl->weekstart) % DAYS_IN_WEEK;
+ ctl->weekdays[i] = nl_langinfo(ABDAY_1 + wd);
+ }
+}
static void headers_init(struct cal_control *ctl)
{
- size_t i, wd;
+ size_t i;
char *cur_dh = day_headings;
char tmp[FMT_ST_CHARS];
int year_len;
for (i = 0; i < DAYS_IN_WEEK; i++) {
size_t space_left;
- wd = (i + ctl->weekstart) % DAYS_IN_WEEK;
if (i)
strcat(cur_dh++, " ");
if (space_left <= (ctl->day_width - 1))
break;
- cur_dh += center_str(nl_langinfo(ABDAY_1 + wd), cur_dh,
+ cur_dh += center_str(ctl->weekdays[i], cur_dh,
space_left, ctl->day_width - 1);
}
for (i = 0; i < MONTHS_IN_YEAR; i++) {
/* The +1 after year_len is space in between month and year. */
- if (ctl->week_width < strlen(ctl->full_month[i]) + year_len + 1)
+ if (ctl->week_width < strlen(ctl->full_month[i]) + year_len)
ctl->header_hint = 1;
}
}
if (ctl->header_hint || ctl->header_year) {
for (i = month; i; i = i->next) {
snprintf(out, sizeof(out), "%s", ctl->full_month[i->month - 1]);
- center(out, ctl->week_width - 1, i->next == NULL ? 0 : ctl->gutter_width);
+ center(out, ctl->week_width, i->next == NULL ? 0 : ctl->gutter_width);
}
if (!ctl->header_year) {
my_putstring("\n");
for (i = month; i; i = i->next) {
snprintf(out, sizeof(out), "%04d", i->year);
- center(out, ctl->week_width - 1, i->next == NULL ? 0 : ctl->gutter_width);
+ center(out, ctl->week_width, i->next == NULL ? 0 : ctl->gutter_width);
}
}
} else {
for (i = month; i; i = i->next) {
snprintf(out, sizeof(out), "%s %04d", ctl->full_month[i->month - 1], i->year);
- center(out, ctl->week_width - 1, i->next == NULL ? 0 : ctl->gutter_width);
+ center(out, ctl->week_width, i->next == NULL ? 0 : ctl->gutter_width);
}
}
my_putstring("\n");
my_putstring("\n");
}
+static void cal_vert_output_header(struct cal_month *month,
+ const struct cal_control *ctl)
+{
+ char out[FMT_ST_CHARS];
+ struct cal_month *m;
+ int month_width;
+
+ month_width = ctl->day_width * (MAXDAYS / DAYS_IN_WEEK);
+ /* Padding for the weekdays */
+ snprintf(out, sizeof(out), "%*s", (int)ctl->day_width + 1, "");
+ my_putstring(out);
+ if (ctl->header_hint || ctl->header_year) {
+ for (m = month; m; m = m->next) {
+ snprintf(out, sizeof(out), "%s", ctl->full_month[m->month - 1]);
+ left(out, month_width, ctl->gutter_width);
+ }
+ if (!ctl->header_year) {
+ my_putstring("\n");
+ /* Padding for the weekdays */
+ snprintf(out, sizeof(out), "%*s", (int)ctl->day_width + 1, "");
+ my_putstring(out);
+ for (m = month; m; m = m->next) {
+ snprintf(out, sizeof(out), "%04d", m->year);
+ left(out, month_width, ctl->gutter_width);
+ }
+ }
+ } else {
+ for (m = month; m; m = m->next) {
+ snprintf(out, sizeof(out), "%s %04d", ctl->full_month[m->month - 1], m->year);
+ left(out, month_width, ctl->gutter_width);
+ }
+ }
+ my_putstring("\n");
+}
+
static void cal_output_months(struct cal_month *month, const struct cal_control *ctl)
{
char out[FMT_ST_CHARS];
}
}
+static void
+cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
+{
+ char out[FMT_ST_CHARS];
+ int i, reqday, week, d;
+ int skip;
+ struct cal_month *m;
+
+ skip = ctl->day_width;
+ for (i = 0; i < DAYS_IN_WEEK; i++) {
+ left(ctl->weekdays[i], ctl->day_width - 1, 0);
+ for (m = month; m; m = m->next) {
+ reqday = 0;
+ if (m->month == ctl->req.month && m->year == ctl->req.year) {
+ if (ctl->julian) {
+ reqday = ctl->req.day;
+ } else {
+ reqday = ctl->req.day + 1 -
+ day_in_year(ctl, 1, m->month, m->year);
+ }
+ }
+ for (week = 0; week < MAXDAYS / DAYS_IN_WEEK; week++) {
+ d = i + DAYS_IN_WEEK * week;
+ if (0 < m->days[d]) {
+ if (reqday == m->days[d]) {
+ snprintf(out, sizeof(out), "%*s%s%*d%s",
+ skip - (ctl->julian ? 3 : 2),
+ "", Senter, (ctl->julian ? 3 : 2),
+ m->days[d], Sexit);
+ } else {
+ snprintf(out, sizeof(out), "%*d",
+ skip, m->days[d]);
+ }
+ } else {
+ snprintf(out, sizeof(out), "%*s",
+ skip, "");
+ }
+ my_putstring(out);
+ skip = ctl->day_width;
+ }
+ if (m->next != NULL) {
+ snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
+ my_putstring(out);
+ }
+ }
+ my_putstring("\n");
+ }
+ if (!ctl->weektype)
+ return;
+
+ snprintf(out, sizeof(out), "%*s", (int)ctl->day_width - 1, "");
+ my_putstring(out);
+ 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])
+ {
+ snprintf(out, sizeof(out), "%s%*d%s",
+ Senter,
+ skip - (ctl->julian ? 3 : 2),
+ m->weeks[week], Sexit);
+ } else {
+ snprintf(out, sizeof(out), "%*d",
+ skip, m->weeks[week]);
+ }
+ } else {
+ snprintf(out, sizeof(out), "%*s", skip, "");
+ }
+ my_putstring(out);
+ }
+ if (m->next != NULL) {
+ snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
+ my_putstring(out);
+ }
+ }
+ my_putstring("\n");
+}
+
+
static void monthly(const struct cal_control *ctl)
{
struct cal_month m1,m2,m3, *m;
- int i, rows, new_month, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
+ int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
int32_t year = ctl->req.year;
/* cal -3, cal -Y --span, etc. */
if (ctl->span_months) {
- new_month = month - ctl->num_months / 2;
+ int new_month = month - ctl->num_months / 2;
if (new_month < 1) {
- month = new_month + MONTHS_IN_YEAR;
- year--;
- }
- else
+ new_month *= -1;
+ year -= (new_month / MONTHS_IN_YEAR) + 1;
+
+ if (new_month > MONTHS_IN_YEAR)
+ new_month %= MONTHS_IN_YEAR;
+ month = MONTHS_IN_YEAR - new_month;
+ } else
month = new_month;
}
}
cal_fill_month(m, ctl);
}
- cal_output_header(&m1, ctl);
- cal_output_months(&m1, ctl);
+ if (ctl->vertical) {
+ if (i > 0) {
+ /* Add a line between row */
+ my_putstring("\n");
+ }
+ cal_vert_output_header(&m1, ctl);
+ cal_vert_output_months(&m1, ctl);
+ } else {
+ cal_output_header(&m1, ctl);
+ cal_output_months(&m1, ctl);
+ }
}
}
static void yearly(const struct cal_control *ctl)
{
char out[FMT_ST_CHARS];
- int year_width = 0;
+ int year_width;
- year_width += (ctl->week_width + 1) * (ctl->julian ? 2 : 3);
- if (ctl->julian)
- year_width--;
+ year_width = ctl->months_in_row * (ctl->week_width) +
+ (ctl->months_in_row - 1) * ctl->gutter_width;
if (ctl->header_year) {
snprintf(out, sizeof(out), "%04d", ctl->req.year);
my_putstring(lineout);
}
}
+static int left_str(const char* src, char* dest,
+ size_t dest_size, size_t width)
+{
+ return mbsalign(src, dest, dest_size, &width,
+ MBS_ALIGN_LEFT, MBA_UNIBYTE_FALLBACK);
+}
+static void left(const char *str, size_t len, int separate)
+{
+ char lineout[FMT_ST_CHARS];
+
+ left_str(str, lineout, sizeof(lineout), len);
+ my_putstring(lineout);
+
+ if (separate) {
+ snprintf(lineout, sizeof(lineout), "%*s", separate, "");
+ my_putstring(lineout);
+ }
+}
static int parse_reform_year(const char *reform_year)
{
size_t i;
fputs(_(" -y, --year show the whole year\n"), out);
fputs(_(" -Y, --twelve show the next twelve months\n"), out);
fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out);
- fputs(_(" --color[=<when>] colorize messages (auto, always or never)\n"), out);
+ fputs(_(" -v, --vertical show day vertically instead of line\n"), out);
+ fprintf(out,
+ _(" --color[=<when>] colorize messages (%s, %s or %s)\n"), "auto", "always", "never");
fprintf(out,
" %s\n", USAGE_COLORS_DEFAULT);