home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sources / misc / 4065 < prev    next >
Encoding:
Text File  |  1992-11-09  |  10.7 KB  |  454 lines

  1. Newsgroups: comp.sources.misc
  2. Path: sparky!kent
  3. From: dfs@doe.carleton.ca (David F. Skoll)
  4. Subject:  v33i060:  remind - A replacement for calendar, Part03/12
  5. Message-ID: <1992Nov10.041835.844@sparky.imd.sterling.com>
  6. Followup-To: comp.sources.d
  7. X-Md4-Signature: 1c4a36d390adc3b60376e75dbe5847ca
  8. Sender: kent@sparky.imd.sterling.com (Kent Landfield)
  9. Organization: Dept. of Electronics, Carleton University
  10. References: <csm-v33i058=remind.221714@sparky.IMD.Sterling.COM>
  11. Date: Tue, 10 Nov 1992 04:18:35 GMT
  12. Approved: kent@sparky.imd.sterling.com
  13. Lines: 439
  14.  
  15. Submitted-by: dfs@doe.carleton.ca (David F. Skoll)
  16. Posting-number: Volume 33, Issue 60
  17. Archive-name: remind/part03
  18. Environment: UNIX, MS-DOS
  19. Supersedes: remind: Volume 17, Issue 3-6
  20.  
  21. #!/bin/sh
  22. # This is part 03 of Remind 03.00.00
  23. if touch 2>&1 | fgrep 'amc' > /dev/null
  24.  then TOUCH=touch
  25.  else TOUCH=true
  26. fi
  27. # ============= dosubst.c ==============
  28. if test X"$1" != X"-c" -a -f 'dosubst.c'; then
  29.     echo "File already exists: skipping 'dosubst.c'"
  30. else
  31. echo "x - extracting dosubst.c (Text)"
  32. sed 's/^X//' << 'SHAR_EOF' > dosubst.c &&
  33. X/***************************************************************/
  34. X/*                                                             */
  35. X/*  DOSUBST.C                                                  */
  36. X/*                                                             */
  37. X/*  This performs all the "%" substitution functions when      */
  38. X/*  reminders are triggered.                                   */
  39. X/*                                                             */
  40. X/*  This file is part of REMIND.                               */
  41. X/*  Copyright (C) 1992 by David F. Skoll.                      */
  42. X/*                                                             */
  43. X/***************************************************************/
  44. X#include <stdio.h>
  45. X#include <ctype.h>
  46. X#include "config.h"
  47. X#ifdef HAVE_STDLIB_H
  48. X#include <stdlib.h>
  49. X#endif
  50. X#ifdef HAVE_MALLOC_H
  51. X#include <malloc.h>
  52. X#endif
  53. X#include "globals.h"
  54. X#include "err.h"
  55. X#include "types.h"
  56. X#include "protos.h"
  57. X
  58. X#define UPPER(c) ( ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c) )
  59. X#define ABS(x) ( (x) < 0 ? -(x) : (x) )
  60. X#ifndef NL
  61. X#define NL "\n"
  62. X#endif
  63. X
  64. Xstatic char TODAY[] = "today";
  65. Xstatic char TOMORROW[] = "tomorrow";
  66. X
  67. X/***************************************************************/
  68. X/*                                                             */
  69. X/*  DoSubst                                                    */
  70. X/*                                                             */
  71. X/*  Process the % escapes in the reminder.  If                 */
  72. X/*  mode==NORMAL_MODE, ignore the %" sequence.  If             */
  73. X/*  mode==CAL_MODE, process the %" sequence.                   */
  74. X/*                                                             */
  75. X/***************************************************************/
  76. X
  77. X#ifdef HAVE_PROTOS
  78. XPUBLIC int DoSubst(ParsePtr p, char *out, Trigger *t, TimeTrig *tt, int jul, int mode)
  79. X#else
  80. Xint DoSubst(p, out, t, tt, jul, mode)
  81. XParsePtr p;
  82. Xchar *out;
  83. XTrigger *t;
  84. XTimeTrig *tt;
  85. Xint jul, mode;
  86. X#endif
  87. X{
  88. X   int diff = jul - JulianToday;
  89. X   int curtime = SystemTime() / 60;
  90. X   int err, done;
  91. X   int c;
  92. X   int d, m, y;
  93. X   int tim = tt->ttime;
  94. X   int h, min, hh, ch, cmin, chh;
  95. X   char *pm, *cpm;
  96. X   int tdiff, adiff, mdiff, hdiff;
  97. X   char *mplu, *hplu, *when, *plu;
  98. X   int has_quote = 0;
  99. X   char *s = out;
  100. X   char *os;
  101. X
  102. X   FromJulian(jul, &y, &m, &d);
  103. X
  104. X   if (tim == NO_TIME) tim = curtime;
  105. X   tdiff = tim - curtime;
  106. X   adiff = ABS(tdiff);
  107. X   mdiff = adiff % 60;
  108. X   hdiff = adiff / 60;
  109. X   mplu = (mdiff == 1 ? "" : "s");
  110. X   hplu = (hdiff == 1 ? "" : "s");
  111. X   when = (tdiff < 0 ? "ago" : "from now");
  112. X   
  113. X   h = tim / 60;
  114. X   min = tim % 60;
  115. X
  116. X   pm = (h < 12) ? "am" : "pm";
  117. X   hh = (h == 12) ? 12 : h % 12;
  118. X   
  119. X   ch = curtime / 60;
  120. X   cmin = curtime % 60;
  121. X
  122. X   cpm = (ch < 12) ? "am" : "pm";
  123. X   chh = (ch == 12) ? 12 : ch % 12;
  124. X
  125. X   switch(d) {
  126. X      case 1:
  127. X      case 21:
  128. X      case 31: plu = "st"; break;
  129. X      
  130. X      case 2:
  131. X      case 22: plu = "nd"; break;
  132. X      
  133. X      case 3:
  134. X      case 23: plu = "rd"; break;
  135. X      
  136. X      default: plu = "th"; break;
  137. X   }
  138. X      
  139. X   
  140. X   while(1) {
  141. X      c = ParseChar(p, &err, 0);
  142. X      if (err) return err;
  143. X      if (c == '\n') continue;
  144. X      if (!c) {
  145. X         if (mode != CAL_MODE) *s++ = '\n';
  146. X     *s++ = 0;
  147. X     break;
  148. X      }
  149. X      if (c != '%') {
  150. X         *s++ = c;
  151. X     continue;
  152. X      }
  153. X      c = ParseChar(p, &err, 0);
  154. X      if (err) return err;
  155. X      if (!c) {
  156. X     *s++ = 0;
  157. X     break;
  158. X      }
  159. X      os = s;
  160. X      done = 0;
  161. X      if (diff <= 1) {
  162. X         switch(UPPER(c)) {
  163. X            case 'A':
  164. X            case 'B':
  165. X        case 'C':
  166. X        case 'E':
  167. X        case 'F':
  168. X        case 'G':
  169. X        case 'H':
  170. X        case 'I':
  171. X        case 'J':
  172. X        case 'K':
  173. X        case 'L':
  174. X        case 'U':
  175. X        case 'V': sprintf(s, "%s", (diff ? TOMORROW : TODAY));
  176. X                 s += strlen(s);
  177. X                 done = 1;
  178. X                  break;
  179. X             
  180. X            default: done = 0;
  181. X         }
  182. X      }
  183. X     
  184. X      if (!done) switch(UPPER(c)) {
  185. X         case 'A':
  186. X            sprintf(s, "on %s, %d %s, %d", DayName[jul%7], d,
  187. X            MonthName[m], y);
  188. X            s += strlen(s);
  189. X        break;
  190. X           
  191. X     case 'B':
  192. X            sprintf(s, "in %d days' time", diff);
  193. X        s += strlen(s);
  194. X            break;
  195. X           
  196. X     case 'C':
  197. X        sprintf(s, "on %s", DayName[jul%7]);
  198. X        s += strlen(s);
  199. X        break;
  200. X
  201. X     case 'D':
  202. X        sprintf(s, "%d", d);
  203. X        s += strlen(s);
  204. X        break;
  205. X
  206. X     case 'E':
  207. X        sprintf(s, "on %02d/%02d/%04d", d, m+1, y);
  208. X        s += strlen(s);
  209. X        break;
  210. X
  211. X     case 'F':
  212. X        sprintf(s, "on %02d/%02d/%04d", m+1, d, y);
  213. X        s += strlen(s);
  214. X        break;
  215. X
  216. X     case 'G':
  217. X        sprintf(s, "on %s, %d %s", DayName[jul%7], d, MonthName[m]);
  218. X        s += strlen(s);
  219. X        break;
  220. X
  221. X     case 'H':
  222. X        sprintf(s, "on %02d/%02d", d, m+1);
  223. X        s += strlen(s);
  224. X        break;
  225. X
  226. X     case 'I':
  227. X        sprintf(s, "on %02d/%02d", m+1, d);
  228. X        s += strlen(s);
  229. X        break;
  230. X
  231. X     case 'J':
  232. X        sprintf(s, "on %s, %s %d%s, %d", DayName[jul%7],
  233. X               MonthName[m], d, plu, y);
  234. X        s += strlen(s);
  235. X        break;
  236. X
  237. X     case 'K':
  238. X        sprintf(s, "on %s, %s %d%s", DayName[jul%7],
  239. X               MonthName[m], d, plu);
  240. X        s += strlen(s);
  241. X        break;
  242. X
  243. X     case 'L':
  244. X        sprintf(s, "on %04d/%02d/%02d", y, m+1, d);
  245. X        s += strlen(s);
  246. X        break;
  247. X
  248. X     case 'M':
  249. X        sprintf(s, "%s", MonthName[m]);
  250. X        s += strlen(s);
  251. X        break;
  252. X
  253. X     case 'N':
  254. X        sprintf(s, "%d", m+1);
  255. X        s += strlen(s);
  256. X        break;
  257. X
  258. X     case 'O':
  259. X        if (RealToday == JulianToday) sprintf(s, " (today)");
  260. X        s += strlen(s);
  261. X        break;
  262. X
  263. X     case 'P':
  264. X        sprintf(s, (diff == 1 ? "" : "s"));
  265. X        s += strlen(s);
  266. X        break;
  267. X
  268. X     case 'Q':
  269. X        sprintf(s, (diff == 1 ? "'s" : "s'"));
  270. X        s += strlen(s);
  271. X        break;
  272. X
  273. X     case 'R':
  274. X        sprintf(s, "%02d", d);
  275. X        s += strlen(s);
  276. X        break;
  277. X
  278. X     case 'S':
  279. X        sprintf(s, plu);
  280. X        s += strlen(s);
  281. X        break;
  282. X
  283. X     case 'T':
  284. X        sprintf(s, "%02d", m+1);
  285. X        s += strlen(s);
  286. X        break;
  287. X
  288. X     case 'U':
  289. X        sprintf(s, "on %s, %d%s %s, %d", DayName[jul%7], d,
  290. X               plu, MonthName[m], y);
  291. X        s += strlen(s);
  292. X        break;
  293. X
  294. X     case 'V':
  295. X        sprintf(s, "on %s, %d%s %s", DayName[jul%7], d, plu,
  296. X               MonthName[m]);
  297. X        s += strlen(s);
  298. X        break;
  299. X
  300. X     case 'W':
  301. X        sprintf(s, DayName[jul%7]);
  302. X        s += strlen(s);
  303. X        break;
  304. X
  305. X     case 'X':
  306. X        sprintf(s, "%d", diff);
  307. X        s += strlen(s);
  308. X        break;
  309. X
  310. X     case 'Y':
  311. X        sprintf(s, "%d", y);
  312. X        s += strlen(s);
  313. X        break;
  314. X
  315. X     case 'Z':
  316. X        sprintf(s, "%d", y % 100);
  317. X        s += strlen(s);
  318. X        break;
  319. X
  320. X     case '1':
  321. X        if (tdiff == 0) 
  322. X           sprintf(s, "now");
  323. X        else if (hdiff == 0) 
  324. X           sprintf(s, "%d minute%s %s", mdiff, mplu, when);
  325. X        else if (mdiff == 0)
  326. X           sprintf(s, "%d hour%s %s", hdiff, hplu, when);
  327. X        else
  328. X           sprintf(s, "%d hour%s and %d minute%s %s", hdiff, hplu, mdiff, mplu, when);
  329. X        s += strlen(s);
  330. X        break;
  331. X
  332. X     case '2':
  333. X        sprintf(s, "at %d:%02d%s", hh, min, pm);
  334. X        s += strlen(s);
  335. X        break;
  336. X
  337. X     case '3': sprintf(s, "at %02d:%02d", h, min);
  338. X        s += strlen(s);
  339. X        break;
  340. X
  341. X     case '4': sprintf(s, "%d", tdiff);
  342. X        s += strlen(s);
  343. X        break;
  344. X
  345. X     case '5': sprintf(s, "%d", adiff);
  346. X        s += strlen(s);
  347. X        break;
  348. X
  349. X     case '6': sprintf(s, when);
  350. X        s += strlen(s);
  351. X        break;
  352. X
  353. X     case '7': sprintf(s, "%d", hdiff);
  354. X        s += strlen(s);
  355. X        break;
  356. X
  357. X     case '8': sprintf(s, "%d", mdiff);
  358. X        s += strlen(s);
  359. X        break;
  360. X
  361. X     case '9': sprintf(s, mplu);
  362. X        s += strlen(s);
  363. X        break;
  364. X
  365. X     case '0': sprintf(s, hplu);
  366. X        s += strlen(s);
  367. X        break;
  368. X
  369. X     case '!': sprintf(s, (tdiff >= 0 ? "is" : "was"));
  370. X        s += strlen(s);
  371. X        break;
  372. X
  373. X     case '@': sprintf(s, "%d:%02d%s", chh, cmin, cpm);
  374. X        s += strlen(s);
  375. X        break;
  376. X
  377. X     case '#': sprintf(s, "%02d:%02d", ch, cmin);
  378. X        s += strlen(s);
  379. X        break;
  380. X
  381. X         case '_': sprintf(s, "%s", NL);
  382. X        s += strlen(s);
  383. X        break;
  384. X
  385. X     case QUOTE_MARKER:
  386. X        /* Swallow any QUOTE_MARKERs which may somehow creep in... */
  387. X        break;
  388. X
  389. X     case '"':
  390. X        *s++ = QUOTE_MARKER;
  391. X        has_quote = 1;
  392. X        break;
  393. X
  394. X         default:
  395. X        *s++ = c;
  396. X      }
  397. X      if (isupper(c)) *os = UPPER(*os);
  398. X   }
  399. X
  400. X/* We're outside the big while loop.  The only way to get here is for c to
  401. X   be null.  Now we go through and delete %" sequences, if it's the
  402. X   NORMAL_MODE, or retain only things within a %" sequence if it's the
  403. X   CAL_MODE. */
  404. X
  405. X/* If there are NO quotes, then:  If CAL_MODE && RUN_TYPE, we don't want the
  406. X   reminder in the calendar.  Zero the output buffer and quit. */
  407. X   if (!has_quote) {
  408. X      if (mode == CAL_MODE && t->typ == RUN_TYPE) *out = 0;
  409. X      return OK;
  410. X   }
  411. X
  412. X/* There ARE quotes.  If in CAL_MODE, delete everything before first quote
  413. X   and after second quote.  If in NORMAL_MODE, delete the %" sequences. */
  414. X
  415. X   s = out;
  416. X   os = out;
  417. X   if (mode == NORMAL_MODE) {
  418. X      while (*s) {
  419. X         if (*s != QUOTE_MARKER) *os++ = *s;
  420. X     s++;
  421. X      }
  422. X      *os = 0;
  423. X   } else {
  424. X
  425. X/* Skip past the quote marker */
  426. X      while (*s && (*s != QUOTE_MARKER)) s++;
  427. X
  428. X/* Security check... actually, *s must == QUOTE_MARKER at this point, but
  429. X   it doesn't hurt to make it a bit robust. */
  430. X      if (*s) s++;
  431. X
  432. X/* Copy the output until the next QUOTE_MARKER */
  433. X      while (*s && (*s != QUOTE_MARKER)) *os++ = *s++;
  434. X      *os = 0;
  435. X   }
  436. X
  437. X   return OK;
  438. X}
  439. X   
  440. X
  441. SHAR_EOF
  442. $TOUCH -am 1109141292 dosubst.c &&
  443. chmod 0600 dosubst.c ||
  444. echo "restore of dosubst.c failed"
  445. set `wc -c dosubst.c`;Wc_c=$1
  446. if test "$Wc_c" != "9144"; then
  447.     echo original size 9144, current size $Wc_c
  448. fi
  449. fi
  450. echo "End of part 3, continue with part 4"
  451. exit 0
  452.  
  453. exit 0 # Just in case...
  454.