aboutsummaryrefslogblamecommitdiffstats
path: root/main.c
blob: 78297434c6b7a4eef87d66da3f9557dc5c06374c (plain) (tree)
1
2
3
4
5
6
7
8
9


                                                                                        

   
                   
                    
                   
               
                      
 
                    
                  
                   
                        


                      
                
 







                                                            
 



                       
                                           

                                             
                                                                                    



                                                                              

                                                                           



                                                                       


                            

                      






                                                           

                                       
                         

                                                

                              
                                       

                              
                                         














                                                                                                                        
 
                            
                                              

         
                          

                                                                              


                                                    



                                               






















                                                                           


                                                                





                                                             






                                                         
                                   
                                  





                                                     


                                                 
                 
 


                                          
 
     
                        
                            
 
 
             

                  
                        
                  
                           




                 
                                   



                                                                                 


                                      
 



                   


                                                                      
 







                                                    
/* Copyright (c) 2021 nytpu <alex@nytpu.com>
 * SPDX-License-Identifier: GPL-3.0-only
 * For more license details, see LICENSE or <https://www.gnu.org/licenses/gpl-3.0.html>.
 */

#include "buffer.h"
#include "command.h"
#include "common.h"
#include "io.h"
#include "linenoise.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <sys/ioctl.h>
#include <unistd.h>

struct Editor E;

static int
get_width(void)
{
	struct winsize ws;
	int err = ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
	if (err < 0 || ws.ws_col == 0) return DEFAULT_WIDTH;
	return ws.ws_col;
}

static void
usage(const char *cmd)
{
	fprintf(stderr,
"Usage: %s [-hlsvV] [-p string] [file]\n\n"
"Flags:\n"
"\t-h           Display this usage prompt.\n"
"\t-l           Use a \"loose exit status\"; i.e. exit 0 even if a command fails.\n"
"\t-s           Suppress diagnostics (if enabled), byte counts when reading\n"
"\t             and writing, and '!' prompt.\n"
"\t-v           Be verbose, equivalent to the 'H' command.\n"
"\t-V           Display version and copyright information\n"
"\t-p 'string'  Use 'string' as the interactive prompt string in command\n"
"\t             mode. Also enables prompt mode. (default '*')\n"
"\nFor more help, see ed(1), ed(1p), and <https://git.nytpu.com/ed>\n",
	cmd);
}

int
main(int argc, char *argv[])
{
	editor_init();

	int c;
	while ((c = getopt(argc, argv, "hlp:svV")) != -1) {
		switch (c) {
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
		case 'l':
			E.loose = true;
			break;
		case 'p':
			E.prompt_enabled = true;
			E.prompt = optarg;
			break;
		case 's':
			E.quiet = true;
			break;
		case 'v':
			E.verbose = true;
			break;
		case 'V':
			printf(
				"nytpu's ed v%s\n\n"
				"Copyright (c) 2021 nytpu <alex@nytpu.com>\n"
				"SPDX-License-Identifier: GPL-3.0-only\n"
				"For more license details, see LICENSE in the source repo <https://git.nytpu.com/ed>\n",
				VERSION
			);
			return EXIT_SUCCESS;
		default:
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}

	if (optind < argc) {
		open_file(argv[optind], true);
	}

	char *line = NULL;
	while ((line = linenoise(E.prompt_enabled ? E.prompt : "")) != NULL) {
		struct command cmd;
		int ret = parse_command(&