<> <> <> <> <> <<>> DIRECTORY BasicTime, CDIO, CD, CDCleanUp, CDDirectory, CDEvents, CDPrivate, CDProperties, CDSequencer, CDValue, Convert, FileNames, FS, IO, Process, Rope, TerminalIO, TokenIO; CDOut: CEDAR PROGRAM --monitored by TokenIO IMPORTS BasicTime, CD, CDIO, CDCleanUp, CDDirectory, CDEvents, CDProperties, CDSequencer, CDValue, Convert, FileNames, FS, IO, Process, Rope, TerminalIO, TokenIO EXPORTS CDIO SHARES CDProperties = BEGIN xChipndaleFile: INT = 12121983; xVersion: INT = 15; propKeyForKey: REF = NEW[INT]; <<-- global variables >> <<-- protected through attachment with Attach, Release and TokenIO>> attached: BOOL _ FALSE; gDesign: CD.Design _ NIL; gDirectoryNum: INT; simpleObjectsInTableMax: INT _ 10000; gCountSimpleObjectsInTable: INT; gThisKey: REF ATOM _ NIL; gLastKey: REF ATOM _ NIL; gDirectoryMark: TokenIO.Mark; gQuiet, gEmergency: BOOL _ FALSE; gRemoveAlso: REF ATOM _ NIL; dotCnt: INT _ 0; --not exact, it's mainly to pacify user colCnt: INT _ 0; --not exact, it's mainly to pacify user FileExists: PROC [name: Rope.ROPE] RETURNS [found: BOOL_TRUE] = { [] _ FS.FileInfo[name: name, remoteCheck: FALSE ! FS.Error => IF error.group#bug THEN {found _ FALSE; CONTINUE} ]; }; GetNameInteractively: PROC [wDir: Rope.ROPE_NIL] RETURNS [name: Rope.ROPE] = { <<--may raise TerminalIO.UserAbort>> TerminalIO.WriteRope["file name:"]; IF ~Rope.IsEmpty[wDir] THEN TerminalIO.WriteRopes[" (", wDir, ")"]; name _ TerminalIO.RequestRope[" > "]; name _ CDIO.MakeName[name, "dale", wDir]; IF FileExists[name] THEN { TerminalIO.WriteRopes["File ", name, " exists already; overwrite?"]; IF ~TerminalIO.Confirm[choice: "overwrite", label: "file exists"] THEN ERROR TerminalIO.UserAbort; TerminalIO.WriteRope[" yes\n"]; }; }; WriteRope: PROC [r: Rope.ROPE] = INLINE { <<--feedback on terminal>> IF ~gQuiet THEN Feedback[gEmergency, r] }; Feedback: PROC [fork: BOOL, r: Rope.ROPE] = INLINE { <<--feedback on terminal>> IF fork THEN TRUSTED { <<--don't hang the output if viewer wedges>> Process.Detach[ FORK TerminalIO.WriteRope[r] ] } ELSE TerminalIO.WriteRope[r] }; WriteLayer: PUBLIC PROC [l: CD.Layer] = { TokenIO.WriteAtom[CD.LayerKey[l]]; }; WritePos: PUBLIC PROC [p: CD.Position] = { TokenIO.WriteInt[p.x]; TokenIO.WriteInt[p.y]; }; WriteRect: PUBLIC PROC[r: CD.Rect] = { TokenIO.WriteInt[r.x1]; TokenIO.WriteInt[r.y1]; TokenIO.WriteInt[r.x2]; TokenIO.WriteInt[r.y2]; }; WriteProperties: PUBLIC PROC [props: CD.PropList] = { IF ~gEmergency THEN IF gQuiet THEN Process.Yield[]--should make output slower than redraw ELSE CDSequencer.CheckAborted[gDesign]; <<--PropertyLists guarantees properties are not reordered; going through list is ok>> FOR l: CD.PropList _ props, l.rest WHILE l#NIL DO WITH l.first.key SELECT FROM a: ATOM => { pp: CDProperties.PropertyProcs = CDProperties.FetchProcs[a]; IF pp#NIL AND pp.internalWrite#NIL THEN { TokenIO.WriteAtom[$Property]; TokenIO.WritePushFlag[a]; pp.internalWrite[a, l.first.val]; TokenIO.WritePopFlag[]; } ELSE WITH l.first.val SELECT FROM r: Rope.ROPE => { TokenIO.WriteAtom[$DefaultProperty]; TokenIO.WriteAtom[a]; TokenIO.WriteRope[r]; }; at: ATOM => { TokenIO.WriteAtom[$DefaultProperty]; TokenIO.WriteAtom[a]; TokenIO.WriteAtom[at]; }; ri: REF INT => { TokenIO.WriteAtom[$DefaultProperty]; TokenIO.WriteAtom[a]; TokenIO.WriteInt[ri^]; }; pl: CDPrivate.LayerRef => { TokenIO.WriteAtom[$DefaultProperty]; TokenIO.WritePushFlag[a]; TokenIO.WriteAtom[$layer]; -- now comes a property list WriteLayer[pl.number]; TokenIO.WritePopFlag[]; }; pl: CD.PropList => { TokenIO.WriteAtom[$DefaultProperty]; TokenIO.WritePushFlag[a]; TokenIO.WriteAtom[$properties]; -- now comes a property list WriteProperties[pl]; TokenIO.WritePopFlag[]; }; rl: LIST OF Rope.ROPE => { TokenIO.WriteAtom[$DefaultProperty]; TokenIO.WritePushFlag[a]; TokenIO.WriteAtom[$ropeList]; -- now comes a rope list FOR l: LIST OF Rope.ROPE _ rl, l.rest WHILE l#NIL DO TokenIO.WriteRope[l.first]; ENDLOOP; TokenIO.WritePopFlag[]; }; ENDCASE => NULL; }; ra: REF ATOM => IF ra^=$MayBeRemoved AND ra#gLastKey THEN gRemoveAlso _ ra; ENDCASE => NULL; ENDLOOP; }; WriteInstance: PUBLIC PROC [ap: CD.Instance] = { TokenIO.WriteInt[ap.location.x]; TokenIO.WriteInt[ap.location.y]; CDIO.WriteOrientation[ap.orientation]; WriteProperties[ap.properties]; WriteObject[ap.ob]; }; Length: PROC [il: CD.InstanceList] RETURNS [leng: INT_0] = INLINE { FOR list: CD.InstanceList _ il, list.rest WHILE list#NIL DO leng _ leng+1; ENDLOOP; }; WriteInstanceList: PUBLIC PROC [list: CD.InstanceList] = { TokenIO.WriteInt[Length[list]]; FOR l: CD.InstanceList _ list, l.rest WHILE l#NIL DO WriteInstance[l.first]; ENDLOOP; }; WriteObject: PUBLIC PROC [ob: CD.Object] = { WITH CDProperties.GetObjectProp[from: ob, prop: gThisKey] SELECT FROM key: REF INT => { IF key^<0 THEN {TerminalIO.WriteRope["**** circular object dependency\n"]; ERROR}; dotCnt _ dotCnt+1; IF dotCnt>=10 THEN {WriteRope["."]; dotCnt_0}; TokenIO.WriteInt[key^]; }; ENDCASE => WriteObjectDefinition[ob] }; WriteObjectDefinition: PROC [ob: CD.Object] = { key: REF INT _ NIL; WriteTableKey: PROC[ob: CD.Object] = INLINE { gDirectoryNum _ gDirectoryNum+1; key _ NEW[INT _ -gDirectoryNum]; --a negative key is not yet finished TokenIO.WriteInt[gDirectoryNum]; CDProperties.PutObjectProp[onto: ob, prop: gThisKey, val: key]; }; colCnt _ colCnt+1; IF colCnt>=10 THEN {WriteRope[":"]; colCnt_0}; IF ob.class.internalWrite=NIL THEN { ob1: CD.Object _ CDDirectory.Expand[ob].new; IF ob1=NIL THEN ob1 _ CDDirectory.ExpandByDraw[me: ob].new; IF ob1=NIL THEN { TokenIO.WritePushFlag[$Unknown]; TokenIO.WritePopFlag[]; WriteRope["*object not written\n"]; } ELSE { WriteRope["*"]; WriteObject[ob1]; CDProperties.PutObjectProp[onto: ob, prop: gThisKey, val: CDProperties.GetObjectProp[ob1, gThisKey]]; }; } ELSE { <<--object has internalWrite proc>> TokenIO.WritePushFlag[ob.class.objectType]; IF ob.class.inDirectory THEN WriteTableKey[ob] ELSE IF gCountSimpleObjectsInTable> WriteObject[me]; }; }; EachDirectoryEntry: CDDirectory.EachEntryAction = { IF ob.class.inDirectory THEN EachChildInDir[ob, NIL]; }; KillOldProps: PROC [lastAgain: BOOL] = { RemoveOldProperties: PROC[design: CD.Design, key: REF] = { <<--at this place we know that CDCleanUp will fork and will set the priority down>> CDCleanUp.RemoveProperties[design, key]; }; temThis: REF ATOM _ gThisKey; temLast: REF ATOM _ gLastKey; temAlso: REF ATOM _ gRemoveAlso; IF temThis#NIL THEN temThis^ _ $MayBeRemoved; --next term to reduce number of processes IF temLast#NIL AND lastAgain THEN RemoveOldProperties[gDesign, temLast]; IF temAlso#NIL AND temLast#temAlso THEN RemoveOldProperties[gDesign, temAlso]; }; InnerWriteDesign: PROC [] = { ENABLE UNWIND => KillOldProps[lastAgain: TRUE]; PrepareOutput: PROC = { <<--uses globals>> WITH CDValue.Fetch[gDesign, propKeyForKey, design] SELECT FROM ra: REF ATOM => gLastKey _ ra; ENDCASE => gLastKey _ NEW[ATOM]; gLastKey^ _ $MayBeRemoved; gThisKey _ NEW[ATOM _ $UsedForIO]; CDValue.Store[gDesign, propKeyForKey, gThisKey]; gDirectoryNum _ 0; }; NameForOutput: PROC [d: CD.Design] RETURNS [Rope.ROPE] = { IF Rope.Length[d.name]<=0 OR Rope.Fetch[d.name]='/ OR Rope.Fetch[d.name]='[ THEN RETURN [NIL] ELSE RETURN [d.name]; }; TimeKey: PROC [] RETURNS [Rope.ROPE] = { RETURN [Convert.RopeFromTime[from: BasicTime.Now[], end: seconds]]; }; PrepareOutput[]; TokenIO.WriteRope[NameForOutput[gDesign]]; TokenIO.WriteRope[TimeKey[]]; gDirectoryMark _ TokenIO.MarkAndWriteInt[gDirectoryNum]; -- number of entries in directory gCountSimpleObjectsInTable _ 0; [] _ CDDirectory.Enumerate[design: gDesign, action: EachDirectoryEntry]; WriteProperties[gDesign.properties^]; WritePushLevel[gDesign.actual]; TokenIO.UpdateMark[mark: gDirectoryMark, value: gDirectoryNum]; TokenIO.WriteAtom[$EndOfDesign]; KillOldProps[lastAgain: FALSE]; }; OpenStream: PROC [design: CD.Design, to: REF, emergency, quiet: BOOL] RETURNS [stream: IO.STREAM, mustClose: BOOL_TRUE, name: Rope.ROPE_NIL] = { <<--opens the file but does not do any output to it yet>> <<--makes the messages on failure>> wDir: Rope.ROPE _ CDIO.GetWorkingDirectory[design]; IF Rope.IsEmpty[wDir] THEN wDir _ FileNames.CurrentWorkingDirectory[]; WITH to SELECT FROM s: IO.STREAM => RETURN [stream_s, mustClose_FALSE, name_NIL]; r: Rope.ROPE => name _ r; ENDCASE => IF to#NIL THEN Feedback[emergency, "bad file parameter in WriteDesign\n"]; IF Rope.IsEmpty[name] THEN name _ IF emergency THEN "///temp/ChipNDale/emergency/emergency.dale" ELSE IF quiet THEN Rope.Cat["///temp/ChipNDale/saved/", design.name] ELSE GetNameInteractively[wDir]; name _ CDIO.MakeName[name, "dale", wDir]; stream _ FS.StreamOpen[fileName: name, accessOptions: $create, keep: 2, createByteCount: 25600 ! FS.Error => { mustClose _ FALSE; stream _ NIL; IF ~quiet THEN Feedback[emergency, Rope.Cat["file not opened: ", error.explanation, "\n"]]; IF error.group#bug THEN CONTINUE; } ]; IF stream=NIL AND ~quiet THEN Feedback[emergency, "file not created\n"]; }; Attach: PROC [stream: IO.STREAM, emergency: BOOL, quiet: BOOL] RETURNS [done: BOOL_TRUE] = TRUSTED { <<--makes messages if failed and no danger of locking >> TokenIO.AttachWriter[stream ! TokenIO.Error => {done _ FALSE; CONTINUE}]; IF ~done AND emergency THEN { FOR i: INT IN [0..5] DO TokenIO.StopWriting[]; Process.Pause[Process.SecondsToTicks[1]]; TokenIO.StopWriting[]; Process.Pause[Process.SecondsToTicks[2]]; Process.Detach[ FORK TerminalIO.WriteRope["TRIES TO BREAK THE LOCK\n"]]; TokenIO.ReleaseWriter[]; -- DANGEROUS Process.Pause[Process.SecondsToTicks[1]]; done _ TRUE; TokenIO.AttachWriter[stream ! TokenIO.Error => {done _ FALSE; CONTINUE}]; IF done THEN RETURN; ENDLOOP; }; IF done THEN {gQuiet _ quiet; gEmergency _ emergency; attached _ TRUE} ELSE IF ~quiet THEN Feedback[emergency, HelpMessage["ChipNDale internal locks are hold"]]; }; Release: PROC [] = { IF attached THEN TokenIO.ReleaseWriter[]; attached _ FALSE; }; WriteDesign: PUBLIC PROC [design: CD.Design, to: REF, quiet: BOOL, emergency: BOOL] RETURNS [done: BOOL_FALSE] = { <<--to is either a IO.STREAM, a Rope.ROPE, or NIL>> <<--if emergency, some locks are ignored, interactive input is skipped; you better >> <<--roll back after an emergency write is done>> ENABLE UNWIND => Release[]; mustClose: BOOL _ FALSE; binFile: IO.STREAM; fileName: Rope.ROPE; sealMark: TokenIO.Mark; [binFile, mustClose, fileName] _ OpenStream[design, to, emergency, quiet]; IF binFile=NIL THEN RETURN; IF ~Attach[binFile, emergency, quiet] THEN RETURN; [] _ CDEvents.ProcessEvent[ev: beforeOutputEvent, design: design, x: NIL, listenToDont: FALSE]; gDesign _ design; TokenIO.WriteInt[xChipndaleFile]; TokenIO.WriteInt[xVersion]; sealMark _ TokenIO.MarkAndWriteInt[0]; -- invalid seal; it will be fixed at the end TokenIO.WriteAtom[design.technology.key]; TokenIO.WriteRope[design.technology.name]; IF CDEvents.ProcessEvent[ev: writeEvent, design: design, x: NIL, listenToDont: TRUE].dont THEN { WriteRope["output not done\n"]; Release[]; RETURN }; InnerWriteDesign[]; TokenIO.UpdateMark[sealMark, -1]; -- validate seal IF mustClose THEN IO.Close[binFile]; WriteRope["\n"]; IF ~emergency AND quiet THEN TerminalIO.WriteF["%ldesign %g written on file %g%l\n", [rope["i"]], [rope[design.name]], [rope[fileName]], [rope[" "]]] ELSE Feedback[emergency, Rope.Cat["design ", design.name, " written on file ", fileName, "\n"]]; Release[]; IF ~quiet THEN { CDValue.Store[boundTo: design, key: $CDxLastFile, value: fileName]; [] _ CDEvents.ProcessEvent[afterOutputEvent, design]; }; done _ TRUE; }; HelpMessage: PROC [r: Rope.ROPE_NIL] RETURNS [msg: Rope.ROPE_NIL] = { Write: PROC [r: Rope.ROPE] = { msg _ Rope.Cat[msg, r, "\n"] }; Write["****************************************"]; Write[r]; Write["Help for saving a design:"]; Write["wait until the background saving is done and make a copy of it! (but don't rely on background saving alone; background saving is done WITHOUT requiring monitor locks, for obvious reasons.)"]; Write["then, try regular output again, before you proceed to any of the more drastic methods following:\n"]; Write["SHIFT-SHIFT-SWAT (>2 seconds) should save all designs"]; Write["or use a command tool: (the command tool commands require you to bringover the necesarry sources and bcd's first)"]; Write[" ""_ CDSequencerImpl.savePeriod _ -1"" "]; Write[" prevents any further un-monitored automatic saving in parallel with your other command tool commands."]; Write[" ""_ CDEmergencyHandling.SaveAll[]"" "]; Write[" or"]; Write[" ""_ TokenIO.ReleaseWriter[]"" "]; Write[" to release the locks; after this try a regular output command"]; Write["****************************************"]; }; <<-- Module initialization>> writeEvent: CDEvents.EventRegistration = CDEvents.RegisterEventType[$WriteTechnologyPrivate]; beforeOutputEvent: CDEvents.EventRegistration = CDEvents.RegisterEventType[$BeforeOutput]; afterOutputEvent: CDEvents.EventRegistration = CDEvents.RegisterEventType[$AfterOutput]; END.