esdutil.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*+----------------------------------------------------------------
  2. esdutil.c - ecu extended string descriptor manipulation
  3. wht@wht.net
  4. Defined functions:
  5. end_of_cmd(tesd)
  6. esd_null_terminate(tesd)
  7. esd_strip_trail_break(ztext)
  8. esdalloc(maxcb)
  9. esdcat(dest, suffix, realloc_ok)
  10. esdfgets(tesd, fileptr)
  11. esdfputs(tesd, fileptr, index_flag, nl_flag)
  12. esdfree(tesd)
  13. esdinit(tesd, cp, maxcb)
  14. esdprefix(tesd, zstr)
  15. esdrealloc(tesd, maxcb)
  16. esdshow(tesd, title)
  17. esdstrcat(tesd, zstr)
  18. esdstrindex(esd1, esd2, index1_flag, index2_flag)
  19. esdzero(tesd)
  20. get_alpha_zstr(tesd, strbuf, strbuf_maxcb)
  21. get_alphanum_zstr(tesd, strbuf, strbuf_maxcb)
  22. get_cmd_char(tesd, pchar)
  23. get_numeric_value(tesd, value)
  24. get_numeric_zstr(tesd, strbuf, strbuf_maxcb)
  25. get_switches(tesd, switches, switches_max)
  26. get_word_zstr(tesd, strbuf, strbuf_maxcb)
  27. keyword_lookup(ktable, keywd)
  28. skip_cmd_break(tesd)
  29. skip_cmd_char(tesd, skipchar)
  30. skip_colon(tesd)
  31. skip_comma(tesd)
  32. skip_paren(tesd, fLeft)
  33. strindex(str1, str2)
  34. This is old code; give me a break
  35. -----------------------------------------------------------------*/
  36. /*+:EDITS:*/
  37. /*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
  38. /*:03-22-2000-18:47-wht@blue-4.41-IMPORTANT esdrealloc fix */
  39. /*:12-28-1999-12:00-wht@menlo-4.40-borrow back get_ipaddr_zstr and esdclone */
  40. /*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
  41. /*:09-11-1996-20:00-wht@yuriatin-3.48-major telnet,curses,structural overhaul */
  42. /*:08-20-1996-12:39-wht@kepler-locale/ctype fixes from ache@nagual.ru */
  43. /*:11-24-1995-10:58-wht@kepler-add esdprefix */
  44. /*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
  45. /*:11-14-1995-10:23-wht@kepler-3.37.80-source control point: SOCKETS */
  46. /*:11-04-1995-13:57-wht@wwtp1-esdfree now ignores zero arg */
  47. /*:09-17-1995-16:28-wht@kepler-remove obsolete #if 0 code */
  48. /*:05-04-1994-04:39-wht@n4hgf-ECU release 3.30 */
  49. /*:12-19-1992-16:30-wht@n4hgf-rectification of names in keyword_lookup */
  50. /*:09-10-1992-13:59-wht@n4hgf-ECU release 3.20 */
  51. /*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
  52. /*:03-20-1992-06:26-wht@n4hgf-esdstrcat will grow an esd */
  53. /*:08-25-1991-23:20-root@n4hgf2-get_switches could overflow result string */
  54. /*:07-25-1991-12:57-wht@n4hgf-ECU release 3.10 */
  55. /*:05-02-1991-04:12-wht@n4hgf-how did esdrealloc ever work? */
  56. /*:04-23-1991-04:33-wht@n4hgf-function name reorganization */
  57. /*:04-23-1991-04:33-wht@n4hgf-add esdcat */
  58. /*:01-31-1991-14:49-wht@n4hgf-rework esdrealloc for speed */
  59. /*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */
  60. #include "ecu.h"
  61. #include "ecuerror.h"
  62. #include "esd.h"
  63. extern int errno;
  64. /*+-------------------------------------------------------------------------
  65. esd_null_terminate(&esd)
  66. puts null at 'cb' position of string (standard esd always
  67. has one more byte in buffer than maxcb says)
  68. --------------------------------------------------------------------------*/
  69. void
  70. esd_null_terminate(tesd)
  71. ESD *tesd;
  72. {
  73. tesd->pb[tesd->cb] = 0;
  74. } /* end of esd_null_terminate */
  75. /*+-----------------------------------------------------------------------
  76. esdzero(tesd) - zero an esd
  77. ------------------------------------------------------------------------*/
  78. void
  79. esdzero(tesd)
  80. ESD *tesd;
  81. {
  82. tesd->cb = 0; /* current count == 0 */
  83. tesd->index = 0; /* parse index to first position */
  84. tesd->old_index = 0; /* parse index to first position */
  85. *tesd->pb = 0; /* start with null terminated string */
  86. } /* end of esdzero */
  87. /*+-----------------------------------------------------------------------
  88. esdinit(tesd,cp,maxcb) - init an esd
  89. ------------------------------------------------------------------------*/
  90. void
  91. esdinit(tesd, cp, maxcb)
  92. ESD *tesd;
  93. char *cp;
  94. int maxcb;
  95. {
  96. tesd->pb = cp; /* pointer to string */
  97. tesd->maxcb = maxcb; /* max characters in buffer */
  98. esdzero(tesd);
  99. } /* end of esdinit */
  100. /*+-----------------------------------------------------------------------
  101. esdptr = esdalloc(maxcb) - allocate an esd and buffer
  102. ------------------------------------------------------------------------*/
  103. ESD *
  104. esdalloc(maxcb)
  105. int maxcb; /* desired maxcb */
  106. {
  107. ESD *tesd;
  108. int actual_cb;
  109. /* we get an extra character to ensure room for null past maxcb */
  110. actual_cb = maxcb + 1;
  111. if (actual_cb & 1) /* even allocation */
  112. ++actual_cb;
  113. if (!(tesd = (ESD *) malloc(sizeof(ESD))))
  114. return ((ESD *) 0); /* return NULL if failure */
  115. if (!(tesd->pb = malloc(actual_cb)))
  116. {
  117. free((char *)tesd);
  118. return ((ESD *) 0); /* return NULL if failure */
  119. }
  120. esdinit(tesd, tesd->pb, maxcb);
  121. return (tesd);
  122. } /* end of esdalloc */
  123. /*+-----------------------------------------------------------------------
  124. esdptr = esdrealloc(maxcb) - realloc an esd buffer
  125. may only be used to enlarge an esd buffer
  126. this used to use realloc(), which did a lot of unnecessary copying
  127. also no more abnormal program termination on memory failure
  128. ------------------------------------------------------------------------*/
  129. int
  130. esdrealloc(tesd, maxcb)
  131. ESD *tesd;
  132. int maxcb; /* desired maxcb */
  133. {
  134. int actual_cb;
  135. char *newpb;
  136. if (!tesd || (tesd->maxcb > maxcb))
  137. return (eInternalLogicError);
  138. /* enforce our limit */
  139. if (maxcb > ESD_MAXSZ)
  140. return (eBufferTooSmall);
  141. /* we get an extra character to ensure room for null past maxcb */
  142. actual_cb = maxcb + 1;
  143. if (actual_cb & 1) /* even allocation */
  144. ++actual_cb;
  145. if (!(newpb = malloc(actual_cb)))
  146. return (eNoMemory);
  147. if (tesd->cb)
  148. memcpy(newpb, tesd->pb, tesd->cb);
  149. free(tesd->pb);
  150. tesd->pb = newpb;
  151. tesd->maxcb = maxcb;
  152. esd_null_terminate(tesd);
  153. return (0);
  154. } /* end of esdrealloc */
  155. /*+-----------------------------------------------------------------------
  156. esdfree(esdptr) - free an allocated esd
  157. ------------------------------------------------------------------------*/
  158. void
  159. esdfree(tesd)
  160. ESD *tesd;
  161. {
  162. if (tesd)
  163. {
  164. free(tesd->pb);
  165. free((char *)tesd);
  166. }
  167. }
  168. /*+-------------------------------------------------------------------------
  169. esdcat(dest,suffix,realloc_ok) - "strcat" for ESDs
  170. Append 'suffix' contents to 'dest'
  171. if realloc_ok true, expand 'dest' as necessary
  172. Returns: 0 - success
  173. eNoMemory
  174. eBufferTooSmall
  175. --------------------------------------------------------------------------*/
  176. int
  177. esdcat(dest, suffix, realloc_ok)
  178. ESD *dest;
  179. ESD *suffix;
  180. int realloc_ok;
  181. {
  182. int erc = 0;
  183. int new_maxcb = dest->cb + suffix->cb;
  184. if (dest->maxcb < new_maxcb)
  185. {
  186. if (!realloc_ok)
  187. return (eBufferTooSmall);
  188. if (erc = esdrealloc(dest, new_maxcb))
  189. return (erc);
  190. }
  191. memcpy(dest->pb + dest->cb, suffix->pb, suffix->cb + 1); /* null too */
  192. dest->cb += suffix->cb; /* but do not include null in count */
  193. return (0);
  194. } /* end of esdcat */
  195. /*+-------------------------------------------------------------------------
  196. esdstrcat(tesd,zstr) - "strcat" for ESDs
  197. similar to esdcat(), but with automatic esd growth
  198. --------------------------------------------------------------------------*/
  199. int
  200. esdstrcat(tesd, zstr)
  201. ESD *tesd;
  202. char *zstr;
  203. {
  204. int zstrlen = strlen(zstr);
  205. int erc = 0;
  206. if (zstrlen > (tesd->maxcb - tesd->cb))
  207. {
  208. if (erc = esdrealloc(tesd, tesd->cb + zstrlen))
  209. return (erc);
  210. }
  211. if (zstrlen)
  212. {
  213. strncpy(tesd->pb + tesd->cb, zstr, zstrlen);
  214. tesd->cb += zstrlen;
  215. esd_null_terminate(tesd);
  216. }
  217. return (erc);
  218. } /* end of esdstrcat */
  219. /*+-------------------------------------------------------------------------
  220. esdprefix(tesd,zstr) - "prefix" for ESDs
  221. similar to esdcat(), but with automatic esd growth
  222. --------------------------------------------------------------------------*/
  223. int
  224. esdprefix(tesd, zstr)
  225. ESD *tesd;
  226. char *zstr;
  227. {
  228. int zstrlen = strlen(zstr);
  229. int erc = 0;
  230. if (zstrlen > (tesd->maxcb - tesd->cb))
  231. {
  232. if (erc = esdrealloc(tesd, tesd->cb + zstrlen))
  233. return (erc);
  234. }
  235. if (zstrlen)
  236. {
  237. mem_cpy(tesd->pb + zstrlen, tesd->pb, tesd->cb);
  238. memcpy(tesd->pb, zstr, zstrlen);
  239. tesd->cb += zstrlen;
  240. esd_null_terminate(tesd);
  241. }
  242. return (erc);
  243. } /* end of esdprefix */
  244. /*+-------------------------------------------------------------------------
  245. esdshow(tesd,title) - display an ESD's contents and indices
  246. --------------------------------------------------------------------------*/
  247. void
  248. esdshow(tesd, title)
  249. ESD *tesd;
  250. char *title;
  251. {
  252. int itmp;
  253. if (title && *title)
  254. {
  255. pputs(title);
  256. pputs("\n");
  257. }
  258. esd_null_terminate(tesd);
  259. pputs(tesd->pb);
  260. pputs("\n");
  261. for (itmp = 0; itmp <= tesd->cb; itmp++)
  262. {
  263. if (itmp == tesd->old_index)
  264. pputc('^');
  265. else if (itmp == tesd->index)
  266. pputc('^');
  267. else
  268. pputc(' ');
  269. if ((itmp > tesd->old_index) && (itmp > tesd->index))
  270. break;
  271. }
  272. pputs("\n");
  273. } /* end of esdshow */
  274. /*+-------------------------------------------------------------------------
  275. esddump(tesd,title) - display an ESD's contents and indices
  276. --------------------------------------------------------------------------*/
  277. void
  278. esddump(tesd, title)
  279. ESD *tesd;
  280. char *title;
  281. {
  282. pprintf("esd@%08X: ",tesd);
  283. if (title && *title)
  284. {
  285. pputs(title);
  286. pputs(": ");
  287. }
  288. pprintf("pb=%08X ",tesd->pb);
  289. pprintf("maxcb=%d ",tesd->maxcb);
  290. pprintf("cb=%d ",tesd->cb);
  291. pprintf("i=%d ",tesd->index);
  292. pprintf("oi=%d\n",tesd->old_index);
  293. hex_dump(tesd, sizeof *tesd, "", 1);
  294. hex_dump(tesd->pb, tesd->cb + 1, "", 1);
  295. } /* end of esddump */
  296. /*+----------------------------------------------------------------
  297. strindex(str1, str2) - string index function
  298. Returns position of 'str2' in 'str1' if found
  299. If 'str2' is null, then 0 is returned (null matches anything)
  300. Returns -1 if not found
  301. -----------------------------------------------------------------*/
  302. int
  303. strindex(str1, str2)
  304. char *str1; /* the (target) string to search */
  305. char *str2; /* the (comparand) string to search for */
  306. {
  307. int istr1 = 0;
  308. int lstr2 = strlen(str2);
  309. char *mstr = str1; /* the (target) string to search */
  310. if (*str2 == 0) /* null string matches anything */
  311. return (0);
  312. while (*mstr)
  313. {
  314. if (*mstr == *str2)
  315. { /* we have a first char match... does rest of
  316. * string match? */
  317. if (!strncmp(mstr, str2, lstr2))
  318. return (istr1); /* if so, return match position */
  319. }
  320. mstr++;
  321. istr1++;
  322. }
  323. return (-1); /* if we exhaust target string, flunk */
  324. } /* end of strindex */
  325. /*+-------------------------------------------------------------------------
  326. esdstrindex(esd1,esd2,index1_flag,index2_flag)
  327. Call strindex with esd1->pb and esd2->pb.
  328. If index1_flag != 0, esd1->pb + esd1->index passed
  329. If index2_flag != 0, esd2->pb + esd2->index passed
  330. --------------------------------------------------------------------------*/
  331. esdstrindex(esd1, esd2, index1_flag, index2_flag)
  332. ESD *esd1;
  333. ESD *esd2;
  334. int index1_flag;
  335. int index2_flag;
  336. {
  337. return (strindex(
  338. (index1_flag) ? esd1->pb : esd1->pb + esd1->index,
  339. (index2_flag) ? esd2->pb : esd2->pb + esd2->index));
  340. } /* end of esdstrindex */
  341. /*+----------------------------------------------------------------
  342. keyword_lookup(ktable,keywd)
  343. Lookup string in keyword_table struct array
  344. Returns table->key_token if 'keywd' found in
  345. 'table', else -1
  346. Beware substrings. "type","typedef" will both match "type"
  347. This procedure fell in here and it too late to move it.
  348. -----------------------------------------------------------------*/
  349. keyword_lookup(ktable, keywd)
  350. KEYTAB *ktable;
  351. char *keywd;
  352. {
  353. /* int plen = strlen(keywd); */
  354. while (ktable->key_word)
  355. {
  356. /* if(!strncmp(ktable->key_word,keywd,plen)) */
  357. if (!strcmp(ktable->key_word, keywd))
  358. return (ktable->key_token);
  359. ++ktable;
  360. } /* end of while */
  361. return (-1); /* search failed */
  362. } /* end of keyword_lookup */
  363. /*+----------------------------------------------------------------
  364. skip_cmd_break(tesd) - finds next non-break
  365. 'tesd' is an esd with valid 'index' field
  366. Returns 0 index field points to non-break character
  367. eNoParameter end of command found
  368. -----------------------------------------------------------------*/
  369. int
  370. skip_cmd_break(tesd)
  371. ESD *tesd;
  372. {
  373. int cb = tesd->cb;
  374. int idx = tesd->index;
  375. char *pb = tesd->pb;
  376. while (idx < cb)
  377. {
  378. if (!isspace((uchar) * (pb + idx)))
  379. break;
  380. idx++;
  381. }
  382. tesd->old_index = tesd->index = idx;
  383. if (idx >= cb)
  384. return (eNoParameter);
  385. return (0);
  386. } /* end of skip_cmd_break */
  387. /*+-------------------------------------------------------------------------
  388. end_of_cmd(tesd) - return 1 if at end of command
  389. --------------------------------------------------------------------------*/
  390. int
  391. end_of_cmd(tesd)
  392. ESD *tesd;
  393. {
  394. if (skip_cmd_break(tesd) || (*(tesd->pb + tesd->index) == ';') ||
  395. (*(tesd->pb + tesd->index) == '#'))
  396. return (1);
  397. return (0);
  398. } /* end of end_of_cmd */
  399. /*+-------------------------------------------------------------------------
  400. erc = skip_cmd_char(tesd,skipchar)
  401. --------------------------------------------------------------------------*/
  402. int
  403. skip_cmd_char(tesd, skipchar)
  404. ESD *tesd;
  405. char skipchar;
  406. {
  407. int erc;
  408. if (erc = skip_cmd_break(tesd))
  409. return (erc);
  410. if (tesd->pb[tesd->index] == skipchar)
  411. {
  412. ++tesd->index;
  413. return (0);
  414. }
  415. return (eSyntaxError);
  416. } /* end of skip_cmd_char */
  417. /*+-------------------------------------------------------------------------
  418. erc = skip_colon(tesd)
  419. --------------------------------------------------------------------------*/
  420. int
  421. skip_colon(tesd)
  422. ESD *tesd;
  423. {
  424. int erc;
  425. if (erc = skip_cmd_break(tesd))
  426. return (erc);
  427. if (tesd->pb[tesd->index] == ':')
  428. {
  429. ++tesd->index;
  430. return (0);
  431. }
  432. return (eCommaExpected);
  433. } /* end of skip_colon */
  434. /*+-------------------------------------------------------------------------
  435. erc = skip_comma(tesd)
  436. --------------------------------------------------------------------------*/
  437. int
  438. skip_comma(tesd)
  439. ESD *tesd;
  440. {
  441. int erc;
  442. if (erc = skip_cmd_break(tesd))
  443. return (erc);
  444. if (tesd->pb[tesd->index] == ',')
  445. {
  446. ++tesd->index;
  447. return (0);
  448. }
  449. return (eCommaExpected);
  450. } /* end of skip_comma */
  451. /*+-------------------------------------------------------------------------
  452. erc = skip_paren(fparam,LEFT or RIGHT)
  453. --------------------------------------------------------------------------*/
  454. int
  455. skip_paren(tesd, fLeft)
  456. ESD *tesd;
  457. int fLeft;
  458. {
  459. int erc;
  460. char search = (fLeft) ? '(' : ')';
  461. if (erc = skip_cmd_break(tesd))
  462. return (erc);
  463. if (tesd->pb[tesd->index] == search)
  464. {
  465. tesd->index++;
  466. return (0);
  467. }
  468. return ((fLeft) ? eMissingLeftParen : eMissingRightParen);
  469. } /* end of skip_paren */
  470. /*+-------------------------------------------------------------------------
  471. get_cmd_char(tesd,pchar)
  472. --------------------------------------------------------------------------*/
  473. int
  474. get_cmd_char(tesd, pchar)
  475. ESD *tesd;
  476. char *pchar;
  477. {
  478. int erc;
  479. if (erc = skip_cmd_break(tesd))
  480. return (erc);
  481. *pchar = tesd->pb[tesd->index++];
  482. return (0);
  483. } /* end of get_cmd_char */
  484. /*+----------------------------------------------------------------
  485. get_alpha_zstr(&esd,&strbuf,strbuf_maxcb)
  486. places next alphabetic string token [A-Za-z_] into
  487. the null-terminated 'strbuf' string. returns 0 or -1
  488. or skip_cmd_break error codes
  489. -----------------------------------------------------------------*/
  490. int
  491. get_alpha_zstr(tesd, strbuf, strbuf_maxcb)
  492. ESD *tesd;
  493. char *strbuf;
  494. int strbuf_maxcb;
  495. {
  496. int izstr;
  497. int schar;
  498. char *pb = tesd->pb;
  499. if (izstr = skip_cmd_break(tesd))
  500. return (izstr);
  501. izstr = 0;
  502. while ((izstr < strbuf_maxcb - 1) && (tesd->index < tesd->cb))
  503. {
  504. schar = pb[tesd->index] & 0xFF;
  505. if ((!isalpha(schar)) && (schar != '_'))
  506. break;
  507. strbuf[izstr++] = schar;
  508. tesd->index++;
  509. }
  510. strbuf[izstr] = 0;
  511. return (izstr ? 0 : eBadParameter);
  512. } /* end of get_alpha_zstr */
  513. /*+----------------------------------------------------------------
  514. get_alphanum_zstr(&esd,&strbuf,strbuf_maxcb)
  515. places next alphanumeric string token [A-Za-z0-9_]
  516. into the null-terminated 'strbuf' string. returns 0
  517. or -1 or skip_cmd_break error codes
  518. -----------------------------------------------------------------*/
  519. int
  520. get_alphanum_zstr(tesd, strbuf, strbuf_maxcb)
  521. ESD *tesd;
  522. char *strbuf;
  523. int strbuf_maxcb;
  524. {
  525. int izstr = 0;
  526. int schar;
  527. int cb = tesd->cb;
  528. int idx;
  529. if (izstr = skip_cmd_break(tesd))
  530. return (izstr);
  531. idx = tesd->index;
  532. while ((izstr < strbuf_maxcb - 1) && (idx < cb))
  533. {
  534. schar = tesd->pb[idx++] & 0xFF;
  535. if (isalnum(schar) || (schar == '_'))
  536. strbuf[izstr++] = schar;
  537. else
  538. {
  539. --idx;
  540. break;
  541. }
  542. }
  543. tesd->index = idx;
  544. strbuf[izstr] = 0;
  545. return (izstr ? 0 : eBadParameter);
  546. } /* end of get_alphanum_zstr */
  547. /*+----------------------------------------------------------------
  548. get_numeric_zstr(&esd,&strbuf,strbuf_maxcb)
  549. gets next numeric string token places it
  550. into the null-terminated 'strbuf' string. returns 0 or -1
  551. or skip_cmd_break error codes
  552. -----------------------------------------------------------------*/
  553. int
  554. get_numeric_zstr(tesd, strbuf, strbuf_maxcb)
  555. ESD *tesd;
  556. char *strbuf;
  557. int strbuf_maxcb;
  558. {
  559. int izstr;
  560. int schar;
  561. if (izstr = skip_cmd_break(tesd))
  562. return (izstr);
  563. while ((izstr < strbuf_maxcb - 1) && (tesd->index < tesd->cb))
  564. {
  565. schar = tesd->pb[tesd->index++] & 0xFF;
  566. if (isdigit(schar))
  567. strbuf[izstr++] = schar;
  568. else
  569. {
  570. --tesd->index;
  571. break;
  572. }
  573. }
  574. strbuf[izstr] = 0;
  575. return (izstr ? 0 : eBadParameter);
  576. } /* end of get_numeric_zstr */
  577. /*+-----------------------------------------------------------------------
  578. get_numeric_value(tesd,&long_var)
  579. ------------------------------------------------------------------------*/
  580. get_numeric_value(tesd, value)
  581. ESD *tesd;
  582. long *value;
  583. {
  584. int erc;
  585. char buf[32];
  586. if (erc = get_numeric_zstr(tesd, buf, sizeof(buf)))
  587. return (erc);
  588. sscanf(buf, "%ld", value);
  589. return (0);
  590. } /* end of get_numeric_value */
  591. /*+----------------------------------------------------------------
  592. get_ipaddr_zstr(&esd,&strbuf,strbuf_maxcb)
  593. places next ip address string token [A-Za-z0-9_.]
  594. into the null-terminated 'strbuf' string. returns 0
  595. or -1 or skip_cmd_break error codes
  596. -----------------------------------------------------------------*/
  597. int
  598. get_ipaddr_zstr(e, strbuf, strbuf_maxcb)
  599. ESD *e;
  600. char *strbuf;
  601. int strbuf_maxcb;
  602. {
  603. int izstr = 0;
  604. int schar;
  605. int erc;
  606. int cb = e->cb;
  607. int entry_idx;
  608. int idx;
  609. int dot_count = 0;
  610. u_long ipaddr;
  611. if (erc = skip_cmd_break(e))
  612. return (end_of_cmd(e) ? eMissingIpAddress : eBadIpAddress);
  613. entry_idx = e->index;
  614. idx = e->index;
  615. while ((izstr < strbuf_maxcb - 1) && (idx < cb))
  616. {
  617. schar = e->pb[idx++] & 0xFF;
  618. if (isdigit(schar))
  619. strbuf[izstr++] = schar;
  620. else if (schar == '.')
  621. {
  622. dot_count++;
  623. strbuf[izstr++] = schar;
  624. }
  625. else
  626. {
  627. --idx;
  628. break;
  629. }
  630. }
  631. e->index = idx;
  632. e->old_index = e->index;
  633. strbuf[izstr] = 0;
  634. ipaddr = inet_atou(strbuf);
  635. erc = ((izstr && (dot_count == 3) && ipaddr) ? 0 : eBadIpAddress);
  636. if (erc)
  637. e->index = entry_idx;
  638. return (erc);
  639. } /* end of get_ipaddr_zstr */
  640. /*+----------------------------------------------------------------
  641. get_word_zstr(&esd,&strbuf,strbuf_maxcb)
  642. gets next word (continuous string of characters without spaces
  643. or tabs) returns 0 or -1 or skip_cmd_break error codes
  644. -----------------------------------------------------------------*/
  645. int
  646. get_word_zstr(tesd, strbuf, strbuf_maxcb)
  647. ESD *tesd;
  648. char *strbuf;
  649. int strbuf_maxcb;
  650. {
  651. int izstr;
  652. int schar;
  653. if (izstr = skip_cmd_break(tesd))
  654. return (izstr);
  655. strbuf_maxcb--;
  656. while ((izstr < strbuf_maxcb) && (tesd->index < tesd->cb))
  657. {
  658. schar = tesd->pb[tesd->index++];
  659. if ((schar > 0x20) && (schar <= 0x7e))
  660. strbuf[izstr++] = schar;
  661. else
  662. {
  663. --tesd->index;
  664. break;
  665. }
  666. }
  667. strbuf[izstr] = 0;
  668. return (izstr ? 0 : eBadParameter);
  669. } /* end of get_word_zstr */
  670. /*+-------------------------------------------------------------------------
  671. esd_strip_trail_break(tesd)
  672. --------------------------------------------------------------------------*/
  673. void
  674. esd_strip_trail_break(ztext)
  675. ESD *ztext;
  676. {
  677. while (ztext->cb &&
  678. ((ztext->pb[ztext->cb - 1] == 0x20) || (ztext->pb[ztext->cb - 1] == 0x20)))
  679. {
  680. ztext->cb--;
  681. }
  682. } /* end of esd_strip_trail_break */
  683. /*+-------------------------------------------------------------------------
  684. esdfgets(&esd,fileptr)
  685. stdio read from FILE *fileptr into esd
  686. returns tesd->cb set up not including trailing nl, tesd->index == 0
  687. --------------------------------------------------------------------------*/
  688. int
  689. esdfgets(tesd, fileptr)
  690. ESD *tesd;
  691. FILE *fileptr;
  692. {
  693. char *cp;
  694. tesd->cb = 0;
  695. if (!fgets(tesd->pb, tesd->maxcb + 1, fileptr))
  696. return (eEOF);
  697. if (!(cp = strchr(tesd->pb, 0x0A)))
  698. return (eBufferTooSmall);
  699. tesd->cb = (int)(cp - tesd->pb);
  700. esd_null_terminate(tesd);
  701. tesd->index = 0;
  702. tesd->old_index = 0;
  703. return (0);
  704. } /* end of esdfgets */
  705. /*+-------------------------------------------------------------------------
  706. esdfputs(&esd,fileptr,index_flag,nl_flag)
  707. write esd contents to stdio FILE *fileptr
  708. if index_flag is true, write from tesd->index thru end of esd
  709. otherwise, from start of esd
  710. if nl_flag is true, append nl to write, else just esd contents
  711. --------------------------------------------------------------------------*/
  712. int
  713. esdfputs(tesd, fileptr, index_flag, nl_flag)
  714. ESD *tesd;
  715. FILE *fileptr;
  716. int index_flag;
  717. int nl_flag;
  718. {
  719. char *cp;
  720. int write_length;
  721. if (index_flag)
  722. {
  723. cp = &tesd->pb[tesd->index];
  724. write_length = tesd->cb - tesd->index;
  725. }
  726. else
  727. {
  728. cp = tesd->pb;
  729. write_length = tesd->cb;
  730. }
  731. if (write_length)
  732. fwrite(cp, write_length, 1, fileptr);
  733. if (nl_flag)
  734. fputc(0x0A, fileptr);
  735. return (0);
  736. } /* end of esdfputs */
  737. /*+-------------------------------------------------------------------------
  738. get_switches(tesd,switches,switches_max)
  739. --------------------------------------------------------------------------*/
  740. int
  741. get_switches(tesd, switches, switches_max)
  742. ESD *tesd;
  743. char *switches;
  744. int switches_max;
  745. {
  746. int erc;
  747. int idx;
  748. char *pb = tesd->pb;
  749. int cb = tesd->cb;
  750. char schar;
  751. *switches = 0;
  752. if (erc = skip_cmd_break(tesd))
  753. return (erc);
  754. idx = tesd->index;
  755. if (*(pb + idx) != '-')
  756. return (eNoSwitches);
  757. if (switches_max < 3)
  758. return (eSwitchesTooLong);
  759. switches_max--; /* for trailing null */
  760. *switches++ = '-';
  761. switches_max--;
  762. idx++;
  763. while (idx < cb)
  764. {
  765. schar = *(pb + idx++);
  766. if (switches_max > 0)
  767. *switches++ = schar;
  768. switches_max--;
  769. if (isspace((uchar) schar))
  770. break;
  771. }
  772. tesd->index = idx;
  773. *switches = 0;
  774. return ((switches_max < 0) ? eSwitchesTooLong : 0);
  775. } /* end of get_switches() */
  776. /*+-------------------------------------------------------------------------
  777. esdclone(e) - clone an esd
  778. --------------------------------------------------------------------------*/
  779. ESD *
  780. esdclone(e)
  781. ESD *e;
  782. {
  783. ESD *e2 = esdalloc(e->maxcb);
  784. esdcat(e2, e, 1);
  785. return(e2);
  786. } /* end of esdcopy */
  787. /* vi: set tabstop=4 shiftwidth=4: */
  788. /* end of esdutil.c */