Parcourir la source

Support args from stdin

Jakob Truelsen il y a 16 ans
Parent
commit
17873c90f9
7 fichiers modifiés avec 51 ajouts et 27 suppressions
  1. 1 0
      src/arguments.cc
  2. 11 9
      src/commandlineparser.cc
  3. 1 1
      src/commandlineparser.hh
  4. 1 0
      src/commandlineparser_p.hh
  5. 11 0
      src/docparts.cc
  6. 2 0
      src/settings.hh
  7. 24 17
      src/wkhtmltopdf.cc

+ 1 - 0
src/arguments.cc

@@ -453,6 +453,7 @@ CommandLineParserPrivate::CommandLineParserPrivate(Settings & s):
 	addarg("redirect-delay",0,"Wait some miliseconds for js-redirects", new IntSetter(s.jsredirectwait,"msec",200));
 	addarg("enable-plugins",0,"Enable installed plugins (such as flash", new ConstSetter<bool>(s.enablePlugins,true,false));
 	addarg("zoom",0,"Use this zoom factor", new FloatSetter(s.zoomFactor,"float",1.0));
+	addarg("read-args-from-stdin",0,"Read command line arguments from stdin", new ConstSetter<bool>(s.readArgsFromStdin,true,false));
 
 	qthack(true);
 	addarg("disable-internal-links",0,"Do no make local links", new ConstSetter<bool>(s.useLocalLinks,false,true));

+ 11 - 9
src/commandlineparser.cc

@@ -209,7 +209,7 @@ void CommandLineParser::loadDefaults() {
  * \param argc the number of command line arguments
  * \param argv a NULL terminated list with the arguments
  */
-void CommandLineParser::parseArguments(int argc, const char ** argv) {
+void CommandLineParser::parseArguments(int argc, const char ** argv, bool final) {
 	bool defaultMode = false;
 	for (int i=1; i < argc; ++i) {
 		if (argv[i][0] != '-' || argv[i][1] == '\0' || defaultMode ) {
@@ -271,14 +271,16 @@ void CommandLineParser::parseArguments(int argc, const char ** argv) {
 		}
 	}
 
-	if (d->settings.in.size() < 2) {
-		#warning "If we do not support more then one input file check for that here"
-		fprintf(stderr, "You need to specify atleast one input file, and exactly one output file\nUse - for stdin or stdout\n\n");
-		d->usage(stderr, false);
-		exit(1);
+	if(final || ! d->settings.readArgsFromStdin) {
+		if (d->settings.in.size() < 2) {
+            #warning "If we do not support more then one input file check for that here"
+			fprintf(stderr, "You need to specify atleast one input file, and exactly one output file\nUse - for stdin or stdout\n\n");
+			d->usage(stderr, false);
+			exit(1);
+		}
+		//The last default argument was realy the name of the output file
+		d->settings.out = d->settings.in.back();
+		d->settings.in.pop_back();
 	}
-	//The last default argument was realy the name of the output file
-	d->settings.out = d->settings.in.back();
-	d->settings.in.pop_back();
 }
 

+ 1 - 1
src/commandlineparser.hh

@@ -31,7 +31,7 @@ public:
 	void manpage(FILE * fd) const;
 	void readme(FILE * fd, bool html) const;
 	void loadDefaults();
-	void parseArguments(int argc, const char ** argv);
+	void parseArguments(int argc, const char ** argv, bool final=false);
 private:
 	CommandLineParserPrivate * d;
 };

+ 1 - 0
src/commandlineparser_p.hh

@@ -90,6 +90,7 @@ public:
 	void outputSynopsis(Outputter * o) const;
 	void outputDescripton(Outputter * o) const;
 	void outputProxyDoc(Outputter * o) const;
+	void outputArgsFromStdin(Outputter * o) const;
 	void outputHeaderFooterDoc(Outputter * o) const;
 	void outputOutlineDoc(Outputter * o) const;
 	void outputNotPatched(Outputter * o, bool sure) const;

+ 11 - 0
src/docparts.cc

@@ -233,6 +233,17 @@ void CommandLineParserPrivate::outputDocStart(Outputter * o) const {
 	o->endSection();
 }
 
+/*!
+  Output information on how to use read-args-from-stdin
+  \param o The outputter to output to
+  \todo Do a better explanation
+*/
+#warning "explain what and why"
+void CommandLineParserPrivate::outputArgsFromStdin(Outputter * o) const {
+	o->beginSection("Reading arguments from stdin");
+	o->endSection();
+}
+
 /*!
   Output information on how to compile
   \param o The outputter to output to

+ 2 - 0
src/settings.hh

@@ -165,6 +165,8 @@ struct Settings {
 	QString out;
 	//! Map of custum header valiabels
 	QHash<QString, QString> customHeaders;
+	//! Should we read arguments from stdin
+	bool readArgsFromStdin;
 
 	static QPrinter::PageSize strToPageSize(const char * s, bool * ok=0);
 	static QPair<qreal, QPrinter::Unit> strToUnitReal(const char * s, bool * ok=0);

+ 24 - 17
src/wkhtmltopdf.cc

@@ -131,26 +131,33 @@ int main(int argc, char * argv[]) {
 	QApplication a(argc, argv, use_graphics);
 	a.setStyle(new QCommonStyle());
 
+
+	if (settings.readArgsFromStdin) {
+		char buff[20400];
+		char *nargv[1000];
+		nargv[0] = argv[0];
+		while (fgets(buff,20398,stdin)) {
+			//x.resetPages();
+			int nargc=1;
+			parseString(buff,nargc,nargv);
+			CommandLineParser parser(settings);
+			//Setup default values in settings
+			parser.loadDefaults();
+			//Parse the arguments
+			parser.parseArguments(argc, (const char**)argv);
+			parser.parseArguments(nargc, (const char**)nargv, true);
+			
+			PageConverter converter(settings);
+			ProgressFeedback feedback(converter);
+			if (!converter.convert())
+				exit(EXIT_FAILURE);
+		}
+		exit(EXIT_SUCCESS);
+	}
 	//Create the actual page converter to convert the pages
 	PageConverter converter(settings);
 	ProgressFeedback feedback(converter);
-//  for (int i=1; i < argc; ++i)
-// 		if (!strcmp(argv[i],"--read-args-from-stdin")) {
-// 			char buff[20400];
-// 			char *nargv[1000];
-// 			nargv[0] = argv[0];
-// 			while (fgets(buff,20398,stdin)) {
-// 				//x.resetPages();
-// 				int nargc=1;
-// 				parseString(buff,nargc,nargv);
-// 				parser.loadDefaults();
-// 				parser.parseArguments(nargc, (const char**)nargv);
-// 				converter.convert();
-// 				//x.run(nargc,(const char**)nargv);
-// 				//qApp->exec(); //Wait for application to terminate
-// 			}
-// 			exit(0);
-// 		}
+	
 	if (!converter.convert())
 		exit(EXIT_FAILURE);
 	switch(converter.httpErrorCode()) {