cal-alt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Calendar program - one page per month (writes to standard output)
  3. *
  4. * calen [month] year [len] [-o]
  5. *
  6. * If one of the numeric arguments {month, year, len} is specified, it
  7. * is assumed to be the year; calendar generated is for entire year.
  8. * (Note: Unlike the UN*X 'cal', nn is treated as 19nn.)
  9. *
  10. * If two numeric arguments are specified, first is assumed to be the
  11. * month (1-12) and second the year; calendar generated is for that
  12. * month/year only.
  13. *
  14. * If all are specified, calendar generated is for 'len' consecutive
  15. * months starting at month/year.
  16. *
  17. * If last argument is '-o' flag, output will contain backspaces and
  18. * overstrikes to emphasize month and year. (Not all printers can
  19. * handle this; check local conventions before printing. If using
  20. * 'lp' on PWB, use its '-o' flag to convert the backspaces to
  21. * overstruck lines.)
  22. *
  23. * Author: AW Rogers
  24. *
  25. * Originally written in Fortran-IV on GE Timesharing, 10/65
  26. * Converted to C/UN*X, 3/83
  27. */
  28. /*
  29. * Constant definitions:
  30. */
  31. #define MIN 1753 /* Valid years (1753: start of Gregorian calendar) */
  32. #define MAX 9999
  33. #define JAN 1 /* Significant months and weekdays */
  34. #define DEC 12
  35. #define SUN 0
  36. #define SAT 6
  37. #define SINGLE "*" /* Normal and overstrike (XIH*) character sequences */
  38. #define OVERSTRIKE "X\bI\bH\b*"
  39. /*
  40. * Macro definitions:
  41. */
  42. #define day_of_week(I) ((I) % 7)
  43. /*
  44. * Global declarations:
  45. */
  46. char output_seq[8]; /* either SINGLE or OVERSTRIKE; used in PrintChar */
  47. /*
  48. * Main(argc, argv)
  49. *
  50. * Gets and checks command line arguments; prints desired calendar(s)
  51. */
  52. main(argc, argv)
  53. int argc;
  54. char *argv[];
  55. {
  56. int mon, year, len; /* Command line arguments */
  57. /*
  58. * Get and verify command line arguments (see above)
  59. */
  60. /* Check for -o as last command line argument (ignore erroneous flags) */
  61. if (argv[argc-1][0] == '-' && argv[--argc][1] == 'o')
  62. strcpy(output_seq, OVERSTRIKE);
  63. else
  64. strcpy(output_seq, SINGLE);
  65. /* Supply defaults for numeric arguments as described above */
  66. switch (argc) {
  67. case 2: /* 1 argument supplied (year) */
  68. sscanf(argv[1], "%d", &year);
  69. mon = JAN;
  70. len = 12;
  71. break;
  72. case 3: /* 2-3 arguments supplied (mon year [len]) */
  73. case 4:
  74. sscanf(argv[1], "%d", &mon);
  75. sscanf(argv[2], "%d", &year);
  76. if (argc == 3)
  77. len = 1;
  78. else
  79. sscanf(argv[3], "%d", &len);
  80. break;
  81. default:
  82. printf("usage: %s [month] year [len] [-o]\n", argv[0]);
  83. return;
  84. } /* end switch */
  85. /* Validate month and year; quit if invalid */
  86. if (year > 0 && year < 100) /* Convert nn to 19nn first */
  87. year += 1900;
  88. if (year < MIN || year > MAX || mon < JAN || mon > DEC) {
  89. printf("valid months: %d..%d; valid years: %d..%d\n", JAN, DEC,
  90. MIN, MAX);
  91. return;
  92. }
  93. /*
  94. * Main loop: prints calendar for each requested month
  95. */
  96. while (len-- > 0) {
  97. PrintCal(mon, year);
  98. /* Prepare for next month's calendar */
  99. if (++mon > DEC) { /* Bump month (and year) */
  100. mon = JAN;
  101. if (++year > MAX) /* Quit if year too big */
  102. return;
  103. }
  104. } /* end calendar generation loop */
  105. } /* end main */
  106. /*
  107. * PrintCal(mon, year)
  108. *
  109. * Prints calendar for specified month and year (assumed valid)
  110. */
  111. PrintCal(mon, year)
  112. int mon, year;
  113. {
  114. int i, j, k, /* General-purpose loop indices */
  115. start, end, /* Range of calendar boxes containing dates */
  116. date = 0, /* Date to print inside box */
  117. chars; /* Length of current month name */
  118. static char blanks[36] = " ";
  119. /* String of blanks for centering heading */
  120. char digits[5], /* Individual digits of year (for printing) */
  121. *padding; /* Pointer to effective start of 'blanks' */
  122. /*
  123. * Names and lengths of months (first element is dummy):
  124. */
  125. static struct {
  126. char *name; /* name of month */
  127. char len[2]; /* number of days (non-leap, leap) */
  128. char first[2]; /* first of month wrt 1/1 (same) */
  129. } month[13] = { {"", {0,0}, {0,0}},
  130. {"JANUARY", {31,31}, {0,0}}, {"FEBRUARY", {28,29}, {3,3}},
  131. {"MARCH", {31,31}, {3,4}}, {"APRIL", {30,30}, {6,0}},
  132. {"MAY", {31,31}, {1,2}}, {"JUNE" , {30,30}, {4,5}},
  133. {"JULY", {31,31}, {6,0}}, {"AUGUST", {31,31}, {2,3}},
  134. {"SEPTEMBER", {30,30}, {5,6}}, {"OCTOBER", {31,31}, {0,1}},
  135. {"NOVEMBER", {30,30}, {3,4}}, {"DECEMBER", {31,31}, {5,6}}};
  136. /*
  137. * Names of weekdays, centered (roughly) in 9-character field:
  138. */
  139. static char weekday[7][10] = {" SUNDAY ", "MONDAY ", " TUESDAY ",
  140. "WEDNESDAY", "THURSDAY ", " FRIDAY ", "SATURDAY "};
  141. /*
  142. * 5 x 7 dot-matrix representations of letters A-Z and digits 0-9.
  143. * Printed as large characters (using ' ' and '*'); see PrintChar.
  144. */
  145. static char large_letters[26][7] = {
  146. 14,17,17,31,17,17,17, 30,17,17,30,17,17,30, 14,17,16,16,16,17,14,
  147. 30,17,17,17,17,17,30, 31,16,16,30,16,16,31, 31,16,16,30,16,16,16,
  148. 14,17,16,23,17,17,14, 17,17,17,31,17,17,17, 31,4,4,4,4,4,31,
  149. 1,1,1,1,1,17,14, 17,18,20,24,20,18,17, 16,16,16,16,16,16,31,
  150. 17,27,21,21,17,17,17, 17,17,25,21,19,17,17, 14,17,17,17,17,17,14,
  151. 30,17,17,30,16,16,16, 14,17,17,17,21,18,13, 30,17,17,30,20,18,17,
  152. 14,17,16,14,1,17,14, 31,4,4,4,4,4,4, 17,17,17,17,17,17,14,
  153. 17,17,17,17,17,10,4, 17,17,17,17,21,21,10, 17,17,10,4,10,17,17,
  154. 17,17,17,14,4,4,4, 31,1,2,4,8,16,31};
  155. static char large_digits[10][7] = {
  156. 14,17,17,17,17,17,14, 2,6,10,2,2,2,31, 14,17,2,4,8,16,31,
  157. 14,17,1,14,1,17,14, 2,6,10,31,2,2,2, 31,16,16,30,1,17,14,
  158. 14,17,16,30,17,17,14, 31,1,2,4,8,16,16, 14,17,17,14,17,17,14,
  159. 14,17,17,15,1,17,14};
  160. /*
  161. * Initialize starting weekday of first month (first box to contain a
  162. * date) and digits in year (for printing in 5x7 format):
  163. */
  164. start = year + (year - 1)/4 - (year - 1)/100 + (year - 1)/400;
  165. start = day_of_week(start + month[mon].first[IsLeap(year)]);
  166. sprintf(digits, "%4d", year);
  167. /*
  168. * Print heading with month and year in large letters/digits
  169. */
  170. printf("\f\n\n\n");
  171. SolidLine(2);
  172. BorderLine(2);
  173. /* Create a string of blanks for centering month/year */
  174. chars = strlen(month[mon].name);
  175. padding = &blanks[3 * (chars - 3)];
  176. /* Print 7 lines containing the month and year in large characters */
  177. for (j = 0; j <= 6; ++j) {
  178. printf(" **%s", padding);
  179. for (k = 0; k <= chars - 1; ++k) /* Letters in month name */
  180. PrintChar(large_letters[month[mon].name[k] - 'A'][j]);
  181. printf("%12s", " ");
  182. for (k = 0; k <= 3; ++k) /* Digits in year */
  183. PrintChar(large_digits[digits[k] - '0'][j]);
  184. printf(" %s**\n", padding);
  185. }
  186. /* Print names of weekdays above calendar boxes */
  187. BorderLine(2);
  188. SolidLine(2);
  189. BoxLine(1);
  190. printf(" **");
  191. for (j = SUN; j <= SAT; ++j)
  192. printf("%13s%5s", weekday[j], "*");
  193. printf("*\n");
  194. /*
  195. * Print body of calendar
  196. */
  197. end = start + month[mon].len[IsLeap(year)] - 1; /* Last date to print */
  198. /* Print first 5 weeks of calendar */
  199. for (j = 0; j <= 34; ++j) {
  200. if (day_of_week(j) == SUN) { /* Top and left border */
  201. BoxLine(1);
  202. SolidLine(1);
  203. printf(" **");
  204. }
  205. if (j >= start && j <= end) /* Date or blank */
  206. printf("%3d%15s", ++date, "*");
  207. else
  208. printf("%18s", "*");
  209. if (day_of_week(j) == SAT) { /* Right border and bottom */
  210. printf("*\n");
  211. BoxLine(5);
  212. }
  213. }
  214. /* Print last line (with 30/31 if needed) and bottom border */
  215. printf(" **");
  216. for (j = 35; j <= 41; ++j)
  217. if (j <= end) /* Print date or blank */
  218. printf("%16d%2s", ++date, "*");
  219. else
  220. printf("%18s", "*");
  221. printf("*\n");
  222. SolidLine(2);
  223. } /* end PrintCal */
  224. /*
  225. * IsLeap(y)
  226. *
  227. * Return 1 if year y is leap; 0 if not
  228. */
  229. int IsLeap(y)
  230. int y;
  231. {
  232. return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
  233. } /* end IsLeap */
  234. /*
  235. * PrintChar(i)
  236. *
  237. * Prints least significant 6 bits of i in binary representation,
  238. * using ' ' for 0 and '*' for 1.
  239. */
  240. PrintChar(i)
  241. char i;
  242. {
  243. int msk = 32; /* Output field width = log2(msk)+1 = 6 */
  244. for (; msk > 0; msk /= 2)
  245. if (i & msk)
  246. printf(output_seq); /* Globally defined */
  247. else
  248. printf(" ");
  249. } /* end PrintChar */
  250. /*
  251. * BorderLine(n)
  252. *
  253. * Print n border lines (** 125 spaces **)
  254. */
  255. BorderLine(n)
  256. int n;
  257. {
  258. static char c[] = "**";
  259. while (n--)
  260. printf(" %2s%127s\n", c, c);
  261. } /* end BorderLine */
  262. /*
  263. * BoxLine(n)
  264. *
  265. * Print n box lines (** 17 spaces * 17 spaces .. **)
  266. */
  267. BoxLine(n)
  268. int n;
  269. {
  270. static char c[] = "*";
  271. while (n--)
  272. printf(" **%18s%18s%18s%18s%18s%18s%18s*\n", c, c, c, c, c, c, c);
  273. } /* end BoxLine */
  274. /*
  275. * SolidLine(n)
  276. *
  277. * Print n solid lines (129 *)
  278. */
  279. SolidLine(n)
  280. int n;
  281. {
  282. static char c[] = "*******************************************";
  283. while (n--)
  284. printf(" %43s%43s%43s\n", c, c, c);
  285. } /* end SolidLine */