home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
util
/
timeplanner-1.0.lha
/
TimePlanner
/
tp.c
< prev
Wrap
C/C++ Source or Header
|
1994-05-29
|
27KB
|
936 lines
//----------------------------------------------------------------------------
// TimePlanner V1.0 © 1993 L. Vanhelsuwé
// ---------------- --------------------
// Background deamon for starting periodic background processes or for reminding
// users of appointments (birthdays, car insurance, ...).
// Uses an Events file to find out when to become active and notify user or to
// execute secondary batch processes.
//
// BUGS:
// -----
// -Program is not localization-proof. When locale is not English : corruption
// occurs in EV.LST
//
// History:
// --------
// Sat 24-JAN-93: started this file.
// Sun 31-JAN-93: finished basic useful version
// Sun 09-MAY-93: added early reminder "X DAYS/HOURS/MINUTES to go before.."
// Mon 10-MAY-93: added ENV:EV.LST marking to find out where we're waiting
// bumped revision to 1.1
// Tue 18-MAY-93: added graceful exit for 1.3 users.
// Fri 21-JAN-94: implemented EXECUTE option to execute batch files or commands
// added delay after initial loading for faster/clearer startup
//
//----------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef AMIGA
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/datetime.h>
#include <intuition/screens.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfx.h>
#include <clib/exec_protos.h> // ANSI function prototypes
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#endif
#define MAX_REMINDERS (12) // upto N-1 early reminders
#define COLON (':')
#define SEMICOLON (';')
#define COMMA (',')
#define HYPHEN ('-')
#define QUOTE ('"')
#define VERT_BAR ('|')
#define TAB (9)
#define LF (10)
#define ASTERISK ('*')
#define SPACE (32)
#define MINUTETICKS (60*50) // 50 Amiga TICKS in a second
#define HOURTICKS (60*MINUTETICKS)
#define DAYTICKS (24*HOURTICKS)
#define WEEKTICKS (7*DAYTICKS)
#define MONTHTICKS (30*DAYTICKS)
#define NUM_DAYS (7)
#define NUM_MONTHS (12)
#define BUT_OK (1)
#define BUT_CANCEL (2)
//-----------------------------------------------------------------------------
// Function prototypes
// -------------------
void parse_user_events ( void );
void skip_line ( void );
void skip_whitespc ( void );
void print_line ( void );
char popup_requester ( char *msg, char buttons);
void make_exe_file ( void );
void process_line ( void );
void process_on_line ( void );
void init_TP ( void );
void adjust ( struct DateStamp *ds, ULONG ticks);
void handle_events ( void );
void mark_file ( char *file, int position, char *tag);
void do_event ( char *req_msg, char *batch_file);
char * load_file ( char * filename);
BOOL is_string ( char * txt, char * string);
BOOL DStampToStr ( struct DateStamp *ds, char *daystr, char *datestr, char *timestr);
short get_num ( void );
short get_month ( void );
short get_dayname ( void );
short process_remind_line ( void );
short month_from_str ( char *month );
//-----------------------------------------------------------------------------
// Global statics
// --------------
struct IntuitionBase *IntuitionBase; // for access to ActiveWindow
struct DateStamp datestamp; // for DateStamp(), DateToStr(),..
struct DateTime datetime;
char day_str[16]; // Wednesday is biggest
char date_str[10]; // 24-JAN-93
char time_str[10]; // 21:47:47
char *file_buf, *events_file;
char *txt; // text pointer (into buffer)
ULONG file_size;
struct REM // REMINDER structure
{
struct DateStamp REM_ds;
char REM_string[80];
};
struct REM reminders[MAX_REMINDERS]; // 0..N reminder dates/times
char month_codes[]="0123456789AB"; // month codes
char *months[]={"JAN","FEB","MAR","APR","MAY","JUN",
"JUL","AUG","SEP","OCT","NOV","DEC"};
char *days[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
char *version = "$VER: TimePlanner 1.0 ©LVA 22/JAN/94\n";
FILE *fh; // file handle
BOOL ok;
int i;
//-----------------------------------------------------------------------------
// outline of program:
// - from user events file, create a sorted formalized version
// - process all future events by waiting for event time, then executing action
//-----------------------------------------------------------------------------
void main (void) {
struct DosBase * libptr;
printf("TimePlanner (TM) V1.0\n");
printf("Designed and implemented by Laurence Vanhelsuwé © Jan 1993\n\n");
libptr = ( struct DosBase*) OpenLibrary("dos.library", 37); // check for 2.0 OS
if (libptr) {
CloseLibrary((struct Library*)libptr);
Delay(5*50); // let startup-sequence complete
init_TP(); // initialize things
events_file = load_file("S:EVENTS"); // cache user events file
parse_user_events(); // generate formalised events list
FreeMem(file_buf, file_size); // free user events file buffer
Execute("Sort ENV:ev.raw TO ENV:ev.lst", NULL, NULL); // sort events on date
DeleteFile("ENV:ev.raw"); // delete unsorted file
handle_events(); // process queue, waiting for right times..
FreeMem(file_buf, file_size); // free events file buffer
} else {
printf("
TimePlanner
needs AmigaDOS 2.0 (V37+)...
Sorry!
\n");
}
printf("TimePlanner quitting. Have a nice day.\n");
}
//-----------------------------------------------------------------------------
// Initialize program.
//-----------------------------------------------------------------------------
void init_TP (void) {
DateStamp(&datestamp);
ok = DStampToStr(&datestamp, day_str, date_str, time_str);
printf("Today is %s %s and the time is %s\n", day_str, date_str, time_str);
IntuitionBase = (struct IntuitionBase*) OpenLibrary("intuition.library", 36);
if (!IntuitionBase) {
printf("TimePlanner ERROR: No IntuitionBase !\n");
exit(100);
}
}
//-----------------------------------------------------------------------------
// Scan through sorted events list, waiting for next event and then executing
// the required action.
//
// Example lines:
//
// 1994 0 22 15:55:00 R 5 Minutes to go before...Jonah !|
// 1994 0 22 16:00:00 N Jonah !|
//
//-----------------------------------------------------------------------------
void handle_events( void ) {
unsigned int delay,offset;
short day,month,year,hour,minute;
ULONG tmin_days,tmin_mins,tmin_ticks; // T-minus X days, minutes, ticks
char ch;
char * req_msg; // pointer to text for requester
char * file_end;
char datestring[20], timestring[20];
char * batch_file; // don't execute anything
static char reminder_msg[256];
txt = events_file = load_file("ENV:EV.LST"); // load sorted events file
file_end = txt + file_size; // calculate EOF in buffer
while (txt < file_end) { // while not past EOF
req_msg = NULL;
batch_file = NULL; // assume no BATCH file
offset = txt - events_file;
mark_file ("ENV:EV.LST", offset, ">"); // label line we're waiting on
year = get_num(); txt++; // get event date and time...
month = (*txt) < 'A' ? (*txt)-'0' : (*txt)-'A'+10;
txt +=2;
day = get_num(); txt++; // skip space
hour = get_num(); txt++; // skip COLON
minute = get_num(); txt++; // skip COLON
get_num(); // discard seconds field
txt++; // skip space char
ch = *txt++;
switch (ch) {
case 'N': txt++; // skip space
req_msg = txt; // notif. msg is user's string
while (*txt++ != VERT_BAR) ;
*(txt-1) = 0; // turn into C-string
if (*txt == LF) break;
txt++; // skip space
if (*txt++ == 'E') {
txt++; // skip space
txt++; // skip filename quote
batch_file = txt;
while (*txt != QUOTE) txt++;
*txt++ = 0; // turn filname into C-string
}
break;
case 'R': txt++; // skip space
req_msg = txt;
while (*txt++ != VERT_BAR) ;
*(txt-1) = 0; // turn into C-string
sprintf(reminder_msg, "EARLY REMINDER MESSAGE:\n\n");
sprintf(reminder_msg+24, req_msg);
req_msg = reminder_msg;
break;
cas