testregex.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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 5
  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. old_main(int unused_param_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, %zd 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 mismatch%s", state.ignored, state.ignored == 1 ? "" : "es");
  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 state.errors;
  2029. }
  2030. int main(int argc, char **argv)
  2031. {
  2032. static char *param[][4] = {
  2033. { NULL, "basic.dat" , NULL },
  2034. { NULL, "categorize.dat" , NULL },
  2035. { NULL, "forcedassoc.dat" , NULL },
  2036. { NULL, "-c", "interpretation.dat", NULL },
  2037. { NULL, "leftassoc.dat" , NULL },
  2038. { NULL, "-c", "nullsubexpr.dat" , NULL },
  2039. { NULL, "repetition.dat" , NULL },
  2040. { NULL, "rightassoc.dat" , NULL },
  2041. };
  2042. int r, i;
  2043. if (argv[1])
  2044. return old_main(argc, argv);
  2045. r = 0;
  2046. for (i = 0; i < sizeof(param) / sizeof(param[0]); i++) {
  2047. param[i][0] = argv[0];
  2048. printf("Testing %s\n", param[i][1][0] != '-' ? param[i][1] : param[i][2]);
  2049. r |= old_main(3 /* not used */, param[i]);
  2050. }
  2051. return r;
  2052. }