home *** CD-ROM | disk | FTP | other *** search
- /* main.c
- *
- * Update !AWShared within !System
- *
- */
-
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
-
- #include "os.h"
- #include "bbc.h"
- #include "wimp.h"
- #include "event.h"
- #include "alarm.h"
- #include "wimpt.h"
- #include "win.h"
- #include "visdelay.h"
- #include "res.h"
- #include "resspr.h"
- #include "template.h"
- #include "version.h"
- #include "swis.h"
- #include "msgs.h"
- #include "werr.h"
-
- #include "cortn.h"
- #include "field.h"
- #include "misc.h"
- #include "tracker.h"
-
- static wimp_w main__w = (wimp_w) -1;
- static coroutine *main__co = NULL;
-
- #define main__BUFSIZE 16384
- #define main__SCANSIZE 1024
-
- static int main__wup(int x) { return (x + 3) & ~3; }
-
- typedef struct
- { int load, exec, size, attr, type;
- } main__finfo;
-
- typedef struct
- { int lo, hi;
- } main__filedate;
-
- static struct
- { char *sysname;
- char *awname;
- BOOL exitafter;
- int slotstart; /* start time of the current time slice */
- int files, bytes;
- } main__info;
-
- enum { main__SYSNAME = 2, main__CANCEL, main__UPDATE, main__MESSAGE };
-
- static void main__yield(void)
- {
- if (main__info.slotstart + 20 < alarm_timenow())
- { static int lastbytes = 0, lastfiles = 0;
- if (main__info.bytes > 0 && (main__info.bytes != lastbytes || main__info.files != lastfiles))
- { field_printf(main__w, main__MESSAGE, msgs_lookup("main10"), main__info.files, main__info.bytes);
- lastbytes = main__info.bytes;
- lastfiles = main__info.files;
- }
- goto_co(MAIN_CO);
- }
- }
-
- static os_error *main__cdir(char *name)
- { os_filestr fcb;
- int c, sc;
- os_error *err;
-
- for (c = 0; name[c]; c++)
- { if (name[c] == '.')
- { sc = name[c];
- name[c] = '\0';
- fcb.name = name;
- fcb.action = 8;
- fcb.end = 0;
- if (err = os_file(&fcb), err) goto fail;
- name[c] = sc;
- }
- }
-
- fcb.name = name;
- fcb.action = 8;
- fcb.end = 0;
- return os_file(&fcb);
-
- fail:
- name[c] = sc;
- return err;
- }
-
- static void main__getdate(main__filedate *d, os_filestr *fcb)
- { if ((fcb->loadaddr & 0xFFF00000) == 0xFFF00000)
- { d->lo = fcb->execaddr;
- d->hi = fcb->loadaddr & 0xFF;
- }
- }
-
- static int main__diffdate(main__filedate *lhs, main__filedate *rhs)
- { if (lhs->hi == rhs->hi)
- return lhs->lo - rhs->lo;
-
- return lhs->hi - rhs->hi;
- }
-
- static os_error *main__copyfile(char *from, char *to)
- { FILE *r, *w;
- os_error *err;
- os_filestr fcb, fcb2;
- main__filedate fd, td;
- char buffer[main__BUFSIZE];
- int len;
-
- fcb.action = 17;
- fcb.name = from;
- if (err = os_file(&fcb), err) return err;
-
- if (fcb.action != 1)
- { fcb.loadaddr = fcb.action;
- fcb.action = 19;
- return os_file(&fcb);
- }
-
- main__getdate(&fd, &fcb);
-
- fcb2.action = 17;
- fcb2.name = to;
- if (err = os_file(&fcb2), err) return err;
-
- if (fcb2.action == 2 || fcb2.action == 3)
- { fcb2.loadaddr = fcb2.action;
- fcb2.action = 19;
- return os_file(&fcb2);
- }
-
- if (fcb2.action != 0)
- { main__getdate(&td, &fcb2);
-
- if (main__diffdate(&fd, &td) <= 0)
- return NULL;
- }
-
- main__yield();
-
- if (r = fopen(from, "r"), !r)
- return misc_err("main8", from);
-
- if (w = fopen(to, "w"), !w)
- { err = misc_err("main8", to);
- goto fail;
- }
-
- main__yield();
- len = fread(buffer, 1, main__BUFSIZE, r);
-
- while (len > 0)
- {
- main__info.bytes += len;
-
- main__yield();
- if (fwrite(buffer, 1, len, w) < len)
- { err = misc_err("main9", to);
- goto fail2;
- }
- main__yield();
- len = fread(buffer, 1, main__BUFSIZE, r);
- }
-
- main__yield();
-
- fclose(r);
- fclose(w);
-
- main__info.files++;
-
- fcb.action = 1;
- fcb.name = to;
-
- return os_file(&fcb);
-
- fail2:
- fclose(w);
-
- fail:
- fclose(r);
- return err;
- }
-
- static os_error *main__copydir(char *from, char *to)
- { os_gbpbstr gbpb;
- char *fromleaf, *toleaf;
- os_error *err;
- char buffer[main__SCANSIZE];
- int n;
- main__finfo *fi;
-
- if (err = main__cdir(to), err) return err;
-
- fromleaf = from + strlen(from) + 1;
- toleaf = to + strlen(to) + 1;
-
- gbpb.action = 10;
- gbpb.file_handle = (int) from;
- gbpb.data_addr = buffer;
-
- gbpb.seq_point = 0;
- gbpb.buf_len = main__SCANSIZE;
- gbpb.wild_fld = "*";
-
- do
- { gbpb.number = 9999; /* as many as possible */
-
- if (err = os_gbpb(&gbpb), err) return err;
-
- fromleaf[-1] = '.';
- toleaf[-1] = '.';
-
- for (n = 0, fi = (main__finfo *) buffer; n < gbpb.number; n++)
- {
- strcpy(fromleaf, (char *) (fi+1));
- strcpy(toleaf, (char *) (fi+1));
-
- switch (fi->type)
- { case 1:
- if (err = main__copyfile(from, to), err)
- goto fail;
- break;
-
- case 2: case 3:
- if (err = main__copydir(from, to), err)
- goto fail;
- break;
- }
-
- main__yield();
-
- fi = (main__finfo *) (((char *) fi) + sizeof(main__finfo) + main__wup(strlen((char *) (fi+1))+1));
- }
-
- fromleaf[-1] = '\0';
- toleaf[-1] = '\0';
-
- } while (gbpb.seq_point >= 0);
-
- return NULL;
-
- fail:
- fromleaf[-1] = '\0';
- toleaf[-1] = '\0';
- return err;
- }
-
- static void main__sysmerge(void *handle)
- { char tobuf[256], frombuf[256];
-
- strcpy(tobuf, main__info.sysname);
- strcat(tobuf, ".!AWShared");
-
- strcpy(frombuf, main__info.awname);
-
- wimpt_complain(main__copydir(frombuf, tobuf));
-
- if (main__info.exitafter)
- exit(0);
- else
- { field_settext(main__w, main__MESSAGE, msgs_lookup(main__info.bytes ? "main14" : "main15"));
- field_settext(main__w, main__SYSNAME, "");
- field_fade(main__w, main__UPDATE, TRUE);
- field_settext(main__w, main__CANCEL, msgs_lookup("main13"));
- field_fade(main__w, main__CANCEL, FALSE);
- }
- }
-
- static void main__alarm(int calledat, void *handle)
- { handle = handle;
-
- main__info.slotstart = calledat;
-
- if (main__co) /* give it a slice of time */
- { goto_co(main__co);
-
- if (!monitor_co(main__co))
- { delete_co(main__co); /* finished */
- main__co = NULL;
- }
- }
-
- alarm_set(calledat + 25, main__alarm, handle);
- }
-
- static void main__update(char *sysname, char *awname, BOOL exitafter)
- { if (main__co) return;
- main__info.sysname = sysname;
- main__info.awname = awname;
- main__info.exitafter = exitafter;
- main__info.files =
- main__info.bytes = 0;
-
- main__co = create_co(main__sysmerge, NULL);
- if (!main__co) werr(TRUE, msgs_lookup("main7"));
- }
-
- static char *main__awname(void)
- { static char myname[257];
- os_regset regs;
-
- regs.r[0] = (int) "AWMerge$Dir";
- regs.r[1] = (int) myname;
- regs.r[2] = 256;
- regs.r[3] = 0;
- regs.r[4] = 3;
-
- if (wimpt_complain(os_swix(OS_ReadVarVal, ®s)))
- return NULL;
-
- myname[regs.r[2]] = '\0';
-
- strcat(myname, ".!AWShared");
- return myname;
- }
-
- static void main__handler(wimp_eventstr *e, void *handle)
- { char *np, sysname[256];
- handle = handle;
-
- switch (e->e)
- { case wimp_ECLOSE:
- exit(1);
-
- case wimp_EOPEN:
- wimpt_complain(wimp_open_wind(&e->data.o));
- break;
-
- case wimp_EBUT:
- switch (e->data.but.m.i)
- { case main__CANCEL:
- exit(1); /* wait for button to pop up */
- break;
-
- case main__UPDATE:
- field_gettext(main__w, main__SYSNAME, sysname, 255);
- field_fade(main__w, main__CANCEL, TRUE);
- field_fade(main__w, main__UPDATE, TRUE);
- field_settext(main__w, main__MESSAGE, msgs_lookup("main8"));
- main__update(sysname, main__awname(), FALSE);
- break;
- }
- break;
-
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- if (main__co) break;
-
- switch (e->data.msg.hdr.action)
- { case wimp_MDATALOAD:
- np = e->data.msg.data.dataload.name +
- strlen (e->data.msg.data.dataload.name) -
- strlen("!System");
- if (misc_strcicmp(np, "!System") == 0)
- { field_settext(main__w, main__SYSNAME, e->data.msg.data.dataload.name);
- field_settext(main__w, main__MESSAGE, msgs_lookup("main4"));
- field_fade(main__w, main__UPDATE, FALSE);
- }
- break;
- }
- }
- }
-
- static os_error *main__interact(void)
- { wimp_wind *wdef;
- os_error *err;
- wimp_wstate state;
- int scx, scy;
- os_regset regs;
- char sysname[257];
- char *sp, *pp;
-
- if (err = misc_template("merger", &wdef), err) return err;
- if (err = wimp_create_wind(wdef, &main__w), err) return err;
- win_register_event_handler(main__w, main__handler, NULL);
-
- field_settext(main__w, main__CANCEL, msgs_lookup("main11"));
- field_settext(main__w, main__UPDATE, msgs_lookup("main12"));
-
- if (err = wimp_get_wind_state(main__w, &state), err) return err;
-
- scx = (bbc_vduvar(bbc_XWindLimit) + 1) << bbc_vduvar(bbc_XEigFactor);
- scy = (bbc_vduvar(bbc_YWindLimit) + 1) << bbc_vduvar(bbc_YEigFactor);
-
- misc_boxto(&state.o.box, (scx - (state.o.box.x1 - state.o.box.x0)) / 2,
- (scy - (state.o.box.y1 - state.o.box.y0)) / 2);
-
- state.o.behind = -1;
-
- if (err = wimp_open_wind(&state.o), err)
- return err;
-
- /* Try to find !System */
-
- regs.r[0] = (int) "System$Path";
- regs.r[1] = (int) sysname;
- regs.r[2] = 256;
- regs.r[3] = 0;
- regs.r[4] = 3;
-
- if (os_swix(OS_ReadVarVal, ®s))
- { field_settext(main__w, main__MESSAGE, msgs_lookup("main5"));
- field_settext(main__w, main__SYSNAME, "");
- field_fade(main__w, main__UPDATE, TRUE);
- }
- else
- { sysname[regs.r[2]] = '\0';
- for (pp = NULL, sp = sysname; *sp > ' ' && *sp != ','; sp++)
- if (*sp == ':' || *sp == '.')
- pp = sp;
-
- if (pp) *pp = '\0';
-
- field_settext(main__w, main__MESSAGE, msgs_lookup("main4"));
- field_settext(main__w, main__SYSNAME, sysname);
- }
-
- return NULL;
- }
-
- int main(int argc, char *argv[])
- { wimpt_wimpversion(200);
- misc_init("AWMerge");
-
- visdelay_init();
- visdelay_begin();
-
- res_init("AWMerge");
- resspr_init();
- alarm_init();
- msgs_init();
- template_init();
-
- if (__versionnumber() < 3.80)
- werr(TRUE, "AWMerge has been linked with an out of date RISC OS library");
-
- main__co = NULL;
-
- /* win_add_unknown_event_processor(main__ukproc, NULL); */
- win_activeinc();
- alarm_set(alarm_timenow() + 25, main__alarm, NULL);
-
- visdelay_end();
-
- if (argc < 2)
- wimpt_complain(main__interact());
- else
- main__update(argv[1], main__awname(), TRUE);
-
- for (;;) event_process();
- }
-