Print this page
Update to 1.12.3.
   1 /*      $Id: main.c,v 1.165 2011/10/06 22:29:12 kristaps Exp $ */
   2 /*
   3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
   4  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
   5  *
   6  * Permission to use, copy, modify, and distribute this software for any
   7  * purpose with or without fee is hereby granted, provided that the above
   8  * copyright notice and this permission notice appear in all copies.
   9  *
  10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17  */
  18 #ifdef HAVE_CONFIG_H
  19 #include "config.h"
  20 #endif
  21 
  22 #include <assert.h>
  23 #include <stdio.h>
  24 #include <stdint.h>


  68 
  69 static  int               moptions(enum mparset *, char *);
  70 static  void              mmsg(enum mandocerr, enum mandoclevel,
  71                                 const char *, int, int, const char *);
  72 static  void              parse(struct curparse *, int, 
  73                                 const char *, enum mandoclevel *);
  74 static  int               toptions(struct curparse *, char *);
  75 static  void              usage(void) __attribute__((noreturn));
  76 static  void              version(void) __attribute__((noreturn));
  77 static  int               woptions(struct curparse *, char *);
  78 
  79 static  const char       *progname;
  80 
  81 int
  82 main(int argc, char *argv[])
  83 {
  84         int              c;
  85         struct curparse  curp;
  86         enum mparset     type;
  87         enum mandoclevel rc;

  88 
  89         progname = strrchr(argv[0], '/');
  90         if (progname == NULL)
  91                 progname = argv[0];
  92         else
  93                 ++progname;
  94 
  95         memset(&curp, 0, sizeof(struct curparse));
  96 
  97         type = MPARSE_AUTO;
  98         curp.outtype = OUTT_ASCII;
  99         curp.wlevel  = MANDOCLEVEL_FATAL;

 100 
 101         /* LINTED */
 102         while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
 103                 switch (c) {













 104                 case ('m'):
 105                         if ( ! moptions(&type, optarg))
 106                                 return((int)MANDOCLEVEL_BADARG);
 107                         break;
 108                 case ('O'):
 109                         (void)strlcat(curp.outopts, optarg, BUFSIZ);
 110                         (void)strlcat(curp.outopts, ",", BUFSIZ);
 111                         break;
 112                 case ('T'):
 113                         if ( ! toptions(&curp, optarg))
 114                                 return((int)MANDOCLEVEL_BADARG);
 115                         break;
 116                 case ('W'):
 117                         if ( ! woptions(&curp, optarg))
 118                                 return((int)MANDOCLEVEL_BADARG);
 119                         break;
 120                 case ('V'):
 121                         version();
 122                         /* NOTREACHED */
 123                 default:
 124                         usage();
 125                         /* NOTREACHED */
 126                 }
 127 
 128         curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
 129 
 130         /*
 131          * Conditionally start up the lookaside buffer before parsing.
 132          */
 133         if (OUTT_MAN == curp.outtype)
 134                 mparse_keep(curp.mp);
 135 
 136         argc -= optind;
 137         argv += optind;
 138 
 139         rc = MANDOCLEVEL_OK;
 140 
 141         if (NULL == *argv)
 142                 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
 143 
 144         while (*argv) {
 145                 parse(&curp, -1, *argv, &rc);
 146                 if (MANDOCLEVEL_OK != rc && curp.wstop)
 147                         break;
 148                 ++argv;
 149         }
 150 
 151         if (curp.outfree)
 152                 (*curp.outfree)(curp.outdata);
 153         if (curp.mp)
 154                 mparse_free(curp.mp);

 155 
 156         return((int)rc);
 157 }
 158 
 159 static void
 160 version(void)
 161 {
 162 
 163         printf("%s %s\n", progname, VERSION);
 164         exit((int)MANDOCLEVEL_OK);
 165 }
 166 
 167 static void
 168 usage(void)
 169 {
 170 
 171         fprintf(stderr, "usage: %s "
 172                         "[-V] "
 173                         "[-foption] "
 174                         "[-mformat] "
 175                         "[-Ooption] "
 176                         "[-Toutput] "
 177                         "[-Wlevel] "
 178                         "[file...]\n", 
 179                         progname);
 180 
 181         exit((int)MANDOCLEVEL_BADARG);
 182 }
 183 
 184 static void
 185 parse(struct curparse *curp, int fd, 
 186                 const char *file, enum mandoclevel *level)
 187 {
 188         enum mandoclevel  rc;
 189         struct mdoc      *mdoc;
 190         struct man       *man;
 191 
 192         /* Begin by parsing the file itself. */
 193 
 194         assert(file);
 195         assert(fd >= -1);
 196 
 197         rc = mparse_readfd(curp->mp, fd, file);
 198 


   1 /*      $Id: main.c,v 1.167 2012/11/19 17:22:26 schwarze Exp $ */
   2 /*
   3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
   4  * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
   5  *
   6  * Permission to use, copy, modify, and distribute this software for any
   7  * purpose with or without fee is hereby granted, provided that the above
   8  * copyright notice and this permission notice appear in all copies.
   9  *
  10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17  */
  18 #ifdef HAVE_CONFIG_H
  19 #include "config.h"
  20 #endif
  21 
  22 #include <assert.h>
  23 #include <stdio.h>
  24 #include <stdint.h>


  68 
  69 static  int               moptions(enum mparset *, char *);
  70 static  void              mmsg(enum mandocerr, enum mandoclevel,
  71                                 const char *, int, int, const char *);
  72 static  void              parse(struct curparse *, int, 
  73                                 const char *, enum mandoclevel *);
  74 static  int               toptions(struct curparse *, char *);
  75 static  void              usage(void) __attribute__((noreturn));
  76 static  void              version(void) __attribute__((noreturn));
  77 static  int               woptions(struct curparse *, char *);
  78 
  79 static  const char       *progname;
  80 
  81 int
  82 main(int argc, char *argv[])
  83 {
  84         int              c;
  85         struct curparse  curp;
  86         enum mparset     type;
  87         enum mandoclevel rc;
  88         char            *defos;
  89 
  90         progname = strrchr(argv[0], '/');
  91         if (progname == NULL)
  92                 progname = argv[0];
  93         else
  94                 ++progname;
  95 
  96         memset(&curp, 0, sizeof(struct curparse));
  97 
  98         type = MPARSE_AUTO;
  99         curp.outtype = OUTT_ASCII;
 100         curp.wlevel  = MANDOCLEVEL_FATAL;
 101         defos = NULL;
 102 
 103         /* LINTED */
 104         while (-1 != (c = getopt(argc, argv, "I:m:O:T:VW:")))
 105                 switch (c) {
 106                 case ('I'):
 107                         if (strncmp(optarg, "os=", 3)) {
 108                                 fprintf(stderr, "-I%s: Bad argument\n",
 109                                                 optarg);
 110                                 return((int)MANDOCLEVEL_BADARG);
 111                         }
 112                         if (defos) {
 113                                 fprintf(stderr, "-I%s: Duplicate argument\n",
 114                                                 optarg);
 115                                 return((int)MANDOCLEVEL_BADARG);
 116                         }
 117                         defos = mandoc_strdup(optarg + 3);
 118                         break;
 119                 case ('m'):
 120                         if ( ! moptions(&type, optarg))
 121                                 return((int)MANDOCLEVEL_BADARG);
 122                         break;
 123                 case ('O'):
 124                         (void)strlcat(curp.outopts, optarg, BUFSIZ);
 125                         (void)strlcat(curp.outopts, ",", BUFSIZ);
 126                         break;
 127                 case ('T'):
 128                         if ( ! toptions(&curp, optarg))
 129                                 return((int)MANDOCLEVEL_BADARG);
 130                         break;
 131                 case ('W'):
 132                         if ( ! woptions(&curp, optarg))
 133                                 return((int)MANDOCLEVEL_BADARG);
 134                         break;
 135                 case ('V'):
 136                         version();
 137                         /* NOTREACHED */
 138                 default:
 139                         usage();
 140                         /* NOTREACHED */
 141                 }
 142 
 143         curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp, defos);
 144 
 145         /*
 146          * Conditionally start up the lookaside buffer before parsing.
 147          */
 148         if (OUTT_MAN == curp.outtype)
 149                 mparse_keep(curp.mp);
 150 
 151         argc -= optind;
 152         argv += optind;
 153 
 154         rc = MANDOCLEVEL_OK;
 155 
 156         if (NULL == *argv)
 157                 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
 158 
 159         while (*argv) {
 160                 parse(&curp, -1, *argv, &rc);
 161                 if (MANDOCLEVEL_OK != rc && curp.wstop)
 162                         break;
 163                 ++argv;
 164         }
 165 
 166         if (curp.outfree)
 167                 (*curp.outfree)(curp.outdata);
 168         if (curp.mp)
 169                 mparse_free(curp.mp);
 170         free(defos);
 171 
 172         return((int)rc);
 173 }
 174 
 175 static void
 176 version(void)
 177 {
 178 
 179         printf("%s %s\n", progname, VERSION);
 180         exit((int)MANDOCLEVEL_OK);
 181 }
 182 
 183 static void
 184 usage(void)
 185 {
 186 
 187         fprintf(stderr, "usage: %s "
 188                         "[-V] "
 189                         "[-Ios=name] "
 190                         "[-mformat] "
 191                         "[-Ooption] "
 192                         "[-Toutput] "
 193                         "[-Wlevel]\n"
 194                         "\t      [file ...]\n", 
 195                         progname);
 196 
 197         exit((int)MANDOCLEVEL_BADARG);
 198 }
 199 
 200 static void
 201 parse(struct curparse *curp, int fd, 
 202                 const char *file, enum mandoclevel *level)
 203 {
 204         enum mandoclevel  rc;
 205         struct mdoc      *mdoc;
 206         struct man       *man;
 207 
 208         /* Begin by parsing the file itself. */
 209 
 210         assert(file);
 211         assert(fd >= -1);
 212 
 213         rc = mparse_readfd(curp->mp, fd, file);
 214