1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/***** spin: msc_tcl.c *****/
/*
* This file is part of the public release of Spin. It is subject to the
* terms in the LICENSE file that is included in this source directory.
* Tool documentation is available at http://spinroot.com
*/
#include <stdlib.h>
#include "spin.h"
#include "version.h"
#define MW 500 /* page width */
#define RH 100 /* right margin */
#define WW 80 /* distance between process lines */
#define HH 12 /* vertical distance between steps */
#define LW 2 /* line width of message arrows */
#define RVC "darkred" /* rendezvous arrows */
#define MPC "darkblue" /* asynchronous message passing arrow */
#define GRC "lightgrey" /* grid lines */
static int MH = 600; /* anticipated page-length */
static FILE *pfd;
static char **I; /* initial procs */
static int *D,*R; /* maps between depth (stepnr) and ldepth (msc-step) */
static short *M; /* x location of each box at index y */
static short *T; /* y index of match for each box at index y */
static char **L; /* text labels */
static int ProcLine[256]; /* active processes */
static int UsedLine[256]; /* process line has at least one entry */
static int ldepth = 1;
static int maxx, TotSteps = 2*4096; /* max nr of steps for simulation output */
static float Scaler = (float) 1.0;
static int xscale = 2;
static int yscale = 1;
static int no_box;
extern int ntrail, s_trail, pno, depth;
extern Symbol *oFname;
extern void exit(int);
extern void putpostlude(void);
static void putpages(void);
static void
psline(int x0, int y0, int x1, int y1, char *color)
{ char *side = "last";
if (x0 == x1) /* gridline */
{ fprintf(pfd, ".c create line %d %d %d %d -fill %s -tags grid -width 1 \n",
xscale*(x0+1)*WW-20, yscale*y0+20,
xscale*(x1+1)*WW-20, yscale*y1+20, color);
fprintf(pfd, ".c lower grid\n");
} else
{ int xm = xscale*(x0+1)*WW + (xscale*(x1 - x0)*WW)/2 - 20; /* mid x */
if (y1 - y0 <= HH+20)
{ y1 = y0+20; /* close enough to horizontal - looks better */
}
fprintf(pfd, ".c create line %d %d %d %d -fill %s -tags mesg -width %d\n",
xscale*(x0+1)*WW-20, yscale*y0+20+10,
xm, yscale*y0+20+10, color, LW);
if (y1 != y0+20)
{ fprintf(pfd, ".c create line %d %d %d %d -fill %s -tags mesg -width %d\n",
xm, yscale*y0+20+10,
xm, yscale*y1+20-10, color, LW);
}
fprintf(pfd, ".c create line %d %d %d %d -fill %s -width %d ",
xm, yscale*y1+20-10,
xscale*(x1+1)*WW-20, yscale*y1+20-10, color, LW);
if (strcmp(color, RVC) == 0)
{ side = "both";
}
fprintf(pfd, "-arrow %s -arrowshape {5 5 5} -tags mesg\n", side);
fprintf(pfd, ".c raise mesg\n");
}
}
static void
colbox(int ix, int iy, int w, int h_unused, char *color)
{ int x = ix*WW;
int y = iy*HH;
if (ix < 0 || ix > 255)
{ fatal("msc_tcl: unexpected\n", (char *) 0);
}
if (ProcLine[ix] < iy)
{ /* if (ProcLine[ix] > 0) */
{ psline(ix-1, ProcLine[ix]*HH+HH+4,
ix-1, iy*HH-HH, GRC);
}
fprintf(pfd, "# ProcLine[%d] from %d to %d (Used %d nobox %d)\n",
ix, ProcLine[ix], iy, UsedLine[ix], no_box);
ProcLine[ix] = iy;
} else
{ fprintf(pfd, "# ProcLine[%d] stays at %d (Used %d nobox %d)\n",
ix, ProcLine[ix], UsedLine[ix], no_box);
}
if (UsedLine[ix])
{ no_box = 2;
}
if (strcmp(color, "black") == 0)
{ if (no_box == 0) /* shadow */
{ fprintf(pfd, ".c create rectangle %d %d %d %d -fill black\n",
xscale*x-(xscale*4*w/3)-20+4, (yscale*y-10)+20+2,
xscale*x+(xscale*4*w/3)-20, (yscale*y+10)+20+2);
}
} else
{ if (no_box == 0) /* box with outline */
{ fprintf(pfd, ".c create rectangle %d %d %d %d -fill ivory\n",
xscale*x-(xscale*4*w/3)-20, (yscale*y-10)+20,
xscale*x+(xscale*4*w/3)-20, (yscale*y+10)+20);
UsedLine[ix]++;
} else /* no outline */
{ fprintf(pfd, ".c create rectangle %d %d %d %d -fill white -width 0\n",
xscale*x-(xscale*4*w/3)-20, (yscale*y-10)+20,
xscale*x+(xscale*4*w/3)-20, (yscale*y+10)+20);
} }
if (no_box > 0)
{ no_box--;
}
}
static void
stepnumber(int i)
{ int y = (yscale*i*HH) + 20;
fprintf(pfd, ".c create text %d %d -fill #eef -text \"%d\"\n",
-10+(xscale*WW)/2, y, i);
/* horizontal dashed grid line */
fprintf(pfd, ".c create line %d %d %d %d -fill #eef -dash {6 4}\n",
-20+WW*xscale, y, (maxx+1)*WW*xscale-20, y);
}
static void
spitbox(int ix, int y, char *s)
{ float bw; /* box width */
char d[256], *t, *z;
int a, i, x = ix+1;
char *color = "black";
if (y > 0)
{ stepnumber(y);
}
bw = (float)1.8*(float)strlen(s); /* guess at default font width */
colbox(x, y, (int) (bw+1.0), 5, "black");
if (s[0] == '~')
{ switch (s[1]) {
default :
case 'R': color = "red"; break;
case 'B': color = "blue"; break;
case 'G': color = "green"; break;
}
s += 2;
} else if (strchr(s, '!'))
{ color = "ivory";
} else if (strchr(s, '?'))
{ color = "azure";
} else
{ color = "pink";
if (sscanf(s, "%d:%250s", &a, d) == 2
&& a >= 0 && a < TotSteps)
{ if (!I[a] || strlen(I[a]) <= strlen(s))
{ I[a] = (char *) emalloc((int) strlen(s)+1);
}
strcpy(I[a], s);
} }
colbox(x, y, (int) bw, 4, color);
z = t = (char *) emalloc(2*strlen(s)+1);
for (i = 0; i < (int) strlen(s); i++)
{ if (s[i] == '\n')
{ continue;
}
if (s[i] == '[' || s[i] == ']')
{ *t++ = '\\';
}
*t++ = s[i];
}
fprintf(pfd, ".c create text %d %d -text \"%s\"\n",
xscale*x*WW-20, yscale*y*HH+20, z);
}
static void
putpages(void)
{ int i, lasti=0; float nmh;
if (maxx*xscale*WW > MW-RH/2)
{ Scaler = (float) (MW-RH/2) / (float) (maxx*xscale*WW);
nmh = (float) MH; nmh /= Scaler; MH = (int) nmh;
fprintf(pfd, "# Scaler %f, MH %d\n", Scaler, MH);
}
if (ldepth >= TotSteps)
{ ldepth = TotSteps-1;
}
/* W: (maxx+2)*xscale*WW */
/* H: ldepth*HH*yscale+50 */
fprintf(pfd, "wm title . \"scenario\"\n");
fprintf(pfd, "wm geometry . %dx600+650+100\n", (maxx+2)*xscale*WW);
fprintf(pfd, "canvas .c -width 800 -height 800 \\\n");
fprintf(pfd, " -scrollregion {0c -1c 30c 100c} \\\n");
fprintf(pfd, " -xscrollcommand \".hscroll set\" \\\n");
fprintf(pfd, " -yscrollcommand \".vscroll set\" \\\n");
fprintf(pfd, " -bg white -relief raised -bd 2\n");
fprintf(pfd, "scrollbar .vscroll -relief sunken ");
fprintf(pfd, " -command \".c yview\"\n");
fprintf(pfd, "scrollbar .hscroll -relief sunken -orient horiz ");
fprintf(pfd, " -command \".c xview\"\n");
fprintf(pfd, "pack append . \\\n");
fprintf(pfd, " .vscroll {right filly} \\\n");
fprintf(pfd, " .hscroll {bottom fillx} \\\n");
fprintf(pfd, " .c {top expand fill}\n");
fprintf(pfd, ".c yview moveto 0\n");
for (i = TotSteps-1; i >= 0; i--)
{ if (I[i])
{ spitbox(i, -1, I[i]);
} }
for (i = 0; i <= ldepth; i++)
{ if (!M[i] && !L[i])
{ continue; /* no box */
}
if (T[i] > 0) /* arrow */
{ if (T[i] == i) /* rv handshake */
{ psline( M[lasti], lasti*HH,
M[i], i*HH, RVC);
} else
{ psline( M[i], i*HH,
M[T[i]], T[i]*HH, MPC);
} }
if (L[i])
{ spitbox(M[i], i, L[i]);
lasti = i;
} }
}
static void
putbox(int x)
{
if (ldepth >= TotSteps)
{ fprintf(stderr, "max length of %d steps exceeded - ps file truncated\n",
TotSteps);
putpostlude();
}
M[ldepth] = x;
if (x > maxx)
{ maxx = x;
fprintf(pfd, "# maxx %d\n", x);
}
}
/* functions called externally: */
extern int WhatSeed(void);
void
putpostlude(void)
{ char cmd[512];
putpages();
fprintf(pfd, ".c lower grid\n");
fprintf(pfd, ".c raise mesg\n");
fclose(pfd);
fprintf(stderr, "seed used: -n%d\n", WhatSeed());
sprintf(cmd, "wish -f %s.tcl &", oFname?oFname->name:"msc");
fprintf(stderr, "%s\n", cmd);
(void) unlink("pan.pre");
exit (system(cmd));
}
void
putprelude(void)
{ char snap[256]; FILE *fd;
sprintf(snap, "%s.tcl", oFname?oFname->name:"msc");
if (!(pfd = fopen(snap, MFLAGS)))
{ fatal("cannot create file '%s'", snap);
}
if (s_trail)
{ if (ntrail)
sprintf(snap, "%s%d.trail", oFname?oFname->name:"msc", ntrail);
else
sprintf(snap, "%s.trail", oFname?oFname->name:"msc");
if (!(fd = fopen(snap, "r")))
{ snap[strlen(snap)-2] = '\0';
if (!(fd = fopen(snap, "r")))
fatal("cannot open trail file", (char *) 0);
}
TotSteps = 1;
while (fgets(snap, 256, fd)) TotSteps++;
fclose(fd);
}
TotSteps *= 2;
R = (int *) emalloc(TotSteps * sizeof(int));
D = (int *) emalloc(TotSteps * sizeof(int));
M = (short *) emalloc(TotSteps * sizeof(short));
T = (short *) emalloc(TotSteps * sizeof(short));
L = (char **) emalloc(TotSteps * sizeof(char *));
I = (char **) emalloc(TotSteps * sizeof(char *));
}
void
putarrow(int from, int to)
{
/* from rv if from == to */
/* which means that D[from] == D[to] */
/* which means that T[x] == x */
if (from < TotSteps
&& to < TotSteps
&& D[from] < TotSteps)
{ T[D[from]] = D[to];
}
}
void
pstext(int x, char *s)
{ char *tmp = emalloc((int) strlen(s)+1);
strcpy(tmp, s);
if (depth == 0)
{ I[x] = tmp;
} else
{ if (depth >= TotSteps || ldepth >= TotSteps)
{ fprintf(stderr, "spin: error: max nr of %d steps exceeded\n",
TotSteps);
fatal("use -uN to limit steps", (char *) 0);
}
putbox(x);
D[depth] = ldepth;
R[ldepth] = depth;
L[ldepth] = tmp;
ldepth += 2;
}
}
void
dotag(FILE *fd, char *s)
{ extern int columns, notabs; extern RunList *X;
int i = (!strncmp(s, "MSC: ", 5))?5:0;
int pid = s_trail ? pno : (X?X->pid:0);
if (columns == 2)
{ pstext(pid, &s[i]);
} else
{ if (!notabs)
{ printf(" ");
for (i = 0; i <= pid; i++)
{ printf(" ");
} }
fprintf(fd, "%s", s);
fflush(fd);
}
}
|