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.
 
 
 
 
 
 

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