Source code of Windows XP (NT5)
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.

776 lines
13 KiB

  1. /*
  2. text.c - Module to work with the text strings.
  3. ...
  4. 10-15-89 MHS More Autodoc and more asserts.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <assert.h>
  12. #include "types.h"
  13. #include "text.h"
  14. #include "docfmt.h"
  15. #include "errstr.h"
  16. #include "misc.h"
  17. /*
  18. * @doc INTERNAL
  19. *
  20. * @func void | stripNewline | This function strips trailing white space.
  21. *
  22. * @parm char * | pch | Specifies the line to strip.
  23. * and trailing white space.
  24. *
  25. */
  26. void
  27. stripNewline(char *pch)
  28. {
  29. int i;
  30. unsigned char *puch;
  31. assert(pch);
  32. i=strlen(pch);
  33. puch=(unsigned char *)pch; // so isspace always works
  34. while(i>=0) // process whole string from end
  35. {
  36. if(!isspace(puch[i])) // if it is evil,
  37. return;
  38. puch[i]='\0'; // nuke it
  39. --i;
  40. }
  41. }
  42. /*
  43. * @doc INTERNAL
  44. *
  45. * @func BOOL | getLine | This function gets a line from the file
  46. * specified by Input file structure and puts it into the line buffer.
  47. *
  48. * @parm EXTFile * | pExt | Specifies the file.
  49. *
  50. * @rdesc The return value is FALSE if there was an error
  51. * reading the file.
  52. *
  53. * @xref stripNewline
  54. */
  55. BOOL getLine( EXTFile *pExt )
  56. {
  57. if(!pExt->lineBuffer) // if it doesn't already exist,
  58. pExt->lineBuffer=my_malloc(MAXLINESIZE + 1); // make it
  59. if(!pExt->lineBuffer)
  60. {
  61. error(ERROR3);
  62. return (FALSE);
  63. }
  64. if( fgets(pExt->lineBuffer, MAXLINESIZE, pExt->fp) != NULL )
  65. {
  66. /* replace the newline with a NULL */
  67. stripNewline(pExt->lineBuffer);
  68. pExt->curlineno++; // increment line count
  69. pExt->curtag=0; // new line. no current tag.
  70. pExt->curlinepos=0; // start at beginning of line.
  71. return( TRUE );
  72. }
  73. else
  74. {
  75. return( FALSE );
  76. }
  77. }
  78. /*
  79. * @doc INTERNAL
  80. *
  81. * @func char * | lineText | This function tells if the line has any
  82. * text on it.
  83. *
  84. * @rdesc The return value is NULL if there is no text on the line.
  85. * It is the location of the text otherwise.
  86. */
  87. char * lineText(char *pch)
  88. {
  89. while( (*pch) && *pch!='\n')
  90. {
  91. if( !isspace(*pch))
  92. {
  93. return( pch );
  94. }
  95. pch++;
  96. }
  97. return( NULL );
  98. }
  99. /*
  100. * @doc INTERNAL
  101. *
  102. * @func char * | lineMake | This allocates the line structure,
  103. * sets the text string and nulls out the next feild of the structure.
  104. *
  105. * @parm char * | string | Specifies a pointer to the
  106. * string to be set up as a line processed.
  107. *
  108. * @rdesc The return value is a pointer to the line structure
  109. */
  110. aLine * lineMake(string)
  111. char *string;
  112. {
  113. aLine *line;
  114. char *pch;
  115. int i;
  116. /* strip leading white space */
  117. while(isspace(*string))
  118. string++;
  119. i=0;
  120. pch=string;
  121. while(*pch)
  122. {
  123. i++;
  124. ++pch;
  125. }
  126. --pch; // get before \0
  127. while(i>0 && isspace(*pch))
  128. {
  129. *pch='\0'; // nuke white space at EOS
  130. --pch;
  131. --i;
  132. }
  133. /* make aLine structure */
  134. line = (aLine *)my_malloc( 1 + strlen(string) + sizeof( aLine ) );
  135. if( line == NULL )
  136. {
  137. error(ERROR3);
  138. return( NULL );
  139. }
  140. strcpy( line->text, string );
  141. line->next = NULL;
  142. return( line );
  143. }
  144. /*
  145. * @doc INTERNAL
  146. *
  147. * @func void | lineDestroy | This de-allocates the list of line structures.
  148. *
  149. * @parm aLine * | line | Specifies the head of the list.
  150. */
  151. void
  152. lineDestroy( line )
  153. aLine *line;
  154. {
  155. aLine *next;
  156. while( line != NULL )
  157. {
  158. next = line->next;
  159. my_free( line );
  160. line = next;
  161. }
  162. }
  163. /*
  164. * @doc INTERNAL
  165. *
  166. * @func void | lineAdd | This adds the given line to the list of
  167. * lines.
  168. *
  169. * @parm aLine * * | place | Specifies a the head of the list lines.
  170. *
  171. * @parm aLine * | line | Specifies the line to add.
  172. *
  173. * @comm The line is added to the end of the list.
  174. */
  175. void
  176. lineAdd( place, line )
  177. aLine * * place;
  178. aLine * line;
  179. {
  180. assert(place);
  181. while( *place != NULL )
  182. {
  183. place = (aLine **)(*place);
  184. }
  185. *place = line;
  186. }
  187. /*
  188. * @doc INTERNAL
  189. *
  190. * @func aFlag * | flagAlloc | This allocates and nulls out
  191. * a flag structure;
  192. *
  193. * @rdesc The return value is a pointer to the flag structure
  194. */
  195. aFlag * flagAlloc()
  196. {
  197. aFlag *flag;
  198. flag = (aFlag *)clear_alloc( sizeof( aFlag ) );
  199. if( !flag )
  200. {
  201. error(ERROR3);
  202. return( NULL );
  203. }
  204. return( flag );
  205. }
  206. /*
  207. * @doc INTERNAL
  208. *
  209. * @func void | flagDestroy | This de-allocates a list of flag structures.
  210. *
  211. * @parm aFlag *|flag| Specifies the head of the list to destroy.
  212. *
  213. * @xref lineDestroy
  214. */
  215. void flagDestroy( flag )
  216. aFlag *flag;
  217. {
  218. aFlag * next;
  219. while( flag )
  220. {
  221. next = flag->next;
  222. lineDestroy( flag->name );
  223. lineDestroy( flag->desc );
  224. my_free( flag );
  225. flag = next;
  226. }
  227. }
  228. /*
  229. * @doc INTERNAL
  230. *
  231. * @func void | flagAdd | This adds the given flag to the list of
  232. * flags.
  233. *
  234. * @parm aFlag * * | place | Specifies a pointer to the pointer
  235. * that is the list start
  236. *
  237. * @parm aFlag * | flag | Specifies the flag to add.
  238. */
  239. void flagAdd( place, flag )
  240. aFlag * * place;
  241. aFlag * flag;
  242. {
  243. if( place == NULL ) {
  244. return;
  245. }
  246. while( *place != NULL ) {
  247. place = &((*place)->next);
  248. }
  249. *place = flag;
  250. }
  251. /*
  252. * @doc INTERNAL
  253. *
  254. * @func aReg* | regAlloc | This allocates and nulls out
  255. * a reg structure;
  256. *
  257. * @rdesc The return value is a pointer to the parm structure
  258. */
  259. aReg * regAlloc()
  260. {
  261. aReg *reg;
  262. reg = (aReg *)clear_alloc( sizeof( aReg ) );
  263. if(! reg )
  264. {
  265. error(ERROR3);
  266. return( NULL );
  267. }
  268. return( reg );
  269. }
  270. /*
  271. * @doc INTERNAL
  272. *
  273. * @func void | regAdd | This adds the given reg to the list of
  274. * reg.
  275. *
  276. * @parm aReg * * | place | Specifies the head of the list of regs.
  277. *
  278. * @parm aReg * | parm | Specifies the reg to add.
  279. *
  280. * @comm The register is added to the end of the list.
  281. */
  282. void
  283. regAdd( aReg * * place, aReg * reg )
  284. {
  285. if( place == NULL )
  286. {
  287. return;
  288. }
  289. while( *place != NULL )
  290. {
  291. place = &((*place)->next);
  292. }
  293. *place = reg;
  294. }
  295. /*
  296. * @doc INTERNAL
  297. *
  298. * @func void | regDestroy | This de-allocates a list of reg structures.
  299. *
  300. * @parm aReg * | reg | Specifies the head of the reg structure to destroy.
  301. *
  302. * @xref lineDestroy, flagDestroy
  303. *
  304. */
  305. void regDestroy( aReg *reg )
  306. {
  307. aReg * next;
  308. while( reg != NULL )
  309. {
  310. next = reg->next;
  311. lineDestroy( reg->name );
  312. // lineDestroy( reg->type );
  313. lineDestroy( reg->desc );
  314. flagDestroy( reg->flag );
  315. my_free( reg );
  316. reg = next;
  317. }
  318. }
  319. /*
  320. * @doc INTERNAL
  321. *
  322. * @func aCond* | condAlloc | This allocates and nulls out
  323. * a cond structure;
  324. *
  325. * @rdesc The return value is a pointer to the parm structure
  326. */
  327. aCond * condAlloc()
  328. {
  329. aCond *cond;
  330. cond = (aCond *)clear_alloc( sizeof( aCond ) );
  331. if(! cond )
  332. {
  333. error(ERROR3);
  334. return( NULL );
  335. }
  336. return( cond );
  337. }
  338. /*
  339. * @doc INTERNAL
  340. *
  341. * @func void | condAdd | This adds the given cond to the list of
  342. * conds.
  343. *
  344. * @parm aCond * * | place | Specifies the head of the list of conds.
  345. *
  346. * @parm aCond * | parm | Specifies the cond to add.
  347. *
  348. * @comm The Condition is added to the end of the list.
  349. */
  350. void
  351. condAdd( aCond * * place, aCond * cond )
  352. {
  353. if( place == NULL )
  354. {
  355. return;
  356. }
  357. while( *place != NULL )
  358. {
  359. place = &((*place)->next);
  360. }
  361. *place = cond;
  362. }
  363. /*
  364. * @doc INTERNAL
  365. *
  366. * @func void | condDestroy | This de-allocates a list of cond structures.
  367. *
  368. * @parm aCond * | cond | Specifies the head of the cond structure to destroy.
  369. *
  370. * @xref lineDestroy, regDestroy
  371. *
  372. */
  373. void condDestroy( aCond *cond )
  374. {
  375. aCond * next;
  376. while( cond != NULL )
  377. {
  378. next = cond->next;
  379. lineDestroy( cond->desc );
  380. regDestroy( cond->regs );
  381. my_free( cond );
  382. cond = next;
  383. }
  384. }
  385. /*
  386. * @doc INTERNAL
  387. *
  388. * @func aParm * | parmAlloc | This allocates and nulls out
  389. * a parm structure;
  390. *
  391. * @rdesc The return value is a pointer to the parm structure
  392. */
  393. aParm * parmAlloc()
  394. {
  395. aParm *parm;
  396. parm = (aParm *)clear_alloc( sizeof( aParm ) );
  397. if(! parm )
  398. {
  399. error(ERROR3);
  400. return( NULL );
  401. }
  402. return( parm );
  403. }
  404. /*
  405. * @doc INTERNAL
  406. *
  407. * @func void | parmDestroy | This de-allocates a list of parm structures.
  408. *
  409. * @parm aParm * |parm| Specifies the head of the parm structure to destroy.
  410. *
  411. * @xref lineDestroy, flagDestroy
  412. *
  413. */
  414. void parmDestroy( parm )
  415. aParm *parm;
  416. {
  417. aParm * next;
  418. while( parm != NULL )
  419. {
  420. next = parm->next;
  421. lineDestroy( parm->name );
  422. lineDestroy( parm->type );
  423. lineDestroy( parm->desc );
  424. flagDestroy( parm->flag );
  425. my_free( parm );
  426. parm = next;
  427. }
  428. }
  429. /*
  430. * @doc INTERNAL
  431. *
  432. * @func void | parmAdd | This adds the given parm to the list of
  433. * parms.
  434. *
  435. * @parm aParm * * | place | Specifies the head of the list of parms.
  436. *
  437. * @parm aParm * | parm | Specifies the parm to add.
  438. *
  439. * @comm The paramater is added to the end of the list.
  440. */
  441. void
  442. parmAdd( place, parm )
  443. aParm * * place;
  444. aParm * parm;
  445. {
  446. if( place == NULL )
  447. {
  448. return;
  449. }
  450. while( *place != NULL )
  451. {
  452. place = &((*place)->next);
  453. }
  454. *place = parm;
  455. }
  456. /*
  457. * @doc INTERNAL
  458. *
  459. * @func aField * | fieldAlloc | This allocates and nulls out
  460. * a structure;
  461. *
  462. * @rdesc The return value is a pointer to the structure
  463. */
  464. aField * fieldAlloc()
  465. {
  466. aField *field;
  467. field = (aField *)clear_alloc( sizeof( aField ) );
  468. if(! field )
  469. {
  470. error(ERROR3);
  471. return( NULL );
  472. }
  473. return( field );
  474. }
  475. /*
  476. * @doc INTERNAL
  477. *
  478. * @func void | FieldDestroy | This de-allocates a list of Field structures.
  479. *
  480. * @Field aField * |Field| Specifies the head of the Field structure to destroy.
  481. *
  482. * @xref lineDestroy, flagDestroy
  483. *
  484. */
  485. void fieldDestroy( Field )
  486. aField *Field;
  487. {
  488. aField * next;
  489. while( Field )
  490. {
  491. next = Field->next;
  492. switch(Field->wType)
  493. {
  494. case FIELD_TYPE:
  495. typeDestroy((aType *)Field->ptr);
  496. break;
  497. case FIELD_STRUCT:
  498. case FIELD_UNION:
  499. SUDestroy((aSU *)Field->ptr);
  500. break;
  501. default:
  502. fprintf(stderr,"INTERNAL ERROR: unknown field type in destroy!\n");
  503. assert(FALSE);
  504. break;
  505. }
  506. my_free( Field );
  507. Field = next;
  508. }
  509. }
  510. /*
  511. * @doc INTERNAL
  512. *
  513. * @func void | FieldAdd | This adds the given Field to the list of
  514. * Fields.
  515. *
  516. * @Field aField * * | place | Specifies the head of the list of Fields.
  517. *
  518. * @Field aField * | Field | Specifies the Field to add.
  519. *
  520. * @comm The paramater is added to the end of the list.
  521. */
  522. void
  523. FieldAdd( place, Field )
  524. aField * * place;
  525. aField * Field;
  526. {
  527. if( place == NULL )
  528. {
  529. return;
  530. }
  531. while( *place != NULL )
  532. {
  533. place = &((*place)->next);
  534. }
  535. *place = Field;
  536. }
  537. /*
  538. * @doc INTERNAL
  539. *
  540. * @func aField * | fieldAlloc | This allocates and nulls out
  541. * a structure;
  542. *
  543. * @rdesc The return value is a pointer to the structure
  544. */
  545. aType * typeAlloc()
  546. {
  547. aType *type;
  548. type = (aType *)clear_alloc( sizeof( aType ) );
  549. if(! type )
  550. {
  551. error(ERROR3);
  552. return( NULL );
  553. }
  554. return( type );
  555. }
  556. /*
  557. * @doc INTERNAL
  558. *
  559. * @func void | FieldDestroy | This de-allocates a list of Field structures.
  560. *
  561. * @Field aField * |Field| Specifies the head of the Field structure to destroy.
  562. *
  563. * @xref lineDestroy, flagDestroy
  564. *
  565. */
  566. void typeDestroy( type )
  567. aType *type;
  568. {
  569. while( type )
  570. {
  571. lineDestroy( type->name );
  572. lineDestroy( type->type );
  573. lineDestroy( type->desc );
  574. flagDestroy( type->flag );
  575. my_free( type );
  576. type=NULL;
  577. }
  578. }
  579. /*
  580. * @doc INTERNAL
  581. *
  582. * @func aField * | fieldAlloc | This allocates and nulls out
  583. * a structure;
  584. *
  585. * @rdesc The return value is a pointer to the structure
  586. */
  587. aSU * SUAlloc()
  588. {
  589. aSU *SU;
  590. SU = (aSU *)clear_alloc( sizeof( aSU ) );
  591. if(! SU )
  592. {
  593. error(ERROR3);
  594. return( NULL );
  595. }
  596. return( SU );
  597. }
  598. /*
  599. * @doc INTERNAL
  600. *
  601. * @func void | FieldDestroy | This de-allocates a list of Field structures.
  602. *
  603. * @Field aField * |Field| Specifies the head of the Field structure to destroy.
  604. *
  605. * @xref lineDestroy, flagDestroy
  606. *
  607. */
  608. void SUDestroy( SU )
  609. aSU *SU;
  610. {
  611. while( SU )
  612. {
  613. lineDestroy( SU->name );
  614. lineDestroy( SU->desc );
  615. fieldDestroy( SU->field );
  616. my_free( SU );
  617. SU=NULL;
  618. }
  619. }
  620. /*
  621. * @doc INTERNAL
  622. *
  623. * @func aField * | fieldAlloc | This allocates and nulls out
  624. * a structure;
  625. *
  626. * @rdesc The return value is a pointer to the structure
  627. */
  628. aOther * otherAlloc()
  629. {
  630. aOther *other;
  631. other = (aOther *)clear_alloc( sizeof( aOther ) );
  632. if(! other )
  633. {
  634. error(ERROR3);
  635. return( NULL );
  636. }
  637. return( other );
  638. }
  639. /*
  640. * @doc INTERNAL
  641. *
  642. * @func void | FieldDestroy | This de-allocates a list of Field structures.
  643. *
  644. * @Field aField * |Field| Specifies the head of the Field structure to destroy.
  645. *
  646. * @xref lineDestroy, flagDestroy
  647. *
  648. */
  649. void otherDestroy( other )
  650. aOther *other;
  651. {
  652. aOther * next;
  653. while( other )
  654. {
  655. next = other->next;
  656. lineDestroy( other->name );
  657. lineDestroy( other->desc );
  658. lineDestroy( other->type );
  659. my_free( other );
  660. other = next;
  661. }
  662. }
  663. /*
  664. * @doc INTERNAL
  665. *
  666. * @func void | FieldAdd | This adds the given Field to the list of
  667. * Fields.
  668. *
  669. * @Field aField * * | place | Specifies the head of the list of Fields.
  670. *
  671. * @Field aField * | Field | Specifies the Field to add.
  672. *
  673. * @comm The paramater is added to the end of the list.
  674. */
  675. void
  676. otherAdd( place, other )
  677. aOther * * place;
  678. aOther * other;
  679. {
  680. if( place == NULL )
  681. {
  682. return;
  683. }
  684. while( *place != NULL )
  685. {
  686. place = &((*place)->next);
  687. }
  688. *place = other;
  689. }