The writing of the Man Handbook for customizing command line utilities and commands under Linux __linux

Source: Internet
Author: User
Tags bz2 gz file sprintf

The function of this command-line program is to solve the C code under Linux, quickly create templates for. h and. c files, in order to standardize the code. The main purpose of implementing this program is that I want to familiarize myself with the writing of the command-line program and how to customize the man manual for the commands I write.

This custom command is create, and the following is the implementation process:

//create.c

/********************************************************** filename:create.c Filefunc:linux to implement. C and. h file templates version:v0.1 author:sunrier date:2012-06-28 descp:linux implement command line utilities * * * /#include <stdio.h> #include <stdlib.h> #
Include <string.h> #include <time.h> void Getlocaltime (char *pouttime);
unsigned char createfile (unsigned char ucfileflag,char *pfilename);
void Strtoupper (char *pcstr);
void Write_h (FILE *fp, char *pfilename,char *paliasname);


void Write_c (FILE *fp,char *pfilename);  
    void Getlocaltime (char *pouttime) {time_t t;  
    struct TM TM1;  
    T = time (NULL);  
    memcpy (&tm1,localtime (&t), sizeof (struct TM)); sprintf (Pouttime, "%04d-%02d-%02d%02d:%02d:%02d", tm1.tm_year+1900,tm1.tm_mon+1,tm1.tm_mday,tm1.tm_hour,tm1.tm_  
    MIN,TM1.TM_SEC); /*yyyymmddhhmmss (days and minutes)/} void Strtoupper (chaR *pcstr) {while ('!=*pcstr ') {if (Islower (*PCSTR)) *pcstr-= 32;
	++PCSTR;
	
	int main (int argc,char *argv[]) {unsigned char ucretcode = 1; if (2==ARGC) {if (0==memcmp (argv[1),--help, strlen (argv[1))) {fprintf (stderr, "Usage: Create option ...")
			File ... \ n "); fprintf (stderr, "Choose to create the specified < File > is. h or. c According to the settings of the < options > Parameters."
			\ n ");
			fprintf (stderr, "\n< option > < file > Parameter settings \ n");
			fprintf (stderr, "-h filename to create \ n" named Filename.h);
			fprintf (stderr, "-c filename is created named filename.c file \ n");
			fprintf (stderr, "--help Display this help message and Exit \ n");
			fprintf (stderr, "--version output version information and exit \ n"); fprintf (stderr, "\ n) If you encounter a problem, report an error to <sunrier@gmail.com>.
		\ n ");
			else if (0==memcmp (argv[1], "--version", strlen (argv[1))) {fprintf (stderr, "Create (free Code) 1.01\n");
			fprintf (stderr, "This is free software, mutual exchange learning can join QQ group: 80060765 \");
			fprintf (stderr, "\ n");
		fprintf (stderr, "written by Sunrier");
else {fprintf (stderr, '%s: invalid option \ '%s\ ' \ n ', argv[0],argv[1]);			fprintf (stderr, "Please try executing \" Create--help\ "to get more information. \ n");  
	} exit (1);
		} if (3!=ARGC) {fprintf (stderr, "%s: invalid option \ n", argv[0]);
		fprintf (stderr, "Please try executing \" Create--help\ "to get more information. \ n");
	Exit (1);
		} if ('-' ==*argv[1]) {if (0==memcmp (argv[1), "-H", strlen (argv[1))) {Ucretcode = CreateFile (0,argv[2));          		
		else if (0==memcmp (argv[1], "-C", strlen (argv[1))) {Ucretcode = CreateFile (1,argv[2));
			else {fprintf (stderr, '%s: invalid option \ '%s\ ' \ '%s\ ' \ n ', argv[0],argv[1],argv[2]); 
			fprintf (stderr, "Please try executing \" Create--help\ "to get more information. \ n");
		Exit (1);
		} else {fprintf (stderr, "%s: invalid option \%s\" \%s\ "\ n", argv[0],argv[1],argv[2]); 
		fprintf (stderr, "Please try executing \" Create--help\ "to get more information. \ n");
	Exit (1);
return ucretcode;
	} unsigned char createfile (unsigned char ucfileflag,char *pfilename) {unsigned char ucretcode = 1;
	Char szfilename[30];
	
	Char szaliasname[30];
	FILE *FP = NULL;
	
	unsigned int uiI; if (strlen (PFIlename) >27) {fprintf (stderr, "%s: Failed to create file, filename length too long!\n", pfilename);
	return 1;
	
	} memset (Szfilename,0,sizeof (szFileName));
	if (!ucfileflag) {sprintf (szFileName, "%s.h", pfilename);
	else {sprintf (szFileName, "%s.c", pfilename);
	} memset (Szaliasname,0,sizeof (szaliasname));
	sprintf (Szaliasname, "%s", pfilename);
	Strtoupper (Szaliasname);
	fp = fopen (szFileName, "R");
		if (NULL!=FP) {fclose (FP);
		fprintf (stderr, "%s: Failed to create file, you specified filename already exists under current directory!\n", pfilename);
	return 1;
	fp = NULL;
	fp = fopen (szFileName, "a");
		if (NULL==FP) {fprintf (stderr, "%s: Create file failed!\n", pfilename);
	return 1;
	} if (!ucfileflag) {write_h (fp,szfilename,szaliasname);
	else {write_c (fp,szfilename);
	
	Fclose (FP);
return 0;
	
	} void Write_h (FILE *fp, char *pfilename,char *paliasname) {char sztime[30];
	memset (sztime,0,sizeof (sztime));
	Getlocaltime (Sztime);
	fprintf (FP, "/*************************************************************\n"); fprintf (FP, "FilenaMe:%s \ n ", pfilename);
	fprintf (FP, "Filefunc: definition header file \ n");
	fprintf (FP, "version:v0.1 \ n");
	fprintf (FP, "Author:sunrier \ n");
	fprintf (FP, "Date:%s \ n", sztime);
	fprintf (FP, "descp:linux head file \ n");
	fprintf (FP, "*************************************************************/\n");
	fprintf (FP, "#ifndef __%s_h__\n", paliasname);
	fprintf (FP, "#define __%s_h__\n", paliasname);
	fprintf (FP, "\ n");
	fprintf (FP, "#ifdef __cplusplus\n");
	fprintf (FP, "extern \" c\ "{\ n");
	fprintf (FP, "#endif \ n");
	fprintf (FP, "\n\n\n\n");
	fprintf (FP, "#ifdef __cplusplus\n");
	fprintf (FP, "}\n");
	fprintf (FP, "#endif \ n");
	fprintf (FP, "\ n");			
	
fprintf (FP, "#endif \ n");
	
	} void Write_c (FILE *fp,char *pfilename) {char sztime[30];
	memset (sztime,0,sizeof (sztime));
	Getlocaltime (Sztime);
	fprintf (FP, "/*************************************************************\n");
	fprintf (FP, "FileName:%s \ n", pfilename);
	fprintf (FP, "Filefunc: Define implementation file \ n"); fprintf (FP, "Version  : V0.1 \ n ");
	fprintf (FP, "Author:sunrier \ n");
	fprintf (FP, "Date:%s \ n", sztime);
	fprintf (FP, "Descp:linux implementation file \ n");
	fprintf (FP, "*************************************************************/\n");
	fprintf (FP, "#include <stdio.h>\n");
	fprintf (FP, "\ n");
	fprintf (FP, "int main (int argc,char *argv[]) \ n");
	fprintf (FP, "{\ n");
	fprintf (FP, "\ n");
	fprintf (FP, "return 0;\n");
	fprintf (FP, "}\n");
fprintf (FP, "\ n");


 }


//makefile

#makefile
objs = Create  
all:$ (OBJS)  
cflags =-o-w-ansi     
#CFLAGS =-o-wall-ansi    
CC = gcc $ (cflags) 
  create:create.c  
	@$ (CC)-o $@ $? 
	@gzip-C create.1 > create.1.gz 
	@mv create.1.gz/usr/share/man/man1
#gzip压缩成. gz file
#gzip不加参数时, Default compressed file, do not save the original file;
#如gzip create.1 Results only compressed file create.1.gz, the original file Create.1 No.
Clean:
	@ls | grep-v ^makefile$$ | grep-v [.] c$$ | Grep-v [.] h$$ | Grep-v [.] 1$$ | Grep-v [.] txt$$ | Xargs RM-RF
#makefile 


The following is the man manual for implementing the Custom Create command:

//create.1

. \ "indicates: Start at the beginning comment and \": Represents a comment in a row. \ ": an instance of the CREATE Command manual page. TH Create 1" 2012-06-28 "" Create 1.01 "" User Commands ". \" Create: Represents the title. \ "1: Indicates that this command appears in the first part of the man page, with a range of 1-8 digits consistent with the number defined by the decimal point of the file name. \ "2012-06-28": means "2012-06-28" is displayed in the bottom of the entire page. \ "" "" Create 1.01 ":" Create 1.01 "appears at the bottom left of the entire page. \" "User Commands: Indicates that" User Commands "is displayed on the top of the entire page. \". Th at the beginning of the document, the main description of the title. SH name. \ "description name. \".
SH starts at the beginning of the line, on the left, the wide body \FBCREATE\FR \-A simple demonstration application creates a file, or C file. . \ "-: expressed as \-. SH synopsis. \ "description syntax format. B create. \ ". B: The width of the body, if the line does not have text, the. b label next row for the wide body \-option ... FILE ..... SH DESCRIPTION. \ "describes the relevant description of this command or procedure." Manual page document is from Sunrier.
\FBCREATE\FR is a common application for create a new file,. C or. h file.
. \ "\FB text \FR: Indicates that the text is set to a wide body. \ \fi Text \FR: Indicates that the text is underlined. \".: expressed as \&. . SH options. \ "Description parameter options set IT will decide to create a. h file or. c file. sp. \". SP: Indicates a blank line. B \-h It would create a new head file. Sp. B \-c It would create a new C file. Sp. B \-\-help Display this Help and exit. sp. B \-\-version Output VersiOn information and exit. SH copyright. \ ": Copyright notice \FBCREATE\FR is copyright sunrier. This are free software, can redistribute it or modify it under the GNU general public License as published by
The Free Software Foundation. .
SH see ALSO. \ ": Other reference this is the template of C and H files to quickly create. . SH REPORTING BUGS. \ ": Vulnerability description. TP 0. \ ". TP N: Indicates that the 2nd line under the TP label begins to indent n characters (under the premise of the 1th row exceeding n characters) n The default value is 7. \.  TP 0: The bugs to <sunrier@gmail.com>. This sentence is indented 0 characters, which is to achieve the function of line wrapping There probably are some,but I don ' t know that what
They are yet.
The bugs to <sunrier@gmail.com>. .

SH AUTHOR. \ ": Document author written by Sunrier. . \ "Look at the results of the Create Man Manual command Groff-tascii-man create.1 (Note: The task file is not created at this time, just look at the display effect). \" gzip create.1 create.1 compressed into. gz files, A create.1.gz file is created, and the Create.1 file is deleted after gzip execution. \ "If you want to keep the original file you can do this: gzip-c create.1 > create.1.gz. \" Put the file create.1.gz Usr/share/man/man1 can complete the man manual for the Create command. \ "When you execute man create, you will be in/var/cache/man/cat1/








 CREATE.1.BZ2 Create a create.1.bz2 compressed file


[Root@localhost sunrier]# ls
CREATE.1 create.c Makefile
[Root@localhost sunrier]# make
[Root@localhost Sunrier] #ls
Create Create.1 create.c Makefile
[Root@localhost sunrier]#./create-h Test
[Root@localhost sunrier]# ls
Create create.1 create.c Makefile Test.h
[Root@localhost sunrier]#./create-c Test
[Root@localhost sunrier]# ls
Create create.1 create.c makefile test.c test.h
[Root@localhost sunrier]# man Create
[Root@localhost sunrier]#

Note: To use as commonly used commands, you can place the create under the executable bin path such as/usr/bin, and you can use echo $PATH to see what the current system default execution path is.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.