testregex.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. /*
  2. * regex(3) test harness
  3. *
  4. * build: cc -o testregex testregex.c
  5. * help: testregex --man
  6. * note: REG_* features are detected by #ifdef; if REG_* are enums
  7. * then supply #define REG_foo REG_foo for each enum REG_foo
  8. *
  9. * Glenn Fowler <gsf@research.att.com>
  10. * AT&T Labs Research
  11. *
  12. * PLEASE: publish your tests so everyone can benefit
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a
  15. * copy of THIS SOFTWARE FILE (the "Software"), to deal in the Software
  16. * without restriction, including without limitation the rights to use,
  17. * copy, modify, merge, publish, distribute, and/or sell copies of the
  18. * Software, and to permit persons to whom the Software is furnished to do
  19. * so, subject to the following disclaimer:
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY AT&T ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL AT&T BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. static const char id[] = "\n@(#)$Id: testregex (AT&T Research) 2005-05-20 $\0\n";
  33. #if _PACKAGE_ast
  34. #include <ast.h>
  35. #else
  36. #include <sys/types.h>
  37. #endif
  38. #include <stdio.h>
  39. #include <regex.h>
  40. #include <ctype.h>
  41. #include <setjmp.h>
  42. #include <signal.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #ifdef __STDC__
  46. #include <stdlib.h>
  47. #include <locale.h>
  48. #endif
  49. #if !_PACKAGE_ast
  50. #undef REG_DISCIPLINE
  51. #endif
  52. #ifndef REG_DELIMITED
  53. #undef _REG_subcomp
  54. #endif
  55. #define TEST_ARE 0x00000001
  56. #define TEST_BRE 0x00000002
  57. #define TEST_ERE 0x00000004
  58. #define TEST_KRE 0x00000008
  59. #define TEST_LRE 0x00000010
  60. #define TEST_SRE 0x00000020
  61. #define TEST_EXPAND 0x00000040
  62. #define TEST_LENIENT 0x00000080
  63. #define TEST_QUERY 0x00000100
  64. #define TEST_SUB 0x00000200
  65. #define TEST_UNSPECIFIED 0x00000400
  66. #define TEST_VERIFY 0x00000800
  67. #define TEST_AND 0x00001000
  68. #define TEST_OR 0x00002000
  69. #define TEST_DELIMIT 0x00010000
  70. #define TEST_OK 0x00020000
  71. #define TEST_SAME 0x00040000
  72. #define TEST_ACTUAL 0x00100000
  73. #define TEST_BASELINE 0x00200000
  74. #define TEST_FAIL 0x00400000
  75. #define TEST_PASS 0x00800000
  76. #define TEST_SUMMARY 0x01000000
  77. #define TEST_IGNORE_ERROR 0x02000000
  78. #define TEST_IGNORE_OVER 0x04000000
  79. #define TEST_IGNORE_POSITION 0x08000000
  80. #define TEST_CATCH 0x10000000
  81. #define TEST_VERBOSE 0x20000000
  82. #define TEST_GLOBAL (TEST_ACTUAL|TEST_AND|TEST_BASELINE|TEST_CATCH|TEST_FAIL|TEST_IGNORE_ERROR|TEST_IGNORE_OVER|TEST_IGNORE_POSITION|TEST_OR|TEST_PASS|TEST_SUMMARY|TEST_VERBOSE)
  83. #ifdef REG_DISCIPLINE
  84. #include <stk.h>
  85. typedef struct Disc_s
  86. {
  87. regdisc_t disc;
  88. int ordinal;
  89. Sfio_t* sp;
  90. } Disc_t;
  91. static void*
  92. compf(const regex_t* re, const char* xstr, size_t xlen, regdisc_t* disc)
  93. {
  94. Disc_t* dp = (Disc_t*)disc;
  95. return (void*)++dp->ordinal;
  96. }
  97. static int
  98. execf(const regex_t* re, void* data, const char* xstr, size_t xlen, const char* sstr, size_t slen, char** snxt, regdisc_t* disc)
  99. {
  100. Disc_t* dp = (Disc_t*)disc;
  101. sfprintf(dp->sp, "{%-.*s}(%d:%d)", xlen, xstr, (int)data, slen);
  102. return atoi(xstr);
  103. }
  104. static void*
  105. resizef(void* handle, void* data, size_t size)
  106. {
  107. if (!size)
  108. return 0;
  109. return stkalloc((Sfio_t*)handle, size);
  110. }
  111. #endif
  112. #ifndef NiL
  113. #ifdef __STDC__
  114. #define NiL 0
  115. #else
  116. #define NiL (char*)0
  117. #endif
  118. #endif
  119. #define H(x) do{if(html)fprintf(stderr,x);}while(0)
  120. #define T(x) fprintf(stderr,x)
  121. static void
  122. help(int html)
  123. {
  124. H("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n");
  125. H("<HTML>\n");
  126. H("<HEAD>\n");
  127. H("<TITLE>testregex man document</TITLE>\n");
  128. H("</HEAD>\n");
  129. H("<BODY bgcolor=white>\n");
  130. H("<PRE>\n");
  131. T("NAME\n");
  132. T(" testregex - regex(3) test harness\n");
  133. T("\n");
  134. T("SYNOPSIS\n");
  135. T(" testregex [ options ]\n");
  136. T("\n");
  137. T("DESCRIPTION\n");
  138. T(" testregex reads regex(3) test specifications, one per line, from the\n");
  139. T(" standard input and writes one output line for each failed test. A\n");
  140. T(" summary line is written after all tests are done. Each successful\n");
  141. T(" test is run again with REG_NOSUB. Unsupported features are noted\n");
  142. T(" before the first test, and tests requiring these features are\n");
  143. T(" silently ignored.\n");
  144. T("\n");
  145. T("OPTIONS\n");
  146. T(" -c catch signals and non-terminating calls\n");
  147. T(" -e ignore error return mismatches\n");
  148. T(" -h list help on standard error\n");
  149. T(" -n do not repeat successful tests with regnexec()\n");
  150. T(" -o ignore match[] overrun errors\n");
  151. T(" -p ignore negative position mismatches\n");
  152. T(" -s use stack instead of malloc\n");
  153. T(" -x do not repeat successful tests with REG_NOSUB\n");
  154. T(" -v list each test line\n");
  155. T(" -A list failed test lines with actual answers\n");
  156. T(" -B list all test lines with actual answers\n");
  157. T(" -F list failed test lines\n");
  158. T(" -P list passed test lines\n");
  159. T(" -S output one summary line\n");
  160. T("\n");
  161. T("INPUT FORMAT\n");
  162. T(" Input lines may be blank, a comment beginning with #, or a test\n");
  163. T(" specification. A specification is five fields separated by one\n");
  164. T(" or more tabs. NULL denotes the empty string and NIL denotes the\n");
  165. T(" 0 pointer.\n");
  166. T("\n");
  167. T(" Field 1: the regex(3) flags to apply, one character per REG_feature\n");
  168. T(" flag. The test is skipped if REG_feature is not supported by the\n");
  169. T(" implementation. If the first character is not [BEASKL] then the\n");
  170. T(" specification is a global control line. One or more of [BEASKL] may be\n");
  171. T(" specified; the test will be repeated for each mode.\n");
  172. T("\n");
  173. T(" B basic BRE (grep, ed, sed)\n");
  174. T(" E REG_EXTENDED ERE (egrep)\n");
  175. T(" A REG_AUGMENTED ARE (egrep with negation)\n");
  176. T(" S REG_SHELL SRE (sh glob)\n");
  177. T(" K REG_SHELL|REG_AUGMENTED KRE (ksh glob)\n");
  178. T(" L REG_LITERAL LRE (fgrep)\n");
  179. T("\n");
  180. T(" a REG_LEFT|REG_RIGHT implicit ^...$\n");
  181. T(" b REG_NOTBOL lhs does not match ^\n");
  182. T(" c REG_COMMENT ignore space and #...\\n\n");
  183. T(" d REG_SHELL_DOT explicit leading . match\n");
  184. T(" e REG_NOTEOL rhs does not match $\n");
  185. T(" f REG_MULTIPLE multiple \\n separated patterns\n");
  186. T(" g FNM_LEADING_DIR testfnmatch only -- match until /\n");
  187. T(" h REG_MULTIREF multiple digit backref\n");
  188. T(" i REG_ICASE ignore case\n");
  189. T(" j REG_SPAN . matches \\n\n");
  190. T(" k REG_ESCAPE \\ to ecape [...] delimiter\n");
  191. T(" l REG_LEFT implicit ^...\n");
  192. T(" m REG_MINIMAL minimal match\n");
  193. T(" n REG_NEWLINE explicit \\n match\n");
  194. T(" o REG_ENCLOSED (|&) magic inside [@|&](...)\n");
  195. T(" p REG_SHELL_PATH explicit / match\n");
  196. T(" q REG_DELIMITED delimited pattern\n");
  197. T(" r REG_RIGHT implicit ...$\n");
  198. T(" s REG_SHELL_ESCAPED \\ not special\n");
  199. T(" t REG_MUSTDELIM all delimiters must be specified\n");
  200. T(" u standard unspecified behavior -- errors not counted\n");
  201. T(" w REG_NOSUB no subexpression match array\n");
  202. T(" x REG_LENIENT let some errors slide\n");
  203. T(" y REG_LEFT regexec() implicit ^...\n");
  204. T(" z REG_NULL NULL subexpressions ok\n");
  205. T(" $ expand C \\c escapes in fields 2 and 3\n");
  206. T(" / field 2 is a regsubcomp() expression\n");
  207. T("\n");
  208. T(" Field 1 control lines:\n");
  209. T("\n");
  210. T(" C set LC_COLLATE and LC_CTYPE to locale in field 2\n");
  211. T("\n");
  212. T(" ?test ... output field 5 if passed and != EXPECTED, silent otherwise\n");
  213. T(" &test ... output field 5 if current and previous passed\n");
  214. T(" |test ... output field 5 if current passed and previous failed\n");
  215. T(" ; ... output field 2 if previous failed\n");
  216. T(" {test ... skip if failed until }\n");
  217. T(" } end of skip\n");
  218. T("\n");
  219. T(" : comment comment copied as output NOTE\n");
  220. T(" :comment:test :comment: ignored\n");
  221. T(" N[OTE] comment comment copied as output NOTE\n");
  222. T(" T[EST] comment comment\n");
  223. T("\n");
  224. T(" number use number for nmatch (20 by default)\n");
  225. T("\n");
  226. T(" Field 2: the regular expression pattern; SAME uses the pattern from\n");
  227. T(" the previous specification.\n");
  228. T("\n");
  229. T(" Field 3: the string to match.\n");
  230. T("\n");
  231. T(" Field 4: the test outcome. This is either one of the posix error\n");
  232. T(" codes (with REG_ omitted) or the match array, a list of (m,n)\n");
  233. T(" entries with m and n being first and last+1 positions in the\n");
  234. T(" field 3 string, or NULL if REG_NOSUB is in effect and success\n");
  235. T(" is expected. BADPAT is acceptable in place of any regcomp(3)\n");
  236. T(" error code. The match[] array is initialized to (-2,-2) before\n");
  237. T(" each test. All array elements from 0 to nmatch-1 must be specified\n");
  238. T(" in the outcome. Unspecified endpoints (offset -1) are denoted by ?.\n");
  239. T(" Unset endpoints (offset -2) are denoted by X. {x}(o:n) denotes a\n");
  240. T(" matched (?{...}) expression, where x is the text enclosed by {...},\n");
  241. T(" o is the expression ordinal counting from 1, and n is the length of\n");
  242. T(" the unmatched portion of the subject string. If x starts with a\n");
  243. T(" number then that is the return value of re_execf(), otherwise 0 is\n");
  244. T(" returned.\n");
  245. T("\n");
  246. T(" Field 5: optional comment appended to the report.\n");
  247. T("\n");
  248. T("CAVEAT\n");
  249. T(" If a regex implementation misbehaves with memory then all bets are off.\n");
  250. T("\n");
  251. T("CONTRIBUTORS\n");
  252. T(" Glenn Fowler gsf@research.att.com (ksh strmatch, regex extensions)\n");
  253. T(" David Korn dgk@research.att.com (ksh glob matcher)\n");
  254. T(" Doug McIlroy mcilroy@dartmouth.edu (ast regex/testre in C++)\n");
  255. T(" Tom Lord lord@regexps.com (rx tests)\n");
  256. T(" Henry Spencer henry@zoo.toronto.edu (original public regex)\n");
  257. T(" Andrew Hume andrew@research.att.com (gre tests)\n");
  258. T(" John Maddock John_Maddock@compuserve.com (regex++ tests)\n");
  259. T(" Philip Hazel ph10@cam.ac.uk (pcre tests)\n");
  260. T(" Ville Laurikari vl@iki.fi (libtre tests)\n");
  261. H("</PRE>\n");
  262. H("</BODY>\n");
  263. H("</HTML>\n");
  264. }
  265. #ifndef elementsof
  266. #define elementsof(x) (sizeof(x)/sizeof(x[0]))
  267. #endif
  268. #ifndef streq
  269. #define streq(a,b) (*(a)==*(b)&&!strcmp(a,b))
  270. #endif
  271. #define HUNG 2
  272. #define NOTEST (~0)
  273. #ifndef REG_TEST_DEFAULT
  274. #define REG_TEST_DEFAULT 0
  275. #endif
  276. #ifndef REG_EXEC_DEFAULT
  277. #define REG_EXEC_DEFAULT 0
  278. #endif
  279. static const char* unsupported[] =
  280. {
  281. "BASIC",
  282. #ifndef REG_EXTENDED
  283. "EXTENDED",
  284. #endif
  285. #ifndef REG_AUGMENTED
  286. "AUGMENTED",
  287. #endif
  288. #ifndef REG_SHELL
  289. "SHELL",
  290. #endif
  291. #ifndef REG_COMMENT
  292. "COMMENT",
  293. #endif
  294. #ifndef REG_DELIMITED
  295. "DELIMITED",
  296. #endif
  297. #ifndef REG_DISCIPLINE
  298. "DISCIPLINE",
  299. #endif
  300. #ifndef REG_ESCAPE
  301. "ESCAPE",
  302. #endif
  303. #ifndef REG_ICASE
  304. "ICASE",
  305. #endif
  306. #ifndef REG_LEFT
  307. "LEFT",
  308. #endif
  309. #ifndef REG_LENIENT
  310. "LENIENT",
  311. #endif
  312. #ifndef REG_LITERAL
  313. "LITERAL",
  314. #endif
  315. #ifndef REG_MINIMAL
  316. "MINIMAL",
  317. #endif
  318. #ifndef REG_MULTIPLE
  319. "MULTIPLE",
  320. #endif
  321. #ifndef REG_MULTIREF
  322. "MULTIREF",
  323. #endif
  324. #ifndef REG_MUSTDELIM
  325. "MUSTDELIM",
  326. #endif
  327. #ifndef REG_NEWLINE
  328. "NEWLINE",
  329. #endif
  330. #ifndef REG_NOTBOL
  331. "NOTBOL",
  332. #endif
  333. #ifndef REG_NOTEOL
  334. "NOTEOL",
  335. #endif
  336. #ifndef REG_NULL
  337. "NULL",
  338. #endif
  339. #ifndef REG_RIGHT
  340. "RIGHT",
  341. #endif
  342. #ifndef REG_SHELL_DOT
  343. "SHELL_DOT",
  344. #endif
  345. #ifndef REG_SHELL_ESCAPED
  346. "SHELL_ESCAPED",
  347. #endif
  348. #ifndef REG_SHELL_GROUP
  349. "SHELL_GROUP",
  350. #endif
  351. #ifndef REG_SHELL_PATH
  352. "SHELL_PATH",
  353. #endif
  354. #ifndef REG_SPAN
  355. "SPAN",
  356. #endif
  357. #if REG_NOSUB & REG_TEST_DEFAULT
  358. "SUBMATCH",
  359. #endif
  360. #if !_REG_nexec
  361. "regnexec",
  362. #endif
  363. #if !_REG_subcomp
  364. "regsubcomp",
  365. #endif
  366. 0
  367. };
  368. #ifndef REG_COMMENT
  369. #define REG_COMMENT NOTEST
  370. #endif
  371. #ifndef REG_DELIMITED
  372. #define REG_DELIMITED NOTEST
  373. #endif
  374. #ifndef REG_ESCAPE
  375. #define REG_ESCAPE NOTEST
  376. #endif
  377. #ifndef REG_ICASE
  378. #define REG_ICASE NOTEST
  379. #endif
  380. #ifndef REG_LEFT
  381. #define REG_LEFT NOTEST
  382. #endif
  383. #ifndef REG_LENIENT
  384. #define REG_LENIENT 0
  385. #endif
  386. #ifndef REG_MINIMAL
  387. #define REG_MINIMAL NOTEST
  388. #endif
  389. #ifndef REG_MULTIPLE
  390. #define REG_MULTIPLE NOTEST
  391. #endif
  392. #ifndef REG_MULTIREF
  393. #define REG_MULTIREF NOTEST
  394. #endif
  395. #ifndef REG_MUSTDELIM
  396. #define REG_MUSTDELIM NOTEST
  397. #endif
  398. #ifndef REG_NEWLINE
  399. #define REG_NEWLINE NOTEST
  400. #endif
  401. #ifndef REG_NOTBOL
  402. #define REG_NOTBOL NOTEST
  403. #endif
  404. #ifndef REG_NOTEOL
  405. #define REG_NOTEOL NOTEST
  406. #endif
  407. #ifndef REG_NULL
  408. #define REG_NULL NOTEST
  409. #endif
  410. #ifndef REG_RIGHT
  411. #define REG_RIGHT NOTEST
  412. #endif
  413. #ifndef REG_SHELL_DOT
  414. #define REG_SHELL_DOT NOTEST
  415. #endif
  416. #ifndef REG_SHELL_ESCAPED
  417. #define REG_SHELL_ESCAPED NOTEST
  418. #endif
  419. #ifndef REG_SHELL_GROUP
  420. #define REG_SHELL_GROUP NOTEST
  421. #endif
  422. #ifndef REG_SHELL_PATH
  423. #define REG_SHELL_PATH NOTEST
  424. #endif
  425. #ifndef REG_SPAN
  426. #define REG_SPAN NOTEST
  427. #endif
  428. #define REG_UNKNOWN (-1)
  429. #ifndef REG_ENEWLINE
  430. #define REG_ENEWLINE (REG_UNKNOWN-1)
  431. #endif
  432. #ifndef REG_ENULL
  433. #ifndef REG_EMPTY
  434. #define REG_ENULL (REG_UNKNOWN-2)
  435. #else
  436. #define REG_ENULL REG_EMPTY
  437. #endif
  438. #endif
  439. #ifndef REG_ECOUNT
  440. #define REG_ECOUNT (REG_UNKNOWN-3)
  441. #endif
  442. #ifndef REG_BADESC
  443. #define REG_BADESC (REG_UNKNOWN-4)
  444. #endif
  445. #ifndef REG_EMEM
  446. #define REG_EMEM (REG_UNKNOWN-5)
  447. #endif
  448. #ifndef REG_EHUNG
  449. #define REG_EHUNG (REG_UNKNOWN-6)
  450. #endif
  451. #ifndef REG_EBUS
  452. #define REG_EBUS (REG_UNKNOWN-7)
  453. #endif
  454. #ifndef REG_EFAULT
  455. #define REG_EFAULT (REG_UNKNOWN-8)
  456. #endif
  457. #ifndef REG_EFLAGS
  458. #define REG_EFLAGS (REG_UNKNOWN-9)
  459. #endif
  460. #ifndef REG_EDELIM
  461. #define REG_EDELIM (REG_UNKNOWN-9)
  462. #endif
  463. static const struct { int code; char* name; } codes[] =
  464. {
  465. {REG_UNKNOWN, "UNKNOWN"},
  466. {REG_NOMATCH, "NOMATCH"},
  467. {REG_BADPAT, "BADPAT"},
  468. {REG_ECOLLATE, "ECOLLATE"},
  469. {REG_ECTYPE, "ECTYPE"},
  470. {REG_EESCAPE, "EESCAPE"},
  471. {REG_ESUBREG, "ESUBREG"},
  472. {REG_EBRACK, "EBRACK"},
  473. {REG_EPAREN, "EPAREN"},
  474. {REG_EBRACE, "EBRACE"},
  475. {REG_BADBR, "BADBR"},
  476. {REG_ERANGE, "ERANGE"},
  477. {REG_ESPACE, "ESPACE"},
  478. {REG_BADRPT, "BADRPT"},
  479. {REG_ENEWLINE, "ENEWLINE"},
  480. {REG_ENULL, "ENULL"},
  481. {REG_ECOUNT, "ECOUNT"},
  482. {REG_BADESC, "BADESC"},
  483. {REG_EMEM, "EMEM"},
  484. {REG_EHUNG, "EHUNG"},
  485. {REG_EBUS, "EBUS"},
  486. {REG_EFAULT, "EFAULT"},
  487. {REG_EFLAGS, "EFLAGS"},
  488. {REG_EDELIM, "EDELIM"},
  489. };
  490. static struct
  491. {
  492. regmatch_t NOMATCH;
  493. int errors;
  494. int extracted;
  495. int ignored;
  496. int lineno;
  497. int passed;
  498. int signals;
  499. int unspecified;
  500. int verify;
  501. int warnings;
  502. char* file;
  503. char* stack;
  504. char* which;
  505. jmp_buf gotcha;
  506. #ifdef REG_DISCIPLINE
  507. Disc_t disc;
  508. #endif
  509. } state;
  510. static void
  511. quote(char* s, int len, unsigned long test)
  512. {
  513. unsigned char* u = (unsigned char*)s;
  514. unsigned char* e;
  515. int c;
  516. if (!u)
  517. printf("NIL");
  518. else if (!*u && len <= 1)
  519. printf("NULL");
  520. else if (test & TEST_EXPAND)
  521. {
  522. if (len < 0)
  523. len = strlen((char*)u);
  524. e = u + len;
  525. if (test & TEST_DELIMIT)
  526. printf("\"");
  527. while (u < e)
  528. switch (c = *u++)
  529. {
  530. case '\\':
  531. printf("\\\\");
  532. break;
  533. case '"':
  534. if (test & TEST_DELIMIT)
  535. printf("\\\"");
  536. else
  537. printf("\"");
  538. break;
  539. case '\a':
  540. printf("\\a");
  541. break;
  542. case '\b':
  543. printf("\\b");
  544. break;
  545. case 033:
  546. printf("\\e");
  547. break;
  548. case '\f':
  549. printf("\\f");
  550. break;
  551. case '\n':
  552. printf("\\n");
  553. break;
  554. case '\r':
  555. printf("\\r");
  556. break;
  557. case '\t':
  558. printf("\\t");
  559. break;
  560. case '\v':
  561. printf("\\v");
  562. break;
  563. default:
  564. if (!iscntrl(c) && isprint(c))
  565. putchar(c);
  566. else
  567. printf("\\x%02x", c);
  568. break;
  569. }
  570. if (test & TEST_DELIMIT)
  571. printf("\"");
  572. }
  573. else
  574. printf("%s", s);
  575. }
  576. static void
  577. report(char* comment, char* fun, char* re, char* s, int len, char* msg, int flags, unsigned long test)
  578. {
  579. if (state.file)
  580. printf("%s:", state.file);
  581. printf("%d:", state.lineno);
  582. if (re)
  583. {
  584. printf(" ");
  585. quote(re, -1, test|TEST_DELIMIT);
  586. if (s)
  587. {
  588. printf(" versus ");
  589. quote(s, len, test|TEST_DELIMIT);
  590. }
  591. }
  592. if (test & TEST_UNSPECIFIED)
  593. {
  594. state.unspecified++;
  595. printf(" unspecified behavior");
  596. }
  597. else
  598. state.errors++;
  599. if (state.which)
  600. printf(" %s", state.which);
  601. if (flags & REG_NOSUB)
  602. printf(" NOSUB");
  603. if (fun)
  604. printf(" %s", fun);
  605. if (comment[strlen(comment)-1] == '\n')
  606. printf(" %s", comment);
  607. else
  608. {
  609. printf(" %s: ", comment);
  610. if (msg)
  611. printf("%s: ", msg);
  612. }
  613. }
  614. static void
  615. error(regex_t* preg, int code)
  616. {
  617. char* msg;
  618. char buf[256];
  619. switch (code)
  620. {
  621. case REG_EBUS:
  622. msg = "bus error";
  623. break;
  624. case REG_EFAULT:
  625. msg = "memory fault";
  626. break;
  627. case REG_EHUNG:
  628. msg = "did not terminate";
  629. break;
  630. default:
  631. regerror(code, preg, msg = buf, sizeof buf);
  632. break;
  633. }
  634. printf("%s\n", msg);
  635. }
  636. static void
  637. bad(char* comment, char* re, char* s, int len, unsigned long test)
  638. {
  639. printf("bad test case ");
  640. report(comment, NiL, re, s, len, NiL, 0, test);
  641. exit(1);
  642. }
  643. static int
  644. escape(char* s)
  645. {
  646. char* b;
  647. char* t;
  648. char* q;
  649. char* e;
  650. int c;
  651. for (b = t = s; (*t = *s); s++, t++)
  652. if (*s == '\\')
  653. switch (*++s)
  654. {
  655. case '\\':
  656. break;
  657. case 'a':
  658. *t = '\a';
  659. break;
  660. case 'b':
  661. *t = '\b';
  662. break;
  663. case 'c':
  664. if ((*t = *++s))
  665. *t &= 037;
  666. else
  667. s--;
  668. break;
  669. case 'e':
  670. case 'E':
  671. *t = 033;
  672. break;
  673. case 'f':
  674. *t = '\f';
  675. break;
  676. case 'n':
  677. *t = '\n';
  678. break;
  679. case 'r':
  680. *t = '\r';
  681. break;
  682. case 's':
  683. *t = ' ';
  684. break;
  685. case 't':
  686. *t = '\t';
  687. break;
  688. case 'v':
  689. *t = '\v';
  690. break;
  691. case 'u':
  692. case 'x':
  693. c = 0;
  694. q = c == 'u' ? (s + 5) : (char*)0;
  695. e = s + 1;
  696. while (!e || !q || s < q)
  697. {
  698. switch (*++s)
  699. {
  700. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  701. c = (c << 4) + *s - 'a' + 10;
  702. continue;
  703. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  704. c = (c << 4) + *s - 'A' + 10;
  705. continue;
  706. case '0': case '1': case '2': case '3': case '4':
  707. case '5': case '6': case '7': case '8': case '9':
  708. c = (c << 4) + *s - '0';
  709. continue;
  710. case '{':
  711. case '[':
  712. if (s != e)
  713. {
  714. s--;
  715. break;
  716. }
  717. e = 0;
  718. continue;
  719. case '}':
  720. case ']':
  721. if (e)
  722. s--;
  723. break;
  724. default:
  725. s--;
  726. break;
  727. }
  728. break;
  729. }
  730. *t = c;
  731. break;
  732. case '0': case '1': case '2': case '3':
  733. case '4': case '5': case '6': case '7':
  734. c = *s - '0';
  735. q = s + 2;
  736. while (s < q)
  737. {
  738. switch (*++s)
  739. {
  740. case '0': case '1': case '2': case '3':
  741. case '4': case '5': case '6': case '7':
  742. c = (c << 3) + *s - '0';
  743. break;
  744. default:
  745. q = --s;
  746. break;
  747. }
  748. }
  749. *t = c;
  750. break;
  751. default:
  752. *(s + 1) = 0;
  753. bad("invalid C \\ escape\n", s - 1, NiL, 0, 0);
  754. }
  755. return t - b;
  756. }
  757. static void
  758. matchoffprint(int off)
  759. {
  760. switch (off)
  761. {
  762. case -2:
  763. printf("X");
  764. break;
  765. case -1:
  766. printf("?");
  767. break;
  768. default:
  769. printf("%d", off);
  770. break;
  771. }
  772. }
  773. static void
  774. matchprint(regmatch_t* match, int nmatch, int nsub, char* ans, unsigned long test)
  775. {
  776. int i;
  777. for (; nmatch > nsub + 1; nmatch--)
  778. if ((match[nmatch-1].rm_so != -1 || match[nmatch-1].rm_eo != -1) && (!(test & TEST_IGNORE_POSITION) || (match[nmatch-1].rm_so >= 0 && match[nmatch-1].rm_eo >= 0)))
  779. break;
  780. for (i = 0; i < nmatch; i++)
  781. {
  782. printf("(");
  783. matchoffprint(match[i].rm_so);
  784. printf(",");
  785. matchoffprint(match[i].rm_eo);
  786. printf(")");
  787. }
  788. if (!(test & (TEST_ACTUAL|TEST_BASELINE)))
  789. {
  790. if (ans)
  791. printf(" expected: %s", ans);
  792. printf("\n");
  793. }
  794. }
  795. static int
  796. matchcheck(regmatch_t* match, int nmatch, int nsub, char* ans, char* re, char* s, int len, int flags, unsigned long test)
  797. {
  798. char* p;
  799. int i;
  800. int m;
  801. int n;
  802. if (streq(ans, "OK"))
  803. return test & (TEST_BASELINE|TEST_PASS|TEST_VERIFY);
  804. for (i = 0, p = ans; i < nmatch && *p; i++)
  805. {
  806. if (*p == '{')
  807. {
  808. #ifdef REG_DISCIPLINE
  809. char* x;
  810. x = sfstruse(state.disc.sp);
  811. if (strcmp(p, x))
  812. {
  813. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  814. return 0;
  815. report("callout failed", NiL, re, s, len, NiL, flags, test);
  816. quote(p, -1, test);
  817. printf(" expected, ");
  818. quote(x, -1, test);
  819. printf(" returned\n");
  820. }
  821. #endif
  822. break;
  823. }
  824. if (*p++ != '(')
  825. bad("improper answer\n", re, s, -1, test);
  826. if (*p == '?')
  827. {
  828. m = -1;
  829. p++;
  830. }
  831. else
  832. m = strtol(p, &p, 10);
  833. if (*p++ != ',')
  834. bad("improper answer\n", re, s, -1, test);
  835. if (*p == '?')
  836. {
  837. n = -1;
  838. p++;
  839. }
  840. else
  841. n = strtol(p, &p, 10);
  842. if (*p++ != ')')
  843. bad("improper answer\n", re, s, -1, test);
  844. if (m!=match[i].rm_so || n!=match[i].rm_eo)
  845. {
  846. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY)))
  847. {
  848. report("failed: match was", NiL, re, s, len, NiL, flags, test);
  849. matchprint(match, nmatch, nsub, ans, test);
  850. }
  851. return 0;
  852. }
  853. }
  854. for (; i < nmatch; i++)
  855. {
  856. if (match[i].rm_so!=-1 || match[i].rm_eo!=-1)
  857. {
  858. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_VERIFY)))
  859. {
  860. if ((test & TEST_IGNORE_POSITION) && (match[i].rm_so<0 || match[i].rm_eo<0))
  861. {
  862. state.ignored++;
  863. return 0;
  864. }
  865. if (!(test & TEST_SUMMARY))
  866. {
  867. report("failed: match was", NiL, re, s, len, NiL, flags, test);
  868. matchprint(match, nmatch, nsub, ans, test);
  869. }
  870. }
  871. return 0;
  872. }
  873. }
  874. if (!(test & TEST_IGNORE_OVER) && match[nmatch].rm_so != state.NOMATCH.rm_so)
  875. {
  876. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY)))
  877. {
  878. report("failed: overran match array", NiL, re, s, len, NiL, flags, test);
  879. matchprint(match, nmatch + 1, nsub, NiL, test);
  880. }
  881. return 0;
  882. }
  883. return 1;
  884. }
  885. static void
  886. sigunblock(int s)
  887. {
  888. #ifdef SIG_SETMASK
  889. int op;
  890. sigset_t mask;
  891. sigemptyset(&mask);
  892. if (s)
  893. {
  894. sigaddset(&mask, s);
  895. op = SIG_UNBLOCK;
  896. }
  897. else op = SIG_SETMASK;
  898. sigprocmask(op, &mask, NiL);
  899. #else
  900. #ifdef sigmask
  901. sigsetmask(s ? (sigsetmask(0L) & ~sigmask(s)) : 0L);
  902. #endif
  903. #endif
  904. }
  905. static void
  906. gotcha(int sig)
  907. {
  908. int ret;
  909. signal(sig, gotcha);
  910. alarm(0);
  911. state.signals++;
  912. switch (sig)
  913. {
  914. case SIGALRM:
  915. ret = REG_EHUNG;
  916. break;
  917. case SIGBUS:
  918. ret = REG_EBUS;
  919. break;
  920. default:
  921. ret = REG_EFAULT;
  922. break;
  923. }
  924. sigunblock(sig);
  925. longjmp(state.gotcha, ret);
  926. }
  927. static char*
  928. my_getline(FILE* fp)
  929. {
  930. static char buf[32 * 1024];
  931. register char* s = buf;
  932. register char* e = &buf[sizeof(buf)];
  933. register char* b;
  934. for (;;)
  935. {
  936. if (!(b = fgets(s, e - s, fp)))
  937. return 0;
  938. state.lineno++;
  939. s += strlen(s);
  940. if (s == b || *--s != '\n' || s == b || *(s - 1) != '\\')
  941. {
  942. *s = 0;
  943. break;
  944. }
  945. s--;
  946. }
  947. return buf;
  948. }
  949. static unsigned long
  950. note(unsigned long level, char* msg, unsigned long skip, unsigned long test)
  951. {
  952. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_SUMMARY)) && !skip)
  953. {
  954. printf("NOTE\t");
  955. if (msg)
  956. printf("%s: ", msg);
  957. printf("skipping lines %d", state.lineno);
  958. }
  959. return skip | level;
  960. }
  961. #define TABS(n) &ts[7-((n)&7)]
  962. static char ts[] = "\t\t\t\t\t\t\t";
  963. static unsigned long
  964. extract(int* tabs, char* spec, char* re, char* s, char* ans, char* msg, char* accept, regmatch_t* match, int nmatch, int nsub, unsigned long skip, unsigned long level, unsigned long test)
  965. {
  966. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_OK|TEST_PASS|TEST_SUMMARY))
  967. {
  968. state.extracted = 1;
  969. if (test & TEST_OK)
  970. {
  971. state.passed++;
  972. if ((test & TEST_VERIFY) && !(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_SUMMARY)))
  973. {
  974. if (msg && strcmp(msg, "EXPECTED"))
  975. printf("NOTE\t%s\n", msg);
  976. return skip;
  977. }
  978. test &= ~(TEST_PASS|TEST_QUERY);
  979. }
  980. if (test & (TEST_QUERY|TEST_VERIFY))
  981. {
  982. if (test & TEST_BASELINE)
  983. test &= ~(TEST_BASELINE|TEST_PASS);
  984. else
  985. test |= TEST_PASS;
  986. skip |= level;
  987. }
  988. if (!(test & TEST_OK))
  989. {
  990. if (test & TEST_UNSPECIFIED)
  991. state.unspecified++;
  992. else
  993. state.errors++;
  994. }
  995. if (test & (TEST_PASS|TEST_SUMMARY))
  996. return skip;
  997. test &= ~TEST_DELIMIT;
  998. printf("%s%s", spec, TABS(*tabs++));
  999. if ((test & (TEST_BASELINE|TEST_SAME)) == (TEST_BASELINE|TEST_SAME))
  1000. printf("SAME");
  1001. else
  1002. quote(re, -1, test);
  1003. printf("%s", TABS(*tabs++));
  1004. quote(s, -1, test);
  1005. printf("%s", TABS(*tabs++));
  1006. if (!(test & (TEST_ACTUAL|TEST_BASELINE)) || (!accept && !match))
  1007. printf("%s", ans);
  1008. else if (accept)
  1009. printf("%s", accept);
  1010. else
  1011. matchprint(match, nmatch, nsub, NiL, test);
  1012. if (msg)
  1013. printf("%s%s", TABS(*tabs++), msg);
  1014. putchar('\n');
  1015. }
  1016. else if (test & TEST_QUERY)
  1017. skip = note(level, msg, skip, test);
  1018. else if (test & TEST_VERIFY)
  1019. state.extracted = 1;
  1020. return skip;
  1021. }
  1022. static int
  1023. catchfree(regex_t* preg, int flags, int* tabs, char* spec, char* re, char* s, char* ans, char* msg, char* accept, regmatch_t* match, int nmatch, int nsub, unsigned long skip, unsigned long level, unsigned long test)
  1024. {
  1025. int eret;
  1026. if (!(test & TEST_CATCH))
  1027. {
  1028. regfree(preg);
  1029. eret = 0;
  1030. }
  1031. else if (!(eret = setjmp(state.gotcha)))
  1032. {
  1033. alarm(HUNG);
  1034. regfree(preg);
  1035. alarm(0);
  1036. }
  1037. else if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1038. extract(tabs, spec, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test);
  1039. else
  1040. {
  1041. report("failed", "regfree", re, NiL, -1, msg, flags, test);
  1042. error(preg, eret);
  1043. }
  1044. return eret;
  1045. }
  1046. int
  1047. main(int argc, char** argv)
  1048. {
  1049. int flags;
  1050. int cflags;
  1051. int eflags;
  1052. int nmatch;
  1053. int nexec;
  1054. int nstr;
  1055. int cret;
  1056. int eret;
  1057. int nsub;
  1058. int i;
  1059. int j;
  1060. int expected;
  1061. int got;
  1062. int locale;
  1063. int subunitlen = 0;
  1064. int testno;
  1065. unsigned long level;
  1066. unsigned long skip;
  1067. char* p;
  1068. char* line;
  1069. char* spec;
  1070. char* re;
  1071. char* s;
  1072. char* ans;
  1073. char* msg;
  1074. char* fun;
  1075. char* ppat = NULL;
  1076. char* subunit = NULL;
  1077. char* version;
  1078. char* field[6];
  1079. char* delim[6];
  1080. FILE* fp;
  1081. int tabs[6];
  1082. char unit[64];
  1083. regmatch_t match[100];
  1084. regex_t preg;
  1085. static char pat[32 * 1024];
  1086. int nonosub = REG_NOSUB == 0;
  1087. int nonexec = 0;
  1088. unsigned long test = 0;
  1089. static char* filter[] = { "-", 0 };
  1090. state.NOMATCH.rm_so = state.NOMATCH.rm_eo = -2;
  1091. p = unit;
  1092. version = (char*)id + 10;
  1093. while (p < &unit[sizeof(unit)-1] && (*p = *version++) && !isspace(*p))
  1094. p++;
  1095. *p = 0;
  1096. while ((p = *++argv) && *p == '-')
  1097. for (;;)
  1098. {
  1099. switch (*++p)
  1100. {
  1101. case 0:
  1102. break;
  1103. case 'c':
  1104. test |= TEST_CATCH;
  1105. continue;
  1106. case 'e':
  1107. test |= TEST_IGNORE_ERROR;
  1108. continue;
  1109. case 'h':
  1110. case '?':
  1111. help(0);
  1112. return 2;
  1113. case '-':
  1114. help(p[1] == 'h');
  1115. return 2;
  1116. case 'n':
  1117. nonexec = 1;
  1118. continue;
  1119. case 'o':
  1120. test |= TEST_IGNORE_OVER;
  1121. continue;
  1122. case 'p':
  1123. test |= TEST_IGNORE_POSITION;
  1124. continue;
  1125. case 's':
  1126. #ifdef REG_DISCIPLINE
  1127. if (!(state.stack = stkalloc(stkstd, 0)))
  1128. fprintf(stderr, "%s: out of space [stack]", unit);
  1129. state.disc.disc.re_resizef = resizef;
  1130. state.disc.disc.re_resizehandle = (void*)stkstd;
  1131. #endif
  1132. continue;
  1133. case 'x':
  1134. nonosub = 1;
  1135. continue;
  1136. case 'v':
  1137. test |= TEST_VERBOSE;
  1138. continue;
  1139. case 'A':
  1140. test |= TEST_ACTUAL;
  1141. continue;
  1142. case 'B':
  1143. test |= TEST_BASELINE;
  1144. continue;
  1145. case 'F':
  1146. test |= TEST_FAIL;
  1147. continue;
  1148. case 'P':
  1149. test |= TEST_PASS;
  1150. continue;
  1151. case 'S':
  1152. test |= TEST_SUMMARY;
  1153. continue;
  1154. default:
  1155. fprintf(stderr, "%s: %c: invalid option\n", unit, *p);
  1156. return 2;
  1157. }
  1158. break;
  1159. }
  1160. if (!*argv)
  1161. argv = filter;
  1162. locale = 0;
  1163. while ((state.file = *argv++))
  1164. {
  1165. if (streq(state.file, "-") || streq(state.file, "/dev/stdin") || streq(state.file, "/dev/fd/0"))
  1166. {
  1167. state.file = 0;
  1168. fp = stdin;
  1169. }
  1170. else if (!(fp = fopen(state.file, "r")))
  1171. {
  1172. fprintf(stderr, "%s: %s: cannot read\n", unit, state.file);
  1173. return 2;
  1174. }
  1175. testno = state.errors = state.ignored = state.lineno = state.passed =
  1176. state.signals = state.unspecified = state.warnings = 0;
  1177. skip = 0;
  1178. level = 1;
  1179. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_SUMMARY)))
  1180. {
  1181. printf("TEST\t%s ", unit);
  1182. if ((s = state.file))
  1183. {
  1184. subunit = p = 0;
  1185. for (;;)
  1186. {
  1187. switch (*s++)
  1188. {
  1189. case 0:
  1190. break;
  1191. case '/':
  1192. subunit = s;
  1193. continue;
  1194. case '.':
  1195. p = s - 1;
  1196. continue;
  1197. default:
  1198. continue;
  1199. }
  1200. break;
  1201. }
  1202. if (!subunit)
  1203. subunit = state.file;
  1204. if (p < subunit)
  1205. p = s - 1;
  1206. subunitlen = p - subunit;
  1207. printf("%-.*s ", subunitlen, subunit);
  1208. }
  1209. else
  1210. subunit = 0;
  1211. for (s = version; *s && (*s != ' ' || *(s + 1) != '$'); s++)
  1212. putchar(*s);
  1213. if (test & TEST_CATCH)
  1214. printf(", catch");
  1215. if (test & TEST_IGNORE_ERROR)
  1216. printf(", ignore error code mismatches");
  1217. if (test & TEST_IGNORE_POSITION)
  1218. printf(", ignore negative position mismatches");
  1219. #ifdef REG_DISCIPLINE
  1220. if (state.stack)
  1221. printf(", stack");
  1222. #endif
  1223. if (test & TEST_VERBOSE)
  1224. printf(", verbose");
  1225. printf("\n");
  1226. #ifdef REG_VERSIONID
  1227. if (regerror(REG_VERSIONID, NiL, pat, sizeof(pat)) > 0)
  1228. s = pat;
  1229. else
  1230. #endif
  1231. #ifdef REG_TEST_VERSION
  1232. s = REG_TEST_VERSION;
  1233. #else
  1234. s = "regex";
  1235. #endif
  1236. printf("NOTE\t%s\n", s);
  1237. if (elementsof(unsupported) > 1)
  1238. {
  1239. #if (REG_TEST_DEFAULT & (REG_AUGMENTED|REG_EXTENDED|REG_SHELL)) || !defined(REG_EXTENDED)
  1240. i = 0;
  1241. #else
  1242. i = REG_EXTENDED != 0;
  1243. #endif
  1244. for (got = 0; i < elementsof(unsupported) - 1; i++)
  1245. {
  1246. if (!got)
  1247. {
  1248. got = 1;
  1249. printf("NOTE\tunsupported: %s", unsupported[i]);
  1250. }
  1251. else
  1252. printf(",%s", unsupported[i]);
  1253. }
  1254. if (got)
  1255. printf("\n");
  1256. }
  1257. }
  1258. #ifdef REG_DISCIPLINE
  1259. state.disc.disc.re_version = REG_VERSION;
  1260. state.disc.disc.re_compf = compf;
  1261. state.disc.disc.re_execf = execf;
  1262. if (!(state.disc.sp = sfstropen()))
  1263. bad("out of space [discipline string stream]\n", NiL, NiL, 0, 0);
  1264. preg.re_disc = &state.disc.disc;
  1265. #endif
  1266. if (test & TEST_CATCH)
  1267. {
  1268. signal(SIGALRM, gotcha);
  1269. signal(SIGBUS, gotcha);
  1270. signal(SIGSEGV, gotcha);
  1271. }
  1272. while ((p = my_getline(fp)))
  1273. {
  1274. /* parse: */
  1275. line = p;
  1276. if (*p == ':' && !isspace(*(p + 1)))
  1277. {
  1278. while (*++p && *p != ':');
  1279. if (!*p++)
  1280. {
  1281. if (test & TEST_BASELINE)
  1282. printf("%s\n", line);
  1283. continue;
  1284. }
  1285. }
  1286. while (isspace(*p))
  1287. p++;
  1288. if (*p == 0 || *p == '#' || *p == 'T')
  1289. {
  1290. if (test & TEST_BASELINE)
  1291. printf("%s\n", line);
  1292. continue;
  1293. }
  1294. if (*p == ':' || *p == 'N')
  1295. {
  1296. if (test & TEST_BASELINE)
  1297. printf("%s\n", line);
  1298. else if (!(test & (TEST_ACTUAL|TEST_FAIL|TEST_PASS|TEST_SUMMARY)))
  1299. {
  1300. while (*++p && !isspace(*p));
  1301. while (isspace(*p))
  1302. p++;
  1303. printf("NOTE %s\n", p);
  1304. }
  1305. continue;
  1306. }
  1307. j = 0;
  1308. i = 0;
  1309. field[i++] = p;
  1310. for (;;)
  1311. {
  1312. switch (*p++)
  1313. {
  1314. case 0:
  1315. p--;
  1316. j = 0;
  1317. goto checkfield;
  1318. case '\t':
  1319. *(delim[i] = p - 1) = 0;
  1320. j = 1;
  1321. checkfield:
  1322. s = field[i - 1];
  1323. if (streq(s, "NIL"))
  1324. field[i - 1] = 0;
  1325. else if (streq(s, "NULL"))
  1326. *s = 0;
  1327. while (*p == '\t')
  1328. {
  1329. p++;
  1330. j++;
  1331. }
  1332. tabs[i - 1] = j;
  1333. if (!*p)
  1334. break;
  1335. if (i >= elementsof(field))
  1336. bad("too many fields\n", NiL, NiL, 0, 0);
  1337. field[i++] = p;
  1338. /*FALLTHROUGH*/
  1339. default:
  1340. continue;
  1341. }
  1342. break;
  1343. }
  1344. if (!(spec = field[0]))
  1345. bad("NIL spec\n", NiL, NiL, 0, 0);
  1346. /* interpret: */
  1347. cflags = REG_TEST_DEFAULT;
  1348. eflags = REG_EXEC_DEFAULT;
  1349. test &= TEST_GLOBAL;
  1350. state.extracted = 0;
  1351. nmatch = 20;
  1352. nsub = -1;
  1353. for (p = spec; *p; p++)
  1354. {
  1355. if (isdigit(*p))
  1356. {
  1357. nmatch = strtol(p, &p, 10);
  1358. if (nmatch >= elementsof(match))
  1359. bad("nmatch must be < 100\n", NiL, NiL, 0, 0);
  1360. p--;
  1361. continue;
  1362. }
  1363. switch (*p)
  1364. {
  1365. case 'A':
  1366. test |= TEST_ARE;
  1367. continue;
  1368. case 'B':
  1369. test |= TEST_BRE;
  1370. continue;
  1371. case 'C':
  1372. if (!(test & TEST_QUERY) && !(skip & level))
  1373. bad("locale must be nested\n", NiL, NiL, 0, 0);
  1374. test &= ~TEST_QUERY;
  1375. if (locale)
  1376. bad("locale nesting not supported\n", NiL, NiL, 0, 0);
  1377. if (i != 2)
  1378. bad("locale field expected\n", NiL, NiL, 0, 0);
  1379. if (!(skip & level))
  1380. {
  1381. #if defined(LC_COLLATE) && defined(LC_CTYPE)
  1382. s = field[1];
  1383. if (!s || streq(s, "POSIX"))
  1384. s = "C";
  1385. if (!(ans = setlocale(LC_COLLATE, s)) || streq(ans, "C") || streq(ans, "POSIX") || !(ans = setlocale(LC_CTYPE, s)) || streq(ans, "C") || streq(ans, "POSIX"))
  1386. skip = note(level, s, skip, test);
  1387. else
  1388. {
  1389. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_SUMMARY)))
  1390. printf("NOTE \"%s\" locale\n", s);
  1391. locale = level;
  1392. }
  1393. #else
  1394. skip = note(level, skip, test, "locales not supported");
  1395. #endif
  1396. }
  1397. cflags = NOTEST;
  1398. continue;
  1399. case 'E':
  1400. test |= TEST_ERE;
  1401. continue;
  1402. case 'K':
  1403. test |= TEST_KRE;
  1404. continue;
  1405. case 'L':
  1406. test |= TEST_LRE;
  1407. continue;
  1408. case 'S':
  1409. test |= TEST_SRE;
  1410. continue;
  1411. case 'a':
  1412. cflags |= REG_LEFT|REG_RIGHT;
  1413. continue;
  1414. case 'b':
  1415. eflags |= REG_NOTBOL;
  1416. continue;
  1417. case 'c':
  1418. cflags |= REG_COMMENT;
  1419. continue;
  1420. case 'd':
  1421. cflags |= REG_SHELL_DOT;
  1422. continue;
  1423. case 'e':
  1424. eflags |= REG_NOTEOL;
  1425. continue;
  1426. case 'f':
  1427. cflags |= REG_MULTIPLE;
  1428. continue;
  1429. case 'g':
  1430. cflags |= NOTEST;
  1431. continue;
  1432. case 'h':
  1433. cflags |= REG_MULTIREF;
  1434. continue;
  1435. case 'i':
  1436. cflags |= REG_ICASE;
  1437. continue;
  1438. case 'j':
  1439. cflags |= REG_SPAN;
  1440. continue;
  1441. case 'k':
  1442. cflags |= REG_ESCAPE;
  1443. continue;
  1444. case 'l':
  1445. cflags |= REG_LEFT;
  1446. continue;
  1447. case 'm':
  1448. cflags |= REG_MINIMAL;
  1449. continue;
  1450. case 'n':
  1451. cflags |= REG_NEWLINE;
  1452. continue;
  1453. case 'o':
  1454. cflags |= REG_SHELL_GROUP;
  1455. continue;
  1456. case 'p':
  1457. cflags |= REG_SHELL_PATH;
  1458. continue;
  1459. case 'q':
  1460. cflags |= REG_DELIMITED;
  1461. continue;
  1462. case 'r':
  1463. cflags |= REG_RIGHT;
  1464. continue;
  1465. case 's':
  1466. cflags |= REG_SHELL_ESCAPED;
  1467. continue;
  1468. case 't':
  1469. cflags |= REG_MUSTDELIM;
  1470. continue;
  1471. case 'u':
  1472. test |= TEST_UNSPECIFIED;
  1473. continue;
  1474. case 'w':
  1475. cflags |= REG_NOSUB;
  1476. continue;
  1477. case 'x':
  1478. if (REG_LENIENT)
  1479. cflags |= REG_LENIENT;
  1480. else
  1481. test |= TEST_LENIENT;
  1482. continue;
  1483. case 'y':
  1484. eflags |= REG_LEFT;
  1485. continue;
  1486. case 'z':
  1487. cflags |= REG_NULL;
  1488. continue;
  1489. case '$':
  1490. test |= TEST_EXPAND;
  1491. continue;
  1492. case '/':
  1493. test |= TEST_SUB;
  1494. continue;
  1495. case '?':
  1496. test |= TEST_VERIFY;
  1497. test &= ~(TEST_AND|TEST_OR);
  1498. state.verify = state.passed;
  1499. continue;
  1500. case '&':
  1501. test |= TEST_VERIFY|TEST_AND;
  1502. test &= ~TEST_OR;
  1503. continue;
  1504. case '|':
  1505. test |= TEST_VERIFY|TEST_OR;
  1506. test &= ~TEST_AND;
  1507. continue;
  1508. case ';':
  1509. test |= TEST_OR;
  1510. test &= ~TEST_AND;
  1511. continue;
  1512. case '{':
  1513. level <<= 1;
  1514. if (skip & (level >> 1))
  1515. {
  1516. skip |= level;
  1517. cflags = NOTEST;
  1518. }
  1519. else
  1520. {
  1521. skip &= ~level;
  1522. test |= TEST_QUERY;
  1523. }
  1524. continue;
  1525. case '}':
  1526. if (level == 1)
  1527. bad("invalid {...} nesting\n", NiL, NiL, 0, 0);
  1528. if ((skip & level) && !(skip & (level>>1)))
  1529. {
  1530. if (!(test & (TEST_BASELINE|TEST_SUMMARY)))
  1531. {
  1532. if (test & (TEST_ACTUAL|TEST_FAIL))
  1533. printf("}\n");
  1534. else if (!(test & TEST_PASS))
  1535. printf("-%d\n", state.lineno);
  1536. }
  1537. }
  1538. #if defined(LC_COLLATE) && defined(LC_CTYPE)
  1539. else if (locale & level)
  1540. {
  1541. locale = 0;
  1542. if (!(skip & level))
  1543. {
  1544. s = "C";
  1545. setlocale(LC_COLLATE, s);
  1546. setlocale(LC_CTYPE, s);
  1547. if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_SUMMARY)))
  1548. printf("NOTE \"%s\" locale\n", s);
  1549. else if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_PASS))
  1550. printf("}\n");
  1551. }
  1552. else if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL))
  1553. printf("}\n");
  1554. }
  1555. #endif
  1556. level >>= 1;
  1557. cflags = NOTEST;
  1558. continue;
  1559. default:
  1560. bad("bad spec\n", spec, NiL, 0, test);
  1561. break;
  1562. }
  1563. break;
  1564. }
  1565. if ((cflags|eflags) == NOTEST || ((skip & level) && (test & TEST_BASELINE)))
  1566. {
  1567. if (test & TEST_BASELINE)
  1568. {
  1569. while (i > 1)
  1570. *delim[--i] = '\t';
  1571. printf("%s\n", line);
  1572. }
  1573. continue;
  1574. }
  1575. if (test & TEST_OR)
  1576. {
  1577. if (!(test & TEST_VERIFY))
  1578. {
  1579. test &= ~TEST_OR;
  1580. if (state.passed == state.verify && i > 1)
  1581. printf("NOTE\t%s\n", field[1]);
  1582. continue;
  1583. }
  1584. else if (state.passed > state.verify)
  1585. continue;
  1586. }
  1587. else if (test & TEST_AND)
  1588. {
  1589. if (state.passed == state.verify)
  1590. continue;
  1591. state.passed = state.verify;
  1592. }
  1593. if (i < 4)
  1594. bad("too few fields\n", NiL, NiL, 0, test);
  1595. while (i < elementsof(field))
  1596. field[i++] = 0;
  1597. if ((re = field[1]))
  1598. {
  1599. if (streq(re, "SAME"))
  1600. {
  1601. re = ppat;
  1602. test |= TEST_SAME;
  1603. }
  1604. else
  1605. {
  1606. if (test & TEST_EXPAND)
  1607. escape(re);
  1608. strcpy(ppat = pat, re);
  1609. }
  1610. }
  1611. else
  1612. ppat = 0;
  1613. nstr = -1;
  1614. if ((s = field[2]) && (test & TEST_EXPAND))
  1615. {
  1616. nstr = escape(s);
  1617. #if _REG_nexec
  1618. if (nstr != strlen(s))
  1619. nexec = nstr;
  1620. #endif
  1621. }
  1622. if (!(ans = field[3]))
  1623. bad("NIL answer\n", NiL, NiL, 0, test);
  1624. msg = field[4];
  1625. fflush(stdout);
  1626. if (test & TEST_SUB)
  1627. #if _REG_subcomp
  1628. cflags |= REG_DELIMITED;
  1629. #else
  1630. continue;
  1631. #endif
  1632. compile:
  1633. if (state.extracted || (skip & level))
  1634. continue;
  1635. #if !(REG_TEST_DEFAULT & (REG_AUGMENTED|REG_EXTENDED|REG_SHELL))
  1636. #ifdef REG_EXTENDED
  1637. if (REG_EXTENDED != 0 && (test & TEST_BRE))
  1638. #else
  1639. if (test & TEST_BRE)
  1640. #endif
  1641. {
  1642. test &= ~TEST_BRE;
  1643. flags = cflags;
  1644. state.which = "BRE";
  1645. }
  1646. else
  1647. #endif
  1648. #ifdef REG_EXTENDED
  1649. if (test & TEST_ERE)
  1650. {
  1651. test &= ~TEST_ERE;
  1652. flags = cflags | REG_EXTENDED;
  1653. state.which = "ERE";
  1654. }
  1655. else
  1656. #endif
  1657. #ifdef REG_AUGMENTED
  1658. if (test & TEST_ARE)
  1659. {
  1660. test &= ~TEST_ARE;
  1661. flags = cflags | REG_AUGMENTED;
  1662. state.which = "ARE";
  1663. }
  1664. else
  1665. #endif
  1666. #ifdef REG_LITERAL
  1667. if (test & TEST_LRE)
  1668. {
  1669. test &= ~TEST_LRE;
  1670. flags = cflags | REG_LITERAL;
  1671. state.which = "LRE";
  1672. }
  1673. else
  1674. #endif
  1675. #ifdef REG_SHELL
  1676. if (test & TEST_SRE)
  1677. {
  1678. test &= ~TEST_SRE;
  1679. flags = cflags | REG_SHELL;
  1680. state.which = "SRE";
  1681. }
  1682. else
  1683. #ifdef REG_AUGMENTED
  1684. if (test & TEST_KRE)
  1685. {
  1686. test &= ~TEST_KRE;
  1687. flags = cflags | REG_SHELL | REG_AUGMENTED;
  1688. state.which = "KRE";
  1689. }
  1690. else
  1691. #endif
  1692. #endif
  1693. {
  1694. if (test & (TEST_BASELINE|TEST_PASS|TEST_VERIFY))
  1695. extract(tabs, line, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test|TEST_OK);
  1696. continue;
  1697. }
  1698. if ((test & (TEST_QUERY|TEST_VERBOSE|TEST_VERIFY)) == TEST_VERBOSE)
  1699. {
  1700. printf("test %-3d %s ", state.lineno, state.which);
  1701. quote(re, -1, test|TEST_DELIMIT);
  1702. printf(" ");
  1703. quote(s, nstr, test|TEST_DELIMIT);
  1704. printf("\n");
  1705. }
  1706. nosub:
  1707. fun = "regcomp";
  1708. #if _REG_nexec
  1709. if (nstr >= 0 && nstr != strlen(s))
  1710. nexec = nstr;
  1711. else
  1712. #endif
  1713. nexec = -1;
  1714. if (state.extracted || (skip & level))
  1715. continue;
  1716. if (!(test & TEST_QUERY))
  1717. testno++;
  1718. #ifdef REG_DISCIPLINE
  1719. if (state.stack)
  1720. stkset(stkstd, state.stack, 0);
  1721. flags |= REG_DISCIPLINE;
  1722. state.disc.ordinal = 0;
  1723. sfstrseek(state.disc.sp, 0, SEEK_SET);
  1724. #endif
  1725. if (!(test & TEST_CATCH))
  1726. cret = regcomp(&preg, re, flags);
  1727. else if (!(cret = setjmp(state.gotcha)))
  1728. {
  1729. alarm(HUNG);
  1730. cret = regcomp(&preg, re, flags);
  1731. alarm(0);
  1732. }
  1733. #if _REG_subcomp
  1734. if (!cret && (test & TEST_SUB))
  1735. {
  1736. fun = "regsubcomp";
  1737. p = re + preg.re_npat;
  1738. if (!(test & TEST_CATCH))
  1739. cret = regsubcomp(&preg, p, NiL, 0, 0);
  1740. else if (!(cret = setjmp(state.gotcha)))
  1741. {
  1742. alarm(HUNG);
  1743. cret = regsubcomp(&preg, p, NiL, 0, 0);
  1744. alarm(0);
  1745. }
  1746. if (!cret && *(p += preg.re_npat) && !(preg.re_sub->re_flags & REG_SUB_LAST))
  1747. {
  1748. if (catchfree(&preg, flags, tabs, line, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test))
  1749. continue;
  1750. cret = REG_EFLAGS;
  1751. }
  1752. }
  1753. #endif
  1754. if (!cret)
  1755. {
  1756. if (!(flags & REG_NOSUB) && nsub < 0 && *ans == '(')
  1757. {
  1758. for (p = ans; *p; p++)
  1759. if (*p == '(')
  1760. nsub++;
  1761. else if (*p == '{')
  1762. nsub--;
  1763. if (nsub >= 0)
  1764. {
  1765. if (test & TEST_IGNORE_OVER)
  1766. {
  1767. if (nmatch > nsub)
  1768. nmatch = nsub + 1;
  1769. }
  1770. else if (nsub != preg.re_nsub)
  1771. {
  1772. if (nsub > preg.re_nsub)
  1773. {
  1774. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1775. skip = extract(tabs, line, re, s, ans, msg, "OK", NiL, 0, 0, skip, level, test|TEST_DELIMIT);
  1776. else
  1777. {
  1778. report("re_nsub incorrect", fun, re, NiL, -1, msg, flags, test);
  1779. printf("at least %d expected, %d returned\n", nsub, preg.re_nsub);
  1780. state.errors++;
  1781. }
  1782. }
  1783. else
  1784. nsub = preg.re_nsub;
  1785. }
  1786. }
  1787. }
  1788. if (!(test & TEST_SUB) && *ans && *ans != '(' && !streq(ans, "OK") && !streq(ans, "NOMATCH"))
  1789. {
  1790. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1791. skip = extract(tabs, line, re, s, ans, msg, "OK", NiL, 0, 0, skip, level, test|TEST_DELIMIT);
  1792. else if (!(test & TEST_LENIENT))
  1793. {
  1794. report("failed", fun, re, NiL, -1, msg, flags, test);
  1795. printf("%s expected, OK returned\n", ans);
  1796. }
  1797. catchfree(&preg, flags, tabs, line, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test);
  1798. continue;
  1799. }
  1800. }
  1801. else
  1802. {
  1803. if (test & TEST_LENIENT)
  1804. /* we'll let it go this time */;
  1805. else if (!*ans || ans[0]=='(' || (cret == REG_BADPAT && streq(ans, "NOMATCH")))
  1806. {
  1807. got = 0;
  1808. for (i = 1; i < elementsof(codes); i++)
  1809. if (cret==codes[i].code)
  1810. got = i;
  1811. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1812. skip = extract(tabs, line, re, s, ans, msg, codes[got].name, NiL, 0, 0, skip, level, test|TEST_DELIMIT);
  1813. else
  1814. {
  1815. report("failed", fun, re, NiL, -1, msg, flags, test);
  1816. printf("%s returned: ", codes[got].name);
  1817. error(&preg, cret);
  1818. }
  1819. }
  1820. else
  1821. {
  1822. expected = got = 0;
  1823. for (i = 1; i < elementsof(codes); i++)
  1824. {
  1825. if (streq(ans, codes[i].name))
  1826. expected = i;
  1827. if (cret==codes[i].code)
  1828. got = i;
  1829. }
  1830. if (!expected)
  1831. {
  1832. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1833. skip = extract(tabs, line, re, s, ans, msg, codes[got].name, NiL, 0, 0, skip, level, test|TEST_DELIMIT);
  1834. else
  1835. {
  1836. report("failed: invalid error code", NiL, re, NiL, -1, msg, flags, test);
  1837. printf("%s expected, %s returned\n", ans, codes[got].name);
  1838. }
  1839. }
  1840. else if (cret != codes[expected].code && cret != REG_BADPAT)
  1841. {
  1842. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1843. skip = extract(tabs, line, re, s, ans, msg, codes[got].name, NiL, 0, 0, skip, level, test|TEST_DELIMIT);
  1844. else if (test & TEST_IGNORE_ERROR)
  1845. state.ignored++;
  1846. else
  1847. {
  1848. report("should fail and did", fun, re, NiL, -1, msg, flags, test);
  1849. printf("%s expected, %s returned: ", ans, codes[got].name);
  1850. state.errors--;
  1851. state.warnings++;
  1852. error(&preg, cret);
  1853. }
  1854. }
  1855. }
  1856. goto compile;
  1857. }
  1858. #if _REG_nexec
  1859. execute:
  1860. if (nexec >= 0)
  1861. fun = "regnexec";
  1862. else
  1863. #endif
  1864. fun = "regexec";
  1865. for (i = 0; i < elementsof(match); i++)
  1866. match[i] = state.NOMATCH;
  1867. #if _REG_nexec
  1868. if (nexec >= 0)
  1869. {
  1870. eret = regnexec(&preg, s, nexec, nmatch, match, eflags);
  1871. s[nexec] = 0;
  1872. }
  1873. else
  1874. #endif
  1875. {
  1876. if (!(test & TEST_CATCH))
  1877. eret = regexec(&preg, s, nmatch, match, eflags);
  1878. else if (!(eret = setjmp(state.gotcha)))
  1879. {
  1880. alarm(HUNG);
  1881. eret = regexec(&preg, s, nmatch, match, eflags);
  1882. alarm(0);
  1883. }
  1884. }
  1885. #if _REG_subcomp
  1886. if ((test & TEST_SUB) && !eret)
  1887. {
  1888. fun = "regsubexec";
  1889. if (!(test & TEST_CATCH))
  1890. eret = regsubexec(&preg, s, nmatch, match);
  1891. else if (!(eret = setjmp(state.gotcha)))
  1892. {
  1893. alarm(HUNG);
  1894. eret = regsubexec(&preg, s, nmatch, match);
  1895. alarm(0);
  1896. }
  1897. }
  1898. #endif
  1899. if (flags & REG_NOSUB)
  1900. {
  1901. if (eret)
  1902. {
  1903. if (eret != REG_NOMATCH || !streq(ans, "NOMATCH"))
  1904. {
  1905. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1906. skip = extract(tabs, line, re, s, ans, msg, "NOMATCH", NiL, 0, 0, skip, level, test|TEST_DELIMIT);
  1907. else
  1908. {
  1909. report("REG_NOSUB failed", fun, re, s, nstr, msg, flags, test);
  1910. error(&preg, eret);
  1911. }
  1912. }
  1913. }
  1914. else if (streq(ans, "NOMATCH"))
  1915. {
  1916. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1917. skip = extract(tabs, line, re, s, ans, msg, NiL, match, nmatch, nsub, skip, level, test|TEST_DELIMIT);
  1918. else
  1919. {
  1920. report("should fail and didn't", fun, re, s, nstr, msg, flags, test);
  1921. error(&preg, eret);
  1922. }
  1923. }
  1924. }
  1925. else if (eret)
  1926. {
  1927. if (eret != REG_NOMATCH || !streq(ans, "NOMATCH"))
  1928. {
  1929. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1930. skip = extract(tabs, line, re, s, ans, msg, "NOMATCH", NiL, 0, nsub, skip, level, test|TEST_DELIMIT);
  1931. else
  1932. {
  1933. report("failed", fun, re, s, nstr, msg, flags, test);
  1934. if (eret != REG_NOMATCH)
  1935. error(&preg, eret);
  1936. else if (*ans)
  1937. printf("expected: %s\n", ans);
  1938. else
  1939. printf("\n");
  1940. }
  1941. }
  1942. }
  1943. else if (streq(ans, "NOMATCH"))
  1944. {
  1945. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1946. skip = extract(tabs, line, re, s, ans, msg, NiL, match, nmatch, nsub, skip, level, test|TEST_DELIMIT);
  1947. else
  1948. {
  1949. report("should fail and didn't", fun, re, s, nstr, msg, flags, test);
  1950. matchprint(match, nmatch, nsub, NiL, test);
  1951. }
  1952. }
  1953. #if _REG_subcomp
  1954. else if (test & TEST_SUB)
  1955. {
  1956. p = preg.re_sub->re_buf;
  1957. if (strcmp(p, ans))
  1958. {
  1959. report("failed", fun, re, s, nstr, msg, flags, test);
  1960. quote(ans, -1, test|TEST_DELIMIT);
  1961. printf(" expected, ");
  1962. quote(p, -1, test|TEST_DELIMIT);
  1963. printf(" returned\n");
  1964. }
  1965. }
  1966. #endif
  1967. else if (!*ans)
  1968. {
  1969. if (match[0].rm_so != state.NOMATCH.rm_so)
  1970. {
  1971. if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  1972. skip = extract(tabs, line, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test);
  1973. else
  1974. {
  1975. report("failed: no match but match array assigned", NiL, re, s, nstr, msg, flags, test);
  1976. matchprint(match, nmatch, nsub, NiL, test);
  1977. }
  1978. }
  1979. }
  1980. else if (matchcheck(match, nmatch, nsub, ans, re, s, nstr, flags, test))
  1981. {
  1982. #if _REG_nexec
  1983. if (nexec < 0 && !nonexec)
  1984. {
  1985. nexec = nstr >= 0 ? nstr : strlen(s);
  1986. s[nexec] = '\n';
  1987. testno++;
  1988. goto execute;
  1989. }
  1990. #endif
  1991. if (!(test & (TEST_SUB|TEST_VERIFY)) && !nonosub)
  1992. {
  1993. if (catchfree(&preg, flags, tabs, line, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test))
  1994. continue;
  1995. flags |= REG_NOSUB;
  1996. goto nosub;
  1997. }
  1998. if (test & (TEST_BASELINE|TEST_PASS|TEST_VERIFY))
  1999. skip = extract(tabs, line, re, s, ans, msg, NiL, match, nmatch, nsub, skip, level, test|TEST_OK);
  2000. }
  2001. else if (test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS|TEST_QUERY|TEST_SUMMARY|TEST_VERIFY))
  2002. skip = extract(tabs, line, re, s, ans, msg, NiL, match, nmatch, nsub, skip, level, test|TEST_DELIMIT);
  2003. if (catchfree(&preg, flags, tabs, line, re, s, ans, msg, NiL, NiL, 0, 0, skip, level, test))
  2004. continue;
  2005. goto compile;
  2006. }
  2007. if (test & TEST_SUMMARY)
  2008. printf("tests=%-4d errors=%-4d warnings=%-2d ignored=%-2d unspecified=%-2d signals=%d\n", testno, state.errors, state.warnings, state.ignored, state.unspecified, state.signals);
  2009. else if (!(test & (TEST_ACTUAL|TEST_BASELINE|TEST_FAIL|TEST_PASS)))
  2010. {
  2011. printf("TEST\t%s", unit);
  2012. if (subunit)
  2013. printf(" %-.*s", subunitlen, subunit);
  2014. printf(", %d test%s", testno, testno == 1 ? "" : "s");
  2015. if (state.ignored)
  2016. printf(", %d ignored mismatche%s", state.ignored, state.ignored == 1 ? "" : "s");
  2017. if (state.warnings)
  2018. printf(", %d warning%s", state.warnings, state.warnings == 1 ? "" : "s");
  2019. if (state.unspecified)
  2020. printf(", %d unspecified difference%s", state.unspecified, state.unspecified == 1 ? "" : "s");
  2021. if (state.signals)
  2022. printf(", %d signal%s", state.signals, state.signals == 1 ? "" : "s");
  2023. printf(", %d error%s\n", state.errors, state.errors == 1 ? "" : "s");
  2024. }
  2025. if (fp != stdin)
  2026. fclose(fp);
  2027. }
  2028. return 0;
  2029. }