makeinfo.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312
  1. /* makeinfo -- convert Texinfo source into other formats.
  2. $Id: makeinfo.c,v 1.124 2011-04-06 21:21:01 gray Exp $
  3. Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
  4. 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
  5. Free Software Foundation, Inc.
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Original author of makeinfo: Brian Fox (bfox@ai.mit.edu). */
  17. #include "system.h"
  18. #include "getopt.h"
  19. #include "mbswidth.h"
  20. #define COMPILING_MAKEINFO
  21. #include "makeinfo.h"
  22. #include "cmds.h"
  23. #include "files.h"
  24. #include "float.h"
  25. #include "footnote.h"
  26. #include "html.h"
  27. #include "index.h"
  28. #include "insertion.h"
  29. #include "lang.h"
  30. #include "macro.h"
  31. #include "node.h"
  32. #include "sectioning.h"
  33. #include "toc.h"
  34. #include "xml.h"
  35. /* You can change some of the behavior of Makeinfo by changing the
  36. following defines: */
  37. /* Define INDENT_PARAGRAPHS_IN_TABLE if you want the paragraphs which
  38. appear within an @table, @ftable, or @itemize environment to have
  39. standard paragraph indentation. Without this, such paragraphs have
  40. no starting indentation. */
  41. /* #define INDENT_PARAGRAPHS_IN_TABLE */
  42. /* Define PARAGRAPH_START_INDENT to be the amount of indentation that
  43. the first lines of paragraphs receive by default, where no other
  44. value has been specified. Users can change this value on the command
  45. line, with the --paragraph-indent option, or within the texinfo file,
  46. with the @paragraphindent command. */
  47. #define PARAGRAPH_START_INDENT 3
  48. /* Define DEFAULT_PARAGRAPH_SPACING as the number of blank lines that you
  49. wish to appear between paragraphs. A value of 1 creates a single blank
  50. line between paragraphs. Paragraphs are defined by 2 or more consecutive
  51. newlines in the input file (i.e., one or more blank lines). */
  52. #define DEFAULT_PARAGRAPH_SPACING 1
  53. /* Global variables. */
  54. /* The output file name. */
  55. char *output_filename = NULL;
  56. /* Name of the output file that the user elected to pass on the command line.
  57. Such a name overrides any name found with the @setfilename command. */
  58. char *command_output_filename = NULL;
  59. static char *save_command_output_filename = NULL;
  60. /* The amount of indentation to add at the starts of paragraphs.
  61. 0 means don't change existing indentation at paragraph starts.
  62. > 0 is amount to indent new paragraphs by.
  63. < 0 means indent to column zero by removing indentation if necessary.
  64. This is normally zero, but some people prefer paragraph starts to be
  65. somewhat more indented than paragraph bodies. A pretty value for
  66. this is 3. */
  67. int paragraph_start_indent = PARAGRAPH_START_INDENT;
  68. /* Indentation that is pending insertion. We have this for hacking lines
  69. which look blank, but contain whitespace. We want to treat those as
  70. blank lines. */
  71. int pending_indent = 0;
  72. /* The index in our internal command table of the currently
  73. executing command. */
  74. int command_index;
  75. /* A search string which is used to find the first @setfilename. */
  76. char setfilename_search[] =
  77. { COMMAND_PREFIX,
  78. 's', 'e', 't', 'f', 'i', 'l', 'e', 'n', 'a', 'm', 'e', 0 };
  79. /* Values for calling handle_variable_internal (). */
  80. #define SET 1
  81. #define CLEAR 2
  82. #define IFSET 3
  83. #define IFCLEAR 4
  84. /* Flags controlling the operation of the program. */
  85. /* Default is to remove output if there were errors. */
  86. int force = 0;
  87. /* Default is to notify users of bad choices. */
  88. int print_warnings = 1;
  89. /* Number of errors that we tolerate on a given fileset. */
  90. int max_error_level = 100;
  91. /* The actual last inserted character. Note that this may be something
  92. other than NEWLINE even if last_char_was_newline is 1. */
  93. int last_inserted_character = 0;
  94. /* Nonzero means that a newline character has already been
  95. inserted, so close_paragraph () should insert one less. */
  96. int line_already_broken = 0;
  97. /* When nonzero we have finished an insertion (see end_insertion ()) and we
  98. want to ignore false continued paragraph closings. */
  99. int insertion_paragraph_closed = 0;
  100. /* Nonzero means attempt to make all of the lines have fill_column width. */
  101. int do_justification = 0;
  102. /* Nonzero means don't replace whitespace with &nbsp; in HTML mode. */
  103. int in_html_elt = 0;
  104. /* Nonzero means we are inserting a block level HTML element that must not be
  105. enclosed in a <p>, such as <ul>, <ol> and <h?>. */
  106. int in_html_block_level_elt = 0;
  107. /* True when expanding a macro definition. */
  108. static int executing_macro = 0;
  109. /* True when we are inside a <li> block of a menu. */
  110. static int in_menu_item = 0;
  111. /* The column position of output_paragraph[0]. This is not saved and restored
  112. in the multitable code because flush_output () is only called in environment
  113. 0. */
  114. static int output_paragraph_start_column = 0;
  115. typedef struct brace_element
  116. {
  117. struct brace_element *next;
  118. COMMAND_FUNCTION *proc;
  119. char *command;
  120. int pos, line;
  121. int in_fixed_width_font;
  122. } BRACE_ELEMENT;
  123. BRACE_ELEMENT *brace_stack = NULL;
  124. static void convert_from_file (char *name);
  125. static void convert_from_loaded_file (char *name);
  126. static void convert_from_stream (FILE *stream, char *name);
  127. static void do_flush_right_indentation (void);
  128. static void handle_variable (int action);
  129. static void handle_variable_internal (int action, char *name);
  130. static void init_brace_stack (void);
  131. static void init_internals (void);
  132. static void pop_and_call_brace (void);
  133. static void remember_brace (COMMAND_FUNCTION (*proc));
  134. static void write_trailer (char *filename, char *trailer);
  135. static int end_of_sentence_p (void);
  136. void maybe_update_execution_strings (char **text, unsigned int new_len);
  137. /* Error handling. */
  138. /* Number of errors encountered. */
  139. int errors_printed = 0;
  140. /* Remember that an error has been printed. If more than
  141. max_error_level have been printed, then exit the program. */
  142. static void
  143. remember_error (void)
  144. {
  145. errors_printed++;
  146. if (max_error_level && (errors_printed > max_error_level))
  147. {
  148. fprintf (stderr, _("Too many errors! Gave up.\n"));
  149. flush_file_stack ();
  150. if (errors_printed - max_error_level < 2)
  151. cm_bye ();
  152. xexit (1);
  153. }
  154. }
  155. /* Print the last error gotten from the file system. */
  156. int
  157. fs_error (char *filename)
  158. {
  159. remember_error ();
  160. perror (filename);
  161. return 0;
  162. }
  163. /* Print an error message, and return false. */
  164. void
  165. error (const char *format, ...)
  166. {
  167. va_list ap;
  168. remember_error ();
  169. va_start (ap, format);
  170. vfprintf (stderr, format, ap);
  171. va_end (ap);
  172. putc ('\n', stderr);
  173. }
  174. /* Just like error (), but print the input file and line number as well. */
  175. void
  176. file_line_error (char *infile, int lno, const char *format, ...)
  177. {
  178. va_list ap;
  179. remember_error ();
  180. fprintf (stderr, "%s:%d: ", infile, lno);
  181. va_start (ap, format);
  182. vfprintf (stderr, format, ap);
  183. va_end (ap);
  184. fprintf (stderr, ".\n");
  185. }
  186. /* Just like file_line_error (), but take the input file and the line
  187. number from global variables. */
  188. void
  189. line_error (const char *format, ...)
  190. {
  191. va_list ap;
  192. remember_error ();
  193. fprintf (stderr, "%s:%d: ", input_filename, line_number);
  194. va_start (ap, format);
  195. vfprintf (stderr, format, ap);
  196. va_end (ap);
  197. fprintf (stderr, ".\n");
  198. }
  199. void
  200. warning (const char *format, ...)
  201. {
  202. va_list ap;
  203. if (print_warnings)
  204. {
  205. fprintf (stderr, _("%s:%d: warning: "), input_filename, line_number);
  206. va_start (ap, format);
  207. vfprintf (stderr, format, ap);
  208. va_end (ap);
  209. fprintf (stderr, ".\n");
  210. }
  211. }
  212. /* The other side of a malformed expression. */
  213. static void
  214. misplaced_brace (void)
  215. {
  216. line_error (_("Misplaced %c"), '}');
  217. }
  218. /* Main. */
  219. /* Display the version info of this invocation of Makeinfo. */
  220. static void
  221. print_version_info (void)
  222. {
  223. printf ("makeinfo (GNU %s) %s\n", PACKAGE, VERSION);
  224. }
  225. /* If EXIT_VALUE is zero, print the full usage message to stdout.
  226. Otherwise, just say to use --help for more info.
  227. Then exit with EXIT_VALUE. */
  228. static void
  229. usage (int exit_value)
  230. {
  231. if (exit_value != 0)
  232. fprintf (stderr, _("Try `%s --help' for more information.\n"), progname);
  233. else
  234. {
  235. printf (_("Usage: %s [OPTION]... TEXINFO-FILE...\n"), progname);
  236. puts ("");
  237. puts (_("\
  238. Translate Texinfo source documentation to various other formats, by default\n\
  239. Info files suitable for reading online with Emacs or standalone GNU Info.\n"));
  240. printf (_("\
  241. General options:\n\
  242. --error-limit=NUM quit after NUM errors (default %d).\n\
  243. --document-language=STR locale to use in translating Texinfo keywords\n\
  244. for the output document (default C).\n\
  245. --force preserve output even if errors.\n\
  246. --help display this help and exit.\n\
  247. --no-validate suppress node cross-reference validation.\n\
  248. --no-warn suppress warnings (but not errors).\n\
  249. -v, --verbose explain what is being done.\n\
  250. --version display version information and exit.\n"),
  251. max_error_level);
  252. puts ("");
  253. /* xgettext: no-wrap */
  254. puts (_("\
  255. Output format selection (default is to produce Info):\n\
  256. --docbook output Docbook XML rather than Info.\n\
  257. --html output HTML rather than Info.\n\
  258. --xml output Texinfo XML rather than Info.\n\
  259. --plaintext output plain text rather than Info.\n\
  260. "));
  261. puts (_("\
  262. General output options:\n\
  263. -E, --macro-expand=FILE output macro-expanded source to FILE,\n\
  264. ignoring any @setfilename.\n\
  265. --no-headers suppress node separators, Node: lines, and menus\n\
  266. from Info output (thus producing plain text)\n\
  267. or from HTML (thus producing shorter output);\n\
  268. also, write to standard output by default.\n\
  269. --no-split suppress the splitting of Info or HTML output,\n\
  270. generate only one output file.\n\
  271. --number-sections output chapter and sectioning numbers.\n\
  272. -o, --output=FILE output to FILE (or directory if split HTML).\n\
  273. "));
  274. printf (_("\
  275. Options for Info and plain text:\n\
  276. --disable-encoding do not output accented and special characters\n\
  277. in Info output based on @documentencoding.\n\
  278. --enable-encoding override --disable-encoding (default).\n\
  279. --fill-column=NUM break Info lines at NUM characters (default %d).\n\
  280. --footnote-style=STYLE output footnotes in Info according to STYLE:\n\
  281. `separate' to put them in their own node;\n\
  282. `end' to put them at the end of the node, in\n\
  283. which they are defined (this is the default).\n\
  284. --paragraph-indent=VAL indent Info paragraphs by VAL spaces (default %d).\n\
  285. If VAL is `none', do not indent; if VAL is\n\
  286. `asis', preserve existing indentation.\n\
  287. --split-size=NUM split Info files at size NUM (default %d).\n"),
  288. fill_column, paragraph_start_indent,
  289. DEFAULT_SPLIT_SIZE);
  290. puts ("");
  291. puts (_("\
  292. Options for HTML:\n\
  293. --css-include=FILE include FILE in HTML <style> output;\n\
  294. read stdin if FILE is -.\n\
  295. --css-ref=URL generate reference to a CSS file.\n\
  296. --internal-links=FILE produce list of internal links in FILE.\n\
  297. --transliterate-file-names\n\
  298. produce file names in ASCII transliteration.\n\
  299. "));
  300. printf (_("\
  301. Options for XML and Docbook:\n\
  302. --output-indent=VAL indent XML elements by VAL spaces (default %d).\n\
  303. If VAL is 0, ignorable whitespace is dropped.\n\
  304. "), xml_indentation_increment);
  305. puts ("");
  306. puts (_("\
  307. Input file options:\n\
  308. --commands-in-node-names allow @ commands in node names.\n\
  309. -D VAR define the variable VAR, as with @set.\n\
  310. -I DIR append DIR to the @include search path.\n\
  311. -P DIR prepend DIR to the @include search path.\n\
  312. -U VAR undefine the variable VAR, as with @clear.\n\
  313. "));
  314. puts (_("\
  315. Conditional processing in input:\n\
  316. --ifdocbook process @ifdocbook and @docbook even if\n\
  317. not generating Docbook.\n\
  318. --ifhtml process @ifhtml and @html even if not generating HTML.\n\
  319. --ifinfo process @ifinfo even if not generating Info.\n\
  320. --ifplaintext process @ifplaintext even if not generating plain text.\n\
  321. --iftex process @iftex and @tex; implies --no-split.\n\
  322. --ifxml process @ifxml and @xml.\n\
  323. --no-ifdocbook do not process @ifdocbook and @docbook text.\n\
  324. --no-ifhtml do not process @ifhtml and @html text.\n\
  325. --no-ifinfo do not process @ifinfo text.\n\
  326. --no-ifplaintext do not process @ifplaintext text.\n\
  327. --no-iftex do not process @iftex and @tex text.\n\
  328. --no-ifxml do not process @ifxml and @xml text.\n\
  329. \n\
  330. Also, for the --no-ifFORMAT options, do process @ifnotFORMAT text.\n\
  331. "));
  332. puts (_("\
  333. The defaults for the @if... conditionals depend on the output format:\n\
  334. if generating HTML, --ifhtml is on and the others are off;\n\
  335. if generating Info, --ifinfo is on and the others are off;\n\
  336. if generating plain text, --ifplaintext is on and the others are off;\n\
  337. if generating XML, --ifxml is on and the others are off.\n\
  338. "));
  339. fputs (_("\
  340. Examples:\n\
  341. makeinfo foo.texi write Info to foo's @setfilename\n\
  342. makeinfo --html foo.texi write HTML to @setfilename\n\
  343. makeinfo --xml foo.texi write Texinfo XML to @setfilename\n\
  344. makeinfo --docbook foo.texi write DocBook XML to @setfilename\n\
  345. makeinfo --no-headers foo.texi write plain text to standard output\n\
  346. \n\
  347. makeinfo --html --no-headers foo.texi write html without node lines, menus\n\
  348. makeinfo --number-sections foo.texi write Info with numbered sections\n\
  349. makeinfo --no-split foo.texi write one Info file however big\n\
  350. "), stdout);
  351. puts (_("\n\
  352. Email bug reports to bug-texinfo@gnu.org,\n\
  353. general questions and discussion to help-texinfo@gnu.org.\n\
  354. Texinfo home page: http://www.gnu.org/software/texinfo/"));
  355. } /* end of full help */
  356. xexit (exit_value);
  357. }
  358. #define OPT_CSSREF 256
  359. #define OPT_TRANSLITERATE_FILE_NAMES 257
  360. #define OPT_INTERNAL_LINKS 258
  361. struct option long_options[] =
  362. {
  363. { "commands-in-node-names", 0, &expensive_validation, 1 },
  364. { "css-include", 1, 0, 'C' },
  365. { "css-ref", 1, 0, OPT_CSSREF },
  366. { "docbook", 0, 0, 'd' },
  367. { "disable-encoding", 0, &enable_encoding, 0 },
  368. { "enable-encoding", 0, &enable_encoding, 1 },
  369. { "document-language", 1, 0, 'l' },
  370. { "error-limit", 1, 0, 'e' },
  371. { "fill-column", 1, 0, 'f' },
  372. { "footnote-style", 1, 0, 's' },
  373. { "force", 0, &force, 1 },
  374. { "help", 0, 0, 'h' },
  375. { "html", 0, 0, 'w' },
  376. { "ifdocbook", 0, &process_docbook, 1 },
  377. { "ifhtml", 0, &process_html, 1 },
  378. { "ifinfo", 0, &process_info, 1 },
  379. { "ifplaintext", 0, &process_plaintext, 1 },
  380. { "iftex", 0, &process_tex, 1 },
  381. { "ifxml", 0, &process_xml, 1 },
  382. { "internal-links", 1, 0, OPT_INTERNAL_LINKS },
  383. { "macro-expand", 1, 0, 'E' },
  384. { "no-headers", 0, &no_headers, 1 },
  385. { "no-ifdocbook", 0, &process_docbook, 0 },
  386. { "no-ifhtml", 0, &process_html, 0 },
  387. { "no-ifinfo", 0, &process_info, 0 },
  388. { "no-ifplaintext", 0, &process_plaintext, 0 },
  389. { "no-iftex", 0, &process_tex, 0 },
  390. { "no-ifxml", 0, &process_xml, 0 },
  391. { "no-number-footnotes", 0, &number_footnotes, 0 },
  392. { "no-number-sections", 0, &number_sections, 0 },
  393. { "no-pointer-validate", 0, &validating, 0 },
  394. { "no-split", 0, &splitting, 0 },
  395. { "no-validate", 0, &validating, 0 },
  396. { "no-warn", 0, &print_warnings, 0 },
  397. { "number-footnotes", 0, &number_footnotes, 1 },
  398. { "number-sections", 0, &number_sections, 1 },
  399. { "output", 1, 0, 'o' },
  400. { "output-indent", 1, 0, 'i' },
  401. { "paragraph-indent", 1, 0, 'p' },
  402. { "plaintext", 0, 0, 't' },
  403. { "reference-limit", 1, 0, 'r' },
  404. { "split-size", 1, 0, 'S'},
  405. { "transliterate-file-names", 0, &transliterate_file_names, 1 },
  406. { "verbose", 0, &verbose_mode, 1 },
  407. { "version", 0, 0, 'V' },
  408. { "xml", 0, 0, 'x' },
  409. {NULL, 0, NULL, 0}
  410. };
  411. /* We use handle_variable_internal for -D and -U, and it depends on
  412. execute_string, which depends on input_filename, which is not defined
  413. while we are handling options. :-\ So we save these defines in this
  414. struct, and handle them later. */
  415. typedef struct command_line_define
  416. {
  417. struct command_line_define *next;
  418. int action;
  419. char *define;
  420. } COMMAND_LINE_DEFINE;
  421. static COMMAND_LINE_DEFINE *command_line_defines = NULL;
  422. /* For each file mentioned in the command line, process it, turning
  423. Texinfo commands into wonderfully formatted output text. */
  424. int
  425. main (int argc, char *argv[])
  426. {
  427. int c, ind;
  428. int reading_from_stdin = 0;
  429. #ifdef HAVE_SETLOCALE
  430. /* Do not use LC_ALL, because LC_NUMERIC screws up the scanf parsing
  431. of the argument to @multicolumn. */
  432. setlocale (LC_TIME, "");
  433. #ifdef LC_MESSAGES /* ultrix, djgpp 2.04 */
  434. setlocale (LC_MESSAGES, "");
  435. #endif
  436. setlocale (LC_CTYPE, "");
  437. setlocale (LC_COLLATE, "");
  438. #endif
  439. #ifdef ENABLE_NLS
  440. /* Set the text message domain. */
  441. bindtextdomain (PACKAGE, LOCALEDIR);
  442. textdomain (PACKAGE);
  443. #endif
  444. /* If TEXINFO_OUTPUT_FORMAT envvar is set, use it to set default output.
  445. Can be overridden with one of the output options. */
  446. if (getenv ("TEXINFO_OUTPUT_FORMAT") != NULL)
  447. {
  448. if (STREQ (getenv ("TEXINFO_OUTPUT_FORMAT"), "docbook"))
  449. {
  450. splitting = 0;
  451. html = 0;
  452. docbook = 1;
  453. xml = 1;
  454. process_docbook = 1;
  455. }
  456. else if (STREQ (getenv ("TEXINFO_OUTPUT_FORMAT"), "html"))
  457. {
  458. html = 1;
  459. docbook = 0;
  460. xml = 0;
  461. process_html = 1;
  462. }
  463. else if (STREQ (getenv ("TEXINFO_OUTPUT_FORMAT"), "info"))
  464. {
  465. html = 0;
  466. docbook = 0;
  467. xml = 0;
  468. }
  469. else if (STREQ (getenv ("TEXINFO_OUTPUT_FORMAT"), "plaintext"))
  470. {
  471. splitting = 0;
  472. no_headers = 1;
  473. html = 0;
  474. docbook = 0;
  475. xml = 0;
  476. process_plaintext = 1;
  477. }
  478. else if (STREQ (getenv ("TEXINFO_OUTPUT_FORMAT"), "xml"))
  479. {
  480. splitting = 0;
  481. html = 0;
  482. docbook = 0;
  483. xml = 1;
  484. process_xml = 1;
  485. }
  486. else
  487. fprintf (stderr,
  488. _("%s: Ignoring unrecognized TEXINFO_OUTPUT_FORMAT value `%s'.\n"),
  489. progname, getenv ("TEXINFO_OUTPUT_FORMAT"));
  490. }
  491. /* Parse argument flags from the input line. */
  492. while ((c = getopt_long (argc, argv, "D:de:E:f:hI:i:o:p:P:r:s:t:U:vV:wx",
  493. long_options, &ind)) != EOF)
  494. {
  495. if (c == 0 && long_options[ind].flag == 0)
  496. c = long_options[ind].val;
  497. switch (c)
  498. {
  499. case 'C': /* --css-include */
  500. css_include = xstrdup (optarg);
  501. break;
  502. case OPT_CSSREF:
  503. css_ref = xstrdup (optarg);
  504. break;
  505. case 'D':
  506. case 'U':
  507. /* User specified variable to set or clear. */
  508. if (xml && !docbook)
  509. {
  510. COMMAND_LINE_DEFINE *new = xmalloc (sizeof (COMMAND_LINE_DEFINE));
  511. new->action = (c == 'D') ? SET : CLEAR;
  512. new->define = xstrdup (optarg);
  513. new->next = command_line_defines;
  514. command_line_defines = new;
  515. }
  516. else
  517. handle_variable_internal ((c == 'D' ? SET : CLEAR), optarg);
  518. break;
  519. case 'd': /* --docbook */
  520. splitting = 0;
  521. xml = 1;
  522. docbook = 1;
  523. html = 0;
  524. process_docbook = 1;
  525. break;
  526. case 'e': /* --error-limit */
  527. if (sscanf (optarg, "%d", &max_error_level) != 1)
  528. {
  529. fprintf (stderr,
  530. _("%s: %s arg must be numeric, not `%s'.\n"),
  531. progname, "--error-limit", optarg);
  532. usage (1);
  533. }
  534. break;
  535. case 'E': /* --macro-expand */
  536. if (!macro_expansion_output_stream)
  537. {
  538. macro_expansion_filename = optarg;
  539. macro_expansion_output_stream
  540. = strcmp (optarg, "-") == 0 ? stdout : fopen (optarg, "w");
  541. if (!macro_expansion_output_stream)
  542. error (_("%s: could not open macro expansion output `%s'"),
  543. progname, optarg);
  544. }
  545. else
  546. fprintf (stderr,
  547. _("%s: ignoring second macro expansion output `%s'.\n"),
  548. progname, optarg);
  549. break;
  550. case 'f': /* --fill-column */
  551. if (sscanf (optarg, "%d", &fill_column) != 1)
  552. {
  553. fprintf (stderr,
  554. _("%s: %s arg must be numeric, not `%s'.\n"),
  555. progname, "--fill-column", optarg);
  556. usage (1);
  557. }
  558. break;
  559. case 'h': /* --help */
  560. usage (0);
  561. break;
  562. case 'I':
  563. /* Append user-specified dir to include file path. */
  564. append_to_include_path (optarg);
  565. break;
  566. case 'l':
  567. /* save the override language code */
  568. document_language = xstrdup (optarg);
  569. break;
  570. case 'i':
  571. if (sscanf (optarg, "%d", &xml_indentation_increment) != 1)
  572. {
  573. fprintf (stderr,
  574. _("%s: %s arg must be numeric, not `%s'.\n"),
  575. progname, "--output-indent", optarg);
  576. usage (1);
  577. }
  578. break;
  579. case OPT_INTERNAL_LINKS:
  580. if (!internal_links_stream)
  581. {
  582. internal_links_filename = xstrdup (optarg);
  583. internal_links_stream = strcmp (optarg, "-") == 0 ? stdout :
  584. fopen (optarg, "w");
  585. if (!internal_links_stream)
  586. error (_("%s: could not open internal links output `%s'"),
  587. progname, optarg);
  588. }
  589. else
  590. fprintf (stderr,
  591. _("%s: ignoring second internal links output `%s'.\n"),
  592. progname, optarg);
  593. break;
  594. case 'o': /* --output */
  595. command_output_filename = xstrdup (optarg);
  596. save_command_output_filename = command_output_filename;
  597. break;
  598. case 'p': /* --paragraph-indent */
  599. if (set_paragraph_indent (optarg) < 0)
  600. {
  601. fprintf (stderr,
  602. _("%s: --paragraph-indent arg must be numeric/`none'/`asis', not `%s'.\n"),
  603. progname, optarg);
  604. usage (1);
  605. }
  606. break;
  607. case 'P':
  608. /* Prepend user-specified include dir to include path. */
  609. prepend_to_include_path (optarg);
  610. break;
  611. case 'r': /* was --reference-limit; obsolete, ignore */
  612. break;
  613. case 's': /* --footnote-style */
  614. if (set_footnote_style (optarg) < 0)
  615. {
  616. fprintf (stderr,
  617. _("%s: --footnote-style arg must be `separate' or `end', not `%s'.\n"),
  618. progname, optarg);
  619. usage (1);
  620. }
  621. footnote_style_preset = 1;
  622. break;
  623. case 'S': /* --split-size */
  624. if (sscanf (optarg, "%d", &split_size) != 1)
  625. {
  626. fprintf (stderr,
  627. _("%s: %s arg must be numeric, not `%s'.\n"),
  628. progname, "--split-size", optarg);
  629. usage (1);
  630. }
  631. break;
  632. case 't': /* --plaintext */
  633. splitting = 0;
  634. no_headers = 1;
  635. html = 0;
  636. docbook = 0;
  637. xml = 0;
  638. process_plaintext = 1;
  639. break;
  640. case 'v':
  641. verbose_mode++;
  642. break;
  643. case 'V': /* --version */
  644. print_version_info ();
  645. puts ("");
  646. printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
  647. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
  648. This is free software: you are free to change and redistribute it.\n\
  649. There is NO WARRANTY, to the extent permitted by law.\n"),
  650. "2008");
  651. xexit (0);
  652. break;
  653. case 'w': /* --html */
  654. xml = 0;
  655. docbook = 0;
  656. html = 1;
  657. process_html = 1;
  658. break;
  659. case 'x': /* --xml */
  660. splitting = 0;
  661. html = 0;
  662. docbook = 0;
  663. xml = 1;
  664. process_xml = 1;
  665. break;
  666. case '?':
  667. usage (1);
  668. break;
  669. }
  670. }
  671. if (macro_expansion_output_stream)
  672. validating = 0;
  673. if (!validating)
  674. expensive_validation = 0;
  675. if (optind == argc)
  676. {
  677. /* Check to see if input is a file. If so, process that. */
  678. if (!isatty (fileno (stdin)))
  679. reading_from_stdin = 1;
  680. else
  681. {
  682. fprintf (stderr, _("%s: missing file argument.\n"), progname);
  683. usage (1);
  684. }
  685. }
  686. if (no_headers)
  687. {
  688. /* If the user did not specify an output file, use stdout. */
  689. if (!command_output_filename)
  690. command_output_filename = xstrdup ("-");
  691. if (html && splitting && !STREQ (command_output_filename, "-"))
  692. { /* --no-headers --no-split --html indicates confusion. */
  693. fprintf (stderr,
  694. "%s: can't split --html output to `%s' with --no-headers.\n",
  695. progname, command_output_filename);
  696. usage (1);
  697. }
  698. /* --no-headers implies --no-split. */
  699. splitting = 0;
  700. }
  701. if (process_info == -1)
  702. { /* no explicit --[no-]ifinfo option, so we'll do @ifinfo
  703. if we're generating info or (for compatibility) plain text. */
  704. process_info = !html && !xml;
  705. }
  706. if (process_plaintext == -1)
  707. { /* no explicit --[no-]ifplaintext option, so we'll do @ifplaintext
  708. if we're generating plain text. */
  709. process_plaintext = no_headers && !html && !xml;
  710. }
  711. if (verbose_mode)
  712. print_version_info ();
  713. /* Remaining arguments are file names of texinfo files.
  714. Convert them, one by one. */
  715. if (!reading_from_stdin)
  716. {
  717. while (optind != argc)
  718. {
  719. if (STREQ (argv[optind], "-"))
  720. convert_from_stream (stdin, "stdin");
  721. else
  722. convert_from_file (argv[optind]);
  723. optind++;
  724. }
  725. }
  726. else
  727. convert_from_stream (stdin, "stdin");
  728. xexit (errors_printed ? 2 : 0);
  729. return 0; /* Avoid bogus warnings. */
  730. }
  731. /* Hacking tokens and strings. */
  732. /* Return the next token as a string pointer. We cons the string. This
  733. `token' means simply a command name. */
  734. /* = is so @alias works. ^ and _ are so macros can be used in math mode
  735. without a space following. Possibly we should simply allow alpha, to
  736. be compatible with TeX. */
  737. #define COMMAND_CHAR(c) (!cr_or_whitespace(c) \
  738. && (c) != '{' \
  739. && (c) != '}' \
  740. && (c) != '=' \
  741. && (c) != '_' \
  742. && (c) != '^' \
  743. )
  744. static char *
  745. read_token (void)
  746. {
  747. int i, character;
  748. char *result;
  749. /* If the first character to be read is self-delimiting, then that
  750. is the command itself. */
  751. character = curchar ();
  752. if (self_delimiting (character))
  753. {
  754. input_text_offset++;
  755. if (character == '\n')
  756. line_number++;
  757. result = xstrdup (" ");
  758. *result = character;
  759. return result;
  760. }
  761. for (i = 0; ((input_text_offset != input_text_length)
  762. && (character = curchar ())
  763. && COMMAND_CHAR (character));
  764. i++, input_text_offset++);
  765. result = xmalloc (i + 1);
  766. memcpy (result, &input_text[input_text_offset - i], i);
  767. result[i] = 0;
  768. return result;
  769. }
  770. /* Return nonzero if CHARACTER is self-delimiting. */
  771. int
  772. self_delimiting (int character)
  773. {
  774. /* @; and @\ are not Texinfo commands, but they are listed here
  775. anyway. I don't know why. --karl, 10aug96. */
  776. return strchr ("~{|}`^\\@?=;:./-,*\'\" !\n\t", character) != NULL;
  777. }
  778. /* Clear whitespace from the front and end of string. */
  779. void
  780. canon_white (char *string)
  781. {
  782. char *p = string;
  783. unsigned len;
  784. if (!*p)
  785. return;
  786. do
  787. {
  788. if (!cr_or_whitespace (*p))
  789. break;
  790. ++p;
  791. }
  792. while (*p);
  793. len = strlen (p);
  794. while (len && cr_or_whitespace (p[len-1]))
  795. --len;
  796. if (p != string)
  797. memmove (string, p, len);
  798. string[len] = 0;
  799. }
  800. /* Bash STRING, replacing all whitespace with just one space. */
  801. void
  802. fix_whitespace (char *string)
  803. {
  804. char *temp = xmalloc (strlen (string) + 1);
  805. int string_index = 0;
  806. int temp_index = 0;
  807. int c;
  808. canon_white (string);
  809. while (string[string_index])
  810. {
  811. c = temp[temp_index++] = string[string_index++];
  812. if (c == ' ' || c == '\n' || c == '\t')
  813. {
  814. temp[temp_index - 1] = ' ';
  815. while ((c = string[string_index]) && (c == ' ' ||
  816. c == '\t' ||
  817. c == '\n'))
  818. string_index++;
  819. }
  820. }
  821. temp[temp_index] = 0;
  822. strcpy (string, temp);
  823. free (temp);
  824. }
  825. /* Discard text until the desired string is found. The string is
  826. included in the discarded text. */
  827. void
  828. discard_until (char *string)
  829. {
  830. int temp = search_forward (string, input_text_offset);
  831. int tt = (temp < 0) ? input_text_length : temp + strlen (string);
  832. int from = input_text_offset;
  833. /* Find out what line we are on. */
  834. while (from != tt)
  835. if (input_text[from++] == '\n')
  836. line_number++;
  837. if (temp < 0)
  838. {
  839. /* not found, move current position to end of string */
  840. input_text_offset = input_text_length;
  841. if (strcmp (string, "\n") != 0)
  842. { /* Give a more descriptive feedback, if we are looking for ``@end ''
  843. during macro execution. That means someone used a multiline
  844. command as an argument to, say, @section ... style commands. */
  845. char *end_block = xmalloc (8);
  846. sprintf (end_block, "\n%cend ", COMMAND_PREFIX);
  847. if (executing_string && strstr (string, end_block))
  848. line_error (_("Multiline command %c%s used improperly"),
  849. COMMAND_PREFIX, command);
  850. else
  851. line_error (_("Expected `%s'"), string);
  852. free (end_block);
  853. return;
  854. }
  855. }
  856. else
  857. /* found, move current position to after the found string */
  858. input_text_offset = temp + strlen (string);
  859. }
  860. /* Read characters from the file until we are at MATCH.
  861. Place the characters read into STRING.
  862. On exit input_text_offset is after the match string.
  863. Return the offset where the string starts. */
  864. int
  865. get_until (char *match, char **string)
  866. {
  867. int len, current_point, x, new_point, tem;
  868. current_point = x = input_text_offset;
  869. new_point = search_forward (match, input_text_offset);
  870. if (new_point < 0)
  871. new_point = input_text_length;
  872. len = new_point - current_point;
  873. /* Keep track of which line number we are at. */
  874. tem = new_point + (strlen (match) - 1);
  875. while (x != tem)
  876. if (input_text[x++] == '\n')
  877. line_number++;
  878. *string = xmalloc (len + 1);
  879. memcpy (*string, &input_text[current_point], len);
  880. (*string)[len] = 0;
  881. /* Now leave input_text_offset in a consistent state. */
  882. input_text_offset = tem;
  883. if (input_text_offset > input_text_length)
  884. input_text_offset = input_text_length;
  885. return new_point;
  886. }
  887. /* Replace input_text[FROM .. TO] with its expansion. */
  888. void
  889. replace_with_expansion (int from, int *to)
  890. {
  891. char *xp;
  892. unsigned xp_len, new_len;
  893. char *old_input = input_text;
  894. unsigned raw_len = *to - from;
  895. char *str;
  896. /* The rest of the code here moves large buffers, so let's
  897. not waste time if the input cannot possibly expand
  898. into anything. Unfortunately, we cannot avoid expansion
  899. when we see things like @code etc., even if they only
  900. asked for expansion of macros, since any Texinfo command
  901. can be potentially redefined with a macro. */
  902. if (only_macro_expansion &&
  903. memchr (input_text + from, COMMAND_PREFIX, raw_len) == 0)
  904. return;
  905. /* Get original string from input. */
  906. str = xmalloc (raw_len + 1);
  907. memcpy (str, input_text + from, raw_len);
  908. str[raw_len] = 0;
  909. /* We are going to relocate input_text, so we had better output
  910. pending portion of input_text now, before the pointer changes. */
  911. if (macro_expansion_output_stream && !executing_string
  912. && !me_inhibit_expansion)
  913. append_to_expansion_output (from);
  914. /* Expand it. */
  915. xp = expansion (str, 0);
  916. xp_len = strlen (xp);
  917. free (str);
  918. /* Plunk the expansion into the middle of `input_text' --
  919. which is terminated by a newline, not a null. Avoid
  920. expensive move of the rest of the input if the expansion
  921. has the same length as the original string. */
  922. if (xp_len != raw_len)
  923. {
  924. new_len = from + xp_len + input_text_length - *to + 1;
  925. if (executing_string)
  926. { /* If we are in execute_string, we might need to update
  927. the relevant element in the execution_strings[] array,
  928. since it could have to be relocated from under our
  929. feet. (input_text is reallocated here as well, if needed.) */
  930. maybe_update_execution_strings (&input_text, new_len);
  931. }
  932. else if (new_len > input_text_length + 1)
  933. /* Don't bother to realloc if we have enough space. */
  934. input_text = xrealloc (input_text, new_len);
  935. memmove (input_text + from + xp_len,
  936. input_text + *to, input_text_length - *to + 1);
  937. *to += xp_len - raw_len;
  938. /* Since we change input_text_length here, the comparison above
  939. isn't really valid, but it seems the worst that might happen is
  940. an extra xrealloc or two, so let's not worry. */
  941. input_text_length += xp_len - raw_len;
  942. }
  943. memcpy (input_text + from, xp, xp_len);
  944. free (xp);
  945. /* Synchronize the macro-expansion pointers with our new input_text. */
  946. if (input_text != old_input)
  947. forget_itext (old_input);
  948. if (macro_expansion_output_stream && !executing_string)
  949. remember_itext (input_text, from);
  950. }
  951. /* Read characters from the file until we are at MATCH or end of line.
  952. Place the characters read into STRING. If EXPAND is nonzero,
  953. expand the text before looking for MATCH for those cases where
  954. MATCH might be produced by some macro. */
  955. void
  956. get_until_in_line (int expand, char *match, char **string)
  957. {
  958. int real_bottom = input_text_length;
  959. int limit = search_forward ("\n", input_text_offset);
  960. if (limit < 0)
  961. limit = input_text_length;
  962. /* Replace input_text[input_text_offset .. limit-1] with its expansion.
  963. This allows the node names and menu entries themselves to be
  964. constructed via a macro, as in:
  965. @macro foo{p, q}
  966. Together: \p\ & \q\.
  967. @end macro
  968. @node @foo{A,B}, next, prev, top
  969. Otherwise, the `,' separating the macro args A and B is taken as
  970. the node argument separator, so the node name is `@foo{A'. This
  971. expansion is only necessary on the first call, since we expand the
  972. whole line then. */
  973. if (expand)
  974. {
  975. replace_with_expansion (input_text_offset, &limit);
  976. }
  977. real_bottom = input_text_length;
  978. input_text_length = limit;
  979. get_until (match, string);
  980. input_text_length = real_bottom;
  981. }
  982. void
  983. get_rest_of_line (int expand, char **string)
  984. {
  985. xml_no_para ++;
  986. if (expand)
  987. {
  988. char *tem;
  989. /* Don't expand non-macros in input, since we want them
  990. intact in the macro-expanded output. */
  991. only_macro_expansion++;
  992. get_until_in_line (1, "\n", &tem);
  993. only_macro_expansion--;
  994. *string = expansion (tem, 0);
  995. free (tem);
  996. }
  997. else
  998. get_until_in_line (0, "\n", string);
  999. canon_white (*string);
  1000. if (curchar () == '\n') /* as opposed to the end of the file... */
  1001. {
  1002. line_number++;
  1003. input_text_offset++;
  1004. }
  1005. xml_no_para --;
  1006. }
  1007. /* Backup the input pointer to the previous character, keeping track
  1008. of the current line number. */
  1009. void
  1010. backup_input_pointer (void)
  1011. {
  1012. if (input_text_offset)
  1013. {
  1014. input_text_offset--;
  1015. if (curchar () == '\n')
  1016. line_number--;
  1017. }
  1018. }
  1019. /* Read characters from the file until we are at MATCH or closing brace.
  1020. Place the characters read into STRING. */
  1021. void
  1022. get_until_in_braces (char *match, char **string)
  1023. {
  1024. char *temp;
  1025. int i, brace = 0;
  1026. int match_len = strlen (match);
  1027. for (i = input_text_offset; i < input_text_length; i++)
  1028. {
  1029. if (i < input_text_length - 1 && input_text[i] == '@')
  1030. {
  1031. i++; /* skip commands like @, and @{ */
  1032. continue;
  1033. }
  1034. else if (input_text[i] == '{')
  1035. brace++;
  1036. else if (input_text[i] == '}')
  1037. {
  1038. brace--;
  1039. /* If looking for a brace, don't stop at the interior brace,
  1040. like after "baz" in "@foo{something @bar{baz} more}". */
  1041. if (brace == 0)
  1042. continue;
  1043. }
  1044. else if (input_text[i] == '\n')
  1045. line_number++;
  1046. if (brace < 0 ||
  1047. (brace == 0 && strncmp (input_text + i, match, match_len) == 0))
  1048. break;
  1049. }
  1050. match_len = i - input_text_offset;
  1051. temp = xmalloc (2 + match_len);
  1052. memcpy (temp, input_text + input_text_offset, match_len);
  1053. temp[match_len] = 0;
  1054. input_text_offset = i;
  1055. *string = temp;
  1056. }
  1057. /* Converting a file. */
  1058. /* Convert the file named by NAME. The output is saved on the file
  1059. named as the argument to the @setfilename command. */
  1060. static char *suffixes[] = {
  1061. /* ".txi" is checked first so that on 8+3 DOS filesystems, if they
  1062. have "texinfo.txi" and "texinfo.tex" in the same directory, the
  1063. former is used rather than the latter, due to file name truncation. */
  1064. ".txi",
  1065. ".texinfo",
  1066. ".texi",
  1067. ".txinfo",
  1068. "",
  1069. NULL
  1070. };
  1071. static void
  1072. initialize_conversion (void)
  1073. {
  1074. init_tag_table ();
  1075. init_indices ();
  1076. init_internals ();
  1077. init_paragraph ();
  1078. /* This is used for splitting the output file and for doing section
  1079. headings. It was previously initialized in `init_paragraph', but its
  1080. use there loses with the `init_paragraph' calls done by the
  1081. multitable code; the tag indices get reset to zero. */
  1082. output_position = 0;
  1083. }
  1084. /* Reverse the chain of structures in LIST. Output the new head
  1085. of the chain. You should always assign the output value of this
  1086. function to something, or you will lose the chain. */
  1087. GENERIC_LIST *
  1088. reverse_list (GENERIC_LIST *list)
  1089. {
  1090. GENERIC_LIST *next;
  1091. GENERIC_LIST *prev = NULL;
  1092. while (list)
  1093. {
  1094. next = list->next;
  1095. list->next = prev;
  1096. prev = list;
  1097. list = next;
  1098. }
  1099. return prev;
  1100. }
  1101. /* We read in multiples of 4k, simply because it is a typical pipe size
  1102. on unix systems. */
  1103. #define READ_BUFFER_GROWTH (4 * 4096)
  1104. /* Convert the Texinfo file coming from the open stream STREAM. Assume the
  1105. source of the stream is named NAME. */
  1106. static void
  1107. convert_from_stream (FILE *stream, char *name)
  1108. {
  1109. char *buffer = NULL;
  1110. int buffer_offset = 0, buffer_size = 0;
  1111. initialize_conversion ();
  1112. /* Read until the end of the stream. This isn't strictly correct, since
  1113. the texinfo input may end before the stream ends, but it is a quick
  1114. working heuristic. */
  1115. while (!feof (stream))
  1116. {
  1117. int count;
  1118. if (buffer_offset + (READ_BUFFER_GROWTH + 1) >= buffer_size)
  1119. buffer = (char *)
  1120. xrealloc (buffer, (buffer_size += READ_BUFFER_GROWTH));
  1121. count = fread (buffer + buffer_offset, 1, READ_BUFFER_GROWTH, stream);
  1122. if (count < 0)
  1123. {
  1124. perror (name);
  1125. xexit (1);
  1126. }
  1127. buffer_offset += count;
  1128. if (count == 0)
  1129. break;
  1130. }
  1131. /* Set the globals to the new file. */
  1132. input_text = buffer;
  1133. input_text_length = buffer_offset;
  1134. input_filename = xstrdup (name);
  1135. node_filename = xstrdup (name);
  1136. input_text_offset = 0;
  1137. line_number = 1;
  1138. /* Not strictly necessary. This magic prevents read_token () from doing
  1139. extra unnecessary work each time it is called (that is a lot of times).
  1140. The INPUT_TEXT_LENGTH is one past the actual end of the text. */
  1141. input_text[input_text_length] = '\n';
  1142. convert_from_loaded_file (name);
  1143. }
  1144. static void
  1145. convert_from_file (char *name)
  1146. {
  1147. int i;
  1148. char *filename = xmalloc (strlen (name) + 50);
  1149. /* Prepend NAME's directory to the search path, so relative links work. */
  1150. prepend_to_include_path (pathname_part (name));
  1151. initialize_conversion ();
  1152. /* Try to load the file specified by NAME, concatenated with our
  1153. various suffixes. Prefer files like `makeinfo.texi' to `makeinfo'. */
  1154. for (i = 0; suffixes[i]; i++)
  1155. {
  1156. strcpy (filename, name);
  1157. strcat (filename, suffixes[i]);
  1158. if (find_and_load (filename, 1))
  1159. break;
  1160. }
  1161. if (!suffixes[i])
  1162. {
  1163. fs_error (name);
  1164. free (filename);
  1165. return;
  1166. }
  1167. /* `find_and_load' (when successful) clobbers this global with new
  1168. memory. We're about to reset it, so may as well free first. */
  1169. free (input_filename);
  1170. /* Set the current filename. */
  1171. input_filename = filename;
  1172. /* Do the main conversion. */
  1173. convert_from_loaded_file (name);
  1174. /* Pop the prepended path, so multiple filenames in the
  1175. command line do not screw each others include paths. */
  1176. pop_path_from_include_path ();
  1177. }
  1178. static int
  1179. create_html_directory (char *dir, int can_remove_file)
  1180. {
  1181. struct stat st;
  1182. /* Already exists. */
  1183. if (stat (dir, &st) == 0)
  1184. {
  1185. /* And it's a directory, so silently reuse it. */
  1186. if (S_ISDIR (st.st_mode))
  1187. return 1;
  1188. /* Not a directory, so move it out of the way if we are allowed. */
  1189. else if (can_remove_file)
  1190. {
  1191. if (unlink (dir) != 0)
  1192. return 0;
  1193. }
  1194. else
  1195. return 0;
  1196. }
  1197. if (mkdir (dir, 0777) == 0)
  1198. /* Success! */
  1199. return 1;
  1200. else
  1201. return 0;
  1202. }
  1203. /* Given OUTPUT_FILENAME == ``/foo/bar/baz.html'', return
  1204. "/foo/bar/baz/baz.html". This routine is called only if html && splitting.
  1205. Split html output goes into the subdirectory of the toplevel
  1206. filename, without extension. For example:
  1207. @setfilename foo.info
  1208. produces output in files foo/index.html, foo/second-node.html, ...
  1209. But if the user said -o foo.whatever on the cmd line, then use
  1210. foo.whatever unchanged. */
  1211. static char *
  1212. insert_toplevel_subdirectory (char *output_filename)
  1213. {
  1214. static const char index_name[] = "index.html";
  1215. char *dir, *subdir, *base, *basename, *p;
  1216. char buf[PATH_MAX];
  1217. const int index_len = sizeof (index_name) - 1;
  1218. strcpy (buf, output_filename);
  1219. dir = pathname_part (buf); /* directory of output_filename */
  1220. base = filename_part (buf); /* strips suffix, too */
  1221. basename = xstrdup (base); /* remember real @setfilename name */
  1222. p = dir + strlen (dir) - 1;
  1223. if (p > dir && IS_SLASH (*p))
  1224. *p = 0;
  1225. p = strrchr (base, '.');
  1226. if (p)
  1227. *p = 0;
  1228. /* Split html output goes into subdirectory of toplevel name. */
  1229. if (save_command_output_filename
  1230. && STREQ (output_filename, save_command_output_filename))
  1231. subdir = basename; /* from user, use unchanged */
  1232. else
  1233. subdir = base; /* implicit, omit suffix */
  1234. free (output_filename);
  1235. output_filename = xmalloc (strlen (dir) + 1
  1236. + strlen (basename) + 1
  1237. + index_len
  1238. + 1);
  1239. strcpy (output_filename, dir);
  1240. if (strlen (dir))
  1241. strcat (output_filename, "/");
  1242. strcat (output_filename, subdir);
  1243. /* First try, do not remove existing file. */
  1244. if (!create_html_directory (output_filename, 0))
  1245. {
  1246. /* That failed, try subdir name with .html.
  1247. Remove it if it exists. */
  1248. strcpy (output_filename, dir);
  1249. if (strlen (dir))
  1250. strcat (output_filename, "/");
  1251. strcat (output_filename, basename);
  1252. if (!create_html_directory (output_filename, 1))
  1253. {
  1254. /* Last try failed too :-\ */
  1255. line_error (_("Can't create directory `%s': %s"),
  1256. output_filename, strerror (errno));
  1257. xexit (1);
  1258. }
  1259. }
  1260. strcat (output_filename, "/");
  1261. strcat (output_filename, index_name);
  1262. return output_filename;
  1263. }
  1264. /* FIXME: this is way too hairy */
  1265. static void
  1266. convert_from_loaded_file (char *name)
  1267. {
  1268. char *real_output_filename = NULL;
  1269. remember_itext (input_text, 0);
  1270. input_text_offset = 0;
  1271. /* Avoid the `\input texinfo' line in HTML output (assuming it starts
  1272. the file). */
  1273. if (looking_at ("\\input"))
  1274. discard_until ("\n");
  1275. /* Search this file looking for the special string which starts conversion.
  1276. Once found, we may truly begin. */
  1277. while (input_text_offset >= 0)
  1278. {
  1279. input_text_offset =
  1280. search_forward (setfilename_search, input_text_offset);
  1281. if (input_text_offset == 0
  1282. || (input_text_offset > 0
  1283. && input_text[input_text_offset -1] == '\n'))
  1284. break;
  1285. else if (input_text_offset > 0)
  1286. input_text_offset++;
  1287. }
  1288. if (input_text_offset < 0)
  1289. {
  1290. if (!command_output_filename)
  1291. {
  1292. #if defined (REQUIRE_SETFILENAME)
  1293. error (_("No `%s' found in `%s'"), setfilename_search, name);
  1294. goto finished;
  1295. #else
  1296. command_output_filename = output_name_from_input_name (name);
  1297. #endif /* !REQUIRE_SETFILENAME */
  1298. }
  1299. {
  1300. int i, end_of_first_line;
  1301. /* Find the end of the first line in the file. */
  1302. for (i = 0; i < input_text_length - 1; i++)
  1303. if (input_text[i] == '\n')
  1304. break;
  1305. end_of_first_line = i + 1;
  1306. for (i = 0; i < end_of_first_line; i++)
  1307. {
  1308. if ((input_text[i] == '\\') &&
  1309. (strncmp (input_text + i + 1, "input", 5) == 0))
  1310. {
  1311. input_text_offset = i;
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. else
  1318. input_text_offset += strlen (setfilename_search);
  1319. if (!command_output_filename)
  1320. {
  1321. get_until ("\n", &output_filename); /* read rest of line */
  1322. if (html || xml)
  1323. { /* Change any extension to .html or .xml. */
  1324. char *html_name, *directory_part, *basename_part, *temp;
  1325. canon_white (output_filename);
  1326. directory_part = pathname_part (output_filename);
  1327. basename_part = filename_part (output_filename);
  1328. /* Zap any existing extension. */
  1329. temp = strrchr (basename_part, '.');
  1330. if (temp)
  1331. *temp = 0;
  1332. /* Construct new filename. */
  1333. html_name = xmalloc (strlen (directory_part)
  1334. + strlen (basename_part) + 6);
  1335. strcpy (html_name, directory_part);
  1336. strcat (html_name, basename_part);
  1337. strcat (html_name, html ? ".html" : ".xml");
  1338. /* Replace name from @setfilename with the html name. */
  1339. free (output_filename);
  1340. output_filename = html_name;
  1341. }
  1342. }
  1343. else
  1344. {
  1345. if (input_text_offset != -1)
  1346. discard_until ("\n");
  1347. else
  1348. input_text_offset = 0;
  1349. real_output_filename = output_filename = command_output_filename;
  1350. command_output_filename = NULL; /* for included files or whatever */
  1351. }
  1352. canon_white (output_filename);
  1353. toplevel_output_filename = xstrdup (output_filename);
  1354. if (real_output_filename && strcmp (real_output_filename, "-") == 0)
  1355. {
  1356. if (macro_expansion_filename
  1357. && strcmp (macro_expansion_filename, "-") == 0)
  1358. {
  1359. fprintf (stderr,
  1360. _("%s: Skipping macro expansion to stdout as Info output is going there.\n"),
  1361. progname);
  1362. macro_expansion_output_stream = NULL;
  1363. }
  1364. real_output_filename = xstrdup (real_output_filename);
  1365. output_stream = stdout;
  1366. splitting = 0; /* Cannot split when writing to stdout. */
  1367. }
  1368. else
  1369. {
  1370. if (html && splitting)
  1371. {
  1372. if (FILENAME_CMP (output_filename, NULL_DEVICE) == 0
  1373. || FILENAME_CMP (output_filename, ALSO_NULL_DEVICE) == 0)
  1374. splitting = 0;
  1375. else
  1376. output_filename = insert_toplevel_subdirectory (output_filename);
  1377. real_output_filename = xstrdup (output_filename);
  1378. }
  1379. else if (!real_output_filename)
  1380. real_output_filename = expand_filename (output_filename, name);
  1381. else
  1382. real_output_filename = xstrdup (real_output_filename);
  1383. output_stream = fopen (real_output_filename, "w");
  1384. }
  1385. set_current_output_filename (real_output_filename);
  1386. if (verbose_mode)
  1387. printf (_("Making %s file `%s' from `%s'.\n"),
  1388. no_headers ? "text"
  1389. : html ? "HTML"
  1390. : xml ? "XML"
  1391. : "info",
  1392. output_filename, input_filename);
  1393. if (output_stream == NULL)
  1394. {
  1395. fs_error (real_output_filename);
  1396. goto finished;
  1397. }
  1398. if (xml)
  1399. xml_begin_document (filename_part (output_filename));
  1400. /* Make the displayable filename from output_filename. Only the base
  1401. portion of the filename need be displayed. */
  1402. flush_output (); /* in case there was no @bye */
  1403. if (output_stream != stdout)
  1404. pretty_output_filename = filename_part (output_filename);
  1405. else
  1406. pretty_output_filename = xstrdup ("stdout");
  1407. /* For this file only, count the number of newlines from the top of
  1408. the file to here. This way, we keep track of line numbers for
  1409. error reporting. Line_number starts at 1, since the user isn't
  1410. zero-based. */
  1411. {
  1412. int temp = 0;
  1413. line_number = 1;
  1414. while (temp != input_text_offset)
  1415. if (input_text[temp++] == '\n')
  1416. line_number++;
  1417. }
  1418. if (xml && !docbook)
  1419. {
  1420. /* Just before the real main loop, let's handle the defines. */
  1421. COMMAND_LINE_DEFINE *temp;
  1422. for (temp = command_line_defines; temp; temp = temp->next)
  1423. {
  1424. handle_variable_internal (temp->action, temp->define);
  1425. free(temp->define);
  1426. }
  1427. }
  1428. reader_loop ();
  1429. if (xml)
  1430. xml_end_document ();
  1431. finished:
  1432. discard_insertions (0);
  1433. close_paragraph ();
  1434. flush_file_stack ();
  1435. if (macro_expansion_output_stream)
  1436. {
  1437. fclose (macro_expansion_output_stream);
  1438. if (errors_printed && !force
  1439. && strcmp (macro_expansion_filename, "-") != 0
  1440. && FILENAME_CMP (macro_expansion_filename, NULL_DEVICE) != 0
  1441. && FILENAME_CMP (macro_expansion_filename, ALSO_NULL_DEVICE) != 0)
  1442. {
  1443. fprintf (stderr,
  1444. _("%s: Removing macro output file `%s' due to errors; use --force to preserve.\n"),
  1445. progname, macro_expansion_filename);
  1446. if (unlink (macro_expansion_filename) < 0)
  1447. perror (macro_expansion_filename);
  1448. }
  1449. }
  1450. if (output_stream)
  1451. {
  1452. output_pending_notes ();
  1453. if (html)
  1454. {
  1455. no_indent = 1;
  1456. start_paragraph ();
  1457. add_word ("</body></html>\n");
  1458. close_paragraph ();
  1459. }
  1460. /* Write stuff makeinfo generates after @bye, ie. info_trailer. */
  1461. flush_output ();
  1462. if (output_stream != stdout)
  1463. if (fclose (output_stream) != 0)
  1464. fs_error (real_output_filename);
  1465. /* If validating, then validate the entire file right now. */
  1466. if (validating)
  1467. validate_file (tag_table);
  1468. handle_delayed_writes ();
  1469. if (tag_table)
  1470. {
  1471. tag_table = (TAG_ENTRY *) reverse_list ((GENERIC_LIST *) tag_table);
  1472. if (!no_headers && !html && !STREQ (current_output_filename, "-"))
  1473. write_tag_table (real_output_filename);
  1474. }
  1475. /* Maybe we want local variables in info output. Must be after
  1476. tag table, since otherwise usually Emacs will not see it. */
  1477. write_trailer (real_output_filename, info_trailer ());
  1478. if (splitting && !html && (!errors_printed || force))
  1479. {
  1480. clean_old_split_files (real_output_filename);
  1481. split_file (real_output_filename, split_size);
  1482. }
  1483. else if (errors_printed
  1484. && !force
  1485. && strcmp (real_output_filename, "-") != 0
  1486. && FILENAME_CMP (real_output_filename, NULL_DEVICE) != 0
  1487. && FILENAME_CMP (real_output_filename, ALSO_NULL_DEVICE) != 0)
  1488. { /* If there were errors, and no --force, remove the output. */
  1489. fprintf (stderr,
  1490. _("%s: Removing output file `%s' due to errors; use --force to preserve.\n"),
  1491. progname, real_output_filename);
  1492. if (unlink (real_output_filename) < 0)
  1493. perror (real_output_filename);
  1494. }
  1495. }
  1496. if (internal_links_stream)
  1497. {
  1498. if (internal_links_stream != stdout
  1499. && fclose (internal_links_stream) != 0)
  1500. fs_error(internal_links_filename);
  1501. internal_links_stream = NULL;
  1502. if (errors_printed && !force
  1503. && strcmp (internal_links_filename, "-") != 0
  1504. && FILENAME_CMP (internal_links_filename, NULL_DEVICE) != 0
  1505. && FILENAME_CMP (internal_links_filename, ALSO_NULL_DEVICE) != 0)
  1506. {
  1507. fprintf (stderr,
  1508. _("%s: Removing internal links output file `%s' due to errors; use --force to preserve.\n"),
  1509. progname, internal_links_filename);
  1510. if (unlink (internal_links_filename) < 0)
  1511. perror (internal_links_filename);
  1512. }
  1513. }
  1514. free (real_output_filename);
  1515. }
  1516. /* If enable_encoding is set and @documentencoding is used, return a
  1517. Local Variables section (as a malloc-ed string) so that Emacs'
  1518. locale features can work. Else return NULL. */
  1519. char *
  1520. info_trailer (void)
  1521. {
  1522. char *encoding;
  1523. if (!enable_encoding)
  1524. return NULL;
  1525. encoding = current_document_encoding ();
  1526. if (encoding && *encoding)
  1527. {
  1528. #define LV_FMT "\n\037\nLocal Variables:\ncoding: %s\nEnd:\n"
  1529. char *lv = xmalloc (sizeof (LV_FMT) + strlen (encoding));
  1530. sprintf (lv, LV_FMT, encoding);
  1531. free (encoding);
  1532. return lv;
  1533. }
  1534. free (encoding);
  1535. return NULL;
  1536. }
  1537. /* Append TRAILER to FILENAME for Info and HTML output. Include HTML
  1538. comments if needed. */
  1539. static void
  1540. write_trailer (char *filename, char *trailer)
  1541. {
  1542. if (!trailer || xml || docbook)
  1543. return;
  1544. if (output_stream != stdout)
  1545. output_stream = fopen (filename, "a");
  1546. if (!output_stream)
  1547. {
  1548. fs_error (filename);
  1549. return;
  1550. }
  1551. if (html)
  1552. fwrite ("<!--", 1, 4, output_stream);
  1553. fwrite (trailer, 1, strlen (trailer), output_stream);
  1554. free (trailer);
  1555. if (html)
  1556. fwrite ("\n-->\n", 1, 5, output_stream);
  1557. if (output_stream != stdout)
  1558. if (fclose (output_stream) != 0)
  1559. fs_error (filename);
  1560. }
  1561. void
  1562. free_and_clear (char **pointer)
  1563. {
  1564. if (*pointer)
  1565. {
  1566. free (*pointer);
  1567. *pointer = NULL;
  1568. }
  1569. }
  1570. /* Initialize some state. */
  1571. static void
  1572. init_internals (void)
  1573. {
  1574. free_and_clear (&output_filename);
  1575. free_and_clear (&command);
  1576. free_and_clear (&input_filename);
  1577. free_node_references ();
  1578. free_node_node_references ();
  1579. toc_free ();
  1580. init_insertion_stack ();
  1581. init_brace_stack ();
  1582. current_node = NULL; /* sometimes already freed */
  1583. command_index = 0;
  1584. in_menu = 0;
  1585. in_detailmenu = 0;
  1586. top_node_seen = 0;
  1587. non_top_node_seen = 0;
  1588. node_number = -1;
  1589. }
  1590. void
  1591. init_paragraph (void)
  1592. {
  1593. if (output_paragraph)
  1594. free (output_paragraph);
  1595. output_paragraph = xmalloc (paragraph_buffer_len);
  1596. output_paragraph[0] = 0;
  1597. output_paragraph_offset = 0;
  1598. output_paragraph_start_column = 0;
  1599. paragraph_is_open = 0;
  1600. current_indent = 0;
  1601. meta_char_pos = 0;
  1602. }
  1603. /* This is called from `reader_loop' when we are at the * beginning a
  1604. menu line. */
  1605. static void
  1606. handle_menu_entry (void)
  1607. {
  1608. char *tem;
  1609. /* Ugh, glean_node_from_menu wants to read the * itself. */
  1610. input_text_offset--;
  1611. /* Find node name in menu entry and save it in references list for
  1612. later validation. Use followed_reference type for detailmenu
  1613. references since we don't want to use them for default node pointers. */
  1614. tem = glean_node_from_menu (1, in_detailmenu
  1615. ? followed_reference : menu_reference);
  1616. if (html && tem)
  1617. { /* Start a menu item with the cleaned-up line. Put an anchor
  1618. around the start text (before `:' or the node name). */
  1619. char *string;
  1620. discard_until ("* ");
  1621. /* The line number was already incremented in reader_loop when we
  1622. saw the newline, and discard_until has now incremented again. */
  1623. line_number--;
  1624. if (had_menu_commentary)
  1625. {
  1626. add_html_block_elt ("<ul class=\"menu\">\n");
  1627. had_menu_commentary = 0;
  1628. in_paragraph = 0;
  1629. }
  1630. if (in_paragraph)
  1631. {
  1632. add_html_block_elt ("</p>\n");
  1633. add_html_block_elt ("<ul class=\"menu\">\n");
  1634. in_paragraph = 0;
  1635. }
  1636. in_menu_item = 1;
  1637. add_html_block_elt ("<li><a");
  1638. if (next_menu_item_number <= 9)
  1639. {
  1640. add_word(" accesskey=");
  1641. add_word_args("\"%d\"", next_menu_item_number);
  1642. next_menu_item_number++;
  1643. }
  1644. add_word (" href=\"");
  1645. string = expansion (tem, 0);
  1646. add_anchor_name (string, 1);
  1647. add_word ("\">");
  1648. free (string);
  1649. /* The menu item may use macros, so expand them now. */
  1650. only_macro_expansion++;
  1651. get_until_in_line (1, ":", &string);
  1652. only_macro_expansion--;
  1653. execute_string ("%s", string); /* get escaping done */
  1654. free (string);
  1655. add_word ("</a>");
  1656. if (looking_at ("::"))
  1657. discard_until (":");
  1658. else
  1659. { /* discard the node name */
  1660. get_until_in_line (0, ".", &string);
  1661. free (string);
  1662. }
  1663. input_text_offset++; /* discard the second colon or the period */
  1664. /* Insert a colon only if there is a description of this menu item. */
  1665. {
  1666. int save_input_text_offset = input_text_offset;
  1667. int save_line_number = line_number;
  1668. char *test_string;
  1669. get_rest_of_line (0, &test_string);
  1670. if (strlen (test_string) > 0)
  1671. add_word (": ");
  1672. input_text_offset = save_input_text_offset;
  1673. line_number = save_line_number;
  1674. }
  1675. }
  1676. else if (xml && tem)
  1677. {
  1678. xml_start_menu_entry (tem);
  1679. }
  1680. else if (tem)
  1681. { /* For Info output, we can just use the input and the main case in
  1682. reader_loop where we output what comes in. Just move off the *
  1683. so the next time through reader_loop we don't end up back here. */
  1684. add_char ('*');
  1685. input_text_offset += 2; /* undo the pointer back-up above. */
  1686. }
  1687. if (tem)
  1688. free (tem);
  1689. }
  1690. /* Find the command corresponding to STRING. If the command is found,
  1691. return a pointer to the data structure. Otherwise return -1. */
  1692. static COMMAND *
  1693. get_command_entry (char *string)
  1694. {
  1695. int i;
  1696. for (i = 0; command_table[i].name; i++)
  1697. if (strcmp (command_table[i].name, string) == 0)
  1698. return &command_table[i];
  1699. /* This command is not in our predefined command table. Perhaps
  1700. it is a user defined command. */
  1701. for (i = 0; i < user_command_array_len; i++)
  1702. if (user_command_array[i] &&
  1703. (strcmp (user_command_array[i]->name, string) == 0))
  1704. return user_command_array[i];
  1705. /* We never heard of this command. */
  1706. return (COMMAND *) -1;
  1707. }
  1708. /* input_text_offset is right at the command prefix character.
  1709. Read the next token to determine what to do. Return zero
  1710. if there's no known command or macro after the prefix character. */
  1711. static int
  1712. read_command (void)
  1713. {
  1714. COMMAND *entry;
  1715. int old_text_offset = input_text_offset++;
  1716. free_and_clear (&command);
  1717. command = read_token ();
  1718. /* Check to see if this command is a macro. If so, execute it here. */
  1719. {
  1720. MACRO_DEF *def;
  1721. def = find_macro (command);
  1722. if (def)
  1723. {
  1724. /* We disallow recursive use of a macro call. Inhibit the expansion
  1725. of this macro during the life of its execution. */
  1726. if (!(def->flags & ME_RECURSE))
  1727. def->inhibited = 1;
  1728. executing_macro++;
  1729. execute_macro (def);
  1730. executing_macro--;
  1731. if (!(def->flags & ME_RECURSE))
  1732. def->inhibited = 0;
  1733. return 1;
  1734. }
  1735. }
  1736. if (only_macro_expansion)
  1737. {
  1738. /* Back up to the place where we were called, so the
  1739. caller will have a chance to process this non-macro. */
  1740. input_text_offset = old_text_offset;
  1741. return 0;
  1742. }
  1743. /* Perform alias expansion */
  1744. command = alias_expand (command);
  1745. if (enclosure_command (command))
  1746. {
  1747. remember_brace (enclosure_expand);
  1748. enclosure_expand (START, output_paragraph_offset, 0);
  1749. return 0;
  1750. }
  1751. entry = get_command_entry (command);
  1752. if (entry == (COMMAND *)-1)
  1753. {
  1754. line_error (_("Unknown command `%s'"), command);
  1755. return 0;
  1756. }
  1757. if (entry->argument_in_braces == BRACE_ARGS)
  1758. remember_brace (entry->proc);
  1759. else if (entry->argument_in_braces == MAYBE_BRACE_ARGS)
  1760. {
  1761. if (curchar () == '{')
  1762. remember_brace (entry->proc);
  1763. else
  1764. { /* No braces, so arg is next char. */
  1765. int ch;
  1766. int saved_offset = output_paragraph_offset;
  1767. (*(entry->proc)) (START, output_paragraph_offset, 0);
  1768. /* Possibilities left for the next character: @ (error), }
  1769. (error), whitespace (skip) anything else (normal char). */
  1770. skip_whitespace ();
  1771. ch = curchar ();
  1772. if (ch == '@')
  1773. {
  1774. line_error (_("Use braces to give a command as an argument to @%s"),
  1775. entry->name);
  1776. return 0;
  1777. }
  1778. else if (ch == '}')
  1779. {
  1780. /* Our caller will give the error message, because this }
  1781. won't match anything. */
  1782. return 0;
  1783. }
  1784. add_char (ch);
  1785. input_text_offset++;
  1786. (*(entry->proc)) (END, saved_offset, output_paragraph_offset);
  1787. return 1;
  1788. }
  1789. }
  1790. /* Get here if we have BRACE_ARGS, NO_BRACE_ARGS, or MAYBE_BRACE_ARGS
  1791. with braces. */
  1792. (*(entry->proc)) (START, output_paragraph_offset, 0);
  1793. return 1;
  1794. }
  1795. /* Okay, we are ready to start the conversion. Call the reader on
  1796. some text, and fill the text as it is output. Handle commands by
  1797. remembering things like open braces and the current file position on a
  1798. stack, and when the corresponding close brace is found, you can call
  1799. the function with the proper arguments. Although the filling isn't
  1800. necessary for HTML, it should do no harm. */
  1801. void
  1802. reader_loop (void)
  1803. {
  1804. int character;
  1805. int done = 0;
  1806. while (!done)
  1807. {
  1808. if (input_text_offset >= input_text_length)
  1809. break;
  1810. character = curchar ();
  1811. /* If only_macro_expansion, only handle macros and leave
  1812. everything else intact. */
  1813. if (!only_macro_expansion && !in_fixed_width_font
  1814. && ((!html && !xml) || escape_html)
  1815. && (character == '\'' || character == '`')
  1816. && input_text[input_text_offset + 1] == character)
  1817. {
  1818. if (html)
  1819. {
  1820. input_text_offset += 2;
  1821. add_word (character == '`' ? "&ldquo;" : "&rdquo;");
  1822. continue;
  1823. }
  1824. else if (xml)
  1825. {
  1826. input_text_offset += 2;
  1827. xml_insert_entity (character == '`' ? "ldquo" : "rdquo");
  1828. continue;
  1829. }
  1830. else
  1831. {
  1832. input_text_offset++;
  1833. character = '"';
  1834. }
  1835. }
  1836. /* Convert --- to --. */
  1837. if (!only_macro_expansion && character == '-' && !in_fixed_width_font
  1838. && ((!html && !xml) || escape_html))
  1839. {
  1840. int dash_count = 0;
  1841. /* Get the number of consequtive dashes. */
  1842. while (input_text[input_text_offset] == '-')
  1843. {
  1844. dash_count++;
  1845. input_text_offset++;
  1846. }
  1847. /* Eat one dash. */
  1848. dash_count--;
  1849. if (html || xml)
  1850. {
  1851. if (dash_count == 0)
  1852. add_char ('-');
  1853. else
  1854. while (dash_count > 0)
  1855. {
  1856. if (dash_count >= 2)
  1857. {
  1858. if (html)
  1859. add_word ("&mdash;");
  1860. else
  1861. xml_insert_entity ("mdash");
  1862. dash_count -= 2;
  1863. }
  1864. else if (dash_count >= 1)
  1865. {
  1866. if (html)
  1867. add_word ("&ndash;");
  1868. else
  1869. xml_insert_entity ("ndash");
  1870. dash_count--;
  1871. }
  1872. }
  1873. }
  1874. else
  1875. {
  1876. add_char ('-');
  1877. while (--dash_count > 0)
  1878. add_char ('-');
  1879. }
  1880. continue;
  1881. }
  1882. /* If this is a whitespace character, then check to see if the line
  1883. is blank. If so, advance to the carriage return. */
  1884. if (!only_macro_expansion && whitespace (character))
  1885. {
  1886. int i = input_text_offset + 1;
  1887. while (i < input_text_length && whitespace (input_text[i]))
  1888. i++;
  1889. if (i == input_text_length || input_text[i] == '\n')
  1890. {
  1891. if (i == input_text_length)
  1892. i--;
  1893. input_text_offset = i;
  1894. character = curchar ();
  1895. }
  1896. }
  1897. if (character == '\n')
  1898. line_number++;
  1899. switch (character)
  1900. {
  1901. case '*': /* perhaps we are at a menu */
  1902. /* We used to check for this in the \n case but an @c in a
  1903. menu swallows its newline, so check here instead. */
  1904. if (!only_macro_expansion && in_menu
  1905. && input_text_offset + 1 < input_text_length
  1906. && input_text_offset > 0
  1907. && input_text[input_text_offset-1] == '\n')
  1908. handle_menu_entry ();
  1909. else
  1910. { /* Duplicate code from below, but not worth twisting the
  1911. fallthroughs to get down there. */
  1912. add_char (character);
  1913. input_text_offset++;
  1914. }
  1915. break;
  1916. /* Escapes for HTML unless we're outputting raw HTML. Do
  1917. this always, even if SGML rules don't require it since
  1918. that's easier and safer for non-conforming browsers. */
  1919. case '&':
  1920. if (html && escape_html)
  1921. add_word ("&amp;");
  1922. else
  1923. add_char (character);
  1924. input_text_offset++;
  1925. break;
  1926. case '<':
  1927. if (html && escape_html)
  1928. add_word ("&lt;");
  1929. else if (xml && escape_html)
  1930. xml_insert_entity ("lt");
  1931. else
  1932. add_char (character);
  1933. input_text_offset++;
  1934. break;
  1935. case '>':
  1936. if (html && escape_html)
  1937. add_word ("&gt;");
  1938. else if (xml && escape_html)
  1939. xml_insert_entity ("gt");
  1940. else
  1941. add_char (character);
  1942. input_text_offset++;
  1943. break;
  1944. case COMMAND_PREFIX: /* @ */
  1945. if (read_command () || !only_macro_expansion)
  1946. break;
  1947. /* FALLTHROUGH (usually) */
  1948. case '{':
  1949. /* Special case. We're not supposed to see this character by itself.
  1950. If we do, it means there is a syntax error in the input text.
  1951. Report the error here, but remember this brace on the stack so
  1952. we can ignore its partner. */
  1953. if (!only_macro_expansion)
  1954. {
  1955. if (command && !STREQ (command, "math"))
  1956. {
  1957. line_error (_("Misplaced %c"), '{');
  1958. remember_brace (misplaced_brace);
  1959. }
  1960. else
  1961. /* We don't mind `extra' braces inside @math. */
  1962. remember_brace (cm_no_op);
  1963. /* remember_brace advances input_text_offset. */
  1964. break;
  1965. }
  1966. /* FALLTHROUGH (usually) */
  1967. case '}':
  1968. if (!only_macro_expansion)
  1969. {
  1970. pop_and_call_brace ();
  1971. input_text_offset++;
  1972. break;
  1973. }
  1974. /* FALLTHROUGH (usually) */
  1975. default:
  1976. add_char (character);
  1977. input_text_offset++;
  1978. }
  1979. }
  1980. if (macro_expansion_output_stream && !only_macro_expansion)
  1981. maybe_write_itext (input_text, input_text_offset);
  1982. }
  1983. static void
  1984. init_brace_stack (void)
  1985. {
  1986. brace_stack = NULL;
  1987. }
  1988. /* Remember the current output position here. Save PROC
  1989. along with it so you can call it later. */
  1990. static void
  1991. remember_brace_1 (COMMAND_FUNCTION (*proc), int position)
  1992. {
  1993. BRACE_ELEMENT *new = xmalloc (sizeof (BRACE_ELEMENT));
  1994. new->next = brace_stack;
  1995. new->proc = proc;
  1996. new->command = xstrdup (command ? command : "");
  1997. new->pos = position;
  1998. new->line = line_number;
  1999. new->in_fixed_width_font = in_fixed_width_font;
  2000. brace_stack = new;
  2001. }
  2002. static void
  2003. remember_brace (COMMAND_FUNCTION (*proc))
  2004. {
  2005. if (curchar () != '{')
  2006. line_error (_("%c%s expected braces"), COMMAND_PREFIX, command);
  2007. else
  2008. input_text_offset++;
  2009. remember_brace_1 (proc, output_paragraph_offset);
  2010. }
  2011. /* Pop the top of the brace stack, and call the associated function
  2012. with the args END and POS. */
  2013. static void
  2014. pop_and_call_brace (void)
  2015. {
  2016. if (brace_stack == NULL)
  2017. {
  2018. line_error (_("Unmatched }"));
  2019. return;
  2020. }
  2021. {
  2022. BRACE_ELEMENT *temp;
  2023. int pos = brace_stack->pos;
  2024. COMMAND_FUNCTION *proc = brace_stack->proc;
  2025. in_fixed_width_font = brace_stack->in_fixed_width_font;
  2026. /* Reset current command, so the proc can know who it is. This is
  2027. used in cm_accent. */
  2028. command = brace_stack->command;
  2029. temp = brace_stack->next;
  2030. free (brace_stack);
  2031. brace_stack = temp;
  2032. (*proc) (END, pos, output_paragraph_offset);
  2033. }
  2034. }
  2035. /* Shift all of the markers in `brace_stack' by AMOUNT. */
  2036. static void
  2037. adjust_braces_following (int here, int amount)
  2038. {
  2039. BRACE_ELEMENT *stack = brace_stack;
  2040. while (stack)
  2041. {
  2042. if (stack->pos >= here)
  2043. stack->pos += amount;
  2044. stack = stack->next;
  2045. }
  2046. }
  2047. /* Return the string which invokes PROC; a pointer to a function.
  2048. Always returns the first function in the command table if more than
  2049. one matches PROC. */
  2050. static const char *
  2051. find_proc_name (COMMAND_FUNCTION (*proc))
  2052. {
  2053. int i;
  2054. for (i = 0; command_table[i].name; i++)
  2055. if (proc == command_table[i].proc)
  2056. return command_table[i].name;
  2057. return _("NO_NAME!");
  2058. }
  2059. /* You call discard_braces () when you shouldn't have any braces on the stack.
  2060. I used to think that this happens for commands that don't take arguments
  2061. in braces, but that was wrong because of things like @code{foo @@}. So now
  2062. I only detect it at the beginning of nodes. */
  2063. void
  2064. discard_braces (void)
  2065. {
  2066. if (!brace_stack)
  2067. return;
  2068. while (brace_stack)
  2069. {
  2070. if (brace_stack->proc != misplaced_brace)
  2071. {
  2072. const char *proc_name;
  2073. proc_name = find_proc_name (brace_stack->proc);
  2074. file_line_error (input_filename, brace_stack->line,
  2075. _("%c%s missing close brace"), COMMAND_PREFIX,
  2076. proc_name);
  2077. pop_and_call_brace ();
  2078. }
  2079. else
  2080. {
  2081. BRACE_ELEMENT *temp;
  2082. temp = brace_stack->next;
  2083. free (brace_stack);
  2084. brace_stack = temp;
  2085. }
  2086. }
  2087. }
  2088. /* Return the 0-based number of the current output column */
  2089. int
  2090. current_output_column (void)
  2091. {
  2092. int i, column;
  2093. for (i = output_paragraph_offset; i > 0 && output_paragraph[i - 1] != '\n';
  2094. i--)
  2095. ;
  2096. if (i == 0)
  2097. column = output_paragraph_start_column;
  2098. else
  2099. column = 0;
  2100. while (i < output_paragraph_offset)
  2101. {
  2102. int j;
  2103. /* Find a span of non-control characters */
  2104. for (j = i; j < output_paragraph_offset; j++)
  2105. {
  2106. char c;
  2107. c = output_paragraph[j];
  2108. if ((0 <= c && c < ' ') || c == '\t' || c == NON_BREAKING_SPACE)
  2109. break;
  2110. }
  2111. if (i < j)
  2112. {
  2113. column += mbsnwidth ((char *)(output_paragraph + i), j - i, 0);
  2114. i = j;
  2115. }
  2116. if (i < output_paragraph_offset)
  2117. {
  2118. char c;
  2119. /* Handle a control character */
  2120. c = output_paragraph[i];
  2121. if (c == '\t')
  2122. {
  2123. column = (column + 8) & ~0x7;
  2124. if (column > fill_column)
  2125. column = fill_column;
  2126. }
  2127. else if (c == NON_BREAKING_SPACE)
  2128. column++;
  2129. else
  2130. {
  2131. /* ASCII control characters appear as two characters in the
  2132. output (e.g., ^A). */
  2133. if (!(0 <= c && c < ' '))
  2134. abort ();
  2135. column += 2;
  2136. }
  2137. i++;
  2138. }
  2139. }
  2140. return column;
  2141. }
  2142. void
  2143. add_word_args (const char *format, ...)
  2144. {
  2145. char buffer[2000]; /* xx no fixed limits */
  2146. va_list ap;
  2147. va_start (ap, format);
  2148. vsnprintf (buffer, sizeof (buffer), format, ap);
  2149. va_end (ap);
  2150. add_word (buffer);
  2151. }
  2152. /* Add STRING to output_paragraph. */
  2153. void
  2154. add_word (char *string)
  2155. {
  2156. while (*string)
  2157. add_char (*string++);
  2158. }
  2159. /* Like add_word, but inhibits conversion of whitespace into &nbsp;.
  2160. Use this to output HTML directives with embedded blanks, to make
  2161. them @w-safe. */
  2162. void
  2163. add_html_elt (char *string)
  2164. {
  2165. in_html_elt++;
  2166. add_word (string);
  2167. in_html_elt--;
  2168. }
  2169. /* These two functions below, add_html_block_elt and add_html_block_elt_args,
  2170. are mixtures of add_html_elt and add_word_args. They inform makeinfo that
  2171. the current HTML element being inserted should not be enclosed in a <p>
  2172. element. */
  2173. void
  2174. add_html_block_elt (char *string)
  2175. {
  2176. in_html_block_level_elt++;
  2177. add_word (string);
  2178. in_html_block_level_elt--;
  2179. }
  2180. void
  2181. add_html_block_elt_args (const char *format, ...)
  2182. {
  2183. char buffer[2000]; /* xx no fixed limits */
  2184. va_list ap;
  2185. va_start (ap, format);
  2186. vsnprintf (buffer, sizeof (buffer), format, ap);
  2187. va_end (ap);
  2188. add_html_block_elt (buffer);
  2189. }
  2190. /* Here is another awful kludge, used in add_char. Ordinarily, macro
  2191. expansions take place in the body of the document, and therefore we
  2192. should html_output_head when we see one. But there's an exception: a
  2193. macro call might take place within @copying, and that does not start
  2194. the real output, even though we fully expand the copying text.
  2195. So we need to be able to check if we are defining the @copying text.
  2196. We do this by looking back through the insertion stack. */
  2197. static int
  2198. defining_copying (void)
  2199. {
  2200. INSERTION_ELT *i;
  2201. for (i = insertion_stack; i; i = i->next)
  2202. {
  2203. if (i->insertion == copying)
  2204. return 1;
  2205. }
  2206. return 0;
  2207. }
  2208. /* Output the header for Info.
  2209. html fixxme: should output this as trailer on first page (at least). */
  2210. static void
  2211. info_output_head (void)
  2212. {
  2213. add_word_args (gdt("This is %s, produced by makeinfo version %s from %s.\n"),
  2214. output_filename, VERSION, input_filename);
  2215. /* Start afresh with whatever real text we have. */
  2216. close_paragraph ();
  2217. /* We do not want indentation in what follows, which is usually going
  2218. to be a node marker (CTRL-_). */
  2219. if (inhibit_paragraph_indentation == 0)
  2220. inhibit_paragraph_indentation = -1;
  2221. }
  2222. void
  2223. output_head (void)
  2224. {
  2225. if (output_head_p) /* no-op if we're mistakenly called twice */
  2226. return;
  2227. output_head_p = 1;
  2228. if (html)
  2229. html_output_head ();
  2230. else if (xml)
  2231. ; /* handled differently, via xml_begin_document */
  2232. else if (no_headers)
  2233. ; /* no header for plain text */
  2234. else
  2235. info_output_head ();
  2236. }
  2237. /* Add the character to the current paragraph. If filling_enabled is
  2238. nonzero, then do filling as well. */
  2239. void
  2240. add_char (int character)
  2241. {
  2242. if (xml)
  2243. {
  2244. xml_add_char (character);
  2245. return;
  2246. }
  2247. /* If we are avoiding outputting headers, and we are currently
  2248. in a menu, then simply return. But if we're only expanding macros,
  2249. then we're being called from glean_node_from_menu to try to
  2250. remember a menu reference, and we need that so we can do defaulting. */
  2251. if (no_headers && !only_macro_expansion && (in_menu || in_detailmenu))
  2252. return;
  2253. /* If we are adding a character now, then we don't have to
  2254. ignore close_paragraph () calls any more. */
  2255. if (must_start_paragraph && character != '\n')
  2256. {
  2257. int column;
  2258. must_start_paragraph = 0;
  2259. line_already_broken = 0; /* The line is no longer broken. */
  2260. column = current_output_column ();
  2261. if (current_indent > column)
  2262. indent (current_indent - column);
  2263. }
  2264. if (non_splitting_words
  2265. && !(html && in_html_elt)
  2266. && strchr (" \t\n", character))
  2267. {
  2268. if (html || docbook)
  2269. { /* Seems cleaner to use &nbsp; than an 8-bit char. */
  2270. int saved_escape_html = escape_html;
  2271. escape_html = 0;
  2272. add_word ("&nbsp");
  2273. escape_html = saved_escape_html;
  2274. character = ';';
  2275. }
  2276. else
  2277. character = NON_BREAKING_SPACE; /* restored in flush_output */
  2278. }
  2279. insertion_paragraph_closed = 0;
  2280. switch (character)
  2281. {
  2282. case '\n':
  2283. if (!filling_enabled && !(html && (in_menu || in_detailmenu)))
  2284. {
  2285. insert ('\n');
  2286. if (force_flush_right)
  2287. {
  2288. close_paragraph ();
  2289. /* Hack to force single blank lines out in this mode. */
  2290. flush_output ();
  2291. }
  2292. if (!no_indent && paragraph_is_open)
  2293. indent (current_indent);
  2294. break;
  2295. }
  2296. else if (end_of_sentence_p () && !french_spacing)
  2297. /* CHARACTER is newline, filling is enabled, and we're at the
  2298. end of a sentence. Insert an extra space, unless
  2299. @frenchspacing is in effect. */
  2300. {
  2301. insert (' ');
  2302. last_inserted_character = character;
  2303. }
  2304. if (last_char_was_newline)
  2305. {
  2306. if (html)
  2307. last_char_was_newline++;
  2308. close_paragraph ();
  2309. pending_indent = 0;
  2310. }
  2311. else
  2312. {
  2313. last_char_was_newline = 1;
  2314. if (html)
  2315. insert ('\n');
  2316. else
  2317. insert (' ');
  2318. }
  2319. break;
  2320. default: /* not at newline */
  2321. {
  2322. int column;
  2323. int suppress_insert = 0;
  2324. if ((character == ' ') && (last_char_was_newline))
  2325. {
  2326. if (!paragraph_is_open)
  2327. {
  2328. pending_indent++;
  2329. return;
  2330. }
  2331. }
  2332. /* This is a sad place to do this, but it seems highly desirable
  2333. to not force any particular order on the front matter
  2334. commands. This way, the document can do @settitle,
  2335. @documentlanguage, etc, in any order and with any omissions,
  2336. and we'll still output the header ("produced by makeinfo",
  2337. HTML <head>, etc.) `just in time'. */
  2338. if ((executing_macro || !executing_string)
  2339. && !only_macro_expansion
  2340. && !defining_copying ()
  2341. && !output_head_p)
  2342. {
  2343. output_head ();
  2344. }
  2345. if (!paragraph_is_open)
  2346. {
  2347. start_paragraph ();
  2348. /* If the paragraph is supposed to be indented a certain
  2349. way, then discard all of the pending whitespace.
  2350. Otherwise, we let the whitespace stay. */
  2351. if (!paragraph_start_indent)
  2352. indent (pending_indent);
  2353. pending_indent = 0;
  2354. /* This check for in_html_block_level_elt prevents <p> from being
  2355. inserted when we already have html markup starting a paragraph,
  2356. as with <ul> and <h1> and the like. */
  2357. if (html && !in_html_block_level_elt)
  2358. {
  2359. if ((in_menu || in_detailmenu) && in_menu_item)
  2360. {
  2361. insert_string ("</li></ul>\n");
  2362. in_menu_item = 0;
  2363. }
  2364. insert_string ("<p>");
  2365. in_paragraph = 1;
  2366. adjust_braces_following (0, 3); /* adjust for <p> */
  2367. }
  2368. }
  2369. output_paragraph[output_paragraph_offset] = character;
  2370. output_paragraph_offset++;
  2371. column = current_output_column ();
  2372. output_paragraph_offset--;
  2373. if (column > fill_column)
  2374. {
  2375. if (filling_enabled && !html)
  2376. {
  2377. int temp = output_paragraph_offset;
  2378. while (--temp > 0 && output_paragraph[temp] != '\n')
  2379. {
  2380. /* If we have found a space, we have the place to break
  2381. the line. */
  2382. if (output_paragraph[temp] == ' ')
  2383. {
  2384. /* Remove trailing whitespace from output. */
  2385. while (temp && whitespace (output_paragraph[temp - 1]))
  2386. temp--;
  2387. /* If we went back all the way to the newline of the
  2388. preceding line, it probably means that the word we
  2389. are adding is itself wider than the space that the
  2390. indentation and the fill_column let us use. In
  2391. that case, do NOT insert another newline, since it
  2392. won't help. Just indent to current_indent and
  2393. leave it alone, since that's the most we can do. */
  2394. if (temp && output_paragraph[temp - 1] != '\n')
  2395. output_paragraph[temp++] = '\n';
  2396. /* We have correctly broken the line where we want
  2397. to. What we don't want is spaces following where
  2398. we have decided to break the line. We get rid of
  2399. them. */
  2400. {
  2401. int t1 = temp;
  2402. for (;; t1++)
  2403. {
  2404. if (t1 == output_paragraph_offset)
  2405. {
  2406. if (whitespace (character))
  2407. suppress_insert = 1;
  2408. break;
  2409. }
  2410. if (!whitespace (output_paragraph[t1]))
  2411. break;
  2412. }
  2413. if (t1 != temp)
  2414. {
  2415. adjust_braces_following (temp, (- (t1 - temp)));
  2416. memmove (&output_paragraph[temp],
  2417. &output_paragraph[t1],
  2418. output_paragraph_offset - t1);
  2419. output_paragraph_offset -= (t1 - temp);
  2420. }
  2421. }
  2422. /* Filled, but now indent if that is right. */
  2423. if (indented_fill && current_indent > 0)
  2424. {
  2425. int buffer_len = ((output_paragraph_offset - temp)
  2426. + current_indent);
  2427. char *temp_buffer = xmalloc (buffer_len);
  2428. int indentation = 0;
  2429. /* We have to shift any markers that are in
  2430. front of the wrap point. */
  2431. adjust_braces_following (temp, current_indent);
  2432. while (current_indent > 0 &&
  2433. indentation != current_indent)
  2434. temp_buffer[indentation++] = ' ';
  2435. memcpy ((char *) &temp_buffer[current_indent],
  2436. (char *) &output_paragraph[temp],
  2437. buffer_len - current_indent);
  2438. if (output_paragraph_offset + buffer_len
  2439. >= paragraph_buffer_len)
  2440. {
  2441. unsigned char *tt = xrealloc
  2442. (output_paragraph,
  2443. (paragraph_buffer_len += buffer_len));
  2444. output_paragraph = tt;
  2445. }
  2446. memcpy ((char *) &output_paragraph[temp],
  2447. temp_buffer, buffer_len);
  2448. output_paragraph_offset += current_indent;
  2449. free (temp_buffer);
  2450. }
  2451. break;
  2452. }
  2453. }
  2454. }
  2455. }
  2456. if (!suppress_insert)
  2457. {
  2458. insert (character);
  2459. last_inserted_character = character;
  2460. }
  2461. last_char_was_newline = 0;
  2462. line_already_broken = 0;
  2463. }
  2464. }
  2465. }
  2466. /* Add a character and store its position in meta_char_pos. */
  2467. void
  2468. add_meta_char (int character)
  2469. {
  2470. meta_char_pos = output_paragraph_offset;
  2471. add_char (character);
  2472. }
  2473. /* Insert CHARACTER into `output_paragraph'. */
  2474. void
  2475. insert (int character)
  2476. {
  2477. /* We don't want to strip trailing whitespace in multitables. Otherwise
  2478. horizontal separators confuse the font locking in Info mode in Emacs,
  2479. because it looks like a @subsection. Adding a trailing space to those
  2480. lines fixes it. */
  2481. if (character == '\n' && !html && !xml && !multitable_active)
  2482. {
  2483. while (output_paragraph_offset
  2484. && whitespace (output_paragraph[output_paragraph_offset-1]))
  2485. output_paragraph_offset--;
  2486. }
  2487. output_paragraph[output_paragraph_offset++] = character;
  2488. if (output_paragraph_offset == paragraph_buffer_len)
  2489. {
  2490. output_paragraph =
  2491. xrealloc (output_paragraph, (paragraph_buffer_len += 100));
  2492. }
  2493. }
  2494. /* Insert the null-terminated string STRING into `output_paragraph'. */
  2495. void
  2496. insert_string (const char *string)
  2497. {
  2498. while (*string)
  2499. insert (*string++);
  2500. }
  2501. /* Sentences might have these characters after the period (or whatever). */
  2502. #define POST_SENTENCE(c) ((c) == ')' || (c) == '\'' || (c) == '"' \
  2503. || (c) == ']')
  2504. /* Return true if at an end-of-sentence character, possibly followed by
  2505. post-sentence punctuation to ignore. */
  2506. static int
  2507. end_of_sentence_p (void)
  2508. {
  2509. int loc = output_paragraph_offset - 1;
  2510. /* If nothing has been output, don't check output_paragraph[-1]. */
  2511. if (loc < 0)
  2512. return 0;
  2513. /* A post-sentence character that is at meta_char_pos is not really
  2514. a post-sentence character; it was produced by a markup such as
  2515. @samp. We don't want the period inside @samp to be treated as a
  2516. sentence ender. */
  2517. while (loc > 0
  2518. && loc != meta_char_pos && POST_SENTENCE (output_paragraph[loc]))
  2519. loc--;
  2520. return loc != meta_char_pos && sentence_ender (output_paragraph[loc]);
  2521. }
  2522. /* Remove upto COUNT characters of whitespace from the
  2523. the current output line. If COUNT is less than zero,
  2524. then remove until none left. */
  2525. void
  2526. kill_self_indent (int count)
  2527. {
  2528. /* Handle infinite case first. */
  2529. if (count < 0)
  2530. {
  2531. while (output_paragraph_offset)
  2532. {
  2533. if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2534. output_paragraph_offset--;
  2535. else
  2536. break;
  2537. }
  2538. }
  2539. else
  2540. {
  2541. while (output_paragraph_offset && count--)
  2542. if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2543. output_paragraph_offset--;
  2544. else
  2545. break;
  2546. }
  2547. }
  2548. /* Nonzero means do not honor calls to flush_output (). */
  2549. static int flushing_ignored = 0;
  2550. /* Prevent calls to flush_output () from having any effect. */
  2551. void
  2552. inhibit_output_flushing (void)
  2553. {
  2554. flushing_ignored++;
  2555. }
  2556. /* Allow calls to flush_output () to write the paragraph data. */
  2557. void
  2558. uninhibit_output_flushing (void)
  2559. {
  2560. flushing_ignored--;
  2561. }
  2562. void
  2563. flush_output (void)
  2564. {
  2565. int i;
  2566. if (!output_paragraph_offset || flushing_ignored)
  2567. return;
  2568. for (i = 0; i < output_paragraph_offset; i++)
  2569. {
  2570. if (output_paragraph[i] == '\n')
  2571. {
  2572. output_line_number++;
  2573. node_line_number++;
  2574. }
  2575. /* If we turned on the 8th bit for a space inside @w, turn it back off
  2576. for output. Don't do this for HTML, since the nbsp character is valid
  2577. input and must be passed along to the browser. */
  2578. if (!html && output_paragraph[i] == NON_BREAKING_SPACE)
  2579. output_paragraph[i] = ' ';
  2580. }
  2581. fwrite (output_paragraph, 1, output_paragraph_offset, output_stream);
  2582. output_position += output_paragraph_offset;
  2583. output_paragraph_start_column = current_output_column ();
  2584. output_paragraph_offset = 0;
  2585. meta_char_pos = 0;
  2586. }
  2587. /* How to close a paragraph controlling the number of lines between
  2588. this one and the last one. */
  2589. /* Paragraph spacing is controlled by this variable. It is the number of
  2590. blank lines that you wish to appear between paragraphs. A value of
  2591. 1 creates a single blank line between paragraphs. */
  2592. int paragraph_spacing = DEFAULT_PARAGRAPH_SPACING;
  2593. static void
  2594. close_paragraph_with_lines (int lines)
  2595. {
  2596. int old_spacing = paragraph_spacing;
  2597. paragraph_spacing = lines;
  2598. close_paragraph ();
  2599. paragraph_spacing = old_spacing;
  2600. }
  2601. /* Close the current paragraph, leaving no blank lines between them. */
  2602. void
  2603. close_single_paragraph (void)
  2604. {
  2605. close_paragraph_with_lines (0);
  2606. }
  2607. /* Close a paragraph after an insertion has ended. */
  2608. void
  2609. close_insertion_paragraph (void)
  2610. {
  2611. if (!insertion_paragraph_closed)
  2612. {
  2613. /* Close the current paragraph, breaking the line. */
  2614. close_single_paragraph ();
  2615. /* Start a new paragraph, with the correct indentation for the now
  2616. current insertion level (one above the one that we are ending). */
  2617. start_paragraph ();
  2618. /* Tell `close_paragraph' that the previous line has already been
  2619. broken, so it should insert one less newline. */
  2620. line_already_broken = 1;
  2621. /* Tell functions such as `add_char' we've already found a newline. */
  2622. ignore_blank_line ();
  2623. }
  2624. else
  2625. {
  2626. int column;
  2627. /* If the insertion paragraph is closed already, then we are seeing
  2628. two `@end' commands in a row. Note that the first one we saw was
  2629. handled in the first part of this if-then-else clause, and at that
  2630. time `start_paragraph' was called, partially to handle the proper
  2631. indentation of the current line. However, the indentation level
  2632. may have just changed again, so we may have to outdent the current
  2633. line to the new indentation level. */
  2634. column = current_output_column ();
  2635. if (current_indent < column)
  2636. kill_self_indent (column - current_indent);
  2637. }
  2638. insertion_paragraph_closed = 1;
  2639. }
  2640. /* Close the currently open paragraph. */
  2641. void
  2642. close_paragraph (void)
  2643. {
  2644. int i;
  2645. /* We don't need these newlines in XML and Docbook outputs for
  2646. paragraph separation. We have the <para> element for that. */
  2647. if (xml)
  2648. return;
  2649. /* The insertion paragraph is no longer closed. */
  2650. insertion_paragraph_closed = 0;
  2651. if (paragraph_is_open && !must_start_paragraph)
  2652. {
  2653. int tindex = output_paragraph_offset;
  2654. /* Back up to last non-newline/space character, forcing all such
  2655. subsequent characters to be newlines. This isn't strictly
  2656. necessary, but a couple of functions use the presence of a newline
  2657. to make decisions. */
  2658. for (tindex = output_paragraph_offset - 1; tindex >= 0; --tindex)
  2659. {
  2660. int c = output_paragraph[tindex];
  2661. if (c == ' '|| c == '\n')
  2662. output_paragraph[tindex] = '\n';
  2663. else
  2664. break;
  2665. }
  2666. /* All trailing whitespace is ignored. */
  2667. output_paragraph_offset = ++tindex;
  2668. /* Break the line if that is appropriate. */
  2669. if (paragraph_spacing >= 0)
  2670. insert ('\n');
  2671. /* Add as many blank lines as is specified in `paragraph_spacing'. */
  2672. if (!force_flush_right)
  2673. {
  2674. for (i = 0; i < (paragraph_spacing - line_already_broken); i++)
  2675. {
  2676. insert ('\n');
  2677. /* Don't need anything extra for HTML in usual case of no
  2678. extra paragraph spacing. */
  2679. if (html && i > 0)
  2680. insert_string ("<br>");
  2681. }
  2682. }
  2683. /* If we are doing flush right indentation, then do it now
  2684. on the paragraph (really a single line). */
  2685. if (force_flush_right)
  2686. do_flush_right_indentation ();
  2687. flush_output ();
  2688. paragraph_is_open = 0;
  2689. no_indent = 0;
  2690. }
  2691. ignore_blank_line ();
  2692. }
  2693. /* Make the last line just read look as if it were only a newline. */
  2694. void
  2695. ignore_blank_line (void)
  2696. {
  2697. last_inserted_character = '\n';
  2698. last_char_was_newline = 1;
  2699. }
  2700. /* Align the end of the text in output_paragraph with fill_column. The last
  2701. character in output_paragraph is '\n'. */
  2702. static void
  2703. do_flush_right_indentation (void)
  2704. {
  2705. char *temp;
  2706. kill_self_indent (-1);
  2707. if (output_paragraph[0] != '\n')
  2708. {
  2709. int width;
  2710. width = mbsnwidth ((char *)output_paragraph, output_paragraph_offset - 1,
  2711. 0) + 1;
  2712. if (width < fill_column)
  2713. {
  2714. int i;
  2715. if (output_paragraph_offset + (fill_column - width)
  2716. >= paragraph_buffer_len)
  2717. output_paragraph =
  2718. xrealloc (output_paragraph,
  2719. (paragraph_buffer_len += fill_column));
  2720. temp = xmalloc (output_paragraph_offset);
  2721. memcpy (temp, (char *)output_paragraph, output_paragraph_offset);
  2722. for (i = 0; i < fill_column - width; i++)
  2723. output_paragraph[i] = ' ';
  2724. memcpy ((char *)output_paragraph + i, temp, output_paragraph_offset);
  2725. free (temp);
  2726. output_paragraph_offset += i;
  2727. adjust_braces_following (0, i);
  2728. }
  2729. }
  2730. }
  2731. /* Begin a new paragraph. */
  2732. void
  2733. start_paragraph (void)
  2734. {
  2735. /* First close existing one. */
  2736. if (paragraph_is_open)
  2737. close_paragraph ();
  2738. /* In either case, the insertion paragraph is no longer closed. */
  2739. insertion_paragraph_closed = 0;
  2740. /* However, the paragraph is open! */
  2741. paragraph_is_open = 1;
  2742. /* If we MUST_START_PARAGRAPH, that simply means that start_paragraph ()
  2743. had to be called before we would allow any other paragraph operations
  2744. to have an effect. */
  2745. if (!must_start_paragraph)
  2746. {
  2747. int amount_to_indent = 0;
  2748. /* If doing indentation, then insert the appropriate amount. */
  2749. if (!no_indent)
  2750. {
  2751. int column;
  2752. if (inhibit_paragraph_indentation)
  2753. {
  2754. amount_to_indent = current_indent;
  2755. if (inhibit_paragraph_indentation < 0)
  2756. inhibit_paragraph_indentation++;
  2757. }
  2758. else if (paragraph_start_indent < 0)
  2759. amount_to_indent = current_indent;
  2760. else
  2761. amount_to_indent = current_indent + paragraph_start_indent;
  2762. column = current_output_column ();
  2763. if (amount_to_indent >= column)
  2764. indent (amount_to_indent - column);
  2765. }
  2766. }
  2767. else
  2768. must_start_paragraph = 0;
  2769. }
  2770. /* Insert the indentation specified by AMOUNT. */
  2771. void
  2772. indent (int amount)
  2773. {
  2774. /* For every START_POS saved within the brace stack which will be affected
  2775. by this indentation, bump that start pos forward. */
  2776. adjust_braces_following (output_paragraph_offset, amount);
  2777. while (--amount >= 0)
  2778. insert (' ');
  2779. }
  2780. /* Search forward for STRING in input_text.
  2781. FROM says where to start. */
  2782. int
  2783. search_forward (const char *string, int from)
  2784. {
  2785. int len = strlen (string);
  2786. while (from < input_text_length)
  2787. {
  2788. if (strncmp (input_text + from, string, len) == 0)
  2789. return from;
  2790. from++;
  2791. }
  2792. return -1;
  2793. }
  2794. /* search_forward until n characters. */
  2795. int
  2796. search_forward_until_pos (char *string, int from, int end_pos)
  2797. {
  2798. int save_input_text_length = input_text_length;
  2799. input_text_length = end_pos;
  2800. from = search_forward (string, from);
  2801. input_text_length = save_input_text_length;
  2802. return from;
  2803. }
  2804. /* Return next non-whitespace and non-cr character. */
  2805. int
  2806. next_nonwhitespace_character (void)
  2807. {
  2808. /* First check the current input_text. Start from the next char because
  2809. we already have input_text[input_text_offset] in ``current''. */
  2810. int pos = input_text_offset + 1;
  2811. while (pos < input_text_length)
  2812. {
  2813. if (!cr_or_whitespace(input_text[pos]))
  2814. return input_text[pos];
  2815. pos++;
  2816. }
  2817. { /* Can't find a valid character, so go through filestack
  2818. in case we are doing @include or expanding a macro. */
  2819. FSTACK *tos = filestack;
  2820. while (tos)
  2821. {
  2822. int tmp_input_text_length = filestack->size;
  2823. int tmp_input_text_offset = filestack->offset;
  2824. char *tmp_input_text = filestack->text;
  2825. while (tmp_input_text_offset < tmp_input_text_length)
  2826. {
  2827. if (!cr_or_whitespace(tmp_input_text[tmp_input_text_offset]))
  2828. return tmp_input_text[tmp_input_text_offset];
  2829. tmp_input_text_offset++;
  2830. }
  2831. tos = tos->next;
  2832. }
  2833. }
  2834. return -1;
  2835. }
  2836. /* Replace " with \" and \ with \\. Used for alt tag in Info output.
  2837. Return a newly-malloced string in all cases. */
  2838. static char *
  2839. bs_escape_quote (const char *src)
  2840. {
  2841. int c;
  2842. char *dest = xmalloc (2 * strlen (src) + 1); /* can't need more. */
  2843. char *p = dest;
  2844. for (; c = *src; src++)
  2845. {
  2846. if (c == '"' || c == '\\')
  2847. *p++ = '\\';
  2848. *p++ = c;
  2849. }
  2850. *p = 0;
  2851. return dest;
  2852. }
  2853. /* An external image is a reference, kind of. The parsing is (not
  2854. coincidentally) similar, anyway. */
  2855. void
  2856. cm_image (int arg)
  2857. {
  2858. char *name_arg, *w_arg, *h_arg, *alt_arg, *ext_arg;
  2859. if (arg == END)
  2860. return;
  2861. name_arg = get_xref_token (1); /* expands all macros in image */
  2862. w_arg = get_xref_token (0);
  2863. h_arg = get_xref_token (0);
  2864. alt_arg = get_xref_token (1); /* expands all macros in alt text */
  2865. ext_arg = get_xref_token (0);
  2866. if (*name_arg)
  2867. {
  2868. struct stat file_info;
  2869. char *pathname = NULL;
  2870. unsigned ext_len = (ext_arg && *ext_arg) ? strlen (ext_arg) : 0;
  2871. /* One byte for the . period separator, one byte for the null.
  2872. The 3 is for the max length of the hardwired extensions we try. */
  2873. char *fullname = xmalloc (strlen (name_arg) + 1 + MAX (ext_len, 3) + 1);
  2874. if (ext_arg && *ext_arg)
  2875. {
  2876. sprintf (fullname, "%s%s", name_arg, ext_arg);
  2877. if (access (fullname, R_OK) != 0)
  2878. pathname = get_file_info_in_path (fullname, include_files_path,
  2879. &file_info);
  2880. if (pathname == NULL)
  2881. {
  2882. /* Backwards compatibility (4.6 <= version < 4.7):
  2883. try prefixing @image's EXTENSION parameter with a period. */
  2884. sprintf (fullname, "%s.%s", name_arg, ext_arg);
  2885. if (access (fullname, R_OK) != 0)
  2886. pathname = get_file_info_in_path (fullname, include_files_path,
  2887. &file_info);
  2888. }
  2889. }
  2890. else
  2891. {
  2892. sprintf (fullname, "%s.png", name_arg);
  2893. if (access (fullname, R_OK) != 0) {
  2894. pathname = get_file_info_in_path (fullname,
  2895. include_files_path, &file_info);
  2896. if (pathname == NULL) {
  2897. sprintf (fullname, "%s.jpg", name_arg);
  2898. if (access (fullname, R_OK) != 0) {
  2899. pathname = get_file_info_in_path (fullname,
  2900. include_files_path,
  2901. &file_info);
  2902. if (pathname == NULL) {
  2903. sprintf (fullname, "%s.gif", name_arg);
  2904. if (access (fullname, R_OK) != 0) {
  2905. pathname = get_file_info_in_path (fullname,
  2906. include_files_path,
  2907. &file_info);
  2908. }
  2909. }
  2910. }
  2911. }
  2912. }
  2913. }
  2914. if (html)
  2915. {
  2916. int image_in_div = 0;
  2917. if (pathname == NULL && access (fullname, R_OK) != 0)
  2918. {
  2919. line_error(_("@image file `%s' (for HTML) not readable: %s"),
  2920. fullname, strerror (errno));
  2921. return;
  2922. }
  2923. if (pathname != NULL && access (pathname, R_OK) != 0)
  2924. {
  2925. line_error (_("No such file `%s'"),
  2926. fullname);
  2927. return;
  2928. }
  2929. if (!paragraph_is_open)
  2930. {
  2931. add_html_block_elt ("<div class=\"block-image\">");
  2932. image_in_div = 1;
  2933. }
  2934. add_html_elt ("<img src=");
  2935. add_word_args ("\"%s\"", fullname);
  2936. add_html_elt (" alt=");
  2937. add_word_args ("\"%s\">",
  2938. escape_string (*alt_arg ? text_expansion (alt_arg) : fullname));
  2939. if (image_in_div)
  2940. add_html_block_elt ("</div>");
  2941. }
  2942. else if (xml && docbook)
  2943. xml_insert_docbook_image (name_arg);
  2944. else if (xml)
  2945. {
  2946. extern int xml_in_para;
  2947. extern int xml_no_para;
  2948. int elt = xml_in_para ? INLINEIMAGE : IMAGE;
  2949. if (!xml_in_para)
  2950. xml_no_para++;
  2951. xml_insert_element_with_attribute (elt,
  2952. START, "width=\"%s\" height=\"%s\" name=\"%s\" extension=\"%s\"",
  2953. w_arg, h_arg, name_arg, ext_arg);
  2954. xml_insert_element (IMAGEALTTEXT, START);
  2955. execute_string ("%s", alt_arg);
  2956. xml_insert_element (IMAGEALTTEXT, END);
  2957. xml_insert_element (elt, END);
  2958. if (!xml_in_para)
  2959. xml_no_para--;
  2960. }
  2961. else
  2962. { /* Prefer foo.txt for Info/ASCII. Seems wrong nowadays. */
  2963. FILE *image_file;
  2964. char *txtpath = NULL;
  2965. char *txtname = xmalloc (strlen (name_arg) + 4 + 1);
  2966. strcpy (txtname, name_arg);
  2967. strcat (txtname, ".txt");
  2968. image_file = fopen (txtname, "r");
  2969. if (image_file == NULL)
  2970. {
  2971. txtpath = get_file_info_in_path (txtname,
  2972. include_files_path, &file_info);
  2973. if (txtpath != NULL)
  2974. image_file = fopen (txtpath, "r");
  2975. }
  2976. if (image_file != NULL
  2977. || access (fullname, R_OK) == 0
  2978. || (pathname != NULL && access (pathname, R_OK) == 0))
  2979. {
  2980. int ch;
  2981. int save_inhibit_indentation = inhibit_paragraph_indentation;
  2982. int save_filling_enabled = filling_enabled;
  2983. int image_in_brackets = paragraph_is_open;
  2984. /* Write magic ^@^H[image ...^@^H] cookie in the info file, if
  2985. there's an accompanying bitmap. Otherwise just include the
  2986. text image. In the plaintext output, always include the text
  2987. image without the magic cookie. */
  2988. int use_magic_cookie = !no_headers
  2989. && access (fullname, R_OK) == 0 && !STREQ (fullname, txtname);
  2990. inhibit_paragraph_indentation = 1;
  2991. filling_enabled = 0;
  2992. last_char_was_newline = 0;
  2993. if (use_magic_cookie)
  2994. {
  2995. add_char ('\0');
  2996. add_word ("\010[image");
  2997. if (access (fullname, R_OK) == 0
  2998. || (pathname != NULL && access (pathname, R_OK) == 0))
  2999. add_word_args (" src=\"%s\"", fullname);
  3000. if (*alt_arg)
  3001. {
  3002. char *expanded = text_expansion (alt_arg);
  3003. char *escaped = bs_escape_quote (expanded);
  3004. add_word_args (" alt=\"%s\"", escaped);
  3005. free (expanded);
  3006. free (escaped);
  3007. }
  3008. }
  3009. if (image_file != NULL)
  3010. {
  3011. if (use_magic_cookie)
  3012. add_word (" text=\"");
  3013. if (image_in_brackets)
  3014. add_char ('[');
  3015. /* Maybe we need to remove the final newline if the image
  3016. file is only one line to allow in-line images. On the
  3017. other hand, they could just make the file without a
  3018. final newline. */
  3019. while ((ch = getc (image_file)) != EOF)
  3020. {
  3021. if (use_magic_cookie && (ch == '"' || ch == '\\'))
  3022. add_char ('\\');
  3023. add_char (ch);
  3024. }
  3025. if (image_in_brackets)
  3026. add_char (']');
  3027. if (use_magic_cookie)
  3028. add_char ('"');
  3029. if (fclose (image_file) != 0)
  3030. perror (txtname);
  3031. }
  3032. if (use_magic_cookie)
  3033. {
  3034. add_char ('\0');
  3035. add_word ("\010]");
  3036. }
  3037. inhibit_paragraph_indentation = save_inhibit_indentation;
  3038. filling_enabled = save_filling_enabled;
  3039. }
  3040. else
  3041. warning (_("@image file `%s' (for text) unreadable: %s"),
  3042. txtname, strerror (errno));
  3043. }
  3044. free (fullname);
  3045. if (pathname)
  3046. free (pathname);
  3047. }
  3048. else
  3049. line_error (_("@image missing filename argument"));
  3050. if (name_arg)
  3051. free (name_arg);
  3052. if (w_arg)
  3053. free (w_arg);
  3054. if (h_arg)
  3055. free (h_arg);
  3056. if (alt_arg)
  3057. free (alt_arg);
  3058. if (ext_arg)
  3059. free (ext_arg);
  3060. }
  3061. /* Conditionals. */
  3062. /* A structure which contains `defined' variables. */
  3063. typedef struct defines {
  3064. struct defines *next;
  3065. char *name;
  3066. char *value;
  3067. } DEFINE;
  3068. /* The linked list of `set' defines. */
  3069. DEFINE *defines = NULL;
  3070. /* Add NAME to the list of `set' defines. */
  3071. static void
  3072. set (char *name, char *value)
  3073. {
  3074. DEFINE *temp;
  3075. for (temp = defines; temp; temp = temp->next)
  3076. if (strcmp (name, temp->name) == 0)
  3077. {
  3078. free (temp->value);
  3079. temp->value = xstrdup (value);
  3080. return;
  3081. }
  3082. temp = xmalloc (sizeof (DEFINE));
  3083. temp->next = defines;
  3084. temp->name = xstrdup (name);
  3085. temp->value = xstrdup (value);
  3086. defines = temp;
  3087. if (xml && !docbook)
  3088. {
  3089. xml_insert_element_with_attribute (SETVALUE, START, "name=\"%s\"", name);
  3090. execute_string ("%s", value);
  3091. xml_insert_element (SETVALUE, END);
  3092. }
  3093. }
  3094. /* Remove NAME from the list of `set' defines. */
  3095. static void
  3096. clear (char *name)
  3097. {
  3098. DEFINE *temp, *last;
  3099. last = NULL;
  3100. temp = defines;
  3101. while (temp)
  3102. {
  3103. if (strcmp (temp->name, name) == 0)
  3104. {
  3105. if (last)
  3106. last->next = temp->next;
  3107. else
  3108. defines = temp->next;
  3109. free (temp->name);
  3110. free (temp->value);
  3111. free (temp);
  3112. break;
  3113. }
  3114. last = temp;
  3115. temp = temp->next;
  3116. }
  3117. if (xml && !docbook)
  3118. {
  3119. xml_insert_element_with_attribute (CLEARVALUE, START, "name=\"%s\"", name);
  3120. xml_insert_element (CLEARVALUE, END);
  3121. }
  3122. }
  3123. /* Return the value of NAME. The return value is NULL if NAME is unset. */
  3124. static char *
  3125. set_p (char *name)
  3126. {
  3127. DEFINE *temp;
  3128. for (temp = defines; temp; temp = temp->next)
  3129. if (strcmp (temp->name, name) == 0)
  3130. return temp->value;
  3131. return NULL;
  3132. }
  3133. /* Create a variable whose name appears as the first word on this line. */
  3134. void
  3135. cm_set (void)
  3136. {
  3137. handle_variable (SET);
  3138. }
  3139. /* Remove a variable whose name appears as the first word on this line. */
  3140. void
  3141. cm_clear (void)
  3142. {
  3143. handle_variable (CLEAR);
  3144. }
  3145. void
  3146. cm_ifset (void)
  3147. {
  3148. handle_variable (IFSET);
  3149. }
  3150. void
  3151. cm_ifclear (void)
  3152. {
  3153. handle_variable (IFCLEAR);
  3154. }
  3155. /* This command takes braces, but we parse the contents specially, so we
  3156. don't use the standard brace popping code.
  3157. The syntax @ifeq{arg1, arg2, texinfo-commands} performs texinfo-commands
  3158. if ARG1 and ARG2 caselessly string compare to the same string, otherwise,
  3159. it produces no output. */
  3160. void
  3161. cm_ifeq (void)
  3162. {
  3163. char **arglist;
  3164. arglist = get_brace_args (quote_none);
  3165. if (arglist)
  3166. {
  3167. if (array_len (arglist) > 1)
  3168. {
  3169. if ((mbscasecmp (arglist[0], arglist[1]) == 0) &&
  3170. (arglist[2]))
  3171. execute_string ("%s\n", arglist[2]);
  3172. }
  3173. free_array (arglist);
  3174. }
  3175. }
  3176. void
  3177. cm_value (int arg, int start_pos, int end_pos)
  3178. {
  3179. static int value_level = 0, saved_meta_pos = -1;
  3180. /* xml_add_char() skips any content inside menus when output format is
  3181. Docbook, so @value{} is no use there. Also start_pos and end_pos does not
  3182. get updated, causing name to be empty string. So just return. */
  3183. if (docbook && in_menu)
  3184. return;
  3185. /* All the text after @value{ upto the matching } will eventually
  3186. disappear from output_paragraph, when this function is called
  3187. with ARG == END. If the text produced until then sets
  3188. meta_char_pos, we will need to restore it to the value it had
  3189. before @value was seen. So we need to save the previous value
  3190. of meta_char_pos here. */
  3191. if (arg == START)
  3192. {
  3193. /* If we are already inside some outer @value, don't overwrite
  3194. the value saved in saved_meta_pos. */
  3195. if (!value_level)
  3196. saved_meta_pos = meta_char_pos;
  3197. value_level++;
  3198. /* While the argument of @value is processed, we need to inhibit
  3199. textual transformations like "--" into "-", since @set didn't
  3200. do that when it grabbed the name of the variable. */
  3201. in_fixed_width_font++;
  3202. }
  3203. else
  3204. {
  3205. char *name = (char *) &output_paragraph[start_pos];
  3206. char *value;
  3207. output_paragraph[end_pos] = 0;
  3208. name = xstrdup (name);
  3209. value = set_p (name);
  3210. output_paragraph_offset = start_pos;
  3211. /* Restore the previous value of meta_char_pos if the stuff
  3212. inside this @value{} moved it. */
  3213. if (saved_meta_pos == -1) /* can't happen inside @value{} */
  3214. abort ();
  3215. if (value_level == 1
  3216. && meta_char_pos >= start_pos && meta_char_pos < end_pos)
  3217. {
  3218. meta_char_pos = saved_meta_pos;
  3219. saved_meta_pos = -1;
  3220. }
  3221. value_level--;
  3222. /* No need to decrement in_fixed_width_font, since before
  3223. we are called with arg == END, the reader loop already
  3224. popped the brace stack, which restored in_fixed_width_font,
  3225. among other things. */
  3226. if (value)
  3227. {
  3228. /* We need to get past the closing brace since the value may
  3229. expand to a context-sensitive macro (e.g. @xref) and produce
  3230. spurious warnings */
  3231. input_text_offset++;
  3232. execute_string ("%s", value);
  3233. input_text_offset--;
  3234. }
  3235. else
  3236. {
  3237. warning (_("undefined flag: %s"), name);
  3238. add_word_args (_("{No value for `%s'}"), name);
  3239. }
  3240. free (name);
  3241. }
  3242. }
  3243. /* Set, clear, or conditionalize based on ACTION. */
  3244. static void
  3245. handle_variable (int action)
  3246. {
  3247. char *name;
  3248. get_rest_of_line (0, &name);
  3249. /* If we hit the end of text in get_rest_of_line, backing up
  3250. input pointer will cause the last character of the last line
  3251. be pushed back onto the input, which is wrong. */
  3252. if (input_text_offset < input_text_length)
  3253. backup_input_pointer ();
  3254. handle_variable_internal (action, name);
  3255. free (name);
  3256. }
  3257. static void
  3258. handle_variable_internal (int action, char *name)
  3259. {
  3260. char *temp;
  3261. int delimiter, additional_text_present = 0;
  3262. /* Only the first word of NAME is a valid tag. */
  3263. temp = name;
  3264. delimiter = 0;
  3265. while (*temp && (delimiter || !whitespace (*temp)))
  3266. {
  3267. /* #if defined (SET_WITH_EQUAL) */
  3268. if (*temp == '"' || *temp == '\'')
  3269. {
  3270. if (*temp == delimiter)
  3271. delimiter = 0;
  3272. else
  3273. delimiter = *temp;
  3274. }
  3275. /* #endif SET_WITH_EQUAL */
  3276. temp++;
  3277. }
  3278. if (*temp)
  3279. additional_text_present++;
  3280. *temp = 0;
  3281. if (!*name)
  3282. line_error (_("%c%s requires a name"), COMMAND_PREFIX, command);
  3283. else
  3284. {
  3285. switch (action)
  3286. {
  3287. case SET:
  3288. {
  3289. char *value;
  3290. #if defined (SET_WITH_EQUAL)
  3291. /* Allow a value to be saved along with a variable. The value is
  3292. the text following an `=' sign in NAME, if any is present. */
  3293. for (value = name; *value && *value != '='; value++);
  3294. if (*value)
  3295. *value++ = 0;
  3296. if (*value == '"' || *value == '\'')
  3297. {
  3298. value++;
  3299. value[strlen (value) - 1] = 0;
  3300. }
  3301. #else /* !SET_WITH_EQUAL */
  3302. /* The VALUE of NAME is the remainder of the line sans
  3303. whitespace. */
  3304. if (additional_text_present)
  3305. {
  3306. value = temp + 1;
  3307. canon_white (value);
  3308. }
  3309. else
  3310. value = "";
  3311. #endif /* !SET_WITH_VALUE */
  3312. set (name, value);
  3313. }
  3314. break;
  3315. case CLEAR:
  3316. clear (name);
  3317. break;
  3318. case IFSET:
  3319. case IFCLEAR:
  3320. /* If IFSET and NAME is not set, or if IFCLEAR and NAME is set,
  3321. read lines from the the file until we reach a matching
  3322. "@end CONDITION". This means that we only take note of
  3323. "@ifset/clear" and "@end" commands. */
  3324. {
  3325. char condition[8];
  3326. int condition_len;
  3327. int orig_line_number = line_number;
  3328. if (action == IFSET)
  3329. strcpy (condition, "ifset");
  3330. else
  3331. strcpy (condition, "ifclear");
  3332. condition_len = strlen (condition);
  3333. if ((action == IFSET && !set_p (name))
  3334. || (action == IFCLEAR && set_p (name)))
  3335. {
  3336. int level = 0, done = 0;
  3337. while (!done && input_text_offset < input_text_length)
  3338. {
  3339. char *freeable_line, *line;
  3340. get_rest_of_line (0, &freeable_line);
  3341. for (line = freeable_line; whitespace (*line); line++);
  3342. if (*line == COMMAND_PREFIX &&
  3343. (strncmp (line + 1, condition, condition_len) == 0))
  3344. level++;
  3345. else if (strncmp (line, "@end", 4) == 0)
  3346. {
  3347. char *cname = line + 4;
  3348. char *temp;
  3349. while (*cname && whitespace (*cname))
  3350. cname++;
  3351. temp = cname;
  3352. while (*temp && !whitespace (*temp))
  3353. temp++;
  3354. *temp = 0;
  3355. if (strcmp (cname, condition) == 0)
  3356. {
  3357. if (!level)
  3358. {
  3359. done = 1;
  3360. }
  3361. else
  3362. level--;
  3363. }
  3364. }
  3365. free (freeable_line);
  3366. }
  3367. if (!done)
  3368. file_line_error (input_filename, orig_line_number,
  3369. _("Reached eof before matching @end %s"),
  3370. condition);
  3371. /* We found the end of a false @ifset/ifclear. If we are
  3372. in a menu, back up over the newline that ends the ifset,
  3373. since that newline may also begin the next menu entry. */
  3374. break;
  3375. }
  3376. else
  3377. {
  3378. if (action == IFSET)
  3379. begin_insertion (ifset);
  3380. else
  3381. begin_insertion (ifclear);
  3382. }
  3383. }
  3384. break;
  3385. }
  3386. }
  3387. }
  3388. /* Execution of random text not in file. */
  3389. typedef struct {
  3390. char *string; /* The string buffer. */
  3391. int size; /* The size of the buffer. */
  3392. int in_use; /* Nonzero means string currently in use. */
  3393. } EXECUTION_STRING;
  3394. static EXECUTION_STRING **execution_strings = NULL;
  3395. static int execution_strings_index = 0;
  3396. static int execution_strings_slots = 0;
  3397. static EXECUTION_STRING *
  3398. get_execution_string (int initial_size)
  3399. {
  3400. int i = 0;
  3401. EXECUTION_STRING *es = NULL;
  3402. if (execution_strings)
  3403. {
  3404. for (i = 0; i < execution_strings_index; i++)
  3405. if (execution_strings[i] && (execution_strings[i]->in_use == 0))
  3406. {
  3407. es = execution_strings[i];
  3408. break;
  3409. }
  3410. }
  3411. if (!es)
  3412. {
  3413. if (execution_strings_index + 1 >= execution_strings_slots)
  3414. {
  3415. execution_strings = xrealloc
  3416. (execution_strings,
  3417. (execution_strings_slots += 3) * sizeof (EXECUTION_STRING *));
  3418. for (; i < execution_strings_slots; i++)
  3419. execution_strings[i] = NULL;
  3420. }
  3421. execution_strings[execution_strings_index] =
  3422. xmalloc (sizeof (EXECUTION_STRING));
  3423. es = execution_strings[execution_strings_index];
  3424. execution_strings_index++;
  3425. es->size = 0;
  3426. es->string = NULL;
  3427. es->in_use = 0;
  3428. }
  3429. if (initial_size > es->size)
  3430. {
  3431. es->string = xrealloc (es->string, initial_size);
  3432. es->size = initial_size;
  3433. }
  3434. return es;
  3435. }
  3436. /* Given a pointer to TEXT and its desired length NEW_LEN, find TEXT's
  3437. entry in the execution_strings[] array and change the .STRING and
  3438. .SIZE members of that entry as appropriate. */
  3439. void
  3440. maybe_update_execution_strings (char **text, unsigned int new_len)
  3441. {
  3442. int i = 0;
  3443. if (execution_strings)
  3444. {
  3445. for (i = 0; i < execution_strings_index; i++)
  3446. if (execution_strings[i] && (execution_strings[i]->in_use == 1) &&
  3447. execution_strings[i]->string == *text)
  3448. {
  3449. /* Don't ever shrink the string storage in execution_strings[]!
  3450. execute_string assumes that it is always big enough to store
  3451. every possible execution_string, and will break if that's
  3452. not true. So we only enlarge the string storage if the
  3453. current size isn't big enough. */
  3454. if (execution_strings[i]->size < new_len)
  3455. {
  3456. execution_strings[i]->string =
  3457. *text = xrealloc (*text, new_len + 1);
  3458. execution_strings[i]->size = new_len + 1;
  3459. }
  3460. return;
  3461. }
  3462. }
  3463. /* We should *never* end up here, since if we are inside
  3464. execute_string, TEXT is always in execution_strings[]. */
  3465. abort ();
  3466. }
  3467. #define EXECUTE_STRING_MAX (32*1024) /* FIXXME: this is an arbitrary limit. */
  3468. /* Execute the string produced by formatting the ARGs with FORMAT. This
  3469. is like submitting a new file with @include. */
  3470. void
  3471. execute_string (char *format, ...)
  3472. {
  3473. EXECUTION_STRING *es;
  3474. char *temp_string, *temp_input_filename;
  3475. va_list ap;
  3476. int insertion_level_at_start = insertion_level;
  3477. es = get_execution_string (EXECUTE_STRING_MAX);
  3478. temp_string = es->string;
  3479. es->in_use = 1;
  3480. va_start (ap, format);
  3481. vsnprintf (temp_string, EXECUTE_STRING_MAX, format, ap);
  3482. va_end (ap);
  3483. pushfile ();
  3484. input_text_offset = 0;
  3485. input_text = temp_string;
  3486. input_text_length = strlen (temp_string);
  3487. input_filename = xstrdup (input_filename);
  3488. temp_input_filename = input_filename;
  3489. executing_string++;
  3490. reader_loop ();
  3491. /* If insertion stack level changes during execution, that means a multiline
  3492. command is used inside braces or @section ... kind of commands. */
  3493. if (insertion_level_at_start != insertion_level && !executing_macro)
  3494. {
  3495. line_error (_("Multiline command %c%s used improperly"),
  3496. COMMAND_PREFIX,
  3497. command);
  3498. /* We also need to keep insertion_level intact to make sure warnings are
  3499. issued for @end ... command. */
  3500. while (insertion_level > insertion_level_at_start)
  3501. pop_insertion ();
  3502. }
  3503. popfile ();
  3504. executing_string--;
  3505. es->in_use = 0;
  3506. free (temp_input_filename);
  3507. }
  3508. /* Return what would be output for STR (in newly-malloced memory), i.e.,
  3509. expand Texinfo commands according to the current output format. If
  3510. IMPLICIT_CODE is set, expand @code{STR}. This is generally used for
  3511. short texts; filling, indentation, and html escapes are disabled. */
  3512. char *
  3513. expansion (char *str, int implicit_code)
  3514. {
  3515. return maybe_escaped_expansion (str, implicit_code, 0);
  3516. }
  3517. /* Do HTML escapes according to DO_HTML_ESCAPE. Needed in
  3518. cm_printindex, q.v. */
  3519. char *
  3520. maybe_escaped_expansion (char *str, int implicit_code, int do_html_escape)
  3521. {
  3522. char *result;
  3523. /* Inhibit indentation and filling, so that extra newlines
  3524. are not added to the expansion. (This is undesirable if
  3525. we write the expanded text to macro_expansion_output_stream.) */
  3526. int saved_filling_enabled = filling_enabled;
  3527. int saved_indented_fill = indented_fill;
  3528. int saved_no_indent = no_indent;
  3529. int saved_escape_html = escape_html;
  3530. filling_enabled = 0;
  3531. indented_fill = 0;
  3532. no_indent = 1;
  3533. escape_html = do_html_escape;
  3534. xml_no_para++;
  3535. result = full_expansion (str, implicit_code);
  3536. filling_enabled = saved_filling_enabled;
  3537. indented_fill = saved_indented_fill;
  3538. no_indent = saved_no_indent;
  3539. escape_html = saved_escape_html;
  3540. xml_no_para--;
  3541. return result;
  3542. }
  3543. /* Expand STR (or @code{STR} if IMPLICIT_CODE is nonzero). No change to
  3544. any formatting parameters -- filling, indentation, html escapes,
  3545. etc., are not reset. Always returned in new memory. */
  3546. char *
  3547. full_expansion (char *str, int implicit_code)
  3548. {
  3549. int length;
  3550. char *result;
  3551. /* Inhibit any real output. */
  3552. int start = output_paragraph_offset;
  3553. int saved_paragraph_is_open = paragraph_is_open;
  3554. /* More output state to save. */
  3555. int saved_meta_pos = meta_char_pos;
  3556. int saved_last_char = last_inserted_character;
  3557. int saved_last_nl = last_char_was_newline;
  3558. /* If we are called in the middle of processing a command, we need
  3559. to dup and save the global variable `command' (which holds the
  3560. name of this command), since the recursive reader loop will free
  3561. it from under our feet if it finds any macros in STR. */
  3562. char *saved_command = command ? xstrdup (command) : NULL;
  3563. inhibit_output_flushing ();
  3564. paragraph_is_open = 1;
  3565. if (strlen (str) > (implicit_code
  3566. ? EXECUTE_STRING_MAX - 1 - sizeof("@code{}")
  3567. : EXECUTE_STRING_MAX - 1))
  3568. line_error (_("`%.40s...' is too long for expansion; not expanded"), str);
  3569. else
  3570. execute_string (implicit_code ? "@code{%s}" : "%s", str);
  3571. uninhibit_output_flushing ();
  3572. /* Copy the expansion from the buffer. */
  3573. length = output_paragraph_offset - start;
  3574. result = xmalloc (1 + length);
  3575. memcpy (result, (char *) (output_paragraph + start), length);
  3576. result[length] = 0;
  3577. /* Pretend it never happened. */
  3578. free_and_clear (&command);
  3579. command = saved_command;
  3580. output_paragraph_offset = start;
  3581. paragraph_is_open = saved_paragraph_is_open;
  3582. meta_char_pos = saved_meta_pos;
  3583. last_inserted_character = saved_last_char;
  3584. last_char_was_newline = saved_last_nl;
  3585. return result;
  3586. }
  3587. /* Return text (info) expansion of STR no matter what the current output
  3588. format is. */
  3589. char *
  3590. text_expansion (char *str)
  3591. {
  3592. char *ret;
  3593. int save_html = html;
  3594. int save_xml = xml;
  3595. int save_docbook = docbook;
  3596. html = 0;
  3597. xml = 0;
  3598. docbook = 0;
  3599. ret = expansion (str, 0);
  3600. html = save_html;
  3601. xml = save_xml;
  3602. docbook = save_docbook;
  3603. return ret;
  3604. }
  3605. /* Set the paragraph indentation variable to the value specified in STRING.
  3606. Values can be:
  3607. `asis': Don't change existing indentation.
  3608. `none': Remove existing indentation.
  3609. NUM: Indent NUM spaces at the starts of paragraphs.
  3610. If NUM is zero, we assume `none'.
  3611. Returns 0 if successful, or nonzero if STRING isn't one of the above. */
  3612. int
  3613. set_paragraph_indent (char *string)
  3614. {
  3615. if (strcmp (string, "asis") == 0 || strcmp (string, _("asis")) == 0)
  3616. paragraph_start_indent = 0;
  3617. else if (strcmp (string, "none") == 0 || strcmp (string, _("none")) == 0)
  3618. paragraph_start_indent = -1;
  3619. else
  3620. {
  3621. if (sscanf (string, "%d", &paragraph_start_indent) != 1)
  3622. return -1;
  3623. else
  3624. {
  3625. if (paragraph_start_indent == 0)
  3626. paragraph_start_indent = -1;
  3627. }
  3628. }
  3629. return 0;
  3630. }
  3631. /* Translate MSGID according to the document language (@documentlanguage
  3632. value or --document-language), rather than the current locale
  3633. (LANGUAGE/LC_ALL/LANG). This code is from the get_title function in
  3634. gettext. (xsetenv and unsetenv come from the gnulib xsetenv module.) */
  3635. char *
  3636. getdocumenttext (const char *msgid)
  3637. {
  3638. /* The original get_title also saves, sets, and restores
  3639. OUTPUT_CHARSET, so that the translation will be given in
  3640. the proper encoding (via canonical_locale_charset). But defining
  3641. that function ends up pulling a whole lot of subsidiary functions.
  3642. Not sure how to handle it; skip the whole thing for now. */
  3643. const char *tmp;
  3644. char *old_LC_ALL;
  3645. char *old_LANGUAGE;
  3646. const char *result;
  3647. #ifdef HAVE_SETLOCALE
  3648. char *old_locale;
  3649. #endif
  3650. /* Save LC_ALL, LANGUAGE environment variables. */
  3651. tmp = getenv ("LC_ALL");
  3652. old_LC_ALL = (tmp != NULL ? xstrdup (tmp) : NULL);
  3653. tmp = getenv ("LANGUAGE");
  3654. old_LANGUAGE = (tmp != NULL ? xstrdup (tmp) : NULL);
  3655. xsetenv ("LC_ALL", document_language, 1);
  3656. unsetenv ("LANGUAGE");
  3657. #ifdef HAVE_SETLOCALE
  3658. old_locale = xstrdup (setlocale (LC_ALL, NULL));
  3659. if (setlocale (LC_ALL, "") == NULL)
  3660. /* Nonexistent locale. Use the original. */
  3661. result = msgid;
  3662. else
  3663. #endif
  3664. {
  3665. /* Fetch the translation. */
  3666. result = gettext ((char *) msgid);
  3667. }
  3668. /* Restore LC_ALL, LANGUAGE environment variables. */
  3669. if (old_LC_ALL != NULL)
  3670. xsetenv ("LC_ALL", old_LC_ALL, 1), free (old_LC_ALL);
  3671. else
  3672. unsetenv ("LC_ALL");
  3673. if (old_LANGUAGE != NULL)
  3674. xsetenv ("LANGUAGE", old_LANGUAGE, 1), free (old_LANGUAGE);
  3675. else
  3676. unsetenv ("LANGUAGE");
  3677. #ifdef HAVE_SETLOCALE
  3678. setlocale (LC_ALL, old_locale);
  3679. free (old_locale);
  3680. #endif
  3681. return (char *) result;
  3682. }