ChipMaster's bwBASIC This also includes history going back to v2.10. *WARN* some binary files might have been corrupted by CRLF.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

547 lines
15 KiB

  1. /*-------------------------------------------------------------------*/
  2. /* renum.c -- Renumbers a BASIC program in an ASCII file. */
  3. /* Originally written in HP 2000 BASIC by David Lance Robinson, 1977 */
  4. /* Adapted to MS BASIC and translated to C 4/1995 by Jon B. Volkoff */
  5. /* (eidetics@cerf.net) */
  6. /*-------------------------------------------------------------------*/
  7. #include <stdio.h>
  8. int instr();
  9. char *midstr1();
  10. char *midstr2();
  11. void binary_search(void);
  12. int f2, l2, n, x;
  13. int sidx[1500][2];
  14. char rstr[255];
  15. main(argc, argv)
  16. int argc;
  17. char *argv[];
  18. {
  19. int f, d, s, p, s1, t, l, g;
  20. int c, f1, c1, i, f8, r, l1, l3;
  21. int v1, f6, l6, b, f9, x9, b1, p8, p9, a, d9;
  22. char pstr[255], sstr[255], f9str[255], s9str[255], tempstr[255];
  23. FILE *fdin, *fdout;
  24. int skip, bp, temp, getout, disp_msg;
  25. f = 1;
  26. if (argc > 1) strcpy(pstr, argv[1]);
  27. else
  28. {
  29. printf("Program in file? ");
  30. gets(pstr);
  31. }
  32. if (strlen(pstr) == 0) strcpy(pstr, "0.doc");
  33. fdin = fopen(pstr, "r");
  34. if (fdin == NULL)
  35. {
  36. printf("Unable to open input file\n");
  37. exit(1);
  38. }
  39. strcpy(f9str, pstr);
  40. strcpy(pstr, "editfl");
  41. fdout = fopen(pstr, "w");
  42. if (fdout == NULL)
  43. {
  44. printf("Unable to open editfl output file\n");
  45. exit(1);
  46. }
  47. /* Main program begins here */
  48. s = 0; l2 = 0; d = 0;
  49. f2 = 10000;
  50. printf ("PLEASE WAIT A FEW SECONDS!\n");
  51. while (fgets(pstr, 255, fdin) != NULL)
  52. {
  53. pstr[strlen(pstr) - 1] = '\0';
  54. p = instr(pstr, " ");
  55. if (p != 0 && p <= 5)
  56. {
  57. n = atoi(midstr2(pstr, 1, p));
  58. if (n != 0)
  59. {
  60. s++;
  61. sidx[s][0] = n;
  62. s1 = s;
  63. while (s1 >= 2)
  64. {
  65. s1--;
  66. if (sidx[s1][0] < sidx[s1 + 1][0]) break;
  67. if (sidx[s1][0] == sidx[s1 + 1][0])
  68. {
  69. printf("ERROR !!! MORE THAN ONE STATEMENT FOR A ");
  70. printf("STATEMENT NUMBER\n");
  71. exit(1);
  72. }
  73. t = sidx[s1][0];
  74. sidx[s1][0] = sidx[s1 + 1][0];
  75. sidx[s1 + 1][0] = t;
  76. }
  77. }
  78. }
  79. }
  80. fclose(fdin);
  81. if (s == 0)
  82. {
  83. printf("NO PROGRAM IS IN THE FILE!\n");
  84. exit(1);
  85. }
  86. for (l = 1; l <= s; l++)
  87. sidx[l][1] = sidx[l][0];
  88. g = 1;
  89. disp_msg = 1;
  90. /*------------------------------------------------------------------------*/
  91. /* Find out how and what to renumber (using HP BASIC renumber parameters) */
  92. /* MS BASIC renumber is: RENUM (newnum) (,(oldnum) (,increment)) */
  93. /*------------------------------------------------------------------------*/
  94. while(1)
  95. {
  96. if (disp_msg == 1)
  97. {
  98. printf("RENUMBER (-starting number (,interval (,first statement ");
  99. printf("(,last))))\n");
  100. disp_msg = 0;
  101. }
  102. skip = 0;
  103. bp = 0;
  104. printf("RENUMBER-");
  105. gets(pstr);
  106. p = strlen(pstr);
  107. if (g == 0)
  108. {
  109. if (strlen(pstr) == 0) break;
  110. if (p == 0) skip = 1;
  111. else
  112. {
  113. t = atoi(midstr2(pstr, 1, 1));
  114. if (t == 0) break;
  115. }
  116. }
  117. if (strlen(pstr) == 0) skip = 1;
  118. if (skip == 0)
  119. {
  120. c = instr(pstr, ",");
  121. temp = 0; if (c != 0) temp = -1;
  122. f1 = atoi(midstr2(pstr, 1, p + temp*(p - c + 1)));
  123. if (f1 == 0) bp = 1;
  124. if (c == 0) skip = 2;
  125. }
  126. if (skip == 0 && bp == 0)
  127. {
  128. c1 = instr(midstr1(pstr, c + 1), ",") + c;
  129. temp = 0; if (c1 != c) temp = -1;
  130. i = atoi(midstr2(pstr, c + 1, p + temp*(p - c1 + 1) - c));
  131. if (i == 0) bp = 1;
  132. if (c1 == c) skip = 3;
  133. }
  134. if (skip == 0 && bp == 0)
  135. {
  136. c = instr(midstr1(pstr, c1 + 1), ",") + c1;
  137. temp = 0; if (c != c1) temp = -1;
  138. f8 = atoi(midstr2(pstr, c1 + 1, p + temp*(p - c + 1) - c1));
  139. if (f8 == 0) bp = 1;
  140. if (c == c1) skip = 4;
  141. }
  142. if (skip == 0 && bp == 0)
  143. {
  144. l = atoi(midstr1(pstr, c + 1));
  145. if (l == 0) bp = 1;
  146. }
  147. if (bp == 0) switch (skip)
  148. {
  149. case 1:
  150. f1 = 10;
  151. i = 10;
  152. f8 = 1;
  153. l = 99999;
  154. break;
  155. case 2:
  156. i = 10;
  157. f8 = 1;
  158. l = 99999;
  159. break;
  160. case 3:
  161. f8 = 1;
  162. l = 99999;
  163. break;
  164. case 4:
  165. l = 99999;
  166. break;
  167. }
  168. if (f1 < 1 || i == 0 || f8 < 1 || l < 1) bp = 1;
  169. if (f1 > 99999 || i > 99999 || f8 > 99999 || l > 99999 || f8 > l)
  170. bp = 1;
  171. c = 0;
  172. for (r = 1; r <= s; r++)
  173. if (sidx[r][0] >= f8 && sidx[r][0] <= l) c = c + 1;
  174. if (c == 0)
  175. {
  176. printf("There is nothing to renumber !!\n");
  177. disp_msg = 1;
  178. }
  179. /*------------------------------------*/
  180. /* Make list of new statement numbers */
  181. /*------------------------------------*/
  182. l1 = f1 + (c - 1)*i;
  183. if (l1 < 1 || l1 > 99999) bp = 1;
  184. x = 0; c = 0;
  185. if (bp == 0 && disp_msg == 0) for (r = 1; r <= s; r++)
  186. {
  187. if (sidx[r][0] < f8 || sidx[r][0] > l)
  188. if (sidx[r][1] >= f1 && sidx[r][1] <= l1)
  189. {
  190. printf("SEQUENCE NUMBER OVERLAP\n");
  191. exit(1);
  192. }
  193. else {}
  194. else
  195. {
  196. if (sidx[r][0] != f1 + c*i)
  197. {
  198. if (x == 0)
  199. {
  200. if (r < f2) f2 = r;
  201. x = 1;
  202. }
  203. if (r > l2) l2 = r;
  204. }
  205. sidx[r][1] = f1 + c*i;
  206. c++;
  207. l3 = r;
  208. }
  209. }
  210. if (bp == 0 && disp_msg == 0) g = 0;
  211. if (bp == 1) printf("BAD PARAMETER\n");
  212. }
  213. /*-------------------*/
  214. /* Start renumbering */
  215. /*-------------------*/
  216. if (l2 == 0)
  217. {
  218. printf("NOTHING RENUMBERED!\n");
  219. exit(1);
  220. }
  221. printf("RENUMBERING\n");
  222. /* for (r = 1; r <= s; r ++)
  223. printf("%d -> %d\n", sidx[r][0], sidx[r][1]); */
  224. printf("VERIFY? ");
  225. gets(pstr);
  226. v1 = 0;
  227. if (strcmp(midstr2(pstr, 1, 1), "N") == 0) v1 = 1;
  228. fdin = fopen(f9str, "r");
  229. if (fdin == NULL)
  230. {
  231. printf("Unable to open input file\n");
  232. exit(1);
  233. }
  234. f6 = sidx[f2][0];
  235. l6 = sidx[l2][0];
  236. while (fgets(pstr, 255, fdin) != NULL)
  237. {
  238. pstr[strlen(pstr) - 1] = '\0';
  239. b = instr(pstr, " ");
  240. if (b != 0)
  241. {
  242. n = atoi(midstr2(pstr, 1, b));
  243. if (n != 0)
  244. {
  245. if (n >= f6 && n <= l6)
  246. {
  247. binary_search();
  248. if (x == 0)
  249. {
  250. strcat(rstr, midstr1(pstr, b));
  251. strcpy(pstr, rstr);
  252. b = instr(pstr, " ");
  253. }
  254. }
  255. b++;
  256. /*-------------------------------------------------------------*/
  257. /* There are differences, of course, between processing for HP */
  258. /* BASIC and MS BASIC. */
  259. /* */
  260. /* CONVERT, PRINT USING, and MAT PRINT USING changes are not */
  261. /* applicable in MS BASIC. */
  262. /* */
  263. /* Had to also add capability for multiple statements here. */
  264. /*-------------------------------------------------------------*/
  265. while(1)
  266. {
  267. if (strcmp(midstr2(pstr, b, 3), "REM") == 0 ||
  268. strcmp(midstr2(pstr, b, 1), "'") == 0) break;
  269. f9 = 0;
  270. skip = 0;
  271. for (x9 = b; x9 <= strlen(pstr); x9++)
  272. {
  273. if ((char)(*midstr2(pstr, x9, 1)) == 34)
  274. {
  275. if (f9 == 0)
  276. f9 = 1;
  277. else
  278. f9 = 0;
  279. }
  280. else if (strcmp(midstr2(pstr, x9, 1), ":") == 0 &&
  281. f9 == 0)
  282. {
  283. b1 = x9 - 1;
  284. skip = 1;
  285. break;
  286. }
  287. }
  288. if (skip == 0) b1 = strlen(pstr);
  289. t = instr("GOSGOTIF ON RESRET", midstr2(pstr, b, 3));
  290. temp = (t + 5)/3;
  291. if (temp != 1)
  292. {
  293. if (temp == 2 || temp == 3 || temp == 4 || temp == 6 ||
  294. temp == 7)
  295. {
  296. /*-------------------------------------------------*/
  297. /* Change GOSUB, GOTO, IF, RESTORE, RESUME, RETURN */
  298. /* routine. */
  299. /* Go word by word through the statement. */
  300. /*-------------------------------------------------*/
  301. getout = 0;
  302. p8 = b;
  303. strcpy(s9str, " ");
  304. }
  305. else if (temp == 5)
  306. {
  307. /*---------------------------------------------------*/
  308. /* Change ON event/expression GOSUB/GOTO routine. */
  309. /* Find starting point appropriate to this statement */
  310. /* type. */
  311. /*---------------------------------------------------*/
  312. getout = 1;
  313. for (x9 = b1; x9 >= b; x9--)
  314. {
  315. if (strcmp(midstr2(pstr, x9, 1), " ") == 0)
  316. {
  317. p8 = x9 + 1;
  318. getout = 0;
  319. break;
  320. }
  321. }
  322. if (getout == 0) strcpy(s9str, ",");
  323. }
  324. /* Start looping here */
  325. if (getout == 0) while(1)
  326. {
  327. f9 = 0;
  328. skip = 0;
  329. for (x9 = p8; x9 <= b1; x9++)
  330. {
  331. if ((char)(*midstr2(pstr, x9, 1)) == 34)
  332. {
  333. if (f9 == 0)
  334. f9 = 1;
  335. else
  336. f9 = 0;
  337. }
  338. else if (strcmp(midstr2(pstr, x9, 1), s9str) == 0 &&
  339. f9 == 0)
  340. {
  341. p9 = x9 - 1;
  342. skip = 1;
  343. break;
  344. }
  345. }
  346. if (skip == 0) p9 = b1;
  347. skip = 0;
  348. for (x9 = p8; x9 <= p9; x9++)
  349. {
  350. a = (char)(*midstr2(pstr, x9, 1));
  351. if (a < 48 || a > 57)
  352. {
  353. skip = 1;
  354. break;
  355. }
  356. }
  357. if (skip == 0)
  358. {
  359. /*---------------------*/
  360. /* Found a line number */
  361. /*---------------------*/
  362. n = atoi(midstr2(pstr, p8, p9 - p8 + 1));
  363. if (n != 0)
  364. {
  365. if (n >= f6 && n <= l6)
  366. {
  367. binary_search();
  368. if (x == 0)
  369. {
  370. if (p9 == strlen(pstr))
  371. {
  372. strcpy(tempstr, midstr2(pstr, 1, p8 - 1));
  373. strcat(tempstr, rstr);
  374. strcpy(pstr, tempstr);
  375. }
  376. else
  377. {
  378. strcpy(tempstr, midstr2(pstr, 1, p8 - 1));
  379. strcat(tempstr, rstr);
  380. strcat(tempstr, midstr1(pstr, p9 + 1));
  381. strcpy(pstr, tempstr);
  382. }
  383. /*-----------------------------------*/
  384. /* Adjust indices to account for new */
  385. /* substring length, if any. */
  386. /*-----------------------------------*/
  387. d9 = strlen(rstr) - (p9 - p8 + 1);
  388. p9 = p9 + d9;
  389. b1 = b1 + d9;
  390. }
  391. }
  392. }
  393. }
  394. p8 = p9 + 2;
  395. if (p8 > b1) break;
  396. }
  397. }
  398. /*--------------------------------------------------*/
  399. /* No more words to process in the statement, go to */
  400. /* next statement. */
  401. /*--------------------------------------------------*/
  402. if (b1 == strlen(pstr)) break;
  403. b = b1 + 2;
  404. }
  405. }
  406. }
  407. fprintf(fdout, "%s\n", pstr);
  408. if (v1 == 0) printf("%s\n", pstr);
  409. }
  410. fclose(fdin);
  411. fclose(fdout);
  412. sprintf(tempstr, "mv editfl %s\0", f9str);
  413. system(tempstr);
  414. return (0);
  415. }
  416. int instr(astr, bstr)
  417. char *astr, *bstr;
  418. {
  419. int p;
  420. p = strstr(astr, bstr);
  421. if (p == NULL) p = (int)(astr) - 1;
  422. p = p - (int)(astr) + 1;
  423. return p;
  424. }
  425. char *midstr1(astr, start)
  426. char *astr;
  427. int start;
  428. {
  429. static char tempstr[255];
  430. char *startptr;
  431. strcpy(tempstr, astr);
  432. startptr = (char *)((long)(tempstr) + start - 1);
  433. return startptr;
  434. }
  435. char *midstr2(astr, start, len)
  436. char *astr;
  437. int start, len;
  438. {
  439. static char tempstr[255];
  440. char *startptr, *endptr;
  441. strcpy(tempstr, astr);
  442. startptr = (char *)((long)(tempstr) + start - 1);
  443. endptr = (char *)((long)(tempstr) + start + len - 1);
  444. strcpy(endptr, "\0");
  445. return startptr;
  446. }
  447. void binary_search(void)
  448. {
  449. int f5, l5, m;
  450. f5 = f2;
  451. l5 = l2 + 1;
  452. while(1)
  453. {
  454. m = (f5 + l5)/2;
  455. if (sidx[m][0] == n)
  456. {
  457. sprintf(rstr, "%d\0", sidx[m][1]);
  458. x = 0;
  459. return;
  460. }
  461. if (m == f5 || m == l5)
  462. {
  463. x = 1;
  464. return;
  465. }
  466. if (sidx[m][0] < n)
  467. f5 = m;
  468. else
  469. l5 = m;
  470. }
  471. }