sysstmac.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. /* sysstmac.c Copyright (C) 1989-2002 Codemist Ltd */
  2. /*
  3. * Macintosh system-specific stuff
  4. */
  5. /* This is a better sysstmac.c. I still need to deal with the help
  6. * system, but the dialogs are greatly improved in this version.
  7. */
  8. /*
  9. * This code may be used and modified, and redistributed in binary
  10. * or source form, subject to the "CCL Public License", which should
  11. * accompany it. This license is a variant on the BSD license, and thus
  12. * permits use of code derived from this in either open and commercial
  13. * projects: but it does require that updates to this code be made
  14. * available back to the originators of the package.
  15. * Before merging other code in with this or linking this code
  16. * with other packages or libraries please check that the license terms
  17. * of the other material are compatible with those of this.
  18. */
  19. /* Signature: 0aa59571 08-Apr-2002 */
  20. #include "machine.h"
  21. #include <stdarg.h>
  22. #include <stddef.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "tags.h"
  26. #include "externs.h"
  27. #include <unix.h>
  28. #include <files.h>
  29. #include <unix.h>
  30. #include <retrace.h>
  31. #include <timer.h>
  32. #include <ansi_private.h>
  33. #include "macwin.h"
  34. #include <Dialogs.h>
  35. #undef nil /* #define'd by Think C headers but not wanted (by me) */
  36. int get_home_directory(char *b, int length);
  37. int get_users_home_directory(char *b, int length);
  38. #include "filename.c"
  39. /*
  40. * This is a dummy definition of get_truename, here so that everything
  41. * will link. Both the calling convention used here and the exact
  42. * meaning and implementation may be under gentle review!
  43. */
  44. char *get_truename(char *filename, char *old, size_t n)
  45. {
  46. char *w;
  47. process_file_name(filename, old, n);
  48. if (*filename == 0)
  49. { aerror("truename");
  50. return NULL;
  51. }
  52. w = (char *)(*malloc_hook)(1+strlen(filename));
  53. if (w == NULL) return w;
  54. strcpy(w, filename);
  55. return w;
  56. }
  57. CSLbool file_exists(char *filename, char *old, size_t n, char *tt)
  58. {
  59. FileParam pb;
  60. char buf[FILENAME_MAX];
  61. process_file_name(filename, old, n);
  62. pb.ioNamePtr = __c2p((const char*)filename, buf);
  63. pb.ioFRefNum = 0;
  64. pb.ioFVersNum = 0;
  65. pb.ioFDirIndex = 0;
  66. if (PBGetFInfo((ParmBlkPtr)&pb, 0) != noErr) return NO;
  67. strcpy(tt, ctime(&(pb.ioFlMdDat)));
  68. return YES;
  69. }
  70. char *my_getenv(char *s)
  71. {
  72. return getenv(s);
  73. }
  74. int
  75. myexec(char *filename, ...)
  76. {
  77. int i;
  78. short ans;
  79. EventRecord event;
  80. #if 1 /* __powerc */
  81. FSSpec fsp;
  82. LaunchParamBlockRec lpb;
  83. C2PStr(filename);
  84. ans = FSMakeFSSpec( 0,0,(unsigned char*)filename,&fsp);
  85. P2CStr((unsigned char*)filename);
  86. if (!ans) {
  87. lpb.launchBlockID = extendedBlock;
  88. lpb.launchEPBLength = extendedBlockLen;
  89. lpb.launchFileFlags = 0;
  90. lpb.launchControlFlags = launchContinue + launchNoFileFlags;
  91. lpb.launchAppSpec = &fsp;
  92. lpb.launchAppParameters = 0;
  93. ans = LaunchApplication(&lpb);
  94. }
  95. #else
  96. static struct {
  97. unsigned char *name;
  98. short memuse;
  99. char lc[2];
  100. long extBlocLen;
  101. short fFlags;
  102. long launchFlags;
  103. } buf;
  104. unsigned char pname[255];
  105. extern void _Launch();
  106. buf.name = __c2p(filename, (char *)pname);
  107. buf.memuse = 0;
  108. buf.lc[0] = 'L'; buf.lc[1] = 'C';
  109. buf.extBlocLen = 6;
  110. buf.fFlags = 128; /* RO */
  111. buf.launchFlags = 0xC0000000;
  112. asm {
  113. lea buf,a0
  114. dc.w _Launch
  115. move.w d0,ans;
  116. }
  117. #endif
  118. if (ans==0)
  119. for (i=0; i<20; i++) WaitNextEvent(everyEvent, &event, 10, 0L);
  120. return ans;
  121. }
  122. int my_system(char *s)
  123. {
  124. return myexec(s);
  125. }
  126. void my_pclose(FILE *a)
  127. {
  128. return;
  129. }
  130. int create_directory(char *filename, char *old, size_t n)
  131. {
  132. process_file_name(filename, old, n);
  133. if (*filename==0) return 1;
  134. /* /* */
  135. return 1; /* Unfinished */
  136. }
  137. int delete_file(char *filename, char *old, size_t n)
  138. {
  139. process_file_name(filename, old, n);
  140. if (*filename==0) return 1;
  141. return remove(filename);
  142. }
  143. int get_home_directory(char *b, int length)
  144. {
  145. char *w = my_getenv("home");
  146. if (w != NULL) strcpy(b, w);
  147. else
  148. strcpy(b, ":"); /* Not satisfactory */
  149. return strlen(b);
  150. }
  151. static int get_users_home_directory(char *b, int length)
  152. {
  153. char *w, h[LONGEST_LEGAL_FILENAME];
  154. sprintf(h, "home$%s", b);
  155. w = my_getenv(h);
  156. if (w != NULL) strcpy(b, w);
  157. else
  158. strcpy(b, ":"); /* Not satisfactory */
  159. return strlen(b);
  160. }
  161. int batchp(void)
  162. {
  163. return !isatty(fileno(stdin));
  164. }
  165. /*static char CurApName[64];*/
  166. /*
  167. * The next procedure is responsible for establishing information about
  168. * where the main checkpoint image should be recovered from, and where
  169. * and fasl files should come from.
  170. */
  171. char *find_image_directory(int argc, char *argv[])
  172. {
  173. char image[LONGEST_LEGAL_FILENAME];
  174. char pgmname[LONGEST_LEGAL_FILENAME];
  175. char *w;
  176. /*
  177. * For the Macintosh I use the data fork of the application image as
  178. * a place to store useful stuff.
  179. */
  180. WDPBRec pb1;
  181. CInfoPBRec pb2;
  182. int i, j, r, p = LONGEST_LEGAL_FILENAME;
  183. long id;
  184. pgmname[--p] = 0;
  185. sprintf(image, "%#s", LMGetCurApName()); /* name of current application */
  186. strcpy(program_name, image);
  187. i = strlen(image);
  188. strcpy(image+i, " Image");
  189. i += 6;
  190. p -= i;
  191. memcpy(&pgmname[p], image, i); /* copy to the end of a buffer */
  192. pb1.ioNamePtr = (unsigned char *)image;
  193. PBHGetVolSync(&pb1); /* find current working dir/volume */
  194. id = pb1.ioWDDirID; /* working directory identifier */
  195. while (id != 0) {
  196. pb2.dirInfo.ioFDirIndex = -1; /* use dir ID, not file name/index */
  197. pb2.dirInfo.ioVRefNum = pb1.ioVRefNum; /* look in this volume */
  198. pb2.dirInfo.ioDrDirID = id; /* get info about this directory */
  199. pb2.dirInfo.ioNamePtr = (unsigned char *)image;
  200. r = PBGetCatInfoSync(&pb2); /* Read catalogue - find name & parent */
  201. if (r != 0) break; /* failed - must be at top of tree */
  202. id = pb2.dirInfo.ioDrParID; /* go on up to parent directory */
  203. i = image[0] & 0xff; /* length of name of this directory */
  204. pgmname[--p] = ':';
  205. p -= i;
  206. if (p < 10) {
  207. fprintf(stderr, "\nPlease re-install this package nearer the top of\n");
  208. fprintf(stderr, "your directory hierarchy. It will not work this far down\n");
  209. abort();
  210. }
  211. memcpy(&pgmname[p], &image[1], i); /* stick names together with ':'s */
  212. }
  213. strcpy(image, &pgmname[p]); /* Put complete name in convenient place */
  214. /*
  215. * I copy from local vectors into malloc'd space to hand my
  216. * answer back.
  217. */
  218. w = (char *)(*malloc_hook)(1+strlen(&pgmname[p]));
  219. /*
  220. * The error exit here seem unsatisfactory...
  221. */
  222. if (w == NULL)
  223. { fprintf(stderr, "\n+++ Panic - run out of space\n");
  224. exit(EXIT_FAILURE);
  225. }
  226. strcpy(w, &pgmname[p]);
  227. return w;
  228. }
  229. int truncate_file(FILE *f, long int where)
  230. {
  231. short a;
  232. short ff = f->refnum;
  233. if (fflush(f) != 0) return 1;
  234. a = SetEOF(ff, where); /* Returns zero if successs */
  235. return (int)a;
  236. }
  237. static CSLbool ticker_active = NO;
  238. int32 software_ticks = INITIAL_SOFTWARE_TICKS;
  239. int32 software_tick_count = 0, prev_software_tick_count = 0;
  240. void add_ticker(void)
  241. {
  242. if (ticker_active) return;
  243. if (countdown<0) countdown = SOFTWARE_TICKS; /* Reset if expired */
  244. ticker_active = YES;
  245. }
  246. void remove_ticker(void)
  247. {
  248. /* if (!ticker_active) return; */
  249. ticker_active = NO;
  250. }
  251. int file_readable(char *filename, char *old, size_t n)
  252. {
  253. FILE *fp;
  254. process_file_name(filename, old, n);
  255. if (*filename == 0) return 0;
  256. /* The "correct" way to do this is via stat, but this is much simpler! */
  257. fp = fopen(filename,"r");
  258. if (fp == NULL) return 0;
  259. else
  260. { fclose(fp);
  261. return 1;
  262. }
  263. }
  264. int file_writeable(char *filename, char *old, size_t n)
  265. {
  266. FILE *fp;
  267. process_file_name(filename, old, n);
  268. if (*filename == 0) return 0;
  269. fp = fopen(filename,"a");
  270. if (fp == NULL) return 0;
  271. else
  272. { fclose(fp);
  273. return 1;
  274. }
  275. }
  276. int rename_file(char *from_name, char *from_old, size_t from_size,
  277. char *to_name, char *to_old, size_t to_size)
  278. {
  279. process_file_name(from_name, from_old, from_size);
  280. process_file_name(to_name, to_old, to_size);
  281. if (*from_name == 0 || *to_name == 0) return 0;
  282. return rename(from_name,to_name);
  283. }
  284. int change_directory(char *filename, char *old, size_t n)
  285. {
  286. WDPBRec P;
  287. Str255 pname;
  288. process_file_name(filename, old, n);
  289. if (*filename==0) return 1;
  290. strcpy((char*)pname, filename);
  291. C2PStr((char*)pname);
  292. P.ioNamePtr = pname;
  293. return PBHSetVol(&P, 0);
  294. }
  295. int directoryp(char *filename, char *old, size_t n)
  296. {
  297. struct stat statrec;
  298. process_file_name(filename, old, n);
  299. stat(filename, &statrec);
  300. return S_ISDIR(statrec.st_mode);
  301. }
  302. int get_current_directory(char *s, int n)
  303. {
  304. return 0;
  305. }
  306. #include "scandir.c"
  307. void list_directory_members(char *filename, char *old,
  308. size_t n, directory_callback *fn)
  309. {
  310. process_file_name(filename, old, n);
  311. scan_files(filename, fn);
  312. }
  313. /* ************************************************************ */
  314. /* ********** StdWin Interface ******************************** */
  315. /* ************************************************************ */
  316. /* ************************************************************ */
  317. #include "stdwin.h"
  318. #include "tools.h"
  319. #include "editwin.h"
  320. #include "text.h" /* Rather a cheat as I need to look
  321. * low down in here */
  322. static WINDOW *CSL_window;
  323. static EDITWIN *CSL_ew;
  324. static EDITWIN *help_ew;
  325. int max_nlines = 250;
  326. #ifndef HELP_DATA
  327. #define HELP_DATA "help.data"
  328. #endif
  329. #ifndef HELP_INDEX
  330. #define HELP_INDEX "help.index"
  331. #endif
  332. static MENU *adminmenu;
  333. static MENU *editmenu;
  334. static MENU *loadmenu;
  335. static MENU *libmenu;
  336. static MENU *switchmenu;
  337. static MENU *helpmenu;
  338. /* static MENU *fontmenu; */
  339. /* static MENU *sizemenu; */
  340. #define ADMIN_MENU (1001)
  341. #define MMFile 0
  342. #define MMSaveas 1
  343. #define MMHelp 2 /* main window menu items */
  344. #define MMHelpSel 3
  345. #define MMHelpAsk 4
  346. #define MMInt 5
  347. #define MMAbort 6
  348. #define MMPause 7
  349. #define MMDummy 8
  350. #define MMTime 9
  351. #define MMSpace 10
  352. #define MMOTime 11
  353. #define EDIT_MENU (1002)
  354. #define MMCut 0
  355. #define MMCopy 1
  356. #define MMPaste 2
  357. #define MMClear 3
  358. #define MMSelAll 4
  359. #define HELP_LEN (36)
  360. #define HELP_MENU (1003)
  361. static struct hlp {
  362. char name[HELP_LEN];
  363. long offset;
  364. int length;
  365. } *hlp;
  366. int help_length;
  367. int helpstamp;
  368. #define LOAD_MENU (1004)
  369. static char *loads[] = {
  370. (char*)NULL};
  371. #define LIB_MENU (1005)
  372. static char *libraries[] = {
  373. (char*)NULL};
  374. #define SWITCH_MENU (1006)
  375. static struct sw {
  376. char *name;
  377. int state;
  378. } switches[] = {
  379. {(char*)NULL, 0}};
  380. /* #define FONT_MENU (1007) */
  381. /* static char *fonts[] = { "Monaco", "Courier", NULL}; */
  382. /* static current_font = 0; */
  383. /* #define SIZE_MENU (1008) */
  384. /* static char *sizes[] = { "8", "9", "10", "11", "12", NULL}; */
  385. /* static current_size = 1; */
  386. get_full_name(char *name, FSSpec *spec)
  387. {
  388. char nn[LONGEST_LEGAL_FILENAME];
  389. char mm[LONGEST_LEGAL_FILENAME];
  390. char *w;
  391. int i, r;
  392. int p = LONGEST_LEGAL_FILENAME;
  393. long id;
  394. CInfoPBRec pb2;
  395. nn[--p] = 0;
  396. i = spec->name[0];
  397. p -= i;
  398. memcpy(&nn[p], &(spec->name[1]), i); /* Copy base name */
  399. id = spec->parID;
  400. while (id != 0) {
  401. pb2.dirInfo.ioFDirIndex = -1; /* use dirID, not file name/index */
  402. pb2.dirInfo.ioVRefNum = spec->vRefNum; /* look in this volume */
  403. pb2.dirInfo.ioDrDirID = id; /* get info about this directory */
  404. pb2.dirInfo.ioNamePtr = (unsigned char *)mm;
  405. if (PBGetCatInfoSync(&pb2)) break; /* Read catalogue - find name & parent*/
  406. /* Fail at top of tree? */
  407. id = pb2.dirInfo.ioDrParID; /* go on up to parent directory */
  408. i = mm[0] & 0xff; /* length of name of this directory */
  409. nn[--p] = ':';
  410. p -= i;
  411. if (p < 10) {
  412. fprintf(stderr, "\nCannot access file so far from root\n");
  413. strcpy(name, "");
  414. return FALSE;
  415. }
  416. memcpy(&nn[p], &mm[1], i); /* stick names together with ':'s */
  417. }
  418. strcpy(name, &nn[p]);
  419. return TRUE;
  420. }
  421. static void menusetup(void)
  422. {
  423. MENU *mp;
  424. int i;
  425. adminmenu= mp= wmenucreate(ADMIN_MENU, "Admin");
  426. wmenuadditem(mp, "Input file...", 'F');
  427. wmenuadditem(mp, "Saveas...", -1);
  428. wmenuadditem(mp, "Help Window", 'H');
  429. wmenuadditem(mp, "Help Selection", 'S');
  430. wmenuadditem(mp, "Help Topic...", -1);
  431. wmenuadditem(mp, "Interrupt", 'I');
  432. wmenuadditem(mp, "Quit", 'Q');
  433. wmenuadditem(mp, "Pause", 'P');
  434. wmenuadditem(mp, "---------", -1);
  435. wmenuadditem(mp, "0.00", -1);
  436. wmenuadditem(mp, "[GC 0]", -1);
  437. wmenuadditem(mp, "", -1);
  438. wmenuadditem(mp, "", -1);
  439. wmenuenable(mp, MMDummy, NO);
  440. wmenuenable(mp, MMTime, NO);
  441. wmenuenable(mp, MMSpace, NO);
  442. wmenuenable(mp, MMOTime, NO);
  443. editmenu= mp= wmenucreate(EDIT_MENU, "Edit");
  444. wmenuadditem(mp, "Cut", 'X');
  445. wmenuadditem(mp, "Copy", 'C');
  446. wmenuadditem(mp, "Paste", 'V');
  447. wmenuadditem(mp, "Clear", 'B');
  448. wmenuadditem(mp, "Select All", 'A');
  449. /* fontmenu= mp= wmenucreate(FONT_MENU, "Font"); */
  450. /* for (i=0; fonts[i]!= NULL; i++) wmenuadditem(mp, fonts[i], -1); */
  451. /* wmenucheck(mp, current_font, YES); */
  452. /* sizemenu= mp= wmenucreate(SIZE_MENU, "FontSize"); */
  453. /* for (i=0; sizes[i]!= NULL; i++) wmenuadditem(mp, sizes[i], -1); */
  454. /* wmenucheck(mp, current_size, YES); */
  455. /* helpmenu= mp= wmenucreate(HELP_MENU, "Help"); */
  456. helpmenu = mp = (MENU*)GetMenu(HELP_MENU);
  457. if (mp != 0) /* This checks against problems */
  458. InsertMenu((MenuHandle)mp, 0); DrawMenuBar();
  459. {
  460. FILE* hlpindx = fopen(HELP_INDEX, "r");
  461. if (hlpindx != NULL) {
  462. fscanf(hlpindx, "%d\n", &helpstamp);
  463. fscanf(hlpindx, "%d", &help_length);
  464. hlp = (struct hlp*) (*malloc_hook)(1+help_length, sizeof(struct hlp));
  465. if (hlp == NULL) {
  466. help_length = 0;
  467. goto loader; /* Insufficient memory */
  468. }
  469. for (i=0; i<help_length; i++) {
  470. int ch, p=0;
  471. fscanf(hlpindx, "%d %d ", &(hlp[i].offset), &(hlp[i].length));
  472. while (p<HELP_LEN-1) {
  473. ch = getc(hlpindx);
  474. if (ch == '\n') break;
  475. hlp[i].name[p++] = ch; /* Was toupper here */
  476. }
  477. while (ch != '\n') ch = getc(hlpindx);
  478. hlp[i].name[p] = '\0';
  479. /*wmenuadditem(mp, hlp[i].name, -1);*/
  480. }
  481. fclose(hlpindx);
  482. }
  483. else {
  484. help_length = 0;
  485. strcpy(hlp[0].name, "No help");
  486. hlp[0].offset = -1;
  487. hlp[0].length = 0;
  488. }
  489. }
  490. loader:
  491. loadmenu= mp= wmenucreate(LOAD_MENU, "Load");
  492. for (i=0; loads[i]!= NULL; i++)
  493. wmenuadditem(mp, loads[i], -1);
  494. libmenu= mp= wmenucreate(LIB_MENU, "Library");
  495. for (i=0; libraries[i]!= NULL; i++)
  496. wmenuadditem(mp, libraries[i], -1);
  497. switchmenu= mp= wmenucreate(SWITCH_MENU, "Switch");
  498. for (i=0; switches[i].name!= NULL; i++) {
  499. wmenuadditem(mp, switches[i].name, -1);
  500. wmenucheck(mp, i, switches[i].state);
  501. }
  502. }
  503. static void fixmenus(void)
  504. {
  505. CSLbool focus;
  506. focus= (CSL_ew->tp->foclen != 0);
  507. wmenuenable(editmenu, MMCut, focus);
  508. wmenuenable(editmenu, MMCopy, focus);
  509. wmenuenable(editmenu, MMClear, focus);
  510. wmenuenable(adminmenu, MMHelpSel, focus);
  511. /* wmenuenable(editmenu, MMPaste, YES); */
  512. if (help_ew != NULL) {
  513. focus= (help_ew->tp->foclen != 0);
  514. if (focus) {
  515. wmenuenable(editmenu, MMCut, focus);
  516. wmenuenable(editmenu, MMCopy, focus);
  517. wmenuenable(editmenu, MMClear, focus);
  518. wmenuenable(adminmenu, MMHelpSel, focus);
  519. }
  520. }
  521. }
  522. #define TTYBUF_SIZE 256
  523. static char tty_buff[TTYBUF_SIZE];
  524. static int tty_index = 0;
  525. static CSLbool tty_ready = NO;
  526. static int tty_nnl = 0;
  527. static int tty_offset;
  528. #define STDOUT_BUFSIZE 1024
  529. #define LONGEST_PRINTF 120
  530. static char stdout_buffer[STDOUT_BUFSIZE];
  531. static char *stdout_p;
  532. static int stdout_n;
  533. extern int _w_font;
  534. void start_up_window_manager(int use_wimp)
  535. {
  536. int CSL_width, CSL_height;
  537. _w_font = 4; /* Font family */
  538. menusetup();
  539. wsetdefwinsize(82*wcharwidth('0'), 25*wlineheight());
  540. CSL_ew = ewcreate(NULL);
  541. CSL_window = CSL_ew->win;
  542. wsettitle(CSL_window, "Reduce 3.7");
  543. fixmenus();
  544. /* wmenudetach(CSL_window, helpmenu); */
  545. wmenuenable(editmenu, MMTime, NO); /* These are not selectable */
  546. wmenuenable(editmenu, MMOTime, NO); /* These are not selectable */
  547. wmenuenable(editmenu, MMSpace, NO);
  548. wmenudetach(CSL_window, loadmenu); /* These are only valid when reading */
  549. wmenudetach(CSL_window, libmenu);
  550. wmenudetach(CSL_window, switchmenu);
  551. /* wmessage("Welcome to X-Reduce\n\n"); */
  552. stdout_n = 0;
  553. stdout_p = stdout_buffer;
  554. help_ew = NULL;
  555. }
  556. static char *help_buff;
  557. CSLbool
  558. helpwindow(int index)
  559. {
  560. FILE* help = fopen(HELP_DATA, "r");
  561. int ch;
  562. int width, height;
  563. int size = hlp[index].length;
  564. if (help == NULL) return FALSE;
  565. fscanf(help, "%d\n", &ch);
  566. if (ch != helpstamp) return FALSE; /* not same generation */
  567. /* fprintf(stderr, "Help on index %d offset=%d size=%d\n",
  568. index, hlp[index].offset, hlp[index].length); */
  569. fseek(help, hlp[index].offset, SEEK_SET);
  570. if (help_ew) {
  571. (*free_hook)(help_buff);
  572. tesetfocus(help_ew->tp, 0, tegetlen(help_ew->tp));
  573. ewreplace(help_ew, "");
  574. }
  575. else {
  576. help_ew = ewcreate(NULL);
  577. wsettitle(help_ew->win, "Help for Reduce 3.7");
  578. }
  579. help_buff = (*malloc_hook)(size+1);
  580. if (help_buff == NULL) {
  581. dprintf("Insufficient memory");
  582. wfleep();
  583. return FALSE;
  584. }
  585. /* fprintf(stderr, "reading %d bytes to buffer\n", size); */
  586. fread(help_buff, size, 1, help);
  587. /* fprintf(stderr, "help starts as %.100s\n", help_buff); */
  588. help_buff[size] = '\0';
  589. wmenudetach(help_ew->win, loadmenu);
  590. wmenudetach(help_ew->win, libmenu);
  591. wmenudetach(help_ew->win, switchmenu);
  592. /* wmenudetach(help_ew->win, adminmenu); */
  593. ewreplace(help_ew, help_buff);
  594. tesetfocus(help_ew->tp, 0, 0);
  595. fclose(help);
  596. return TRUE;
  597. }
  598. void help_buffer(char *buf, int len)
  599. {
  600. int i;
  601. for (i=0; i<help_length; i++) {
  602. /* fprintf(stderr, "Check against %s\n", hlp[i].name); */
  603. if (strncmp(hlp[i].name, buf, len) == 0) {
  604. helpwindow(i);
  605. break;
  606. }
  607. }
  608. if (i == help_length) wfleep();
  609. }
  610. void help_selection(EDITWIN *ew)
  611. {
  612. int i;
  613. char *text;
  614. TEXTEDIT *tp = ew->tp;
  615. if (tp->foclen == 0) {
  616. wfleep();
  617. return;
  618. }
  619. text= tegettext(tp);
  620. help_buffer(text+tp->foc, tp->foclen);
  621. }
  622. extern int pause_set = 0;
  623. void event_loop(void)
  624. {
  625. for (;;) { /* Loop while there are events */
  626. EVENT e;
  627. EDITWIN *ew;
  628. CSLbool closed;
  629. wgetevent(&e);
  630. /* fprintf(stderr, "Event: type=%d win=%p data=%d\n", e.type, e.window, e.u.command); */
  631. ew= ewfind(e.window);
  632. if (ew == CSL_ew) {
  633. switch (e.type) {
  634. case WE_CHAR:
  635. switch(e.u.character) {
  636. case 'A'-'@': /* ^A == beginning of line */
  637. {
  638. TEXTEDIT* tp = CSL_ew->tp;
  639. tesetfocus(tp,
  640. zsubgap(tp->start[tewhichline(tp,
  641. zaddgap(tp->foc),
  642. tp->focprev)]),
  643. -1);
  644. break;
  645. }
  646. case 'B'-'@': /* ^B back a character */
  647. tearrow(CSL_ew->tp, WC_LEFT);
  648. break;
  649. case 127: /* Delete character */
  650. case 'D'-'@': /* ^D delete at cursor */
  651. {
  652. TEXTEDIT* tp = CSL_ew->tp;
  653. tearrow(tp, WC_RIGHT);
  654. {
  655. int f1 = tp->foc;
  656. int i;
  657. int ss = tp->start[tp->nlines-tty_nnl];
  658. int ee = ss + tty_offset;
  659. if (ss <= f1 && f1 < ee) {
  660. if (tty_offset>0) tty_offset--;
  661. }
  662. e.type = WE_COMMAND;
  663. e.u.command = WC_BACKSPACE;
  664. ewevent(ew,&e, NULL);
  665. break;
  666. }
  667. }
  668. case 'E'-'@': /* ^E end of line */
  669. {
  670. TEXTEDIT* tp = CSL_ew->tp;
  671. int line = tewhichline(tp, zaddgap(tp->foc), tp->focprev);
  672. int xxx = zsubgap(tetextround(tp, line, tp->right));
  673. tesetfocus(tp, xxx, -1);
  674. break;
  675. }
  676. case 'F'-'@': /* ^F forward a character */
  677. tearrow(CSL_ew->tp, WC_RIGHT);
  678. break;
  679. case 'K'-'@': /* ^K kill to end of line */
  680. { /* This does not take notice of offset */
  681. TEXTEDIT* tp = CSL_ew->tp;
  682. int f1;
  683. int i;
  684. int ss;
  685. int ee;
  686. tesetfocus(tp,
  687. tp->foc,
  688. zsubgap(tetextround(tp,
  689. tewhichline(tp, zaddgap(tp->foc),
  690. tp->focprev),
  691. tp->right)));
  692. f1 = tp->foc;
  693. ss = tp->start[tp->nlines-tty_nnl];
  694. ee = ss + tty_offset;
  695. for (i=0; i<tp->foclen; i++)
  696. if (ss <= f1+i && f1+i < ee) {
  697. if (tty_offset>0) tty_offset--;
  698. }
  699. ewreplace(ew, "");
  700. break;
  701. }
  702. case 'N'-'@': /* ^N next line */
  703. tearrow(CSL_ew->tp, WC_DOWN);
  704. break;
  705. case 'P'-'@': /* ^P previous line */
  706. tearrow(CSL_ew->tp, WC_UP);
  707. break;
  708. case 'Z'-'@': /* ^Z move to end of buffer */
  709. {
  710. TEXTEDIT* tp = CSL_ew->tp;
  711. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  712. break;
  713. }
  714. default:
  715. ewevent(ew, &e, &closed);
  716. if (tty_ready == YES && !pause_set) return;
  717. }
  718. break;
  719. case WE_COMMAND:
  720. switch (e.u.command) {
  721. case WC_CANCEL:
  722. interrupt_pending = YES;
  723. break;
  724. case WC_CLOSE:
  725. pause_for_user();
  726. exit(1);
  727. case WC_RETURN:
  728. {
  729. TEXTEDIT* tp = CSL_ew->tp;
  730. int f1 = zaddgap(tp->foc);
  731. int ee = tp->start[tp->nlines-tty_nnl] + tty_offset;
  732. /* printf("f1=%d ee=%d offset=%d\n", f1, ee, tty_offset); */
  733. if (f1 >= ee) {
  734. tty_nnl++;
  735. if (f1 == tp->buflen) {
  736. ewevent(ew, &e, &closed);
  737. tty_ready = YES;
  738. if (!pause_set) return;
  739. }
  740. }
  741. }
  742. break;
  743. case WC_DEL:
  744. case WC_BACKSPACE:
  745. /* Need to check to see if we are deleting
  746. * in the offset area */
  747. {
  748. TEXTEDIT* tp = CSL_ew->tp;
  749. int f1 = tp->foc;
  750. int i;
  751. int ss = tp->start[tp->nlines-tty_nnl];
  752. int ee = ss + tty_offset;
  753. if (tp->foclen == 0) {
  754. if (ss < f1 && f1 <= ee && tty_offset>0) tty_offset--;
  755. }
  756. else {
  757. for (i=0; i<tp->foclen; i++) {
  758. if (ss <= f1+i && f1+i < ee && tty_offset>0) tty_offset--;
  759. }
  760. }
  761. }
  762. break;
  763. case WC_LEFT:
  764. case WC_RIGHT:
  765. default:
  766. break;
  767. }
  768. ewevent(ew, &e, &closed);
  769. break;
  770. case WE_MENU:
  771. switch (e.u.m.id) {
  772. case ADMIN_MENU:
  773. switch (e.u.m.item) {
  774. case MMFile:
  775. {
  776. short wd;
  777. TEXTEDIT *tp = CSL_ew->tp;
  778. struct StandardFileReply sfr;
  779. StandardGetFile(NULL, -1, NULL, &sfr);
  780. if (sfr.sfGood && !sfr.sfIsFolder && !sfr.sfIsVolume) {
  781. char name[256];
  782. if (!get_full_name(name, &(sfr.sfFile))) break;
  783. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  784. ewreplace(CSL_ew, "in \"");
  785. ewreplace(CSL_ew, name);
  786. ewreplace(CSL_ew, "\"");
  787. /*tetoend(tp); */
  788. }
  789. break;
  790. }
  791. case MMSaveas:
  792. ewsaveas(CSL_ew);
  793. break;
  794. case MMHelp:
  795. helpwindow(0);
  796. break;
  797. case MMHelpSel:
  798. help_selection(ew);
  799. break;
  800. case MMHelpAsk:
  801. {
  802. char buf[40];
  803. int len;
  804. if (waskstr("Help on topic", buf, 40)) {
  805. len = strlen(buf);
  806. help_buffer(buf, len);
  807. }
  808. }
  809. break;
  810. case MMAbort:
  811. pause_for_user();
  812. exit(1);
  813. case MMInt:
  814. interrupt_pending = YES;
  815. return;
  816. case MMPause:
  817. pause_set = !pause_set;
  818. wmenucheck(adminmenu, MMPause, pause_set);
  819. if (pause_set) wsettitle(CSL_window, "<Paused> Reduce 3.7");
  820. else wsettitle(CSL_window, "Reduce 3.7");
  821. break;
  822. }
  823. break;
  824. case EDIT_MENU:
  825. switch (e.u.m.item) {
  826. case MMCopy:
  827. ewcopy(ew);
  828. break;
  829. case MMPaste:
  830. {
  831. char* text = wgetclip();
  832. char* tt = text;
  833. int valid = YES;
  834. if (text == NULL) wfleep();
  835. else {
  836. TEXTEDIT *tp = CSL_ew->tp;
  837. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  838. while (*tt != '\0')
  839. if (*tt++=='\n') valid = NO;
  840. if (valid) ewreplace(ew, text);
  841. else /*wmessage("Cannot Paste text including new line"); */
  842. Alert(129, NULL);
  843. }
  844. }
  845. break;
  846. case MMCut:
  847. ewcopy(ew);
  848. case MMClear:
  849. /* Need to check to see if we are deleting
  850. * in the offset area */
  851. {
  852. TEXTEDIT* tp = CSL_ew->tp;
  853. int f1 = tp->foc;
  854. int i;
  855. int ss = tp->start[tp->nlines-tty_nnl];
  856. int ee = ss + tty_offset;
  857. for (i=0; i<tp->foclen; i++)
  858. if (ss <= f1+i && f1+i < ee) {
  859. if (tty_offset>0) tty_offset--;
  860. }
  861. }
  862. ewreplace(ew, "");
  863. break;
  864. case MMSelAll:
  865. tesetfocus(ew->tp, 0, tegetlen(ew->tp));
  866. break;
  867. }
  868. break;
  869. case LOAD_MENU:
  870. {
  871. char *load = "load_package ";
  872. char *tt = loads[e.u.m.item];
  873. TEXTEDIT *tp = CSL_ew->tp;
  874. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  875. tp->aim= UNDEF;
  876. tp->focprev= FALSE;
  877. ewreplace(CSL_ew, load);
  878. ewreplace(CSL_ew, tt);
  879. tty_ready = YES;
  880. ewreplace(CSL_ew, ";\n");
  881. tty_nnl++;
  882. wmenuenable(loadmenu, e.u.m.item, NO);
  883. return;
  884. }
  885. case LIB_MENU:
  886. {
  887. char *load = "load_package ";
  888. char *tt = libraries[e.u.m.item];
  889. TEXTEDIT *tp = CSL_ew->tp;
  890. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  891. tp->aim= UNDEF;
  892. tp->focprev= FALSE;
  893. ewreplace(CSL_ew, load);
  894. ewreplace(CSL_ew, tt);
  895. tty_ready = YES;
  896. ewreplace(CSL_ew, ";\n");
  897. tty_nnl++;
  898. wmenuenable(loadmenu, e.u.m.item, NO);
  899. return;
  900. }
  901. case SWITCH_MENU:
  902. {
  903. char *load = (switches[e.u.m.item].state ? "off " : "on ");
  904. char *tt = switches[e.u.m.item].name;
  905. TEXTEDIT *tp = CSL_ew->tp;
  906. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  907. ewreplace(CSL_ew, load);
  908. ewreplace(CSL_ew, tt);
  909. tty_ready = YES;
  910. ewreplace(CSL_ew, ";\n");
  911. tty_nnl++;
  912. switches[e.u.m.item].state = ~ switches[e.u.m.item].state;
  913. wmenucheck(switchmenu, e.u.m.item, switches[e.u.m.item].state);
  914. return;
  915. }
  916. case HELP_MENU:
  917. helpwindow(e.u.m.item);
  918. break;
  919. /* case FONT_MENU: */
  920. /* if (ew == NULL) { */
  921. /* wfleep(); */
  922. /* break; */
  923. /* } */
  924. /* { */
  925. /* int f = wsetfont(fonts[e.u.m.item]); */
  926. /* if (f) { */
  927. /* TEXTATTR attr; */
  928. /* extern void getinfo_(); */
  929. /* wgetwintextattr(CSL_window, &attr); */
  930. /* attr.font = f; */
  931. /* wsetwintextattr(CSL_window, &attr); */
  932. /* getfinfo_(); */
  933. /* wmenucheck(fontmenu, current_font, NO); */
  934. /* wmenucheck(fontmenu, current_font = e.u.m.item, YES); */
  935. /* } */
  936. /* } */
  937. /* break; */
  938. /* case SIZE_MENU: */
  939. /* if (ew == NULL) { */
  940. /* wfleep(); */
  941. /* break; */
  942. /* } */
  943. /* wsetsize(atoi(sizes[e.u.m.item])); */
  944. /* { */
  945. /* TEXTATTR attr; */
  946. /* extern void getinfo_(); */
  947. /* wgetwintextattr(CSL_window, &attr); */
  948. /* attr.size = atoi(sizes[e.u.m.item]); */
  949. /* wsetwintextattr(CSL_window, &attr); */
  950. /* getfinfo_(); */
  951. /* } */
  952. /* wmenucheck(sizemenu, current_size, NO); */
  953. /* wmenucheck(sizemenu, current_size = e.u.m.item, YES); */
  954. /* break; */
  955. }
  956. break;
  957. case WE_CLOSE:
  958. pause_for_user();
  959. exit(1);
  960. default:
  961. ewevent(ew, &e, &closed);
  962. break;
  963. }
  964. }
  965. else if (ew == help_ew) {
  966. /* printf("%d %d\n", e.type, e.u.character); */
  967. switch (e.type) {
  968. case WE_MOUSE_UP: /* Extend to a word on single click */
  969. if (!ew->tp->mdown) break;
  970. ew->tp->mdown= FALSE;
  971. if (e.u.where.clicks > 1) {
  972. teclicknew(ew->tp, e.u.where.h, e.u.where.v, FALSE, TRUE);
  973. help_selection(ew);
  974. }
  975. break;
  976. case WE_CHAR:
  977. switch(e.u.character) {
  978. case 'A'-'@': /* ^A == beginning of line */
  979. {
  980. TEXTEDIT* tp = help_ew->tp;
  981. tesetfocus(tp,
  982. zsubgap(tp->start[tewhichline(tp,
  983. zaddgap(tp->foc),
  984. tp->focprev)]),
  985. -1);
  986. break;
  987. }
  988. case 'B'-'@': /* ^B back a character */
  989. tearrow(help_ew->tp, WC_LEFT);
  990. break;
  991. case 'E'-'@': /* ^E end of line */
  992. {
  993. TEXTEDIT* tp = help_ew->tp;
  994. int line = tewhichline(tp, zaddgap(tp->foc), tp->focprev);
  995. int xxx = zsubgap(tetextround(tp, line, tp->right));
  996. tesetfocus(tp, xxx, -1);
  997. break;
  998. }
  999. case 'F'-'@': /* ^F forward a character */
  1000. tearrow(help_ew->tp, WC_RIGHT);
  1001. break;
  1002. case 'K'-'@': /* ^K is Page up */
  1003. {
  1004. int i;
  1005. int j;
  1006. wgetdefwinsize(&i, &j);
  1007. j = j/wlineheight() -1;
  1008. for (i=0; i<j; i++) tearrow(help_ew->tp, WC_UP);
  1009. break;
  1010. }
  1011. case 'N'-'@': /* ^N next line */
  1012. tearrow(help_ew->tp, WC_DOWN);
  1013. break;
  1014. case 'P'-'@': /* ^P previous line */
  1015. tearrow(help_ew->tp, WC_UP);
  1016. break;
  1017. case 'Z'-'@': /* ^Z move to end of buffer */
  1018. {
  1019. TEXTEDIT* tp = help_ew->tp;
  1020. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  1021. break;
  1022. }
  1023. case 'V'-'@': /* ^V page down */
  1024. case ' ':
  1025. case 'L'-'@': /* ^L is Page down */
  1026. {
  1027. int i;
  1028. int j;
  1029. wgetdefwinsize(&i, &j);
  1030. j = j/wlineheight() -1;
  1031. for (i=0; i<j; i++) tearrow(help_ew->tp, WC_DOWN);
  1032. break;
  1033. }
  1034. case 'i':
  1035. case 'I':
  1036. {
  1037. char buf[40];
  1038. int len;
  1039. if (waskstr("Help on topic", buf, 40)) {
  1040. len = strlen(buf);
  1041. help_buffer(buf, len);
  1042. }
  1043. break;
  1044. }
  1045. case 'n':
  1046. case 'N':
  1047. {
  1048. extern char *strstr(const char*, const char*);
  1049. char *p = strstr((const char*)help_buff, (const char*)"Next: ");
  1050. int i = 0;
  1051. if (p == NULL) {
  1052. wfleep(); break;
  1053. }
  1054. p += 6;
  1055. while (p[i]!= ',') i++;
  1056. help_buffer(p,i);
  1057. break;
  1058. }
  1059. case 'p':
  1060. case 'P':
  1061. {
  1062. extern char *strstr(const char*, const char*);
  1063. char *p = strstr((const char*)help_buff, "Prev: ");
  1064. int i = 0;
  1065. if (p == NULL) {
  1066. wfleep(); break;
  1067. }
  1068. p += 6;
  1069. while (p[i]!= ',') i++;
  1070. help_buffer(p,i);
  1071. break;
  1072. }
  1073. case 'q':
  1074. case 'Q':
  1075. help_ew->saved = TRUE; /* Fake it */
  1076. (*free_hook)(help_buff);
  1077. ewclose(help_ew);
  1078. help_ew = 0;
  1079. break;
  1080. case 'u':
  1081. case 'U':
  1082. {
  1083. extern char *strstr(const char*, const char*);
  1084. char *p = strstr((const char*)help_buff, "Up: ");
  1085. int i = 0;
  1086. if (p == NULL) {
  1087. wfleep(); break;
  1088. }
  1089. p += 4;
  1090. while (p[i]!= '\n') i++;
  1091. help_buffer(p,i);
  1092. break;
  1093. }
  1094. }
  1095. break;
  1096. case WE_MENU:
  1097. switch (e.u.m.id) {
  1098. case ADMIN_MENU:
  1099. switch (e.u.m.item) {
  1100. case MMSaveas:
  1101. ewsaveas(help_ew);
  1102. break;
  1103. case MMHelp:
  1104. if (help_length) helpwindow(0);
  1105. break;
  1106. case MMHelpSel:
  1107. help_selection(ew);
  1108. break;
  1109. case MMHelpAsk:
  1110. {
  1111. char buf[40];
  1112. int len;
  1113. if (waskstr("Help on topic", buf, 40)) {
  1114. len = strlen(buf);
  1115. help_buffer(buf, len);
  1116. }
  1117. break;
  1118. }
  1119. case MMAbort:
  1120. help_ew->saved = TRUE; /* Fake it */
  1121. (*free_hook)(help_buff);
  1122. ewclose(help_ew);
  1123. help_ew = 0;
  1124. break;
  1125. }
  1126. break;
  1127. case EDIT_MENU:
  1128. switch (e.u.m.item) {
  1129. case MMCopy:
  1130. ewcopy(ew);
  1131. break;
  1132. case MMSelAll:
  1133. tesetfocus(ew->tp, 0, tegetlen(ew->tp));
  1134. break;
  1135. }
  1136. break;
  1137. case HELP_MENU:
  1138. helpwindow(e.u.m.item);
  1139. break;
  1140. }
  1141. break;
  1142. case WE_COMMAND:
  1143. switch (e.u.command) {
  1144. case WC_CANCEL:
  1145. interrupt_pending=YES;
  1146. break;
  1147. case WC_RETURN:
  1148. help_selection(ew);
  1149. break;
  1150. case WC_DEL:
  1151. case WC_BACKSPACE:
  1152. helpwindow(0);
  1153. break;
  1154. default:
  1155. ewevent(ew, &e, &closed);
  1156. break;
  1157. }
  1158. if (e.u.command != WC_CLOSE) {
  1159. break;
  1160. }
  1161. case WE_CLOSE:
  1162. help_ew->saved = TRUE; /* Fake it */
  1163. (*free_hook)(help_buff);
  1164. ewclose(help_ew);
  1165. help_ew = 0;
  1166. break;
  1167. default:
  1168. ewevent(ew, &e, &closed);
  1169. if (closed) {
  1170. help_ew->saved = TRUE; /* Fake it */
  1171. (*free_hook)(help_buff);
  1172. ewclose(help_ew);
  1173. help_ew = 0;
  1174. }
  1175. break;
  1176. }
  1177. }
  1178. else if (ew != NULL) ewevent(ew, &e, &closed);
  1179. else wfleep();
  1180. fixmenus();
  1181. }
  1182. }
  1183. void poll_for_attn(void)
  1184. {
  1185. for (;;) { /* Loop while there are events */
  1186. EVENT e;
  1187. EDITWIN *ew;
  1188. CSLbool closed;
  1189. if (!pause_set) {
  1190. if (!wpollevent(&e)) return;
  1191. }
  1192. else wgetevent(&e);
  1193. /* fprintf(stderr, "Event: type=%d win=%p data=%d\n", e.type, e.window, e.u.command); */
  1194. ew= ewfind(e.window);
  1195. if (ew == CSL_ew) {
  1196. switch (e.type) {
  1197. case WE_CHAR:
  1198. switch(e.u.character) {
  1199. case 'A'-'@': /* ^A == beginning of line */
  1200. {
  1201. TEXTEDIT* tp = CSL_ew->tp;
  1202. tesetfocus(tp,
  1203. zsubgap(tp->start[tewhichline(tp,
  1204. zaddgap(tp->foc),
  1205. tp->focprev)]),
  1206. -1);
  1207. break;
  1208. }
  1209. case 'B'-'@': /* ^B back a character */
  1210. tearrow(CSL_ew->tp, WC_LEFT);
  1211. break;
  1212. case 'D'-'@': /* ^D delete at cursor */
  1213. {
  1214. TEXTEDIT* tp = CSL_ew->tp;
  1215. tearrow(tp, WC_RIGHT);
  1216. e.type = WE_COMMAND;
  1217. e.u.command = WC_BACKSPACE;
  1218. ewevent(ew,&e, NULL);
  1219. break;
  1220. }
  1221. case 'E'-'@': /* ^E end of line */
  1222. {
  1223. TEXTEDIT* tp = CSL_ew->tp;
  1224. int line = tewhichline(tp, zaddgap(tp->foc), tp->focprev);
  1225. int xxx = zsubgap(tetextround(tp, line, tp->right));
  1226. tesetfocus(tp, xxx, -1);
  1227. break;
  1228. }
  1229. case 'F'-'@': /* ^F forward a character */
  1230. tearrow(CSL_ew->tp, WC_RIGHT);
  1231. break;
  1232. case 'K'-'@': /* ^K kill all type ahead */
  1233. tty_index = 0;
  1234. break;
  1235. case 'N'-'@': /* ^N next line */
  1236. tearrow(CSL_ew->tp, WC_DOWN);
  1237. break;
  1238. case 'P'-'@': /* ^P previous line */
  1239. tearrow(CSL_ew->tp, WC_UP);
  1240. break;
  1241. case 'Z'-'@': /* ^Z move to end of buffer */
  1242. {
  1243. TEXTEDIT* tp = CSL_ew->tp;
  1244. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  1245. break;
  1246. }
  1247. case 'V'-'@': /* ^V page down */
  1248. case ' ':
  1249. {
  1250. int i;
  1251. int j;
  1252. wgetdefwinsize(&i, &j);
  1253. j = j/wlineheight() -1;
  1254. for (i=0; i<j; i++) tearrow(help_ew->tp, WC_DOWN);
  1255. break;
  1256. }
  1257. default:
  1258. if (tty_index > TTYBUF_SIZE-2) wfleep();
  1259. else tty_buff[tty_index++] = e.u.character;
  1260. break;
  1261. }
  1262. break;
  1263. case WE_COMMAND:
  1264. switch (e.u.command) {
  1265. case WC_CANCEL:
  1266. interrupt_pending = YES;
  1267. break;
  1268. case WC_CLOSE:
  1269. pause_for_user();
  1270. exit(1);
  1271. case WC_RETURN:
  1272. if (tty_index > TTYBUF_SIZE-2) wfleep();
  1273. else tty_buff[tty_index++] = '\n';
  1274. break;
  1275. case WC_TAB:
  1276. if (tty_index > TTYBUF_SIZE-2) wfleep();
  1277. else tty_buff[tty_index++] = '\t';
  1278. break;
  1279. case WC_DEL:
  1280. case WC_BACKSPACE:
  1281. if (tty_index>0) tty_index--;
  1282. break;
  1283. }
  1284. break;
  1285. case WE_MENU:
  1286. switch (e.u.m.id) {
  1287. case ADMIN_MENU:
  1288. switch (e.u.m.item) {
  1289. case MMHelp:
  1290. helpwindow(0);
  1291. break;
  1292. case MMHelpSel:
  1293. help_selection(ew);
  1294. break;
  1295. case MMAbort:
  1296. pause_for_user();
  1297. exit(1);
  1298. case MMInt:
  1299. interrupt_pending = YES;
  1300. return;
  1301. case MMPause:
  1302. pause_set = !pause_set;
  1303. wmenucheck(adminmenu, MMPause, pause_set);
  1304. if (pause_set) wsettitle(CSL_window, "<Paused> Reduce 3.7");
  1305. else wsettitle(CSL_window, "Reduce 3.7");
  1306. break;
  1307. }
  1308. break;
  1309. case EDIT_MENU:
  1310. switch (e.u.m.item) {
  1311. case MMCopy:
  1312. ewcopy(ew);
  1313. break;
  1314. case MMPaste:
  1315. break;
  1316. case MMCut:
  1317. ewcopy(ew);
  1318. case MMClear:
  1319. ewreplace(ew, "");
  1320. break;
  1321. case MMSelAll:
  1322. tesetfocus(ew->tp, 0, tegetlen(ew->tp));
  1323. break;
  1324. }
  1325. break;
  1326. case HELP_MENU:
  1327. helpwindow(e.u.m.item);
  1328. break;
  1329. }
  1330. break;
  1331. case WE_CLOSE:
  1332. pause_for_user();
  1333. exit(1);
  1334. default:
  1335. ewevent(ew, &e, &closed);
  1336. break;
  1337. }
  1338. }
  1339. else if (ew == help_ew) {
  1340. switch (e.type) {
  1341. case WE_MOUSE_UP: /* Extend to a word on single click */
  1342. if (!ew->tp->mdown)
  1343. break;
  1344. ew->tp->mdown= FALSE;
  1345. if (e.u.where.clicks > 1) {
  1346. teclicknew(ew->tp, e.u.where.h, e.u.where.v, FALSE, TRUE);
  1347. help_selection(ew);
  1348. }
  1349. break;
  1350. case WE_CHAR:
  1351. /* fprintf(stderr, "WE_CHAR %d(%c)\n", e.u.character,e.u.character); */
  1352. switch(e.u.character) {
  1353. case 'A'-'@': /* ^A == beginning of line */
  1354. {
  1355. TEXTEDIT* tp = help_ew->tp;
  1356. tesetfocus(tp,
  1357. zsubgap(tp->start[tewhichline(tp,
  1358. zaddgap(tp->foc),
  1359. tp->focprev)]),
  1360. -1);
  1361. break;
  1362. }
  1363. case 'B'-'@': /* ^B back a character */
  1364. tearrow(help_ew->tp, WC_LEFT);
  1365. break;
  1366. case 'E'-'@': /* ^E end of line */
  1367. {
  1368. TEXTEDIT* tp = help_ew->tp;
  1369. int line = tewhichline(tp, zaddgap(tp->foc), tp->focprev);
  1370. int xxx = zsubgap(tetextround(tp, line, tp->right));
  1371. tesetfocus(tp, xxx, -1);
  1372. break;
  1373. }
  1374. case 'F'-'@': /* ^F forward a character */
  1375. tearrow(help_ew->tp, WC_RIGHT);
  1376. break;
  1377. case 'N'-'@': /* ^N next line */
  1378. tearrow(help_ew->tp, WC_DOWN);
  1379. break;
  1380. case 'P'-'@': /* ^P previous line */
  1381. tearrow(help_ew->tp, WC_UP);
  1382. break;
  1383. case 'Z'-'@': /* ^Z move to end of buffer */
  1384. {
  1385. TEXTEDIT* tp = help_ew->tp;
  1386. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  1387. break;
  1388. }
  1389. case 'L'-'@': /* ^L is PgDn */
  1390. case 'V'-'@': /* ^V page down */
  1391. case ' ':
  1392. {
  1393. int i;
  1394. int j;
  1395. wgetdefwinsize(&i, &j);
  1396. j = j/wlineheight() -1;
  1397. for (i=0; i<j; i++) tearrow(help_ew->tp, WC_DOWN);
  1398. break;
  1399. }
  1400. case 'K'-'@': /* ^K is PgUp */
  1401. {
  1402. int i;
  1403. int j;
  1404. wgetdefwinsize(&i, &j);
  1405. j = j/wlineheight() -1;
  1406. for (i=0; i<j; i++) tearrow(help_ew->tp, WC_UP);
  1407. break;
  1408. }
  1409. }
  1410. break;
  1411. case WE_MENU:
  1412. switch (e.u.m.id) {
  1413. case ADMIN_MENU:
  1414. switch (e.u.m.item) {
  1415. case MMHelp:
  1416. if (help_length) helpwindow(0);
  1417. break;
  1418. case MMHelpSel:
  1419. help_selection(ew);
  1420. break;
  1421. }
  1422. break;
  1423. case EDIT_MENU:
  1424. switch (e.u.m.item) {
  1425. case MMCopy:
  1426. ewcopy(ew);
  1427. break;
  1428. case MMSelAll:
  1429. tesetfocus(ew->tp, 0,
  1430. tegetlen(ew->tp));
  1431. break;
  1432. }
  1433. break;
  1434. case HELP_MENU:
  1435. helpwindow(e.u.m.item);
  1436. break;
  1437. }
  1438. break;
  1439. case WE_COMMAND:
  1440. switch (e.u.command) {
  1441. case WC_CANCEL:
  1442. interrupt_pending = YES;
  1443. break;
  1444. case WC_RETURN:
  1445. help_selection(ew);
  1446. break;
  1447. case WC_DEL:
  1448. case WC_BACKSPACE:
  1449. helpwindow(0);
  1450. break;
  1451. default:
  1452. ewevent(ew, &e, &closed);
  1453. break;
  1454. }
  1455. if (e.u.command != WC_CLOSE) {
  1456. break;
  1457. }
  1458. case WE_CLOSE:
  1459. help_ew->saved = TRUE; /* Fake it */
  1460. (*free_hook)(help_buff);
  1461. ewclose(help_ew);
  1462. help_ew = 0;
  1463. break;
  1464. default:
  1465. ewevent(ew, &e, &closed);
  1466. if (closed) {
  1467. help_ew->saved = TRUE; /* Fake it */
  1468. (*free_hook)(help_buff);
  1469. ewclose(help_ew);
  1470. help_ew = 0;
  1471. }
  1472. break;
  1473. }
  1474. }
  1475. else if (ew != NULL) ewevent(ew, &e, &closed);
  1476. else wfleep();
  1477. fixmenus();
  1478. }
  1479. }
  1480. void flush_screen(void)
  1481. {
  1482. TEXTEDIT* tp = CSL_ew->tp;
  1483. if (stdout_n != 0) {
  1484. /* fprintf(stderr, stdout_buffer); */
  1485. ewreplace(CSL_ew, stdout_buffer);
  1486. stdout_n = 0; stdout_p = stdout_buffer;
  1487. }
  1488. if (tp->nlines > max_nlines+5) { /* Allow a five line slop */
  1489. techangefocus(tp, 0, zsubgap(tp->start[tp->nlines-max_nlines]));
  1490. tp->aim= UNDEF;
  1491. tp->focprev= FALSE;
  1492. /* ewreplace(CSL_ew, ""); */
  1493. tereplace(tp, "");
  1494. /* fprintf(stderr, "-> %d\n", tp->nlines); */
  1495. /* focus to end */
  1496. tesetfocus(tp, tp->buflen - tp->gaplen, -1);
  1497. }
  1498. wupdate(CSL_window);
  1499. }
  1500. static clock_t prev_clock = 0;
  1501. void accept_tick(void)
  1502. {
  1503. clock_t t0 = clock();
  1504. #ifdef SOFTWARE_TICKS_PER_SECOND
  1505. software_tick_count++;
  1506. #endif
  1507. #ifdef SOFTWARE_TICKS_PER_SECOND
  1508. if (prev_clock != 0)
  1509. { double t1 = (double)(t0-prev_clock)/(double)CLOCKS_PER_SEC;
  1510. double ratio =
  1511. (double)(software_tick_count - prev_software_tick_count)/t1;
  1512. int32 w;
  1513. /*
  1514. * t1 is how long since I was last here, ratio is the number of
  1515. * ticks per second over that time-span.
  1516. */
  1517. ratio = ratio / (double)SOFTWARE_TICKS_PER_SECOND;
  1518. prev_software_tick_count = software_tick_count;
  1519. /*
  1520. * Now ratio is the extent by which I was taking ticks too fast.
  1521. * To dampen out my correction I will scale software_ticks by the
  1522. * square root of this.
  1523. */
  1524. ratio = sqrt(ratio);
  1525. w = (int)(1000.0 * ratio);
  1526. /*
  1527. * I clamp the correction fator so I never adjust my clock rate by
  1528. * a factor of more than (about) 3.
  1529. */
  1530. if (w > 3000) w = 3000;
  1531. else if (w < 300) w = 300;
  1532. /*
  1533. * Furthermore I attempt to keep software_ticks within integer range.
  1534. */
  1535. if (software_ticks < (0x7fffffff/3000) &&
  1536. software_ticks > 50)
  1537. software_ticks = (w*software_ticks)/1000;
  1538. }
  1539. #endif
  1540. /*
  1541. * This is where I can put things that need to be done regularly.
  1542. * This will need to involve prodding the window manager etc etc.
  1543. * At present I do NOTHING...
  1544. */
  1545. t0 = clock();
  1546. remove_ticker();
  1547. if (t0 > prev_clock + CLOCKS_PER_SEC) {
  1548. /*
  1549. * Time spent doing all of this is counted as "overhead" or "system time"
  1550. * and not included in the times that I will report to my users...
  1551. */
  1552. if (clock_stack == &consolidated_time[0]) {
  1553. consolidated_time[0] +=
  1554. ((double)(clock_t)(t0 - base_time))/((double)CLOCKS_PER_SEC);
  1555. base_time = clock();
  1556. }
  1557. report_time((int32)(100.0*consolidated_time[0]), (int32)(100.0*gc_time));
  1558. }
  1559. push_clock();
  1560. wupdate(CSL_window);
  1561. poll_for_attn();
  1562. polltick_pending = NO;
  1563. ensure_screen();
  1564. add_ticker();
  1565. pop_clock();
  1566. return;
  1567. }
  1568. static char time_string[32], space_string[32];
  1569. void report_time(int32 t, int32 gct)
  1570. {
  1571. sprintf(time_string, "%d.%.2d secs", t/100, t%100);
  1572. wmenusetitem(adminmenu, MMTime, time_string);
  1573. sprintf(time_string, "+ %d.%.2d secs", gct/100, gct%100);
  1574. wmenusetitem(adminmenu, MMOTime, time_string);
  1575. }
  1576. void report_space(int gc_count, double percent)
  1577. {
  1578. sprintf(space_string, "[GC %d]:%.2f%%", gc_count, percent);
  1579. wmenusetitem(adminmenu, MMSpace, space_string);
  1580. }
  1581. #ifndef HAVE_UNIVERSAL_HEADERS
  1582. #define ModalFilterUPP ProcPtr
  1583. #define NewModalFilterProc(x) ((ProcPtr)x)
  1584. #endif
  1585. extern pascal Boolean
  1586. filter_addnl(DialogPtr, EventRecord, short *);
  1587. void pause_for_user(void)
  1588. /*
  1589. * This is called at the end of a run so that the user gets a chance to read
  1590. * the final screen-full of output. It pops up a dialog box that will
  1591. * wait for a button push. I take the view that if output is going to a
  1592. * file then the delay is not needed, since the user can always check for
  1593. * messages there. This has the effect that non-interactive build sequences
  1594. * will often run without the pause - a good thing! Note however that this
  1595. * mean that you MUST use the close box to exit from a wimp session. Just
  1596. * "quit;" or "(stop 0)" will not do.
  1597. */
  1598. {
  1599. ModalFilterUPP filterupp = NewModalFilterProc(filter_addnl);
  1600. remove_ticker();
  1601. ensure_screen();
  1602. CSL_ew->saved = TRUE; /* Fake it */
  1603. if (spool_file != NULL) return;
  1604. /*
  1605. * Here I just loop handling events until the user hits the CLOSE box
  1606. * on my window.
  1607. */
  1608. /* wmessage("Leaving Reduce 3.7"); */
  1609. set_arrow();
  1610. Alert(128, filterupp);
  1611. ewclose(CSL_ew);
  1612. wdone();
  1613. }
  1614. int wimpget(char *tty_buffer)
  1615. /*
  1616. * This is the main call from the body of CSL into the window manager
  1617. * to obtain input from the user. It is expected to copy some input
  1618. * characters into tty_buffer and return the number of characters
  1619. * provided. In this implementation I read the text back from the
  1620. * stdwin textedit buffer, looking rather too low.
  1621. * Any type ahead is handled in here.
  1622. */
  1623. {
  1624. TEXTEDIT* tp = CSL_ew->tp;
  1625. int i;
  1626. int n;
  1627. int lineno;
  1628. tty_nnl = 1; /* Offset from nlines */
  1629. push_clock();
  1630. remove_ticker();
  1631. if (stdout_n != 0) flush_screen();
  1632. wmenuattach(CSL_window, loadmenu);
  1633. wmenuattach(CSL_window, libmenu);
  1634. wmenuattach(CSL_window, switchmenu);
  1635. lineno = tp->nlines-1; /* Need to do this after the toend */
  1636. tty_offset = tp->foc - tp->start[lineno];
  1637. if (tty_offset<0) tty_offset += tp->gaplen;
  1638. wmenuenable(adminmenu, MMPause, NO);
  1639. if(tty_index>0) {
  1640. int i = 0;
  1641. for (; i<tty_index; i++)
  1642. if (tty_buff[i] == '\n') tty_nnl++;
  1643. tty_buff[tty_index] = '\0';
  1644. ewreplace(CSL_ew, tty_buff);
  1645. tty_index = 0;
  1646. }
  1647. event_loop();
  1648. /* fprintf(stderr, "Event loop ended\n"); */
  1649. lineno = tp->nlines-tty_nnl;
  1650. /* fprintf(stderr, "Offset=%d Length=%d\n",
  1651. tty_offset,
  1652. tp->foc- tp->start[lineno]-tty_offset);
  1653. for (i=0; i<tp->foc- tp->start[lineno]-tty_offset; i++) {
  1654. fprintf(stderr, "%c", *(tegettext(tp)+tp->start[lineno]+tty_offset+i));
  1655. }
  1656. fprintf(stderr, "\n"); */
  1657. memcpy(tty_buffer, tegettext(tp)+tp->start[lineno]+tty_offset,
  1658. tp->foc- tp->start[lineno]-tty_offset);
  1659. wmenudetach(CSL_window, loadmenu);
  1660. wmenudetach(CSL_window, libmenu);
  1661. wmenudetach(CSL_window, switchmenu);
  1662. wmenuenable(adminmenu, MMPause, YES);
  1663. add_ticker();
  1664. n = tp->foc- tp->start[lineno]-tty_offset;
  1665. if (n<0) {
  1666. n += tp->gaplen;
  1667. fprintf(stderr, "N correction\n");
  1668. }
  1669. tty_ready = NO;
  1670. pop_clock();
  1671. return n;
  1672. }
  1673. int char_cnt = 0;
  1674. void putc_stdout(int c)
  1675. {
  1676. *stdout_p++ = c;
  1677. *stdout_p = 0;
  1678. stdout_n++;
  1679. char_cnt++;
  1680. remove_ticker();
  1681. if (c == '\n' || stdout_n > STDOUT_BUFSIZE - LONGEST_PRINTF) {
  1682. push_clock(); flush_screen();
  1683. if (char_cnt>200) { /* Where 200 is a random number */
  1684. poll_for_attn(); char_cnt = 0;
  1685. }
  1686. pop_clock();
  1687. }
  1688. if (polltick_pending) accept_tick();
  1689. add_ticker();
  1690. }
  1691. void vfprintf_stdout(char *fmt, va_list a)
  1692. {
  1693. int n;
  1694. /*
  1695. * I have given myself LONGEST_PRINTF locations in the array for
  1696. * this vsprintf to fill in. If it overflows I am in big trouble!
  1697. */
  1698. n = vsprintf(stdout_p, fmt, a);
  1699. stdout_p += n;
  1700. stdout_n += n;
  1701. char_cnt += n;
  1702. remove_ticker();
  1703. if (stdout_n > STDOUT_BUFSIZE - LONGEST_PRINTF) {
  1704. push_clock(); flush_screen();
  1705. if (char_cnt>200) {
  1706. poll_for_attn(); char_cnt = 0;
  1707. }
  1708. pop_clock();
  1709. }
  1710. if (polltick_pending) accept_tick();
  1711. add_ticker();
  1712. }
  1713. #define NARGS 25
  1714. static char *argv[NARGS+1];
  1715. static char argbuf[256];
  1716. /*
  1717. * init_command_line - prepare initial command line
  1718. *
  1719. * The command line is preset to show the name of the program.
  1720. * The program name is quoted as necessary.
  1721. *
  1722. */
  1723. static char*
  1724. init_command_line(char *buf1, char *buf2)
  1725. {
  1726. register char *s, *t = buf2;
  1727. int c, space = 0, dquote = 0, squote = 0, quote = 0;
  1728. sprintf(s = buf1, "%#s", LMGetCurApName());
  1729. while (c = *s++) {
  1730. if (c == ' ')
  1731. space = 1;
  1732. else if (c == '"')
  1733. dquote = 1;
  1734. else if (c == '\'')
  1735. squote = 1;
  1736. }
  1737. if (space || dquote || squote)
  1738. *t++ = quote = dquote && !squote ? '\'' : '"';
  1739. for (s = buf1; c = *s++; *t++ = c) {
  1740. if (c == quote || c == '\\')
  1741. *t++ = '\\';
  1742. }
  1743. if (quote)
  1744. *t++ = quote;
  1745. *t++ = ' ';
  1746. *t++ = 0;
  1747. return t;
  1748. }
  1749. /*
  1750. * parse - divide command line into "words"
  1751. *
  1752. * Words are delimited by one or more spaces. Any characters within
  1753. * matching single (') or double (") quotes are taken literally. Any
  1754. * character preceded by a backslash (\) is taken literally.
  1755. *
  1756. */
  1757. static int
  1758. parse(char *s, char *t)
  1759. {
  1760. int c, quote = 0, argc = 0;
  1761. while (c = *s++) {
  1762. if (c == ' ')
  1763. continue;
  1764. if (argc < NARGS)
  1765. argv[argc++] = t;
  1766. do {
  1767. if (c == '\\' && *s)
  1768. c = *s++;
  1769. else if (c == '"' || c == '\'') {
  1770. if (!quote) {
  1771. quote = c;
  1772. continue;
  1773. }
  1774. if (c == quote) {
  1775. quote = 0;
  1776. continue;
  1777. }
  1778. }
  1779. *t++ = c;
  1780. } while (*s && ((c = *s++) != ' ' || quote));
  1781. *t++ = 0;
  1782. }
  1783. return(argc);
  1784. }
  1785. #ifdef OLD
  1786. int wgetargs(char ***av)
  1787. {
  1788. short i, argc;
  1789. char buf[256];
  1790. char *t = init_command_line(buf, argbuf);
  1791. if( !waskstr(" Arguments for REDUCE 3.7", t, 256)) exit(1);
  1792. t[-1] = ' ';
  1793. sprintf(buf, "%s", argbuf);
  1794. argc = parse(buf, argbuf);
  1795. *av = argv;
  1796. /* for(i=0;i<argc; i++) wmessage(argv[i]); */
  1797. return(argc);
  1798. }
  1799. #else
  1800. enum { /* Dialog names for main box */
  1801. exDEF = 3,
  1802. exLOG,
  1803. exVER,
  1804. exXTA
  1805. };
  1806. enum { /* Dialog names for additional box */
  1807. exBAT = 3,
  1808. exBAC,
  1809. exTRA,
  1810. exCLD,
  1811. exLAP,
  1812. exQUI,
  1813. exREC,
  1814. exRED,
  1815. exRAN,
  1816. exSIZ,
  1817. exOTH,
  1818. exLGG,
  1819. exDFF,
  1820. exVBE
  1821. };
  1822. /*
  1823. * get_args - process "command line"
  1824. *
  1825. */
  1826. /* Dialog numbers */
  1827. #define START_1 (129)
  1828. #define START_2 (128)
  1829. /*Rect bounds = {60, 20, 320, 220};
  1830. Rect boundx = {80, 30, 400, 500};
  1831. */
  1832. int DoExtra(char ***av)
  1833. {
  1834. DialogPtr dd;
  1835. short i;
  1836. int verbos = 0;
  1837. int deflt = 0;
  1838. int logging = 0;
  1839. char buff[256];
  1840. char name[256];
  1841. char nams[256];
  1842. Rect box;
  1843. short type;
  1844. Handle item;
  1845. char *t = init_command_line(buff, argbuf);
  1846. char *s = t-1;
  1847. int argc;
  1848. ModalFilterUPP filterupp = NewModalFilterProc(filter_addnl);
  1849. int batch =0, back = 0, cold = 0, lap = 0, quiet = 0;
  1850. int trap = 0, recover = 0, redirect = 0;
  1851. /* ee = NewDialog(NULL, &boundx, "\p", 1, 1, (WindowPtr)-1, 0, 256L,
  1852. GetResource('DITL',132)); */
  1853. dd = GetNewDialog(START_2, NULL, (WindowPtr)-1);
  1854. GetDItem(dd, 1, &type, &item, &box);
  1855. SetPort(dd);
  1856. InsetRect(&box, -4, -4);
  1857. PenSize(3, 3);
  1858. FrameRoundRect(&box, 16, 16);
  1859. set_arrow();
  1860. do { /* engage in dialog */
  1861. ModalDialog(filterupp, &i);
  1862. switch (i) {
  1863. case cancel:
  1864. DisposDialog(dd);
  1865. exit(0);
  1866. case exBAT:
  1867. batch = !batch;
  1868. GetDItem(dd, exBAT, &type, &item, &box);
  1869. SetCtlValue((struct ControlRecord **)item, batch);
  1870. break;
  1871. case exBAC:
  1872. back = !back;
  1873. GetDItem(dd, exBAC, &type, &item, &box);
  1874. SetCtlValue((struct ControlRecord **)item, back);
  1875. break;
  1876. case exTRA:
  1877. trap = !trap;
  1878. GetDItem(dd, exTRA, &type, &item, &box);
  1879. SetCtlValue((struct ControlRecord **)item, trap);
  1880. break;
  1881. case exCLD:
  1882. cold = !cold;
  1883. GetDItem(dd, exCLD, &type, &item, &box);
  1884. SetCtlValue((struct ControlRecord **)item, cold);
  1885. break;
  1886. case exLAP:
  1887. lap = !lap;
  1888. GetDItem(dd, exLAP, &type, &item, &box);
  1889. SetCtlValue((struct ControlRecord **)item, lap);
  1890. break;
  1891. case exQUI:
  1892. quiet = !quiet;
  1893. GetDItem(dd, exQUI, &type, &item, &box);
  1894. SetCtlValue((struct ControlRecord **)item, quiet);
  1895. break;
  1896. case exREC:
  1897. recover = !recover;
  1898. GetDItem(dd, exREC, &type, &item, &box);
  1899. SetCtlValue((struct ControlRecord **)item, recover);
  1900. break;
  1901. case exRED:
  1902. redirect = waskfile("Redirection File: ", nams, 256, 1);
  1903. break;
  1904. case exLGG:
  1905. logging = waskfile("Log File: ", name, 256, 1);
  1906. break;
  1907. case exDFF:
  1908. deflt = !deflt;
  1909. GetDItem(dd, exDFF, &type, &item, &box);
  1910. SetCtlValue((struct ControlRecord **)item, deflt);
  1911. break;
  1912. case exVBE:
  1913. verbos = !verbos;
  1914. GetDItem(dd, exVBE, &type, &item, &box);
  1915. SetCtlValue((struct ControlRecord **)item, verbos);
  1916. break;
  1917. }
  1918. } while (i != ok);
  1919. if (batch) { strcpy(t, " -b"); t += 3; }
  1920. if (back) { strcpy(t, " -g"); t += 3; }
  1921. if (cold) { strcpy(t, " -z"); t += 3; }
  1922. if (lap) { strcpy(t, " -s"); t += 3; }
  1923. if (quiet) { strcpy(t, " -q"); t += 3; }
  1924. if (recover) { strcpy(t, " -y"); t += 3; }
  1925. if (trap) { strcpy(t, " -x"); t += 3; }
  1926. if (verbos) { strcpy(t, " -v"); t += 3; }
  1927. if (logging) { strcpy(t, " -l"); t += 3;
  1928. strcpy(t, name); t += strlen(name);
  1929. }
  1930. if (redirect) { strcpy(t, " --"); t += 3;
  1931. strcpy(t, nams); t += strlen(nams);
  1932. }
  1933. {
  1934. char bb[256];
  1935. GetDItem(dd, exRAN, &type, &item, &box);
  1936. GetIText(item, (unsigned char *)bb);
  1937. #ifndef CLEVERGLUE
  1938. P2CStr((unsigned char *)bb);
  1939. #endif
  1940. if (bb[0]!='\0') {
  1941. strcpy(t, " -r"); t += 3;
  1942. strcpy(t, bb); t += strlen(bb);
  1943. }
  1944. GetDItem(dd, exSIZ, &type, &item, &box);
  1945. GetIText(item, (unsigned char *)bb);
  1946. #ifndef CLEVERGLUE
  1947. P2CStr((unsigned char *)bb);
  1948. #endif
  1949. if (bb[0]!='\0') {
  1950. strcpy(t, " -k"); t += 3;
  1951. strcpy(t, bb); t += strlen(bb);
  1952. }
  1953. GetDItem(dd, exOTH, &type, &item, &box);
  1954. GetIText(item, (unsigned char *)bb);
  1955. #ifndef CLEVERGLUE
  1956. P2CStr((unsigned char *)bb);
  1957. #endif
  1958. if (bb[0]!='\0') {
  1959. strcpy(t, bb); t += strlen(bb);
  1960. }
  1961. }
  1962. out:
  1963. DisposDialog(dd);
  1964. if (!deflt) *s = ' ';
  1965. sprintf(buff, "%s", argbuf);
  1966. argc = parse(buff, argbuf);
  1967. *av = argv;
  1968. /* for(i=0;i<argc; i++) wmessage(argv[i]); */
  1969. return(argc);
  1970. }
  1971. int wgetargs(char ***av)
  1972. {
  1973. DialogPtr dd;
  1974. short i;
  1975. int verbos = 0;
  1976. int deflt = 0;
  1977. int logging = 0;
  1978. char buff[256];
  1979. char name[256];
  1980. char nams[256];
  1981. int p = 0;
  1982. Rect box;
  1983. short type;
  1984. Handle item;
  1985. char *t = init_command_line(buff, argbuf);
  1986. char *s = t-1;
  1987. int argc;
  1988. ModalFilterUPP filterupp = NewModalFilterProc(filter_addnl);
  1989. /* dd = NewDialog(NULL, &bounds, "\p", 1, 1, (WindowPtr)-1, 0, 256L,
  1990. GetResource('DITL',131)); */
  1991. dd = GetNewDialog(START_1, NULL, (WindowPtr)-1);
  1992. GetDItem(dd, 1, &type, &item, &box);
  1993. SetPort(dd);
  1994. InsetRect(&box, -4, -4);
  1995. PenSize(3, 3);
  1996. FrameRoundRect(&box, 16, 16);
  1997. set_arrow();
  1998. do { /* engage in dialog */
  1999. ModalDialog(filterupp, &i);
  2000. switch (i) {
  2001. case cancel:
  2002. DisposDialog(dd);
  2003. exit(0);
  2004. case exDEF:
  2005. deflt = !deflt;
  2006. break;
  2007. case exVER:
  2008. verbos = !verbos;
  2009. GetDItem(dd, exVER, &type, &item, &box);
  2010. SetCtlValue((struct ControlRecord **)item, verbos);
  2011. break;
  2012. case exLOG:
  2013. logging = waskfile("Log File: ", name, 256, 1);
  2014. break;
  2015. case exXTA:
  2016. set_watch();
  2017. DisposDialog(dd);
  2018. return DoExtra(av);
  2019. }
  2020. } while (i != ok);
  2021. set_watch();
  2022. {
  2023. char aa[256];
  2024. if (verbos) {
  2025. strcpy(t, " -v");
  2026. t += 3; }
  2027. if (logging) {
  2028. strcpy(t, " -l"); t += 3;
  2029. strcpy(t, name); p += strlen(aa);
  2030. }
  2031. }
  2032. if (!deflt) *s = ' ';
  2033. DisposDialog(dd);
  2034. sprintf(buff, "%s", argbuf);
  2035. argc = parse(buff, argbuf);
  2036. *av = argv;
  2037. /* for(i=0;i<argc; i++) wmessage(argv[i]); */
  2038. return(argc);
  2039. }
  2040. #endif
  2041. /* end of sysmac.c */