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.
 
 
 
 
 
 

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